List of usage examples for javax.swing.tree TreePath getParentPath
public TreePath getParentPath()
From source file:MainClass.java
public static void main(String args[]) { JFrame frame = new JFrame("Traverse Tree"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTree tree = new JTree(); tree.setRootVisible(true);/*w ww . java 2s. com*/ TreeModel model = tree.getModel(); Object rootObject = model.getRoot(); if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) { DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject; printDescendents(r); Enumeration breadth = r.breadthFirstEnumeration(); Enumeration depth = r.depthFirstEnumeration(); Enumeration preOrder = r.preorderEnumeration(); printEnumeration(breadth, "Breadth"); printEnumeration(depth, "Depth"); printEnumeration(preOrder, "Pre"); } TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent treeSelectionEvent) { JTree treeSource = (JTree) treeSelectionEvent.getSource(); TreePath path = treeSource.getSelectionPath(); System.out.println(path); System.out.println(path.getPath()); System.out.println(path.getParentPath()); System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject()); System.out.println(path.getPathCount()); } }; tree.addTreeSelectionListener(treeSelectionListener); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 400); frame.setVisible(true); }
From source file:Main.java
/** * @return true, if path1 is a descendant of path2. Else, returns false. *//*from w ww. ja v a2 s.co m*/ public static boolean isDescendant(TreePath path1, TreePath path2) { int count1 = path1.getPathCount(); int count2 = path2.getPathCount(); if (count1 <= count2) return false; while (count1 != count2) { path1 = path1.getParentPath(); count1--; } return path1.equals(path2); }
From source file:Main.java
public void expandSelectedPaths() { TreePath[] paths = getSelectionPaths(); if (paths == null || paths.length == 0) { return;/* w ww . ja v a 2 s .com*/ } setSelectionPath(paths[0]); for (TreePath path : paths) { TreePath parentPath = path.getParentPath(); while (parentPath != null) { if (isExpanded(parentPath)) { parentPath = null; } else { s.push(parentPath); parentPath = parentPath.getParentPath(); } } for (int i = s.size() - 1; i >= 0; i--) { parentPath = s.pop(); if (!isExpanded(parentPath)) { expandedState.put(parentPath, Boolean.TRUE); } } } if (accessibleContext != null) { ((AccessibleJTree) accessibleContext).fireVisibleDataPropertyChange(); } for (TreePath path : paths) { fireTreeExpanded(path); try { fireTreeWillExpand(path); } catch (Exception eve) { return; } } }
From source file:it.unibas.spicygui.vista.listener.ScrollPaneAdjustmentListener.java
private Point findNewLocationForTree(ICaratteristicheWidget icaratteristicheWidget, JTree albero) { CaratteristicheWidgetTree caratteristicheWidget = (CaratteristicheWidgetTree) icaratteristicheWidget; Point oldPoint = caratteristicheWidget.getPosizione(); if (logger.isTraceEnabled()) logger.trace("oldPoint: " + oldPoint); TreePath treePath = caratteristicheWidget.getTreePath(); Rectangle rect = albero.getPathBounds(treePath); if (rect != null && this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { Point newPoint = albero.getPathBounds(treePath).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePath).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y/* ww w . ja v a 2s . c om*/ + (albero.getPathBounds(treePath).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } else if (this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { TreePath treePathInterno = treePath; Rectangle rectInterno = albero.getPathBounds(treePathInterno); while (rectInterno == null) { if (treePathInterno == null) { return null; } treePathInterno = treePathInterno.getParentPath(); rectInterno = albero.getPathBounds(treePathInterno); } Point newPoint = albero.getPathBounds(treePathInterno).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePathInterno).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y + (albero.getPathBounds(treePathInterno).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } return null; }
From source file:it.unibas.spicygui.vista.listener.WidgetMoveExpansionListener.java
private Point findNewLocationForTree(ICaratteristicheWidget icaratteristicheWidget, JTree albero) { CaratteristicheWidgetTree caratteristicheWidget = (CaratteristicheWidgetTree) icaratteristicheWidget; Point oldPoint = caratteristicheWidget.getPosizione(); if (logger.isTraceEnabled()) logger.trace("oldPoint: " + oldPoint); TreePath treePath = caratteristicheWidget.getTreePath(); Rectangle rect = albero.getPathBounds(treePath); if (rect != null && this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { Point newPoint = albero.getPathBounds(treePath).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePath).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y// w w w .jav a 2 s. co m + (albero.getPathBounds(treePath).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } else if (this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { TreePath treePathInterno = treePath; Rectangle rectInterno = albero.getPathBounds(treePathInterno); while (rectInterno == null) { if (treePathInterno == null) { return null; } treePathInterno = treePathInterno.getParentPath(); rectInterno = albero.getPathBounds(treePathInterno); } Point newPoint = albero.getPathBounds(treePathInterno).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePathInterno).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y + (albero.getPathBounds(treePathInterno).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } return null; }
From source file:FileTreeFrame.java
public void valueForPathChanged(TreePath path, Object value) { File oldFile = (File) path.getLastPathComponent(); String fileParentPath = oldFile.getParent(); String newFileName = (String) value; File targetFile = new File(fileParentPath, newFileName); oldFile.renameTo(targetFile);/*from ww w . jav a2 s . c o m*/ File parent = new File(fileParentPath); int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) }; Object[] changedChildren = { targetFile }; fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren); }
From source file:mobac.gui.components.JMapSourceTree.java
/** * This method is selecting a requested MapSource and focusing on it IN A TREE. MapSource is not being marked * internally as selected in this method. * /*from w ww .j ava2 s .c o m*/ * @param mapSourceToSelect * - MapSource that will be selected */ private void chooseAndShowTreeNodeInTree(MapSource mapSourceToSelect) { TreePath pathFound = findTreePathOfMapSource(mapSourceToSelect); if (pathFound != null) { // Expand all folders and subfolders to show a chosen node expandPath(pathFound.getParentPath()); // Choose the node setSelectionPath(pathFound); // Scroll a JTree viewport to the chosen node scrollPathToVisibleVerticalOnly(pathFound); // Signaling to refresh a node, after its font/color was changed ((DefaultTreeModel) this.getModel()).nodeChanged((TreeNode) pathFound.getLastPathComponent()); } }
From source file:eu.europa.ec.markt.dss.applet.view.validationpolicy.EditView.java
private void nodeActionAdd(MouseEvent mouseEvent, final int selectedRow, final TreePath selectedPath, final XmlDomAdapterNode clickedItem, final JTree tree) { final Element clickedElement = (Element) clickedItem.node; // popup menu for list -> add final JPopupMenu popup = new JPopupMenu(); //delete node final JMenuItem menuItemToDelete = new JMenuItem(ResourceUtils.getI18n("DELETE")); menuItemToDelete.addActionListener(new ActionListener() { @Override// w w w . ja v a 2 s . c om public void actionPerformed(ActionEvent actionEvent) { // find the order# of the child to delete final int index = clickedItem.getParent().index(clickedItem); Node oldChild = clickedElement.getParentNode().removeChild(clickedElement); if (index > -1) { validationPolicyTreeModel.fireTreeNodesRemoved(selectedPath.getParentPath(), index, oldChild); } } }); popup.add(menuItemToDelete); //Add node/value final Map<XsdNode, Object> xsdTree = validationPolicyTreeModel.getXsdTree(); final List<XsdNode> children = getChildrenToAdd(clickedElement, xsdTree); for (final XsdNode xsdChild : children) { final String xmlName = xsdChild.getLastNameOfPath(); final JMenuItem menuItem = new JMenuItem(ResourceUtils.getI18n("ADD") + " (" + xmlName + " " + xsdChild.getType().toString().toLowerCase() + ")"); popup.add(menuItem); menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { Document document = getModel().getValidationPolicy().getDocument(); final Node newElement; if (xsdChild.getType() == XsdNodeType.TEXT) { // TEXT element always appended (last child) newElement = clickedElement.appendChild(document.createTextNode("VALUE")); } else if (xsdChild.getType() == XsdNodeType.ATTRIBUTE) { newElement = document.createAttributeNS(null, xmlName); ((Attr) newElement).setValue("VALUE"); clickedElement.setAttributeNode((Attr) newElement); } else if (xsdChild.getType() == XsdNodeType.ELEMENT) { final Element childToAdd = document .createElementNS("http://dss.markt.ec.europa.eu/validation/diagnostic", xmlName); // find the correct possition to add the child // Get all allowed children Map<XsdNode, Object> childrenMap = getChild(getXPath(clickedElement), xsdTree); boolean toAddSeen = false; Element elementIsToAddBeforeThisOne = null; for (final XsdNode allowed : childrenMap.keySet()) { if (!toAddSeen && allowed == xsdChild) { toAddSeen = true; continue; } if (toAddSeen) { final NodeList elementsByTagNameNS = clickedElement.getElementsByTagNameNS( "http://dss.markt.ec.europa.eu/validation/diagnostic", allowed.getLastNameOfPath()); if (elementsByTagNameNS.getLength() > 0) { // we found an element that is supposed to be after the one to add elementIsToAddBeforeThisOne = (Element) elementsByTagNameNS.item(0); break; } } } if (elementIsToAddBeforeThisOne != null) { newElement = clickedElement.insertBefore(childToAdd, elementIsToAddBeforeThisOne); } else { newElement = clickedElement.appendChild(childToAdd); } } else { throw new IllegalArgumentException("Unknow XsdNode NodeType " + xsdChild.getType()); } document.normalizeDocument(); int indexOfAddedItem = 0; final int childCount = clickedItem.childCount(); for (int i = 0; i < childCount; i++) { if (clickedItem.child(i).node == newElement) { indexOfAddedItem = i; break; } } TreeModelEvent event = new TreeModelEvent(validationPolicyTreeModel, selectedPath, new int[] { indexOfAddedItem }, new Object[] { newElement }); validationPolicyTreeModel.fireTreeNodesInserted(event); tree.expandPath(selectedPath); //Update model getModel().getValidationPolicy().setXmlDom(new XmlDom(document)); } }); } popup.show(tree, mouseEvent.getX(), mouseEvent.getY()); }
From source file:eu.europa.esig.dss.applet.view.validationpolicy.EditView.java
private void nodeActionAdd(MouseEvent mouseEvent, final int selectedRow, final TreePath selectedPath, final XmlDomAdapterNode clickedItem, final JTree tree) { final Element clickedElement = (Element) clickedItem.node; // popup menu for list -> add final JPopupMenu popup = new JPopupMenu(); //delete node final JMenuItem menuItemToDelete = new JMenuItem(ResourceUtils.getI18n("DELETE")); menuItemToDelete.addActionListener(new ActionListener() { @Override/*w w w. j av a2s . co m*/ public void actionPerformed(ActionEvent actionEvent) { // find the order# of the child to delete final int index = clickedItem.getParent().index(clickedItem); Node oldChild = clickedElement.getParentNode().removeChild(clickedElement); if (index > -1) { validationPolicyTreeModel.fireTreeNodesRemoved(selectedPath.getParentPath(), index, oldChild); } } }); popup.add(menuItemToDelete); //Add node/value final Map<XsdNode, Object> xsdTree = validationPolicyTreeModel.getXsdTree(); final List<XsdNode> children = getChildrenToAdd(clickedElement, xsdTree); for (final XsdNode xsdChild : children) { final String xmlName = xsdChild.getLastNameOfPath(); final JMenuItem menuItem = new JMenuItem(ResourceUtils.getI18n("ADD") + " (" + xmlName + " " + xsdChild.getType().toString().toLowerCase() + ")"); popup.add(menuItem); menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { Document document = getModel().getValidationPolicy().getDocument(); final Node newElement; if (xsdChild.getType() == XsdNodeType.TEXT) { // TEXT element always appended (last child) newElement = clickedElement.appendChild(document.createTextNode("VALUE")); } else if (xsdChild.getType() == XsdNodeType.ATTRIBUTE) { newElement = document.createAttributeNS(null, xmlName); ((Attr) newElement).setValue("VALUE"); clickedElement.setAttributeNode((Attr) newElement); } else if (xsdChild.getType() == XsdNodeType.ELEMENT) { final Element childToAdd = document .createElementNS("http://dss.esig.europa.eu/validation/diagnostic", xmlName); // find the correct possition to add the child // Get all allowed children Map<XsdNode, Object> childrenMap = getChild(getXPath(clickedElement), xsdTree); boolean toAddSeen = false; Element elementIsToAddBeforeThisOne = null; for (final XsdNode allowed : childrenMap.keySet()) { if (!toAddSeen && (allowed == xsdChild)) { toAddSeen = true; continue; } if (toAddSeen) { final NodeList elementsByTagNameNS = clickedElement.getElementsByTagNameNS( "http://dss.esig.europa.eu/validation/diagnostic", allowed.getLastNameOfPath()); if (elementsByTagNameNS.getLength() > 0) { // we found an element that is supposed to be after the one to add elementIsToAddBeforeThisOne = (Element) elementsByTagNameNS.item(0); break; } } } if (elementIsToAddBeforeThisOne != null) { newElement = clickedElement.insertBefore(childToAdd, elementIsToAddBeforeThisOne); } else { newElement = clickedElement.appendChild(childToAdd); } } else { throw new IllegalArgumentException("Unknow XsdNode NodeType " + xsdChild.getType()); } document.normalizeDocument(); int indexOfAddedItem = 0; final int childCount = clickedItem.childCount(); for (int i = 0; i < childCount; i++) { if (clickedItem.child(i).node == newElement) { indexOfAddedItem = i; break; } } TreeModelEvent event = new TreeModelEvent(validationPolicyTreeModel, selectedPath, new int[] { indexOfAddedItem }, new Object[] { newElement }); validationPolicyTreeModel.fireTreeNodesInserted(event); tree.expandPath(selectedPath); //Update model getModel().getValidationPolicy().setXmlDom(new XmlDom(document)); } }); } popup.show(tree, mouseEvent.getX(), mouseEvent.getY()); }
From source file:com.nbt.TreeFrame.java
private void add(final Tag<?> tag, TreePath path) { Object last = path.getLastPathComponent(); if (!(last instanceof Mutable)) { TreePath parentPath = path.getParentPath(); add(tag, parentPath);//w w w. j a v a 2s.c om return; } Object scroll = tag; if (last instanceof Mutable) { Mutable mutable = (Mutable) last; if (last instanceof ByteArrayTag) { mutable.add(null); } else if (last instanceof ByteWrapper) { ByteWrapper wrapper = (ByteWrapper) last; int index = wrapper.getIndex() + 1; mutable.add(index, null); scroll = index; } else if (last instanceof ListTag) { mutable.add(tag); } else if (last instanceof CompoundTag || last instanceof TagWrapper) { mutable.add(tag); } nodesInserted(last, path); } scrollTo(scroll); }