List of usage examples for javax.swing.tree DefaultMutableTreeNode toString
public String toString()
toString()
to this node's user object, or the empty string if the node has no user object. From source file:Main.java
/** * Method by Adrian: [//w w w . j a v a 2 s . c om * <a href="http://stackoverflow.com/a/15704264/5620200">StackOverflow</a> ] * & Mike: [ <a href= * "http://stackoverflow.com/questions/1542170/arranging-nodes-in-a-jtree"> * StackOverflow</a> ] * * @param node * @return */ @SuppressWarnings("unchecked") public static DefaultMutableTreeNode sort(DefaultMutableTreeNode node) { List<DefaultMutableTreeNode> children = Collections.list(node.children()); List<String> orgCnames = new ArrayList<String>(); List<String> cNames = new ArrayList<String>(); DefaultMutableTreeNode temParent = new DefaultMutableTreeNode(); for (DefaultMutableTreeNode child : children) { DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child; temParent.insert(ch, 0); String uppser = ch.toString().toUpperCase(); // Not dependent on package name, so if duplicates are found // they will later on be confused. Adding this is of // very little consequence and fixes the issue. if (cNames.contains(uppser)) { uppser += "$COPY"; } cNames.add(uppser); orgCnames.add(uppser); if (!child.isLeaf()) { sort(child); } } Collections.sort(cNames); for (String name : cNames) { int indx = orgCnames.indexOf(name); int insertIndex = node.getChildCount(); node.insert(children.get(indx), insertIndex); } // Fixing folder placement for (int i = 0; i < node.getChildCount() - 1; i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i); for (int j = i + 1; j <= node.getChildCount() - 1; j++) { DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j); if (!prevNode.isLeaf() && child.isLeaf()) { node.insert(child, j); node.insert(prevNode, i); } } } return node; }
From source file:TreeUtil.java
public static TreePath makeTreePath(String path, DefaultMutableTreeNode parentNode) { DefaultMutableTreeNode tempNode = parentNode; TreePath res = new TreePath(parentNode); StringTokenizer tok = new StringTokenizer(path, "."); String currentPath = null;// ww w .j a va 2 s . c o m while (tok.hasMoreTokens()) { String myTok = tok.nextToken(); currentPath = (currentPath == null) ? myTok : currentPath + "." + myTok; for (int j = 0; j < tempNode.getChildCount(); j++) { DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) tempNode.getChildAt(j); if (childNode.toString().equals(myTok)) { tempNode = childNode; res = res.pathByAddingChild(tempNode); break; } } } return res; }
From source file:OAT.ui.util.UiUtil.java
public static DefaultMutableTreeNode addChildNode(DefaultMutableTreeNode parent, Object child) { HashSet hashSet = new HashSet(); DefaultMutableTreeNode childNode; for (int i = 0; i < parent.getChildCount(); i++) { hashSet.add(parent.getChildAt(i).toString()); }/*from w w w. j a va2s. co m*/ if (hashSet.add(child.toString())) { childNode = new DefaultMutableTreeNode(child); parent.insert(childNode, parent.getChildCount()); } else { int i = 0; do { childNode = (DefaultMutableTreeNode) parent.getChildAt(i++); //System.out.println("childNode: " + childNode); } while (!childNode.toString().equals(child.toString())); } return childNode; }
From source file:UsingTreeSelectionListener.java
public void valueChanged(TreeSelectionEvent se) { JTree tree = (JTree) se.getSource(); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); String selectedNodeName = selectedNode.toString(); if (selectedNode.isLeaf()) { System.out.println(selectedNodeName); }//from w w w . j av a2s . c o m }
From source file:TreeUtil.java
public void addNode(String name, Object obj) { itsMap.put(name, obj);/* w ww . ja v a 2 s.com*/ DefaultMutableTreeNode tempNode = itsRootNode; StringTokenizer tok = new StringTokenizer(name, "."); String currentName = null; while (tok.hasMoreTokens()) { String myTok = tok.nextToken(); currentName = (currentName == null) ? myTok : currentName + "." + myTok; boolean createNew = true; for (int j = 0; j < tempNode.getChildCount(); j++) { DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) tempNode.getChildAt(j); if (childNode.toString().equals(myTok)) { tempNode = childNode; createNew = false; break; } } if (createNew) { DefaultMutableTreeNode aNode = new DefaultMutableTreeNode(myTok); itsTreeMap.put(currentName, aNode); // Let's give some consideration to where in the tree we place // the new node. // We want any nodes with children to be listed first, in // alphabetical order. // Then come nodes with no children, in alphabetical order. if (tok.hasMoreTokens()) { // This node is not a leaf node int targeti; for (targeti = 0; targeti < tempNode.getChildCount(); targeti++) { TreeNode bNode = tempNode.getChildAt(targeti); if (bNode.isLeaf() || bNode.toString().compareToIgnoreCase(myTok) > 0) { break; } } tempNode.insert(aNode, targeti); } else { // This node is a leaf node int targeti; for (targeti = 0; targeti < tempNode.getChildCount(); targeti++) { TreeNode bNode = tempNode.getChildAt(targeti); if (bNode.isLeaf() && bNode.toString().compareToIgnoreCase(myTok) > 0) { break; } } tempNode.insert(aNode, targeti); } tempNode = aNode; } } }
From source file:de.quadrillenschule.azocamsyncd.gui.ExploreWifiSDPanel.java
private void createSubNodes(DefaultMutableTreeNode parent, LinkedList<AZoFTPFile> afs) { String parentNodeName = parent.toString(); for (AZoFTPFile af : afs) { String nodeName = af.dir + af.ftpFile.getName(); if (af.ftpFile.isDirectory()) { if (!parentNodeName.equals(nodeName)) { if (nodeName.contains(parentNodeName)) { if (StringUtils.countMatches(nodeName, "/") - 1 == StringUtils.countMatches(parentNodeName, "/")) { DefaultMutableTreeNode tn = new DefaultMutableTreeNode(nodeName); parent.add(tn);//from w w w. j ava 2s. c o m createSubNodes(tn, afs); } } ; } } //isFile if (af.ftpFile.isFile()) { if (nodeName.contains(parentNodeName)) { if (StringUtils.countMatches(nodeName, "/") == 1 + StringUtils.countMatches(parentNodeName, "/")) { DefaultMutableTreeNode tn = new DefaultMutableTreeNode(nodeName); parent.add(tn); } } ; } } }
From source file:be.ugent.maf.cellmissy.gui.controller.load.generic.DragAndDropController.java
/** * Given an imaging node, find the upper dataset * * @param draggedNode// w w w . j av a 2s. c om * @return */ private Algorithm findAlgorithm(DefaultMutableTreeNode draggedNode) { ObservableList<Algorithm> algorithmsBindingList = genericImagedPlateController.getAlgorithmsBindingList(); Algorithm foundAlgorithm = null; DefaultMutableTreeNode algoNode = (DefaultMutableTreeNode) draggedNode.getParent().getParent(); for (Algorithm algorithm : algorithmsBindingList) { if (algorithm.getAlgorithmName().equals(algoNode.toString())) { foundAlgorithm = algorithm; } } return foundAlgorithm; }
From source file:dotaSoundEditor.Controls.EditorPanel.java
protected TreeNode getTreeNodeFromWavePath(String wavePath) { TreeModel model = this.currentTreeModel; TreeNode root = (TreeNode) model.getRoot(); for (Enumeration e = ((DefaultMutableTreeNode) root).breadthFirstEnumeration(); e.hasMoreElements() && root != null;) {/*from www. j a va 2 s. com*/ DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.toString().contains(wavePath)) { return node; } } return null; }
From source file:com.pironet.tda.SunJDKParser.java
private void reNormalizeBlockingThreadTree(final MonitorMap mmap, final Map<String, DefaultMutableTreeNode> directChildMap) { Map<String, DefaultMutableTreeNode> allBlockingThreadsMap = new HashMap<>(directChildMap); // All threads that are blocking at least one other thread // First, re-normalize based on monitors to get our unique tree // Tree will be unique as long as there are no deadlocks aka monitor loops for (Iterator iter = mmap.iterOfKeys(); iter.hasNext();) { String monitor1 = (String) iter.next(); Map<String, String>[] threads1 = mmap.getFromMonitorMap(monitor1); final DefaultMutableTreeNode thread1Node = allBlockingThreadsMap.get(monitor1); if (thread1Node == null) { continue; }//from ww w . ja v a2 s .c o m // Get information on the one thread holding this lock Iterator it = threads1[MonitorMap.LOCK_THREAD_POS].keySet().iterator(); if (!it.hasNext()) { continue; } String threadLine1 = (String) it.next(); for (Iterator iter2 = mmap.iterOfKeys(); iter2.hasNext();) { String monitor2 = (String) iter2.next(); if (monitor1 == monitor2) { continue; } Map<String, String>[] threads2 = mmap.getFromMonitorMap(monitor2); if (threads2[MonitorMap.WAIT_THREAD_POS].containsKey(threadLine1)) { // Get the node of the thread that is holding this lock DefaultMutableTreeNode thread2Node = (DefaultMutableTreeNode) allBlockingThreadsMap .get(monitor2); // Get the node of the monitor itself DefaultMutableTreeNode monitor2Node = (DefaultMutableTreeNode) thread2Node.getFirstChild(); // If a redundant node for thread2 exists with no children, remove it // To compare, we have to remove "Thread - " from the front of display strings for (int i = 0; i < monitor2Node.getChildCount(); i++) { DefaultMutableTreeNode child2 = (DefaultMutableTreeNode) monitor2Node.getChildAt(i); if (child2.toString().substring(9).equals(threadLine1) && child2.getChildCount() == 0) { monitor2Node.remove(i); break; } } // Thread1 is blocked by monitor2 held by thread2, so move thread1 under thread2 monitor2Node.insert(thread1Node, 0); directChildMap.remove(monitor1); break; } } } allBlockingThreadsMap.clear(); // Second, re-normalize top level based on threads for cases where one thread holds multiple monitors boolean changed; do { changed = false; for (final Object o : directChildMap.entrySet()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) ((Map.Entry) o).getValue(); if (checkForDuplicateThreadItem(directChildMap, node)) { changed = true; break; } } } while (changed); // Third, renormalize lower levels of the tree based on threads for cases where one thread holds multiple monitors for (final Object o : directChildMap.entrySet()) { renormalizeThreadDepth((DefaultMutableTreeNode) ((Map.Entry) o).getValue()); } }
From source file:com.emental.mindraider.ui.outline.OutlineJPanel.java
/** * Discard concept./* www. j a va 2 s. c om*/ */ public void conceptDiscard() { if (MindRaider.profile.getActiveOutlineUri() == null) { JOptionPane.showMessageDialog(OutlineJPanel.this, Messages.getString("NotebookOutlineJPanel.toDiscardConceptTheNotebookMustBeOpened"), Messages.getString("NotebookOutlineJPanel.discardConceptError"), JOptionPane.ERROR_MESSAGE); return; } // move concept up in the tree DefaultMutableTreeNode node = getSelectedTreeNode(); if (node != null) { if (node.isLeaf() && node.getParent() != null) { try { if (JOptionPane.showConfirmDialog(MindRaider.mainJFrame, Messages.getString("NotebookOutlineJPanel.doYouWantToDiscardConcept", node.toString()), Messages.getString("NotebookOutlineJPanel.discardConcept"), JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { return; } DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent(); String notebookUri = MindRaider.outlineCustodian.getActiveOutlineResource().resource .getMetadata().getUri().toString(); MindRaider.noteCustodian.discard(notebookUri, ((OutlineNode) parent).getUri(), ((OutlineNode) node).getUri()); refresh(); MindRaider.spidersGraph.selectNodeByUri(notebookUri); MindRaider.spidersGraph.renderModel(); conceptJPanel.clear(); } catch (Exception e1) { logger.debug(Messages.getString("NotebookOutlineJPanel.unableToDiscardConcept"), e1); StatusBar.show(Messages.getString("NotebookOutlineJPanel.unableToDiscardConcept")); } } else { StatusBar.show(Messages.getString("NotebookOutlineJPanel.discardingOnlyLeafConcepts")); JOptionPane.showMessageDialog(OutlineJPanel.this, Messages.getString("NotebookOutlineJPanel.discardingOnlyLeafConcepts"), "Concept Discard Error", JOptionPane.ERROR_MESSAGE); return; } } else { logger.debug(Messages.getString("NotebookOutlineJPanel.noNodeSelected")); } }