Example usage for javax.swing.tree TreePath getLastPathComponent

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

Introduction

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

Prototype

public Object getLastPathComponent() 

Source Link

Document

Returns the last element of this path.

Usage

From source file:org.apache.cayenne.modeler.ProjectTreeView.java

/**
 * Processes node selection regardless of whether a new node was selected, or an
 * already selected node was clicked again. Normally called from event listener
 * methods.//from w  ww  .j av a 2 s .c o  m
 */
public void processSelection(TreePath path) {
    if (path == null) {
        return;
    }

    DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) path.getLastPathComponent();

    Object[] data = getUserObjects(currentNode);
    if (data.length == 0) {
        // this should clear the right-side panel
        DomainDisplayEvent domEvent = new DomainDisplayEvent(this, null);
        domEvent.setDomain((DataChannelDescriptor) mediator.getProject().getRootNode());
        mediator.fireDomainDisplayEvent(domEvent);
        return;
    }

    Object obj = data[data.length - 1];
    if (obj instanceof DataChannelDescriptor) {
        mediator.fireDomainDisplayEvent(new DomainDisplayEvent(this, (DataChannelDescriptor) obj));
    } else if (obj instanceof DataMap) {
        if (data.length == 2) {
            mediator.fireDataMapDisplayEvent(new DataMapDisplayEvent(this, (DataMap) obj,
                    (DataChannelDescriptor) mediator.getProject().getRootNode(),
                    (DataNodeDescriptor) data[data.length - 2]));
        } else if (data.length == 1) {
            mediator.fireDataMapDisplayEvent(new DataMapDisplayEvent(this, (DataMap) obj,
                    (DataChannelDescriptor) mediator.getProject().getRootNode()));
        }
    } else if (obj instanceof DataNodeDescriptor) {
        if (data.length == 1) {
            mediator.fireDataNodeDisplayEvent(new DataNodeDisplayEvent(this,
                    (DataChannelDescriptor) mediator.getProject().getRootNode(), (DataNodeDescriptor) obj));
        }
    } else if (obj instanceof Entity) {
        EntityDisplayEvent e = new EntityDisplayEvent(this, (Entity) obj);
        e.setUnselectAttributes(true);
        if (data.length == 3) {
            e.setDataMap((DataMap) data[data.length - 2]);
            e.setDomain((DataChannelDescriptor) mediator.getProject().getRootNode());
            e.setDataNode((DataNodeDescriptor) data[data.length - 3]);
        } else if (data.length == 2) {
            e.setDataMap((DataMap) data[data.length - 2]);
            e.setDomain((DataChannelDescriptor) mediator.getProject().getRootNode());
        }

        if (obj instanceof ObjEntity) {
            mediator.fireObjEntityDisplayEvent(e);
        } else if (obj instanceof DbEntity) {
            mediator.fireDbEntityDisplayEvent(e);
        }
    } else if (obj instanceof Embeddable) {
        EmbeddableDisplayEvent e = new EmbeddableDisplayEvent(this, (Embeddable) obj,
                (DataMap) data[data.length - 2], (DataChannelDescriptor) mediator.getProject().getRootNode());
        mediator.fireEmbeddableDisplayEvent(e);
    } else if (obj instanceof Procedure) {
        ProcedureDisplayEvent e = new ProcedureDisplayEvent(this, (Procedure) obj,
                (DataMap) data[data.length - 2], (DataChannelDescriptor) mediator.getProject().getRootNode());
        mediator.fireProcedureDisplayEvent(e);
    } else if (obj instanceof QueryDescriptor) {
        QueryDisplayEvent e = new QueryDisplayEvent(this, (QueryDescriptor) obj,
                (DataMap) data[data.length - 2], (DataChannelDescriptor) mediator.getProject().getRootNode());
        mediator.fireQueryDisplayEvent(e);
    }

    this.scrollPathToVisible(path);
}

From source file:org.apache.jmeter.gui.MainFrame.java

/**
 * Create and initialize the GUI representation of the test tree.
 *
 * @param treeModel/*from  w  w w  .  j a  va 2 s  .  com*/
 *            the test tree model
 * @param treeListener
 *            the test tree listener
 *
 * @return the initialized test tree GUI
 */
private JTree makeTree(TreeModel treeModel, JMeterTreeListener treeListener) {
    JTree treevar = new JTree(treeModel) {
        private static final long serialVersionUID = 240L;

        @Override
        public String getToolTipText(MouseEvent event) {
            TreePath path = this.getPathForLocation(event.getX(), event.getY());
            if (path != null) {
                Object treeNode = path.getLastPathComponent();
                if (treeNode instanceof DefaultMutableTreeNode) {
                    Object testElement = ((DefaultMutableTreeNode) treeNode).getUserObject();
                    if (testElement instanceof TestElement) {
                        String comment = ((TestElement) testElement).getComment();
                        if (comment != null && comment.length() > 0) {
                            return comment;
                        }
                    }
                }
            }
            return null;
        }
    };
    treevar.setToolTipText("");
    treevar.setCellRenderer(getCellRenderer());
    treevar.setRootVisible(false);
    treevar.setShowsRootHandles(true);

    treeListener.setJTree(treevar);
    treevar.addTreeSelectionListener(treeListener);
    treevar.addMouseListener(treeListener);
    treevar.addKeyListener(treeListener);

    // enable drag&drop, install a custom transfer handler
    treevar.setDragEnabled(true);
    treevar.setDropMode(DropMode.ON_OR_INSERT);
    treevar.setTransferHandler(new JMeterTreeTransferHandler());

    addQuickComponentHotkeys(treevar);

    return treevar;
}

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  w  ww .j  a  v a 2  s.c o  m*/
    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();
    }/*ww w.  j ava2 s.c  o  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.apache.pdfbox.debugger.PDFDebugger.java

private void jTree1ValueChanged(TreeSelectionEvent evt) {
    TreePath path = tree.getSelectionPath();
    if (path != null) {
        try {/* www  .  j  ava2  s  . co m*/
            Object selectedNode = path.getLastPathComponent();

            statusBar.getStatusLabel().setText("");

            if (isPage(selectedNode)) {
                showPage(selectedNode);
                return;
            }

            if (isSpecialColorSpace(selectedNode) || isOtherColorSpace(selectedNode)) {
                showColorPane(selectedNode);
                return;
            }
            if (path.getParentPath() != null
                    && isFlagNode(selectedNode, path.getParentPath().getLastPathComponent())) {
                Object parentNode = path.getParentPath().getLastPathComponent();
                showFlagPane(parentNode, selectedNode);
                return;
            }
            if (isStream(selectedNode)) {
                showStream((COSStream) getUnderneathObject(selectedNode), path);
                return;
            }
            if (isFont(selectedNode)) {
                showFont(selectedNode, path);
                return;
            }
            if (isString(selectedNode)) {
                showString(selectedNode);
                return;
            }
            if (jSplitPane1.getRightComponent() == null
                    || !jSplitPane1.getRightComponent().equals(jScrollPane2)) {
                replaceRightComponent(jScrollPane2);
            }
            jTextPane1.setText(convertToString(selectedNode));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

From source file:org.apache.pdfbox.debugger.PDFDebugger.java

private void showStream(COSStream stream, TreePath path) throws IOException {
    boolean isContentStream = false;
    boolean isThumb = false;

    COSName key = getNodeKey(path.getLastPathComponent());
    COSName parentKey = getNodeKey(path.getParentPath().getLastPathComponent());
    COSDictionary resourcesDic = null;/*w ww .ja v  a  2 s .c  om*/

    if (COSName.CONTENTS.equals(key)) {
        Object pageObj = path.getParentPath().getLastPathComponent();
        COSDictionary page = (COSDictionary) getUnderneathObject(pageObj);
        resourcesDic = (COSDictionary) page.getDictionaryObject(COSName.RESOURCES);
        isContentStream = true;
    } else if (COSName.CONTENTS.equals(parentKey) || COSName.CHAR_PROCS.equals(parentKey)) {
        Object pageObj = path.getParentPath().getParentPath().getLastPathComponent();
        COSDictionary page = (COSDictionary) getUnderneathObject(pageObj);
        resourcesDic = (COSDictionary) page.getDictionaryObject(COSName.RESOURCES);
        isContentStream = true;
    } else if (COSName.FORM.equals(stream.getCOSName(COSName.SUBTYPE))
            || COSName.PATTERN.equals(stream.getCOSName(COSName.TYPE))
            || stream.getInt(COSName.PATTERN_TYPE) == 1) {
        if (stream.containsKey(COSName.RESOURCES)) {
            resourcesDic = (COSDictionary) stream.getDictionaryObject(COSName.RESOURCES);
        }
        isContentStream = true;
    } else if (COSName.THUMB.equals(key)) {
        resourcesDic = null;
        isThumb = true;
    } else if (COSName.IMAGE.equals((stream).getCOSName(COSName.SUBTYPE))) {
        // not to be used for /Thumb, even if it contains /Subtype /Image
        Object resourcesObj = path.getParentPath().getParentPath().getLastPathComponent();
        resourcesDic = (COSDictionary) getUnderneathObject(resourcesObj);
    }
    StreamPane streamPane = new StreamPane(stream, isContentStream, isThumb, resourcesDic);
    replaceRightComponent(streamPane.getPanel());
}

From source file:org.codinjutsu.tools.jenkins.view.BrowserPanel.java

private Tree createTree(final List<JenkinsSettings.FavoriteJob> favoriteJobs) {

    SimpleTree tree = new SimpleTree();
    tree.getEmptyText().setText(LOADING);
    tree.setCellRenderer(new JenkinsTreeRenderer(favoriteJobs));
    tree.setName("jobTree");
    tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(jenkins)));

    new TreeSpeedSearch(tree, new Convertor<TreePath, String>() {

        @Override/*from  ww w.ja  va 2s.  c om*/
        public String convert(TreePath treePath) {
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            final Object userObject = node.getUserObject();
            if (userObject instanceof Job) {
                return ((Job) userObject).getName();
            }
            return "<empty>";
        }
    });

    return tree;
}

From source file:org.executequery.gui.browser.TreeFindAction.java

protected boolean changed(JComponent comp, String searchString, Position.Bias bias) {

    if (StringUtils.isBlank(searchString)) {

        return false;
    }/* www.j  av a2  s  .c o m*/

    JTree tree = (JTree) comp;
    String prefix = searchString;

    if (ignoreCase()) {

        prefix = prefix.toUpperCase();
    }

    boolean wildcardStart = prefix.startsWith("*");
    if (wildcardStart) {

        prefix = prefix.substring(1);

    } else {

        prefix = "^" + prefix;
    }
    prefix = prefix.replaceAll("\\*", ".*");

    Matcher matcher = Pattern.compile(prefix).matcher("");
    List<TreePath> matchedPaths = new ArrayList<TreePath>();
    for (int i = 1; i < tree.getRowCount(); i++) {

        TreePath path = tree.getPathForRow(i);
        String text = tree.convertValueToText(path.getLastPathComponent(), tree.isRowSelected(i),
                tree.isExpanded(i), true, i, false);

        if (ignoreCase()) {

            text = text.toUpperCase();
        }

        //            if ((wildcardStart && text.contains(prefix)) || text.startsWith(prefix, 0)) {
        //
        //                matchedPaths.add(path);
        //            }

        matcher.reset(text);
        if (matcher.find()) {

            matchedPaths.add(path);
        }

    }

    foundValues(matchedPaths);

    return !(matchedPaths.isEmpty());
}

From source file:org.executequery.gui.browser.TreeFindAction.java

public TreePath getNextMatch(JTree tree, String prefix, int startingRow, Position.Bias bias) {

    int max = tree.getRowCount();
    if (prefix == null) {
        throw new IllegalArgumentException();
    }/*  www  .j  a v a2  s.  c  om*/
    if (startingRow < 0 || startingRow >= max) {
        throw new IllegalArgumentException();
    }

    if (ignoreCase()) {
        prefix = prefix.toUpperCase();
    }

    // start search from the next/previous element froom the
    // selected element
    int increment = (bias == null || bias == Position.Bias.Forward) ? 1 : -1;

    int row = startingRow;
    do {

        TreePath path = tree.getPathForRow(row);
        String text = tree.convertValueToText(path.getLastPathComponent(), tree.isRowSelected(row),
                tree.isExpanded(row), true, row, false);

        if (ignoreCase()) {

            text = text.toUpperCase();
        }

        if (text.startsWith(prefix)) {

            return path;
        }

        row = (row + increment + max) % max;

    } while (row != startingRow);

    return null;
}

From source file:org.feistymeow.dragdrop.dragdrop_tree_test.java

public void valueChanged(TreeSelectionEvent e) {
    fileName.setText("");
    TreePath sel_path = larch.getSelectionPath();
    if (sel_path != null) {
        Hashtable<String, String> table = NodeToTable(sel_path.getLastPathComponent());
        if (table != null) {
            String name = (String) table.get("name");
            fileName.setText(name);// www  . jav a2s .  com
        }
    }
}