Java Data Sources > Java Data Source Adapters > Using IlsTreeDS2JTreeAdapter > Dereferencing a Referenced Item
 
Dereferencing a Referenced Item
Strategies are enhanced listeners for adapters. They are listening for connection and disconnection of the data source, for creation, deletion, or update of items in the data source, and for events batching. Moreover, strategies can encapsulate the data model (using the setModel method), and the graphic component (using the plugComponent/unplugComponent methods).
In the following example, we are going to listen for mouse events from the plugged JTree instance and react on a double click on a tree item. If the double-clicked item is a hyper-reference, the corresponding view will open.
Example
view MultipleView (any containerIndex=0,
string containerClass="tree.MultipleFrame"):
represent IlsDSRepresentation repres:
any containerIndex = view.containerIndex;
string containerClass = view.containerClass;
string title = "Domains multiple views";
 
subscribe Domain:
represent IlsRpTreeItem domain:
mandatory Ref<IlsRpTreeItem> parent = network->root;
boolean allowChildren = false;
href label = {DomainTableView, name};
 
subscribe origin Network:
represent IlsRpTree tree:
Ref<IlsRpRootItem> root = view.origin->root;
string label = identifier;
boolean expandOnDemand = true;
boolean collapseOnDemand = false;
 
represent IlsRpRootItem root:
mandatory Ref<IlsRpTree> tree = view.origin->tree;
boolean allowChildren = true;
string label = identifier;
 
propagate domains;
The previous view defines a simple tree view based on the object model of the network7 demo. The tree root represents the network and each child item of the network is a domain of the network. Double-clicking on a domain item opens a DomainTableView view on the selected domain. For this purpose, we have defined a subclass derived from IlsTreeDS2JTreeStrategy:
package mypackage;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
 
import ilog.server.jsds.tree.*;
import ilog.server.jsds.adapter.*;
 
public class MyTreeDS2JTreeMouseStrategy extends IlsTreeDS2JTreeStrategy
implements MouseListener {
public MyTreeDS2JTreeMouseStrategy(IlsTreeDS2JTreeAdapter adapter) {
super(adapter);
}
/**
* Listen to graphic component's mouse events.
* Called by adapter when a JTree is connected to adapter.
*/
public Component plugComponent(Component component) {
if (component != null)
component.addMouseListener(this);
return component;
}
/**
* Stops listening to JTree graphic component's mouse events.
* Called by adapter when a JTree is disconnected from adapter.
*/
public Component unplugComponent(Component component) {
if (component != null)
component.removeMouseListener(this);
return component;
}
/**
* Dereference <code>item</code> if its property <code>label</code> is a
hyper-reference.
*/
protected void dereferenceItem(Object item) {
IlsTreeDataSource tds = (IlsTreeDataSource)_adapter.getTreeDataSource();
if (tds != null) {
if (tds.isItemPropertyHRef(item, "label"))
tds.dsDereference(item, "label", 0, false);
}
}
/**
* Implements <code>MouseListener</code>.
* When double clicking on a node, if the item value is a hyper reference
then dereferences it.
*/
public void mouseClicked(MouseEvent e) {
JTree tree = (JTree)e.getSource();
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if ( (selRow != -1) && (e.getClickCount() == 2) &&
(_adapter.getTreeDataSource() != null) ) {
Object item = _adapter.getDSItem((MutableTreeNode)
selPath.getLastPathComponent());
dereferenceItem(item);
}
}
/**
* Implements <code>MouseListener</code>. By default do nothing.
*/
public void mouseEntered(MouseEvent e) {}
/**
* Implements <code>MouseListener</code>. By default do nothing.
*/
public void mouseExited(MouseEvent e) {}
/**
* Implements <code>MouseListener</code>. By default do nothing.
*/
public void mousePressed(MouseEvent e) {}
/**
* Implements <code>MouseListener</code>. By default do nothing.
*/
public void mouseReleased(MouseEvent e) {}
 
}
This strategy implements a MouseListener object which subscribes to the corresponding JTree mouse events. If you double-clicks on a tree item and this item is a hyper-reference, the item on which you double-click is dereferenced and the referenced view is opened. Dereferencing affects the property named label. (To test whether the tree item is a hyper-reference, use the method isItemPropertyHRef. For more information about hyper-references, see “Hyper-References”..)
To use your strategy within your adapter, all you have to do is add your strategy using the adapter method addStrategy. The strategies are ordered in reverse chronological order, that is from the latest (the one added last) to the first. Notification to the strategies also follows this order. This order may be significant if you encapsulate the data model with strategies such as IlsTableDS2JTableBufferedRowStrategy.
Demo network 7 also includes an example of dereferencing, showing how to create a pop-up menu to dereference a tree item. Unlike our Example, demo network 7 shows how to subclass the adapter rather than using a strategy.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.