List of usage examples for javax.swing.tree TreePath pathByAddingChild
public TreePath pathByAddingChild(Object child)
child
. From source file:Main.java
public Main() { setLayout(new GridLayout(1, 3)); tree = new JTree(getTreeModel()); tree.setDragEnabled(true);/*from w w w. j a v a 2 s .c o m*/ tree.setPreferredSize(new Dimension(200, 400)); JScrollPane scroll = new JScrollPane(); scroll.setViewportView(tree); treeModel = getTreeModel(); JTree secondTree = new JTree(treeModel); secondTree.setPreferredSize(new Dimension(200, 400)); secondTree.setTransferHandler(new TransferHandler() { @Override public boolean importData(TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation(); TreePath path = dl.getPath(); int childIndex = dl.getChildIndex(); String data; try { data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { return false; } if (childIndex == -1) { childIndex = tree.getModel().getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(data); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); treeModel.insertNodeInto(newNode, parentNode, childIndex); tree.makeVisible(path.pathByAddingChild(newNode)); tree.scrollRectToVisible(tree.getPathBounds(path.pathByAddingChild(newNode))); return true; } public boolean canImport(TransferSupport support) { if (!support.isDrop()) { return false; } support.setShowDropLocation(true); if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) { System.out.println("only string is supported"); return false; } JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation(); TreePath path = dl.getPath(); if (path == null) { return false; } return true; } }); JScrollPane secondScroll = new JScrollPane(); secondScroll.setViewportView(secondTree); JPanel topPanel = new JPanel(new BorderLayout()); topPanel.add(scroll, BorderLayout.CENTER); JPanel btmPanel = new JPanel(new BorderLayout()); btmPanel.add(secondScroll, BorderLayout.CENTER); add(topPanel); add(btmPanel); }
From source file:gdt.jgui.entity.JEntityStructurePanel.java
private void expandAll(JTree tree, TreePath path, boolean expand) { TreeNode node = (TreeNode) path.getLastPathComponent(); if (node.getChildCount() >= 0) { Enumeration enumeration = node.children(); while (enumeration.hasMoreElements()) { TreeNode n = (TreeNode) enumeration.nextElement(); TreePath p = path.pathByAddingChild(n); expandAll(tree, p, expand);/*from w ww .j a v a 2 s . c o m*/ } } if (expand) { tree.expandPath(path); } else { tree.collapsePath(path); } }
From source file:fxts.stations.ui.help.ContentTree.java
/** * Fires expanding of top nodes./*from www . ja v a 2 s. c o m*/ */ private void expandTopNodes() { DefaultMutableTreeNode node; DefaultMutableTreeNode root; TreePath path; TreePath rootPath; root = (DefaultMutableTreeNode) mTree.getModel().getRoot(); rootPath = new TreePath(root); for (Enumeration enumeration = root.children(); enumeration.hasMoreElements();) { node = (DefaultMutableTreeNode) enumeration.nextElement(); //increase path path = rootPath.pathByAddingChild(node); //expands node mTree.expandPath(path); } }
From source file:gdt.jgui.entity.JEntityDigestDisplay.java
private void expandAll(JTree tree, TreePath path, boolean expand) { TreeNode node = (TreeNode) path.getLastPathComponent(); if (node.getChildCount() >= 0) { Enumeration enumeration = node.children(); while (enumeration.hasMoreElements()) { DefaultMutableTreeNode n = (DefaultMutableTreeNode) enumeration.nextElement(); TreePath p = path.pathByAddingChild(n); expandAll(tree, p, expand);//from w w w . j a v a2 s . c o m } } if (expand) { tree.expandPath(path); } else { tree.collapsePath(path); } }
From source file:de.erdesignerng.visual.common.OutlineComponent.java
private void expandOrCollapseAllChildrenOfNode(TreePath aParentPath, boolean aExpand) { TreeNode node = (TreeNode) aParentPath.getLastPathComponent(); if (node.getChildCount() > 0) { for (Enumeration<TreeNode> en = node.children(); en.hasMoreElements();) { TreeNode n = en.nextElement(); TreePath path = aParentPath.pathByAddingChild(n); expandOrCollapseAllChildrenOfNode(path, aExpand); }/*from w w w .j a v a 2 s . c om*/ } if (aExpand) { tree.expandPath(aParentPath); } else { tree.collapsePath(aParentPath); } }
From source file:fxts.stations.ui.help.ContentTree.java
/** * Returns path to node containing the specified url. * * @param aParentPath parent node/*w w w .jav a 2 s .com*/ * @param aUrl specified url * * @return path to node containing specified url */ public TreePath findPathByUrl(TreePath aParentPath, String aUrl) { DefaultMutableTreeNode parentNode; DefaultMutableTreeNode node; TreePath resultPath; NodeInfo info; parentNode = (DefaultMutableTreeNode) aParentPath.getLastPathComponent(); if (!parentNode.getAllowsChildren()) { return null; } for (Enumeration enumeration = parentNode.children(); enumeration.hasMoreElements();) { node = (DefaultMutableTreeNode) enumeration.nextElement(); info = (NodeInfo) node.getUserObject(); if (aUrl.equals(info.getUrl())) { return aParentPath.pathByAddingChild(node); } else { resultPath = findPathByUrl(aParentPath.pathByAddingChild(node), aUrl); if (resultPath != null) { return resultPath; } } } return null; }
From source file:net.sf.jabref.gui.FindUnlinkedFilesDialog.java
/** * Expands or collapses the specified tree according to the * <code>expand</code>-parameter. *///from ww w . ja v a 2 s . co m private void expandTree(JTree currentTree, TreePath parent, boolean expand) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration<TreeNode> e = node.children(); e.hasMoreElements();) { TreePath path = parent.pathByAddingChild(e.nextElement()); expandTree(currentTree, path, expand); } } if (expand) { currentTree.expandPath(parent); } else { currentTree.collapsePath(parent); } }
From source file:de.tor.tribes.ui.views.DSWorkbenchSelectionFrame.java
private TreePath find2(JTree tree, TreePath parent, Village pNode, int depth) { TreeNode node = (TreeNode) parent.getLastPathComponent(); DefaultMutableTreeNode o = (DefaultMutableTreeNode) node; // If equal, go down the branch if (o.getUserObject().equals(pNode)) { // If at end, return match return parent; } else {//from ww w . ja v a2 s .com // Traverse children if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements();) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); TreePath result = find2(tree, path, pNode, depth + 1); // Found a match if (result != null) { return result; } } } } // No match at this branch return null; }
From source file:gdt.jgui.entity.index.JIndexPanel.java
private void expandAll(JTree tree, TreePath parent, boolean expand) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements();) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandAll(tree, path, expand); }//from www . ja va 2 s .c om } if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } }
From source file:LocationSensitiveDemo.java
public LocationSensitiveDemo() { super("Location Sensitive Drag and Drop Demo"); treeModel = getDefaultTreeModel();//w w w . ja v a 2s . c o m tree = new JTree(treeModel); tree.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4)); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); tree.setDropMode(DropMode.ON); namesPath = tree.getPathForRow(2); tree.expandRow(2); tree.expandRow(1); tree.setRowHeight(0); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport info) { // for the demo, we'll only support drops (not clipboard paste) if (!info.isDrop()) { return false; } String item = (String) indicateCombo.getSelectedItem(); if (item.equals("Always")) { info.setShowDropLocation(true); } else if (item.equals("Never")) { info.setShowDropLocation(false); } // we only import Strings if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } // fetch the drop location JTree.DropLocation dl = (JTree.DropLocation) info.getDropLocation(); TreePath path = dl.getPath(); // we don't support invalid paths or descendants of the names folder if (path == null || namesPath.isDescendant(path)) { return false; } return true; } public boolean importData(TransferHandler.TransferSupport info) { // if we can't handle the import, say so if (!canImport(info)) { return false; } // fetch the drop location JTree.DropLocation dl = (JTree.DropLocation) info.getDropLocation(); // fetch the path and child index from the drop location TreePath path = dl.getPath(); int childIndex = dl.getChildIndex(); // fetch the data and bail if this fails String data; try { data = (String) info.getTransferable().getTransferData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException e) { return false; } catch (IOException e) { return false; } // if child index is -1, the drop was on top of the path, so we'll // treat it as inserting at the end of that path's list of children if (childIndex == -1) { childIndex = tree.getModel().getChildCount(path.getLastPathComponent()); } // create a new node to represent the data and insert it into the model DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(data); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); treeModel.insertNodeInto(newNode, parentNode, childIndex); // make the new node visible and scroll so that it's visible tree.makeVisible(path.pathByAddingChild(newNode)); tree.scrollRectToVisible(tree.getPathBounds(path.pathByAddingChild(newNode))); // demo stuff - remove for blog model.removeAllElements(); model.insertElementAt("String " + (++count), 0); // end demo stuff return true; } }); JList dragFrom = new JList(model); dragFrom.setFocusable(false); dragFrom.setPrototypeCellValue("String 0123456789"); model.insertElementAt("String " + count, 0); dragFrom.setDragEnabled(true); dragFrom.setBorder(BorderFactory.createLoweredBevelBorder()); JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); JPanel wrap = new JPanel(); wrap.add(new JLabel("Drag from here:")); wrap.add(dragFrom); p.add(Box.createHorizontalStrut(4)); p.add(Box.createGlue()); p.add(wrap); p.add(Box.createGlue()); p.add(Box.createHorizontalStrut(4)); getContentPane().add(p, BorderLayout.NORTH); getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER); indicateCombo = new JComboBox(new String[] { "Default", "Always", "Never" }); indicateCombo.setSelectedItem("INSERT"); p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); wrap = new JPanel(); wrap.add(new JLabel("Show drop location:")); wrap.add(indicateCombo); p.add(Box.createHorizontalStrut(4)); p.add(Box.createGlue()); p.add(wrap); p.add(Box.createGlue()); p.add(Box.createHorizontalStrut(4)); getContentPane().add(p, BorderLayout.SOUTH); getContentPane().setPreferredSize(new Dimension(400, 450)); }