Example usage for javax.swing.tree TreePath TreePath

List of usage examples for javax.swing.tree TreePath TreePath

Introduction

In this page you can find the example usage for javax.swing.tree TreePath TreePath.

Prototype

public TreePath(Object lastPathComponent) 

Source Link

Document

Creates a TreePath containing a single element.

Usage

From source file:org.apache.jmeter.visualizers.ViewResultsFullVisualizer.java

/**
 * Update the visualizer with new data.// w ww  .j  a v a 2 s  .  co  m
 */
private synchronized void updateGui(SampleResult res) {
    // Add sample
    DefaultMutableTreeNode currNode = new SearchableTreeNode(res, treeModel);
    treeModel.insertNodeInto(currNode, root, root.getChildCount());
    addSubResults(currNode, res);
    // Add any assertion that failed as children of the sample node
    AssertionResult[] assertionResults = res.getAssertionResults();
    int assertionIndex = currNode.getChildCount();
    for (AssertionResult assertionResult : assertionResults) {
        if (assertionResult.isFailure() || assertionResult.isError()) {
            DefaultMutableTreeNode assertionNode = new SearchableTreeNode(assertionResult, treeModel);
            treeModel.insertNodeInto(assertionNode, currNode, assertionIndex++);
        }
    }

    if (root.getChildCount() == 1) {
        jTree.expandPath(new TreePath(root));
    }
    if (autoScrollCB.isSelected() && root.getChildCount() > 1) {
        jTree.scrollPathToVisible(
                new TreePath(new Object[] { root, treeModel.getChild(root, root.getChildCount() - 1) }));
    }
}

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultTreeView.java

public TreePath getTreePath(DefaultMutableTreeNode node, ModelGraph graph) {
    if (node.getUserObject().equals(graph)) {
        return new TreePath(node.getPath());
    } else {//from ww  w.  j  a  v  a  2  s . c o  m
        for (int i = 0; i < node.getChildCount(); i++) {
            // System.out.println("i: " + ((DefaultMutableTreeNode)
            // node.getChildAt(i)).getUserObject());
            TreePath treePath = this.getTreePath((DefaultMutableTreeNode) node.getChildAt(i), graph);
            if (treePath != null) {
                return treePath;
            }
        }
        return null;
    }
}

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultTreeView.java

private TreePath getTreePath(TreePath currentPath, ViewState state) {
    String lookingForPath = state.getCurrentMetGroup();
    Stack<DefaultMutableTreeNode> stack = new Stack<DefaultMutableTreeNode>();
    DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) currentPath.getLastPathComponent();
    for (int i = 0; i < baseNode.getChildCount(); i++) {
        stack.push((DefaultMutableTreeNode) baseNode.getChildAt(i));
    }/*from   ww w  .ja  v a2 s .  c om*/
    while (!stack.empty()) {
        DefaultMutableTreeNode node = stack.pop();
        if (node.getUserObject().equals("static-metadata")) {
            for (int i = 0; i < node.getChildCount(); i++) {
                stack.push((DefaultMutableTreeNode) node.getChildAt(i));
            }
        } else if (node.getUserObject() instanceof ConcurrentHashMap) {
            String key = (String) ((ConcurrentHashMap<String, String>) node.getUserObject()).keySet().iterator()
                    .next();
            if (lookingForPath.equals(key)) {
                return new TreePath(node.getPath());
            } else if (lookingForPath.startsWith(key + "/")) {
                lookingForPath = lookingForPath.substring(lookingForPath.indexOf("/") + 1);
                stack.clear();
                for (int i = 0; i < node.getChildCount(); i++) {
                    stack.add((DefaultMutableTreeNode) node.getChildAt(i));
                }
            }
        }
    }
    return currentPath;
}

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultTreeView.java

@Override
public void refreshView(final ViewState state) {
    Rectangle visibleRect = null;
    if (this.tree != null) {
        visibleRect = this.tree.getVisibleRect();
    }//  w  ww.j  a va2s  .co  m

    this.removeAll();

    this.actionsMenu = this.createPopupMenu(state);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("WORKFLOWS");
    for (ModelGraph graph : state.getGraphs()) {
        root.add(this.buildTree(graph, state));
    }
    tree = new JTree(root);
    tree.setShowsRootHandles(true);
    tree.setRootVisible(false);
    tree.add(this.actionsMenu);

    if (state.getSelected() != null) {
        // System.out.println("SELECTED: " + state.getSelected());
        TreePath treePath = this.getTreePath(root, state.getSelected());
        if (state.getCurrentMetGroup() != null) {
            treePath = this.getTreePath(treePath, state);
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_STATIC_METADATA))) {
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("static-metadata")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_PRECONDITIONS))) {
            if (treePath == null) {
                treePath = this.getTreePath(root, state.getSelected().getPreConditions());
            }
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("pre-conditions")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_POSTCONDITIONS))) {
            if (treePath == null) {
                treePath = this.getTreePath(root, state.getSelected().getPostConditions());
            }
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("post-conditions")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        }
        this.tree.expandPath(treePath);
        this.tree.setSelectionPath(treePath);
    }

    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            if (e.getPath().getLastPathComponent() instanceof DefaultMutableTreeNode) {
                DefaultTreeView.this.resetProperties(state);
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                if (node.getUserObject() instanceof ModelGraph) {
                    state.setSelected((ModelGraph) node.getUserObject());
                    state.setCurrentMetGroup(null);
                    DefaultTreeView.this.notifyListeners();
                } else if (node.getUserObject().equals("static-metadata")
                        || node.getUserObject().equals("pre-conditions")
                        || node.getUserObject().equals("post-conditions")) {
                    state.setSelected((ModelGraph) ((DefaultMutableTreeNode) node.getParent()).getUserObject());
                    state.setCurrentMetGroup(null);
                    state.setProperty(EXPAND_STATIC_METADATA,
                            Boolean.toString(node.getUserObject().equals("static-metadata")));
                    state.setProperty(EXPAND_PRECONDITIONS,
                            Boolean.toString(node.getUserObject().equals("pre-conditions")));
                    state.setProperty(EXPAND_POSTCONDITIONS,
                            Boolean.toString(node.getUserObject().equals("post-conditions")));
                    DefaultTreeView.this.notifyListeners();
                } else if (node.getUserObject() instanceof ConcurrentHashMap) {
                    DefaultMutableTreeNode metNode = null;
                    String group = null;
                    Object[] path = e.getPath().getPath();
                    for (int i = path.length - 1; i >= 0; i--) {
                        if (((DefaultMutableTreeNode) path[i]).getUserObject() instanceof ModelGraph) {
                            metNode = (DefaultMutableTreeNode) path[i];
                            break;
                        } else if (((DefaultMutableTreeNode) path[i])
                                .getUserObject() instanceof ConcurrentHashMap) {
                            if (group == null) {
                                group = (String) ((ConcurrentHashMap<String, String>) ((DefaultMutableTreeNode) path[i])
                                        .getUserObject()).keySet().iterator().next();
                            } else {
                                group = (String) ((ConcurrentHashMap<String, String>) ((DefaultMutableTreeNode) path[i])
                                        .getUserObject()).keySet().iterator().next() + "/" + group;
                            }
                        }
                    }
                    ModelGraph graph = (ModelGraph) metNode.getUserObject();
                    state.setSelected(graph);
                    state.setCurrentMetGroup(group);
                    DefaultTreeView.this.notifyListeners();
                } else {
                    state.setSelected(null);
                    DefaultTreeView.this.notifyListeners();
                }
            }
        }

    });
    tree.setCellRenderer(new TreeCellRenderer() {

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            if (node.getUserObject() instanceof String) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                JLabel label = new JLabel((String) node.getUserObject());
                label.setForeground(Color.blue);
                panel.add(label, BorderLayout.CENTER);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else if (node.getUserObject() instanceof ModelGraph) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                JLabel iconLabel = new JLabel(
                        ((ModelGraph) node.getUserObject()).getModel().getExecutionType() + ": ");
                iconLabel.setForeground(((ModelGraph) node.getUserObject()).getModel().getColor());
                iconLabel.setBackground(Color.white);
                JLabel idLabel = new JLabel(((ModelGraph) node.getUserObject()).getModel().getModelName());
                idLabel.setBackground(Color.white);
                panel.add(iconLabel, BorderLayout.WEST);
                panel.add(idLabel, BorderLayout.CENTER);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else if (node.getUserObject() instanceof ConcurrentHashMap) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                String group = (String) ((ConcurrentHashMap<String, String>) node.getUserObject()).keySet()
                        .iterator().next();
                JLabel nameLabel = new JLabel(group + " : ");
                nameLabel.setForeground(Color.blue);
                nameLabel.setBackground(Color.white);
                JLabel valueLabel = new JLabel(
                        ((ConcurrentHashMap<String, String>) node.getUserObject()).get(group));
                valueLabel.setForeground(Color.darkGray);
                valueLabel.setBackground(Color.white);
                panel.add(nameLabel, BorderLayout.WEST);
                panel.add(valueLabel, BorderLayout.EAST);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else {
                return new JLabel();
            }
        }

    });
    tree.addMouseListener(new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3 && DefaultTreeView.this.tree.getSelectionPath() != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) DefaultTreeView.this.tree
                        .getSelectionPath().getLastPathComponent();
                if (node.getUserObject() instanceof String && !(node.getUserObject().equals("pre-conditions")
                        || node.getUserObject().equals("post-conditions"))) {
                    return;
                }
                orderSubMenu.setEnabled(node.getUserObject() instanceof ModelGraph
                        && !((ModelGraph) node.getUserObject()).isCondition()
                        && ((ModelGraph) node.getUserObject()).getParent() != null);
                DefaultTreeView.this.actionsMenu.show(DefaultTreeView.this.tree, e.getX(), e.getY());
            }
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }
    });
    this.scrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    this.add(this.scrollPane, BorderLayout.CENTER);
    if (visibleRect != null) {
        this.tree.scrollRectToVisible(visibleRect);
    }

    this.revalidate();
}

From source file:org.eclipse.jubula.rc.swing.tester.util.TreeOperationContext.java

/**
 * Returns the row for the given node. The row is calculated based on how 
 * many nodes are visible above this node.
 * @param node  The node for which to find the row.
 * @return//from   w ww.j  a  v a 2  s.  c  o m
 *      A zero-based index representing the row that the given node 
 *      occupies in the tree.
 * @throws StepExecutionException
 *      if the node cannot be found.
 */
protected int getRowForTreeNode(final Object node) throws StepExecutionException {

    Integer row = (Integer) getQueuer().invokeAndWait("getRowForTreeNode", new IRunnable() { //$NON-NLS-1$
        public Object run() {
            TreePath pathToRoot = new TreePath(getPathToRoot(node));
            return new Integer(((JTree) getTree()).getRowForPath(pathToRoot));
        }
    });
    return row.intValue();
}

From source file:org.eclipse.jubula.rc.swing.tester.util.TreeOperationContext.java

/**
 * {@inheritDoc}// w w  w  . j  av a2  s.c om
 */
public boolean isVisible(final Object node) {
    Boolean visible = (Boolean) getQueuer().invokeAndWait("isVisible", //$NON-NLS-1$
            new IRunnable() {
                public Object run() {
                    Object[] path = getPathToRoot(node);
                    return (((JTree) getTree()).isVisible(new TreePath(path))) ? Boolean.TRUE : Boolean.FALSE;
                }
            });
    return visible.booleanValue();
}

From source file:org.geopublishing.atlasViewer.GpCoreUtil.java

/**
 * e1027. Converting a {@link TreeNode} in a {@link JTree} Component to a
 * {@link TreePath} Returns a {@link TreePath} containing the specified
 * node.//from   ww w.j  a v  a 2s .com
 */
public static final TreePath getPath(TreeNode node) {
    final List<TreeNode> list = new ArrayList<TreeNode>();

    // Add all nodes to list
    while (node != null) {
        list.add(node);
        node = node.getParent();
    }
    Collections.reverse(list);

    // Convert array of nodes to TreePath
    return new TreePath(list.toArray());
}

From source file:org.geopublishing.atlasViewer.GpCoreUtil.java

/**
 * If expand is true, expands all nodes in the tree. Otherwise, collapses
 * all nodes in the tree.//  ww w.j av  a  2  s  .  c om
 */
public final static void expandAll(final JTree tree, final boolean expand) {
    final TreeNode root = (TreeNode) tree.getModel().getRoot();

    // Traverse tree from root
    expandAll(tree, new TreePath(root), expand);
}

From source file:org.hippoecm.frontend.model.tree.ObservableTreeModel.java

public TreePath lookup(JcrNodeModel nodeModel) {
    IJcrTreeNode node = root;/* www  . j  a  v a  2 s . c  o m*/
    if (nodeModel != null) {
        String basePath = ((JcrNodeModel) root.getNodeModel()).getItemModel().getPath();
        String path = nodeModel.getItemModel().getPath();
        if (path != null && path.startsWith(basePath)) {
            String[] elements = StringUtils.split(path.substring(basePath.length()), '/');
            List<Object> nodes = new LinkedList<Object>();
            nodes.add(node);
            try {
                for (String element : elements) {
                    IJcrTreeNode child = node.getChild(element);
                    if (child != null) {
                        nodes.add(child);
                        node = child;
                    } else {
                        break;
                    }
                }
            } catch (RepositoryException ex) {
                log.error("Unable to find node in tree", ex.getMessage());
            }
            return new TreePath(nodes.toArray(new Object[nodes.size()]));
        }
    }
    return null;
}

From source file:org.kepler.gui.OntLibrarySearcher.java

private void buildHashTable(NamedObj parent, TreeModel model) {
    for (int i = 0; i < model.getChildCount(parent); i++) {
        NamedObj child = (NamedObj) model.getChild(parent, i);
        _pathStack.push(child);//from   www.  ja  v  a 2s.  c  o m
        if (model.isLeaf(child)) {
            Iterator<String> iter = getIds(child).iterator();
            while (iter.hasNext()) {
                TreePath path = new TreePath(_pathStack.toArray());
                hashTablePut(iter.next(), path);
            }
        } else {
            buildHashTable(child, model);
        }

        _pathStack.pop();
    }

}