public DefaultMutableTreeNode findNode(DefaultMutableTreeNode parent, String match) { if (parent == null) return null; // Commented by Balan on 15/03/03 // String treename = (String)parent.getUserObject (); // Comment Ends // Added by Balan on 15/03/03 String treename = "" + ((Hashtable) parent.getUserObject()).get("TREE-NAME"); // Add Ends if (treename != null) { if (treename.equals(match)) return parent; } if (frame.model.isLeaf(parent)) return null; Enumeration en = parent.children(); if ((en == null) || (!en.hasMoreElements())) return null; for (; en.hasMoreElements(); ) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) en.nextElement(); DefaultMutableTreeNode returnNode = findNode(child, match); if (returnNode != null) return returnNode; } return null; }
private void checkNodeValidity(@NotNull DefaultMutableTreeNode node) { Enumeration enumeration = node.children(); while (enumeration.hasMoreElements()) { checkNodeValidity((DefaultMutableTreeNode) enumeration.nextElement()); } if (node instanceof Node && node != getModelRoot()) ((Node) node).update(this); }
// {{{ traverseNodes() method public static boolean traverseNodes( DefaultMutableTreeNode node, HyperSearchTreeNodeCallback callbackInterface) { if (!callbackInterface.processNode(node)) return false; for (Enumeration e = node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) e.nextElement(); if (!traverseNodes(childNode, callbackInterface)) return false; } return true; } // }}}
@Override public void actionPerformed(ActionEvent evt) { TreePath path = resultTree.getSelectionPath(); DefaultMutableTreeNode operNode = (DefaultMutableTreeNode) path.getLastPathComponent(); for (Enumeration e = operNode.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); resultTree.collapsePath(new TreePath(node.getPath())); } resultTree.scrollPathToVisible(new TreePath(operNode.getPath())); }
/** * Update the layouts tree. * * @param current The name of the current layout or <CODE>null</CODE> if none. */ public void updateLayouts(Path current) throws PipelineException { DefaultMutableTreeNode root = null; { root = new DefaultMutableTreeNode(new TreeData(), true); { Path path = new Path(PackageInfo.getSettingsPath(), "layouts"); rebuildTreeModel(path, new Path("/"), root); } DefaultTreeModel model = (DefaultTreeModel) pTree.getModel(); model.setRoot(root); { Enumeration e = root.depthFirstEnumeration(); if (e != null) { while (e.hasMoreElements()) { DefaultMutableTreeNode tnode = (DefaultMutableTreeNode) e.nextElement(); pTree.expandPath(new TreePath(tnode.getPath())); } } } } pTree.clearSelection(); if (current != null) { TreePath tpath = null; DefaultMutableTreeNode tnode = root; for (String comp : current.getComponents()) { DefaultMutableTreeNode next = null; Enumeration e = tnode.children(); if (e != null) { while (e.hasMoreElements()) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement(); TreeData data = (TreeData) child.getUserObject(); if (data.toString().equals(comp)) { tpath = new TreePath(child.getPath()); next = child; break; } } } if (next == null) break; tnode = next; } if (tpath != null) { pTree.setSelectionPath(tpath); pTree.makeVisible(tpath); } } }
private static void collectUsages( @NotNull DefaultMutableTreeNode node, @NotNull Set<Usage> usages) { if (node instanceof UsageNode) { UsageNode usageNode = (UsageNode) node; final Usage usage = usageNode.getUsage(); usages.add(usage); } Enumeration enumeration = node.children(); while (enumeration.hasMoreElements()) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) enumeration.nextElement(); collectUsages(child, usages); } }
public JTrees() { JFrame frame = new JFrame("JTree Demo"); Container c = frame.getContentPane(); c.setLayout(new BorderLayout()); // Create top node of a tree final DefaultMutableTreeNode top = new DefaultMutableTreeNode("Course"); // Create a subtree UG final DefaultMutableTreeNode UG = new DefaultMutableTreeNode("UG"); top.add(UG); final DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("B.E"); UG.add(a1); final DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("B.C.A"); UG.add(a2); final DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("B.Sc"); UG.add(a3); final DefaultMutableTreeNode a4 = new DefaultMutableTreeNode("B.Com"); UG.add(a4); final DefaultMutableTreeNode a5 = new DefaultMutableTreeNode("B.A"); UG.add(a5); // Create a subtree PG final DefaultMutableTreeNode PG = new DefaultMutableTreeNode("PG"); top.add(PG); final DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("M.E"); PG.add(b1); final DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("M.C.A"); PG.add(b2); final DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("M.Sc"); PG.add(b3); final DefaultMutableTreeNode b4 = new DefaultMutableTreeNode("M.Com"); PG.add(b4); final DefaultMutableTreeNode b5 = new DefaultMutableTreeNode("M.A"); PG.add(b5); // Creating tree final JTree tree = new JTree(top); int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; final JScrollPane jsp = new JScrollPane(tree, v, h); c.add(jsp, BorderLayout.CENTER); final JTextField text = new JTextField("", 20); c.add(text, BorderLayout.SOUTH); tree.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent me) { TreePath tp = tree.getPathForLocation(me.getX(), me.getY()); if (tp != null) text.setText(tp.toString()); else text.setText(""); } }); if (top.children() == b1) { System.out.println("M.E Cource Selected"); } frame.setSize(300, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public void valueChanged(TreeSelectionEvent event) { // DefaultMutableTreeNode node = new DefaultMutableTreeNode(); DefaultMutableTreeNode node; TreePath paths[] = event.getPaths(); // Statistical variables Object bestNode; Object worstNode; double avgFitness = 0; double bestFitness = -1; double worstFitness = -1; // Update Selection Panel to reflect current selections for (int i = 0; i < paths.length; i++) { // If a parent node is selected, select all children nodes DefaultMutableTreeNode tmpNode = (DefaultMutableTreeNode) paths[i].getLastPathComponent(); // If Root is selected, clear all selections if (tmpNode.isRoot()) { tree.clearSelection(); selectedChildrenPaths.removeAllElements(); break; } if (tmpNode.getAllowsChildren()) { ArrayList tmpNodeChildren = new ArrayList(); for (Enumeration e = tmpNode.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) e.nextElement(); TreePath treePath = new TreePath(childNode.getPath()); if (event.isAddedPath(paths[i])) { if (!selectedChildrenPaths.contains(treePath)) selectedChildrenPaths.addElement(treePath); } else selectedChildrenPaths.removeElement(treePath); } // TreePath [] treePaths = new TreePath[tmpNodeChildren.size()]; // if ( event.isAddedPath(paths[i]) ) // tree.addSelectionPaths( (TreePath []) tmpNodeChildren.toArray(treePaths) ); // else // tree.removeSelectionPaths( (TreePath[]) tmpNodeChildren.toArray(treePaths) ); // Collapse parent view -- commented for future use, DO NOT IMPLEMENT AS SHOWN // tree.collapsePath( paths[i] ); } else // only a single node is being examined { if (event.isAddedPath(paths[i])) { if (!selectedChildrenPaths.contains(paths[i])) selectedChildrenPaths.addElement(paths[i]); } else selectedChildrenPaths.removeElement(paths[i]); } } // If selections exist, enabled "Save Selected" menu item if (selectedChildrenPaths.size() > 0) miSave.setEnabled(true); else miSave.setEnabled(false); // Calculate selection information statistics for (int j = 0; j < selectedChildrenPaths.size(); j++) { double fitness = 0; node = (DefaultMutableTreeNode) ((TreePath) selectedChildrenPaths.elementAt(j)).getLastPathComponent(); fitness = gaMonitor.getFitness(node.getUserObject()); avgFitness += fitness; if (fitness > bestFitness || bestFitness == -1) { bestFitness = fitness; bestNode = node; } if (fitness < worstFitness || worstFitness == -1) { worstFitness = fitness; worstNode = node; } } // Finialize Statistics if (bestFitness != -1) avgFitness = avgFitness / (double) selectedChildrenPaths.size(); // Panel may not exist! If null, ignore if (selectionStatsPanel != null) { if (bestFitness != -1) selectionStatsPanel.setSelectionStats( selectedChildrenPaths.size(), avgFitness, bestFitness, worstFitness); else selectionStatsPanel.setEmptySelection(); } }