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.pironet.tda.TDA.java

/**
 * navigate to the currently selected dump in logfile
 *//*from  w w w.  ja  v  a2  s.  c o m*/
private void navigateToDumpInLogfile() {
    Object userObject = ((DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent())
            .getUserObject();
    if (userObject instanceof ThreadDumpInfo) {
        ThreadDumpInfo ti = (ThreadDumpInfo) userObject;
        int lineNumber = ti.getLogLine();

        // find log file node.
        TreePath selPath = tree.getSelectionPath();
        while (selPath != null && !checkNameFromNode((DefaultMutableTreeNode) selPath.getLastPathComponent(),
                File.separator)) {

            selPath = selPath.getParentPath();
        }

        tree.setSelectionPath(selPath);
        tree.scrollPathToVisible(selPath);
        if (selPath == null) {
            return;
        }

        Enumeration childs = ((DefaultMutableTreeNode) selPath.getLastPathComponent()).children();
        boolean found = false;
        DefaultMutableTreeNode logfileContent = null;
        while (!found && childs.hasMoreElements()) {
            logfileContent = (DefaultMutableTreeNode) childs.nextElement();
            found = logfileContent.getUserObject() instanceof LogFileContent;
        }

        if (found) {
            TreePath monitorPath = new TreePath(logfileContent.getPath());
            tree.setSelectionPath(monitorPath);
            tree.scrollPathToVisible(monitorPath);
            displayLogFileContent(logfileContent.getUserObject());
            jeditPane.setFirstLine(lineNumber - 1);
        }
    }
}

From source file:com.pironet.tda.TDA.java

/**
 * close the currently selected dump./*ww w.  j a v a  2s  .c  om*/
 */
private void closeCurrentDump() {
    TreePath selPath = tree.getSelectionPath();

    while (selPath != null && !(checkNameFromNode((DefaultMutableTreeNode) selPath.getLastPathComponent(),
            File.separator)
            || checkNameFromNode((DefaultMutableTreeNode) selPath.getLastPathComponent(), 2, File.separator))) {
        selPath = selPath.getParentPath();
    }

    Object[] options = { "Close File", "Cancel close" };

    String fileName = ((DefaultMutableTreeNode) selPath.getLastPathComponent()).getUserObject().toString();
    fileName = fileName.substring(fileName.indexOf(File.separator));

    int selectValue = JOptionPane.showOptionDialog(null,
            "<html><body>Are you sure, you want to close the currently selected dump file<br><b>" + fileName
                    + "</b></body></html>",
            "Confirm closing...", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
            options[0]);

    // if first option "close file" is selected.
    if (selectValue == 0) {
        // remove stuff from the top nodes
        topNodes.remove(selPath.getLastPathComponent());

        if (topNodes.size() == 0) {
            // simply do a reinit, as there isn't anything to display
            removeAll();
            revalidate();

            init(runningAsJConsolePlugin, runningAsVisualVMPlugin);
            getMainMenu().getLongMenuItem().setEnabled(false);
            getMainMenu().getCloseMenuItem().setEnabled(false);
            getMainMenu().getSaveSessionMenuItem().setEnabled(false);
            getMainMenu().getCloseToolBarButton().setEnabled(false);
            getMainMenu().getExpandButton().setEnabled(false);
            getMainMenu().getCollapseButton().setEnabled(false);
            getMainMenu().getFindLRThreadsToolBarButton().setEnabled(false);
            getMainMenu().getCloseAllMenuItem().setEnabled(false);
            getMainMenu().getExpandAllMenuItem().setEnabled(false);
            getMainMenu().getCollapseAllMenuItem().setEnabled(false);

        } else {
            // rebuild jtree
            getMainMenu().getCloseMenuItem().setEnabled(false);
            getMainMenu().getCloseToolBarButton().setEnabled(false);
            createTree();
        }
        revalidate();
    }

}

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

private String getPathAtRow(int theRowIndex) {

    AbstractLayoutCache layout = ((OutlineModel) getModel()).getLayout();
    int messageIndex = -1;
    for (int i = 0; i < layout.getRowCount(); i++) {
        TreePath path = layout.getPathForRow(i);

        Object baseObj = path.getLastPathComponent();
        if (baseObj instanceof TreeNodeMessage || baseObj instanceof TreeNodeUnknown) {

            messageIndex++;//from ww  w .j  av a  2 s. co  m
            if (i == theRowIndex) {
                return Integer.toString(messageIndex);
            }

        } else {

            if (i == theRowIndex && baseObj instanceof TreeNodeBase) {
                return (Integer.toString(messageIndex) + ((TreeNodeBase) baseObj).getTerserPath());
            }

        }

    }

    return null;
}

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

private void expandPaths(Set<String> theOpenPaths, String theSelectedPath) {
    AbstractLayoutCache layout = ((OutlineModel) getModel()).getLayout();
    int messageIndex = -1;
    for (int i = 0; i < layout.getRowCount(); i++) {
        TreePath path = layout.getPathForRow(i);

        Object baseObj = path.getLastPathComponent();
        String pathString = null;
        if (baseObj instanceof TreeNodeMessage || baseObj instanceof TreeNodeUnknown) {
            messageIndex++;/* w  w w  . ja v a2s .c  o m*/
            pathString = Integer.toString(messageIndex);
        } else if (baseObj instanceof TreeNodeBase) {
            pathString = (Integer.toString(messageIndex) + ((TreeNodeBase) baseObj).getTerserPath());
        }

        if (pathString != null) {
            if (theOpenPaths.contains(pathString)) {
                expandPath(path);
            } else {
                collapsePath(path);
            }
            if (pathString.equals(theSelectedPath)) {
                getSelectionModel().setSelectionInterval(i, i);
            }
        }

    }

}

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

private Set<String> getOpenPaths() {
    Set<String> retVal = new HashSet<String>();

    TableModel model = getModel();
    AbstractLayoutCache layout = ((OutlineModel) model).getLayout();
    int messageIndex = -1;
    for (int i = 0; i < layout.getRowCount(); i++) {
        TreePath path = layout.getPathForRow(i);

        Object baseObj = path.getLastPathComponent();
        if (baseObj instanceof TreeNodeMessage || baseObj instanceof TreeNodeUnknown) {

            messageIndex++;/*from ww w.j  a  v  a 2  s.  c  om*/

            if (layout.getExpandedState(path)) {
                retVal.add(Integer.toString(messageIndex));
            }

        } else {

            baseObj = path.getPathComponent(path.getPathCount() - 2);
            if (baseObj instanceof TreeNodeBase) {
                retVal.add(Integer.toString(messageIndex) + ((TreeNodeBase) baseObj).getTerserPath());
            }
        }

    }

    return retVal;
}

From source file:GUI.MainWindow.java

private void doDelete() {

    DefaultTreeModel dtm = (DefaultTreeModel) VulnTree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) dtm.getRoot();
    TreePath[] paths = VulnTree.getSelectionPaths();

    if (paths == null) {
        return;//from  w w  w . ja v a 2  s.c o  m
    }

    for (int i = 0; i < paths.length; i++) {
        TreePath path = paths[i];
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        if (i == 0) {
            // This is the first delete operation
            DefaultMutableTreeNode previous = (DefaultMutableTreeNode) node.getPreviousSibling();
            // Consider saving deleted vulns into a scratchpad file TODO
            //System.out.println("previous:" + previous);

            // If it is null here we have no nodes above it, we get the next sibling below
            if (previous == null) {
                previous = (DefaultMutableTreeNode) node.getNextSibling();
            }

            // If it is still null here there are no nodes in the tree. Point to the root. Avoids NullPointerException
            if (previous == null) {
                previous = root;
            }

            TreePath p = new TreePath(previous.getPath());
            VulnTree.setSelectionPath(p);
        }

        if (node.getParent() != null) {
            dtm.removeNodeFromParent(node);
        }
    }

    if (root.getChildCount() == 0) {
        clearGUI();
    }

}

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

private void handleKeyPress(KeyEvent theE) {
    int row = getSelectedRow();
    if (row == -1) {
        return;//from   w ww .  j  a v  a 2  s .c  o m
    }

    if (theE.getKeyCode() == KeyEvent.VK_ENTER || theE.getKeyCode() == KeyEvent.VK_F2) {
        AbstractLayoutCache layout = ((OutlineModel) getModel()).getLayout();
        TreePath path = layout.getPathForRow(row);
        TreeNodeBase baseObj = (TreeNodeBase) path.getLastPathComponent();
        if (myTableModel.isCellEditable(baseObj, TreeRowModel.COL_VALUE)) {
            editCellAt(row, TreeRowModel.COL_VALUE + 1);
            theE.consume();
        }
    }
}

From source file:GUI.MainWindow.java

private void handleCVELookup(File save_file) {

    final File sf = save_file;
    // Best to do this as a background task it'll take time
    Runnable r = new Runnable() {
        public void run() {
            HashSet cves = new HashSet();
            // Find all selected vulns in the tree.
            TreePath[] paths = VulnTree.getSelectionPaths();
            for (int i = 0; i < paths.length; i++) {
                // Loop through them and merge all CVEs into the cves HashSet
                TreePath path = paths[i];
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object obj = node.getUserObject();
                if (obj instanceof Vulnerability) {
                    Vulnerability vuln = (Vulnerability) obj;
                    // add these references to the HashSet
                    cves.addAll(vuln.getCVEReferences());
                }//from w w w.ja v a 2 s .  co m
            }

            // Get the answers from our local CSV file
            CVEUtils cveu = new CVEUtils();
            Vector answers = cveu.getCVEs(cves);

            try {
                String[] headerrow = { "CVE ID", "Risk Score", "Summary" };
                // Write header column to file
                writeCSVLine(sf, headerrow);
                // Now get all the details and make a CSV for the user.
                Enumeration enums = answers.elements();
                while (enums.hasMoreElements()) {
                    CVE c = (CVE) enums.nextElement();
                    System.out.println(c.getCveId() + ":" + c.getRiskScore());
                    writeCSVLine(sf, c.toStringArray());
                }

                // Open file in user's default programme
                Desktop.getDesktop().open(sf);

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
    };

    new Thread(r).start();
}

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

private void doSynchronizeTreeWithHighlitedPath() {
    String highlitedPath = myMessages.getHighlitedPath();
    if (highlitedPath == null) {
        return;/*  w  ww  .j ava2s.com*/
    }

    final AbstractLayoutCache layout = ((OutlineModel) getModel()).getLayout();
    int lastSegmentRow = -1;
    int currentSegmentRow = -1;
    int currentSelectedRow = -1;
    int currentMessageIndex = -1;
    for (int row = 0; row < layout.getRowCount(); row++) {

        TreePath path = layout.getPathForRow(row);
        Object component = path.getLastPathComponent();
        if (component instanceof TreeNodeMessage) {
            currentMessageIndex = ((TreeNodeMessage) component).getMessageIndex();
            if (highlitedPath.startsWith(currentMessageIndex + "/")) {
                expandPath(path);
            } else {
                // collapsePath(path);
            }
            continue;
        }

        if (component instanceof TreeNodeUnknown) {
            continue;
        }

        if (component instanceof TreeNodeSegment) {
            lastSegmentRow = row;
        }

        TreeNodeBase node = (TreeNodeBase) component;

        String terserPath = (currentMessageIndex) + node.getTerserPath();
        if (highlitedPath != null && highlitedPath.startsWith(terserPath)
                && !highlitedPath.startsWith(terserPath + "(")) {
            expandPath(path);
            if (highlitedPath.equals(terserPath)) {
                currentSelectedRow = row;
                getSelectionModel().setSelectionInterval(row, row);
                currentSegmentRow = lastSegmentRow;
            }
        } else {
            // collapsePath(path);
        }

    }

    // Adjust the tree scrollpane's scroll position so that the newly
    // selected row is visible
    if (currentSegmentRow != -1 && currentSelectedRow != -1 && !myRespondingToManualRangeChange) {
        JViewport viewPort = (JViewport) getParent();
        final JScrollPane scrollPane = (JScrollPane) viewPort.getParent();

        int tableHeaderHeight = getTableHeader().getHeight();

        int numRowsVisible = ((scrollPane.getHeight() - tableHeaderHeight) / layout.getRowHeight()) - 1;
        int segmentDelta = currentSelectedRow - currentSegmentRow;
        if (segmentDelta > numRowsVisible) {
            currentSegmentRow = currentSegmentRow + (segmentDelta - numRowsVisible);
        }

        final int scrollToRow = currentSegmentRow;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                scrollPane.getVerticalScrollBar().setValue(layout.getRowHeight() * scrollToRow);
            }
        });

    }
}

From source file:GUI.MainWindow.java

private void AddToPersonalVulnsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddToPersonalVulnsActionPerformed

    System.out.println("Add to personal vulns section");

    DefaultTreeModel dtm = (DefaultTreeModel) VulnTree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) dtm.getRoot();
    TreePath[] paths = VulnTree.getSelectionPaths();

    // If there are no paths then no nodes have been selected, ignore the request
    if (paths == null) {
        return;//from ww w.  j  a  va  2s.c o  m
    }

    DefaultTreeModel personaldtm = (DefaultTreeModel) this.personal_vulns_window.PersonalVulnsTree.getModel();
    DefaultMutableTreeNode personalroot = (DefaultMutableTreeNode) personaldtm.getRoot();

    // For each selected node
    for (int i = 0; i < paths.length; i++) {
        TreePath path = paths[i]; // Get the node
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        Object obj = node.getUserObject();
        // Make sure it has an object of type Vulnerability
        if (obj != null && obj instanceof Vulnerability) {
            Vulnerability vuln = (Vulnerability) obj;
            if (vuln.containsIdentifier("24d459a81449d721c8f9a86c2913034", "ReportCompiler")) {
                JOptionPane.showMessageDialog(null,
                        "Attempting to add a personal vuln with the title 'NEW' has a dangerous identifier, please change the title first\n"
                                + vuln.getTitle());
                return;
            }
            // Clone that vulnerability so it isn't the same object that is in the main tree
            Vulnerability newvuln = new Vulnerability();
            newvuln.setTitle(vuln.getTitle());
            newvuln.setRecommendation(vuln.getRecommendation());
            newvuln.setDescription(vuln.getDescription());
            newvuln.cloneReferences(vuln);
            newvuln.cloneRisk(vuln);
            newvuln.cloneIdentifiers(vuln);
            // Add the clone to the personal vulns database
            // TODO do something clever where a vuln exists that has an ID in this one.
            if (new TreeUtils().OkToaddToPersonalVulns(personalroot, newvuln)) {
                personalroot.add(new DefaultMutableTreeNode(newvuln));
                System.out.println("Added to personal vulns: " + newvuln);
                System.out.println("Hosts: " + newvuln.getAffectedHosts().size());
            } else {
                JOptionPane.showMessageDialog(null, "A personal vuln exists that may match that already");
            }

        }
    }

    // Sort the tree
    personalroot = new TreeUtils().sortVulns(personalroot);
    // Update the model
    personaldtm.setRoot(personalroot);

    try {

        // Save the tree again
        new SaveFileExporter().save(this.personal_vulns_window.person_vulns_file, personalroot);
        System.out.println(
                "Personal Vulns Saved: " + this.personal_vulns_window.person_vulns_file.getAbsolutePath());
    } catch (Exception ex) {
        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
    }

}