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.optaplanner.benchmark.impl.aggregator.swingui.BenchmarkAggregatorFrame.java

private CheckBoxTree createCheckBoxTree() {
    final CheckBoxTree resultCheckBoxTree = new CheckBoxTree(initBenchmarkHierarchy(true));
    resultCheckBoxTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override/*from   w  ww  .j  a v a  2s . c  o  m*/
        public void valueChanged(TreeSelectionEvent e) {
            TreePath treeSelectionPath = e.getNewLeadSelectionPath();
            if (treeSelectionPath != null) {
                DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treeSelectionPath
                        .getLastPathComponent();
                MixedCheckBox checkBox = (MixedCheckBox) treeNode.getUserObject();
                detailTextArea.setText(checkBox.getDetail());
                detailTextArea.setCaretPosition(0);
                renameNodeButton.setEnabled(checkBox.getBenchmarkResult() instanceof PlannerBenchmarkResult
                        || checkBox.getBenchmarkResult() instanceof SolverBenchmarkResult);
            }
        }
    });
    resultCheckBoxTree.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            // Enable button if checked singleBenchmarkResults exist
            generateReportButton.setEnabled(!resultCheckBoxTree.getSelectedSingleBenchmarkNodes().isEmpty());
        }
    });
    checkBoxTree = resultCheckBoxTree;
    return resultCheckBoxTree;
}

From source file:org.orbisgis.sif.components.fstree.FileTree.java

/**
 * Called when the tree selection change.
 * Update the tree editable state//from  w w  w  .  ja  va2s  . co  m
 */
public void onSelectionChange() {
    TreePath[] Selected = getSelectionPaths();

    if (Selected != null) {
        TreePath firstSelected = Selected[Selected.length - 1];
        Object comp = firstSelected.getLastPathComponent();
        if (comp instanceof AbstractTreeNode) {
            if (!isEditing()) {
                setEditable(((AbstractTreeNode) comp).isEditable());
            }
        }
    }
}

From source file:org.orbisgis.sif.components.fstree.FileTree.java

/**
 * Fetch all selected items to make a pop-up menu
 * @return // w  ww. j a va2 s  . c  o  m
 */
private JPopupMenu makePopupMenu() {
    JPopupMenu menu = new JPopupMenu();
    TreePath[] paths = getSelectionPaths();
    if (paths != null) {
        // Generic action on single TreeNode
        if (paths.length == 1) {
            Object component = paths[0].getLastPathComponent();
            if (component instanceof AbstractTreeNode) {
                AbstractTreeNode aTreeNode = (AbstractTreeNode) component;
                if (aTreeNode.isEditable()) {
                    JMenuItem editMenu = new JMenuItem(I18N.tr("Rename"));
                    editMenu.addActionListener(EventHandler.create(ActionListener.class, this, "onRenameItem"));
                    editMenu.setActionCommand("rename");
                    menu.add(editMenu);
                }
            }
        }
        for (TreePath treePath : paths) {
            Object component = treePath.getLastPathComponent();
            // All nodes
            if (component instanceof MutableTreeNode) {
                MutableTreeNode node = (MutableTreeNode) component;
                for (TreeNodeFileFactory fact : getFactories()) {
                    fact.feedTreeNodePopupMenu(node, menu);
                }
            }
            // Specific nodes
            if (component instanceof PopupTreeNode) {
                PopupTreeNode treeNode = (PopupTreeNode) component;
                treeNode.feedPopupMenu(menu);
            }
        }
    }
    return menu;
}

From source file:org.owasp.jbrofuzz.headers.HeaderLoader.java

protected Header getHeader(final TreePath treePath) {

    if (!((HeaderTreeNode) treePath.getLastPathComponent()).isLeaf()) {
        return Header.ZERO;
    }/*  w  w  w  . j  av a  2s  .c  o m*/

    for (final String headerName : headersMap.keySet()) {

        final Prototype proto = headersMap.get(headerName);

        final int catLength = proto.getNoOfCategories();

        final String[] categories = new String[catLength];
        proto.getCategories().toArray(categories);

        final Object[] path = treePath.getPath();
        int success = path.length - 1;

        for (int i = 0; i < path.length; i++) {
            try {

                if (path[i + 1].toString().equalsIgnoreCase(categories[i])) {
                    success--;
                } else {
                    i = 32;
                }

            } catch (final ArrayIndexOutOfBoundsException exp) {
                i = 32;
            }
        }
        // We have found the header we were looking for
        if (success == 0) {
            final int noOfFields = proto.size();

            final String[] output = new String[noOfFields];
            proto.getPayloads().toArray(output);

            final StringBuffer myBuffer = new StringBuffer();
            for (final String payload : output) {
                myBuffer.append(payload);
                myBuffer.append('\n');
            }
            myBuffer.append('\n');

            final String commentL = proto.getComment();

            return new Header(noOfFields, myBuffer.toString(), commentL);

        }

    }

    return Header.ZERO;

}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.dialogs.RepositoryTreeDialog.java

public FileObject getSelectedPath() {
    final TreePath selectionPath = repositoryBrowser.getSelectionPath();
    if (selectionPath == null) {
        return null;
    }//from   w  w w . j a  v a  2 s.  co m
    if (selectionPath.getLastPathComponent() instanceof FileObject) {
        return (FileObject) selectionPath.getLastPathComponent();
    }
    return null;
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.model.RepositoryTreeModelTest.java

@Test
public void testGetTreePathForSelection() {
    RepositoryTreeModel treeModel = new RepositoryTreeModel();
    assertNotNull(treeModel);// ww w .  j a va  2 s  . c  o  m
    treeModel.setFileSystemRoot(repositoryRoot);
    FileObject[] childFiles = new FileObject[] { childFile1, childFile2, childFile3 };
    try {
        doReturn(childFiles).when(repositoryRoot).getChildren();
        doReturn(childFileName1).when(childFile1).getName();
        doReturn(childFileName2).when(childFile2).getName();
        doReturn(childFileName3).when(childFile3).getName();
        doReturn(repositoryRoot).when(childFile1).getParent();
        doReturn(repositoryRoot).when(childFile2).getParent();
        doReturn(repositoryRoot).when(childFile3).getParent();
        TreePath path = treeModel.getTreePathForSelection(childFile2, null);
        assertEquals(2, path.getPath().length);
        assertEquals(childFile2, path.getLastPathComponent());
    } catch (FileSystemException e) {
        e.printStackTrace();
    }

}

From source file:org.pentaho.ui.xul.swing.tags.SwingTree.java

public Object getSelectedItem() {
    if (this.isHierarchical && this.elements != null) {

        int[] vals = tree.getSelectionRows();
        if (vals == null || vals.length == 0) {
            return null;
        }//  w ww.  j  a  v  a  2 s .c  om

        TreePath path = tree.getSelectionPath();
        if (path.getLastPathComponent() instanceof XulTreeNode) {
            XulTreeNode node = (XulTreeNode) path.getLastPathComponent();
            // now link node.item to object via bindings
            SearchBundle b = findSelectedIndex(new SearchBundle(), getRootChildren(), node.item);
            vals[0] = b.curPos;
        }

        String property = ((XulTreeCol) this.getColumns().getChildNodes().get(0)).getChildrenbinding();
        property = "get" + (property.substring(0, 1).toUpperCase() + property.substring(1)); //$NON-NLS-1$
        // Method childrenMethod = null;
        // try {
        // childrenMethod = elements.getClass().getMethod(property, new Class[] {});
        // } catch (NoSuchMethodException e) {
        // // Since this tree is built recursively, when at a leaf it will throw this exception.
        // logger.debug(e);
        // return null;
        // }

        FindSelectedItemTuple tuple = findSelectedItem(this.elements, property,
                new FindSelectedItemTuple(vals[0]));
        return tuple != null ? tuple.selectedItem : null;
    } else if (!this.isHierarchical() && elements != null && this.getSelectedRows().length > 0
            && elements.toArray().length > this.getSelectedRows()[0]) {
        return elements.toArray()[this.getSelectedRows()[0]];
    }
    return null;
}

From source file:org.piraso.ui.base.RequestTreeTopComponent.java

public RequestTreeTopComponent() {
    setName(Bundle.CTL_RequestTreeTopComponent());
    setToolTipText(Bundle.HINT_RequestTreeTopComponent());

    initComponents();/*from w  ww.  j a v a2 s.c  o  m*/

    InstanceContent content = new InstanceContent();
    associateLookup(new AbstractLookup(content));

    this.entryContent = new SingleClassInstanceContent<Entry>(content);

    jTree.setFont(FontProviderManager.INSTANCE.getEditorDefaultFont());
    jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    jTree.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int selRow = jTree.getRowForLocation(e.getX(), e.getY());
            TreePath selPath = jTree.getPathForLocation(e.getX(), e.getY());
            if (selRow != -1 && selPath != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
                Child child = null;
                if (Child.class.isInstance(node.getUserObject())) {
                    child = (Child) node.getUserObject();
                }

                if (child != null && e.getClickCount() == 1) {
                    child.select();
                }
            }
        }
    });

    jTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            if (e.getPath() != null && e.getPath().getLastPathComponent() != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                if (node.getUserObject() != null && Child.class.isInstance(node.getUserObject())) {
                    Child child = (Child) node.getUserObject();
                    child.showRequestView();
                }
            }
        }
    });
}

From source file:org.rdv.ui.ChannelListPanel.java

private void handleDoubleClick(MouseEvent e) {
    TreePath treePath = tree.getSelectionPath();
    if (treePath == null) {
        return;// w w  w . jav  a2  s . c o m
    }

    Object o = treePath.getLastPathComponent();
    if (o != treeModel.getRoot()) {
        ChannelTree.Node node = (ChannelTree.Node) o;
        if (node.getType() == ChannelTree.CHANNEL) {
            String channelName = node.getFullName();
            viewChannel(channelName);
        }
    }
}

From source file:org.rdv.ui.ChannelListPanel.java

private void handlePopup(MouseEvent e) {
    TreePath treePath = tree.getPathForLocation(e.getX(), e.getY());
    if (treePath == null) {
        return;/*w w w .java  2  s  .  c om*/
    }

    // select only the node under the mouse if it is not already selected
    if (!tree.isPathSelected(treePath)) {
        tree.setSelectionPath(treePath);
    }

    JPopupMenu popup = null;

    Object o = treePath.getLastPathComponent();
    if (o == treeModel.getRoot()) {
        popup = getRootPopup();
    } else {
        popup = getChannelPopup();
    }

    if (popup != null && popup.getComponentCount() > 0) {
        popup.show(tree, e.getX(), e.getY());
    }
}