// action to expand a node and all its children
final InputMap treeInputMap = tree.getInputMap(JComponent.WHEN_FOCUSED);
treeInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_MULTIPLY, 0), "MultPressed");
tree.getActionMap().put("MultPressed", new ExpandTreeNodeAction(tree));
GumTreeNode rootNode = new GumTreeNode("Root");
treeModel = new DefaultTreeModel(rootNode, false);
tree.setModel(treeModel);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row,
hasFocus);
if (value instanceof GumTreeNode) {
GumTreeNode gtn = (GumTreeNode) value;
setText(getNodeName(gtn));
Object userObject = gtn.getUserObject();
if (userObject instanceof IDataItem) {
IDataItem di = (IDataItem) userObject;
NodeDataType nodeDataType = getNodeDataType(di);
switch (nodeDataType) {
case SCALAR:
setIcon(ICONS.getIcon("NodeScalar"));
break;
case SPECTRUM:
setIcon(ICONS.getIcon("NodeSpectrum"));
break;
case IMAGE:
setIcon(ICONS.getIcon("NodeImage"));
break;
case IMAGE_STACK:
setIcon(ICONS.getIcon("NodeImageStack"));
break;
default:
setIcon(ICONS.getIcon("NodeUnknown"));
break;
}
}
}
return this;
}
});
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
JTree source = (JTree) e.getSource();
if (source.getSelectionCount() == 1) {// unnecessary with single selection mode
GumTreeNode gtn = (GumTreeNode) source.getLastSelectedPathComponent();
Object userObject = gtn.getUserObject();
displayNodeInfos(userObject);
}
}
});
tree.addMouseListener(new MouseAdapter() {
/* (non-Javadoc)
* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
*/
@Override
public void mouseClicked(MouseEvent e) {
JXTree sourceTree = (JXTree) e.getSource();
if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
TreePath path = sourceTree.getSelectionPath();
// TODO récupérer la coordonnée dans le scrollpane -> conversion
// TreePath path = sourceTree.getPathForLocation(e.getX(), e.getY());
// we ensure we clicked on a node, not in the blank part or a handle
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
.getLastPathComponent();
// TODO sysout SMA
// System.out.println("Double-click on node \""
// + getNodeName((GumTreeNode) node) + "\"");
if (node instanceof GumTreeNode) {
GumTreeNode gtn = (GumTreeNode) node;
displayNodeValue(gtn);
}
}
}
}