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:com.projity.grouping.core.hierarchy.MutableNodeHierarchy.java

public void valueForPathChanged(TreePath path, Object newValue) {
    Node aNode = (Node) path.getLastPathComponent();
    //TODO do we need to treat this?
}

From source file:com.peter.mavenrunner.MavenRunnerTopComponent.java

private void editGoalMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editGoalMenuItemActionPerformed
    TreePath path = projectTree.getSelectionPath();
    if (path == null) {
        return;//from w  w  w . j av  a2s. com
    }
    MyTreeNode node = (MyTreeNode) ((MyTreeNode) path.getLastPathComponent());
    if (node.type.equals("goal")) {
        MavenGoalDialog dialog = new MavenGoalDialog(null, true);
        dialog.setTitle("Edit goals");
        dialog.setLocationRelativeTo(addGoalMenuItem);
        dialog.nameTextField.setText(node.name);
        dialog.goalsTextField.setText(node.goals);
        dialog.profileTextField.setText(node.profile);
        dialog.propertiesTextArea.setText(StringUtils.join(node.properties, "\n"));
        dialog.skipTestsCheckBox.setSelected(node.skipTests);
        dialog.setVisible(true);

        if (!dialog.isCancel) {
            String name = dialog.nameTextField.getText();
            if (name.trim().equals("")) {
                return;
            }
            String key = node.projectInformation.getDisplayName();
            ArrayList<PersistData> list = data.get(key);
            if (list == null) {
                list = new ArrayList<PersistData>();
                data.put(key, list);
            }

            int index = 0;
            Iterator<PersistData> i = list.iterator();
            while (i.hasNext()) {
                PersistData p = i.next();
                if (p.name.equals(node.name)) {
                    break;
                }
                index++;
            }

            String goals = dialog.goalsTextField.getText();
            String profile = dialog.profileTextField.getText();
            List<String> properties = Arrays.asList(dialog.propertiesTextArea.getText().split("\n"));
            boolean skipTests = dialog.skipTestsCheckBox.isSelected();

            log("index=" + index);
            list.remove(index);
            list.add(index, new PersistData(node.type, node.projectInformation.getDisplayName(), name, goals,
                    profile, properties, skipTests));

            node.name = name;
            node.goals = goals;
            node.profile = profile;
            node.properties = properties;
            node.skipTests = skipTests;

            projectTree.updateUI();
            NbPreferences.forModule(this.getClass()).put("data", toString(data));
        }
    }
}

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 w  w  .j a v  a 2 s  .com
 * @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:com.qspin.qtaste.ui.TestCaseTree.java

public void dragOver(DragSourceDragEvent dsde) {
    ///*from  w  ww  . j  a va  2  s. c o  m*/
    Point dropPoint = dsde.getLocation();
    TreePath path = getPathForLocation(dropPoint.x, dropPoint.y);
    if (path == null)
        return;
    Object targetNode = path.getLastPathComponent();
    if (targetNode instanceof TCTreeNode) {
        TCTreeNode tcTreeNode = (TCTreeNode) targetNode;
        if (tcTreeNode.getUserObject() instanceof FileNode) {
            FileNode fn = (FileNode) tcTreeNode.getUserObject();
        }
    }
}

From source file:com.qspin.qtaste.ui.TestCaseTree.java

public void drop(DropTargetDropEvent dtde) {
    //try/*from  w  ww  .j  av  a 2s. c  o m*/
    {
        try {
            TCTreeNode tcTreeNode = (TCTreeNode) dtde.getTransferable().getTransferData(localObjectFlavor);
            Point dropPoint = dtde.getLocation();
            // int index = locationToIndex (dropPoint);
            TreePath path = getPathForLocation(dropPoint.x, dropPoint.y);
            Object targetNode = path.getLastPathComponent();
            if (targetNode instanceof TCTreeNode) {
                // rename the dragged dir into the new target one
                TCTreeNode tcTargetNode = (TCTreeNode) targetNode;
                FileNode fn = (FileNode) tcTargetNode.getUserObject();
                if (fn.isTestcaseDir()) {
                    dtde.rejectDrop();
                    return;
                }

                FileNode draggedFileNode = (FileNode) tcTreeNode.getUserObject();
                draggedFileNode.getFile()
                        .renameTo(new File(fn.getFile() + "/" + draggedFileNode.getFile().getName()));
                // update target tree

                testCasePane.parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

                TCTreeNode parentTreeNode = (TCTreeNode) tcTargetNode.getParent();
                if (parentTreeNode != null) {
                    parentTreeNode.removeAllChildren();
                    FileNode parentFileNode = (FileNode) parentTreeNode.getUserObject();
                    addTreeToDir(parentFileNode.getFile(), parentTreeNode);
                    ((DefaultTreeModel) getModel()).reload(parentTreeNode);
                } else {
                    tcTargetNode.removeAllChildren();
                    FileNode targetFileNode = (FileNode) tcTargetNode.getUserObject();
                    addTreeToDir(targetFileNode.getFile(), tcTargetNode);
                    ((DefaultTreeModel) getModel()).reload(tcTargetNode);
                }
                // update source tree
                parentTreeNode = (TCTreeNode) tcTreeNode.getParent();
                if (parentTreeNode != null) {
                    parentTreeNode.removeAllChildren();
                    FileNode parentFileNode = (FileNode) parentTreeNode.getUserObject();
                    addTreeToDir(parentFileNode.getFile(), parentTreeNode);
                    ((DefaultTreeModel) getModel()).reload(parentTreeNode);
                } else {
                    tcTreeNode.removeAllChildren();
                    FileNode targetFileNode = (FileNode) tcTreeNode.getUserObject();
                    addTreeToDir(targetFileNode.getFile(), tcTreeNode);
                    ((DefaultTreeModel) getModel()).reload(tcTreeNode);
                }
                testCasePane.parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                dtde.getDropTargetContext().dropComplete(true);
            } else {
                dtde.rejectDrop();
            }
        } catch (UnsupportedFlavorException ex) {
            logger.error(ex.getMessage());
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }

    }
}

From source file:com.qspin.qtaste.ui.TestCaseTree.java

public void dragOver(DropTargetDragEvent dtde) {
    ///*  ww w. j a  v  a  2s .  c  o  m*/
    Point dropPoint = dtde.getLocation();
    // int index = locationToIndex (dropPoint);
    TreePath path = getPathForLocation(dropPoint.x, dropPoint.y);
    if (path == null) {
        return;
    }
    Object targetNode = path.getLastPathComponent();
    if (targetNode instanceof TCTreeNode) {
        TCTreeNode tcTreeNode = (TCTreeNode) targetNode;
        if (tcTreeNode.getUserObject() instanceof FileNode) {
            FileNode fn = (FileNode) tcTreeNode.getUserObject();
            if (fn.isTestcaseDir()) {
                return;
            }
        }
    }
}

From source file:EditorPaneExample19.java

public EditorPaneExample19() {
    super("JEditorPane Example 19");

    pane = new JEditorPane();
    pane.setEditable(true); // Editable
    getContentPane().add(new JScrollPane(pane), "Center");

    // Add a menu bar
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);//  w ww. ja v  a  2  s  . c  o m

    // Populate it
    createMenuBar();

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);
    c.gridy = 3;
    panel.add(new JLabel(LOAD_TIME), c);

    c.gridy = 4;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    onlineLoad = new JCheckBox("Online Load");
    panel.add(onlineLoad, c);
    onlineLoad.setSelected(true);
    onlineLoad.setForeground(typeLabel.getForeground());

    c.gridy = 5;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    editableBox = new JCheckBox("Editable JEditorPane");
    panel.add(editableBox, c);
    editableBox.setSelected(true);
    editableBox.setForeground(typeLabel.getForeground());

    c.gridy = 6;
    c.weightx = 0.0;
    JButton saveButton = new JButton("Save");
    panel.add(saveButton, c);
    saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            EditorKit kit = pane.getEditorKit();
            try {
                if (kit instanceof RTFEditorKit) {
                    kit.write(System.out, pane.getDocument(), 0, pane.getDocument().getLength());
                    System.out.flush();
                } else {
                    if (writer == null) {
                        writer = new OutputStreamWriter(System.out);
                        pane.write(writer);
                        writer.flush();
                    }
                    kit.write(writer, pane.getDocument(), 0, pane.getDocument().getLength());
                    writer.flush();
                }
            } catch (Exception e) {
                System.out.println("Write failed");
            }
        }
    });

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;

    urlCombo = new JComboBox();
    panel.add(urlCombo, c);
    urlCombo.setEditable(true);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);
    timeLabel = new JLabel("");
    c.gridy = 3;
    panel.add(timeLabel, c);

    getContentPane().add(panel, "South");

    // Register a custom EditorKit for HTML
    ClassLoader loader = getClass().getClassLoader();
    if (loader != null) {
        // Java 2
        JEditorPane.registerEditorKitForContentType("text/html", "AdvancedSwing.Chapter4.EnhancedHTMLEditorKit",
                loader);
    } else {
        // JDK 1.1
        JEditorPane.registerEditorKitForContentType("text/html",
                "AdvancedSwing.Chapter4.EnhancedHTMLEditorKit");
    }

    // Allocate the empty tree model
    DefaultMutableTreeNode emptyRootNode = new DefaultMutableTreeNode("Empty");
    emptyModel = new DefaultTreeModel(emptyRootNode);

    // Create and place the heading tree
    tree = new JTree(emptyModel);
    tree.setPreferredSize(new Dimension(200, 200));
    getContentPane().add(new JScrollPane(tree), "East");

    // Change page based on combo selection
    urlCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (populatingCombo == true) {
                return;
            }
            Object selection = urlCombo.getSelectedItem();
            loadNewPage(selection);
        }
    });

    // Change editability based on the checkbox
    editableBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            pane.setEditable(editableBox.isSelected());
            pane.revalidate();
            pane.repaint();
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadComplete();
                displayLoadTime();
                populateCombo(findLinks(pane.getDocument(), null));
                TreeNode node = buildHeadingTree(pane.getDocument());
                tree.setModel(new DefaultTreeModel(node));

                createMenuBar();
                enableMenuBar(true);
                getRootPane().revalidate();

                enableInput();
                loadingPage = false;
            }
        }
    });

    // Listener for tree selection
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
            TreePath path = evt.getNewLeadSelectionPath();
            if (path != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object userObject = node.getUserObject();
                if (userObject instanceof Heading) {
                    Heading heading = (Heading) userObject;
                    try {
                        Rectangle textRect = pane.modelToView(heading.getOffset());
                        textRect.y += 3 * textRect.height;
                        pane.scrollRectToVisible(textRect);
                    } catch (BadLocationException e) {
                    }
                }
            }
        }
    });

    // Listener for hypertext events
    pane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            // Ignore hyperlink events if the frame is busy
            if (loadingPage == true) {
                return;
            }
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane sp = (JEditorPane) evt.getSource();
                if (evt instanceof HTMLFrameHyperlinkEvent) {
                    HTMLDocument doc = (HTMLDocument) sp.getDocument();
                    doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) evt);
                } else {
                    loadNewPage(evt.getURL());
                }
            } else if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                pane.setCursor(handCursor);
            } else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
                pane.setCursor(defaultCursor);
            }
        }
    });
}

From source file:de.erdesignerng.visual.common.OutlineComponent.java

/**
 * Refresh the model tree as there were changes.
 *
 * @param aModel - model//from ww w.  j a va2  s .  c o  m
 */
public void refresh(Model aModel) {

    if (aModel != null) {

        TreePath theSelected = tree.getSelectionPath();

        DefaultMutableTreeNode theGroup = null;
        DefaultMutableTreeNode theSelectedNode = theSelected != null
                ? (DefaultMutableTreeNode) theSelected.getLastPathComponent()
                : null;
        if (theSelected != null && theSelected.getPathCount() > 1) {
            theGroup = (DefaultMutableTreeNode) theSelected.getPath()[1];
        }

        Set<Object> theExpandedUserObjects = expandedUserObjects;

        setModel(aModel);

        List<TreePath> thePathsToExpand = new ArrayList<>();
        TreePath theNewSelection = null;

        for (int theRow = 0; theRow < tree.getRowCount(); theRow++) {
            TreePath thePath = tree.getPathForRow(theRow);

            DefaultMutableTreeNode theLastNew = (DefaultMutableTreeNode) thePath.getLastPathComponent();
            if (theExpandedUserObjects.contains(theLastNew.getUserObject())) {
                thePathsToExpand.add(thePath);
            }
            if (theSelectedNode != null) {
                DefaultMutableTreeNode theLastGroup = null;
                if (thePath.getPathCount() > 1) {
                    theLastGroup = (DefaultMutableTreeNode) thePath.getPath()[1];
                }
                if (theLastGroup != null && theGroup != null) {
                    if (!theLastGroup.getUserObject().equals(theGroup.getUserObject())) {
                        continue;
                    }
                }
                if (theLastNew.getUserObject().equals(theSelectedNode.getUserObject())) {
                    theNewSelection = thePath;
                }
            }
        }

        thePathsToExpand.forEach(tree::expandPath);

        if (theNewSelection != null) {
            tree.setSelectionPath(theNewSelection);
            tree.scrollPathToVisible(theNewSelection);
        }
    }
}

From source file:org.jfree.chart.demo.SuperDemo.java

public void valueChanged(TreeSelectionEvent treeselectionevent) {
    TreePath treepath = treeselectionevent.getPath();
    Object obj = treepath.getLastPathComponent();
    if (obj != null) {
        DefaultMutableTreeNode defaultmutabletreenode = (DefaultMutableTreeNode) obj;
        Object obj1 = defaultmutabletreenode.getUserObject();
        if (obj1 instanceof DemoDescription) {
            DemoDescription demodescription = (DemoDescription) obj1;
            SwingUtilities.invokeLater(new DisplayDemo(this, demodescription));
        } else {/*from   w  w  w  .  ja  v a2s.  c  o  m*/
            chartContainer.removeAll();
            chartContainer.add(createNoDemoSelectedPanel());
            displayPanel.validate();
            displayDescription("select.html");
        }
    }
    System.out.println(obj);
}

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 w ww.  j  ava  2 s.  c o  m
        // 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;
}