List of usage examples for javax.swing.event TreeSelectionEvent getPaths
public TreePath[] getPaths()
From source file:Main.java
public static void main(String[] argv) throws Exception { JTree tree = new JTree(); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent evt) { TreePath[] paths = evt.getPaths(); for (int i = 0; i < paths.length; i++) { if (evt.isAddedPath(i)) { System.out.println("This node has been selected"); } else { System.out.println("This node has been deselected"); }/*from w w w .j av a2s. c o m*/ } } }); }
From source file:vnreal.gui.mapping.MappingTreeSelectionListener.java
@SuppressWarnings("unchecked") @Override//w ww. j a v a 2s . co m public void valueChanged(TreeSelectionEvent e) { if (owner.getGraphPanel() == null) return; List<TreePath> linkProxies = new LinkedList<TreePath>(); for (TreePath path : e.getPaths()) if (path.getLastPathComponent() instanceof LinkProxy) linkProxies.add(path); // if we have a select and unselect of LinkProxies in one step ... if (linkProxies.size() == 2) { // ... ensure unselect comes before select if (e.isAddedPath(linkProxies.get(0))) Collections.reverse(linkProxies); } for (TreePath path : linkProxies) { Object obj = path.getLastPathComponent(); final boolean state = e.isAddedPath(path); if (state) { if (obj instanceof VirtualLinkProxy) { // Highlight one virtual link and its nodes as well several // substrate elements VirtualLinkProxy lp = (VirtualLinkProxy) obj; VirtualLink vl = lp.getLink(); backupVN.put(vl.getLayer(), ((LayerViewer<VirtualNode, VirtualLink>) owner.getGraphPanel() .getViewers().get(vl.getLayer())).getRenderContext().getVertexFillPaintTransformer()); backupVL.put(vl.getLayer(), ((LayerViewer<VirtualNode, VirtualLink>) owner.getGraphPanel() .getViewers().get(vl.getLayer())).getRenderContext().getEdgeDrawPaintTransformer()); highlightVN.addNodes(lp.getvNet().getEndpoints(vl)); highlightVL.addLink(vl); backupSN = ((LayerViewer<SubstrateNode, SubstrateLink>) owner.getGraphPanel().getViewers() .get(0)).getRenderContext().getVertexFillPaintTransformer(); backupSL = ((LayerViewer<SubstrateNode, SubstrateLink>) owner.getGraphPanel().getViewers() .get(0)).getRenderContext().getEdgeDrawPaintTransformer(); int max = lp.getChildCount() - 1; for (int i = 0; i <= max; i++) { MappingProxy child = (MappingProxy) lp.getChildAt(i); Mapping mp = child.getMapping(); if (mp.getResource().getOwner() instanceof SubstrateLink) { highlightSL.links.add((SubstrateLink) mp.getResource().getOwner()); } else if (mp.getResource().getOwner() instanceof SubstrateNode) { SubstrateNode sn = (SubstrateNode) mp.getResource().getOwner(); if (i == 0 || i == max) highlightSN.nodes.add(sn); else highlightSN.nodesHH.add(sn); } } for (Integer layer : backupVN.keySet()) setVNetVertexTransformer(layer, highlightVN); for (Integer layer : backupVL.keySet()) setVNetEdgeTransformer(layer, highlightVL); setSNetVertexTransformer(highlightSN); setSNetEdgeTransformer(highlightSL); } else if (obj instanceof SubstrateLinkProxy) { // Highlight one substrate link and its nodes as well // several virtual elements in different virtual networks SubstrateLinkProxy lp = (SubstrateLinkProxy) obj; SubstrateLink sl = lp.getLink(); backupSN = ((LayerViewer<SubstrateNode, SubstrateLink>) owner.getGraphPanel().getViewers() .get(0)).getRenderContext().getVertexFillPaintTransformer(); backupSL = ((LayerViewer<SubstrateNode, SubstrateLink>) owner.getGraphPanel().getViewers() .get(0)).getRenderContext().getEdgeDrawPaintTransformer(); highlightSL.links.add(sl); highlightSN.nodes.addAll(lp.getNet().getEndpoints(sl)); int max = lp.getChildCount() - 1; for (int i = 0; i <= max; i++) { MappingProxy child = (MappingProxy) lp.getChildAt(i); Mapping mp = child.getMapping(); if (mp.getDemand().getOwner() instanceof VirtualLink) { VirtualLink vl = (VirtualLink) mp.getDemand().getOwner(); backupVL.put(vl.getLayer(), ((LayerViewer<VirtualNode, VirtualLink>) owner.getGraphPanel().getViewers() .get(vl.getLayer())).getRenderContext().getEdgeDrawPaintTransformer()); highlightVL.addLink(vl); } else if (mp.getDemand().getOwner() instanceof VirtualNode) { VirtualNode vn = (VirtualNode) mp.getDemand().getOwner(); backupVN.put(vn.getLayer(), ((LayerViewer<VirtualNode, VirtualLink>) owner.getGraphPanel().getViewers() .get(vn.getLayer())).getRenderContext() .getVertexFillPaintTransformer()); highlightVN.addNode(vn); } } for (Integer layer : backupVN.keySet()) setVNetVertexTransformer(layer, highlightVN); for (Integer layer : backupVL.keySet()) setVNetEdgeTransformer(layer, highlightVL); setSNetVertexTransformer(highlightSN); setSNetEdgeTransformer(highlightSL); } } else { for (Integer layer : backupVN.keySet()) setVNetVertexTransformer(layer, backupVN.get(layer)); for (Integer layer : backupVL.keySet()) setVNetEdgeTransformer(layer, backupVL.get(layer)); backupVN.clear(); backupVL.clear(); highlightVN.vns.clear(); highlightVL.vls.clear(); highlightSN.nodes.clear(); highlightSN.nodesHH.clear(); highlightSL.links.clear(); setSNetVertexTransformer(backupSN); setSNetEdgeTransformer(backupSL); backupSN = null; backupSL = null; } } update(); }