Example usage for javax.swing.tree DefaultMutableTreeNode getUserObject

List of usage examples for javax.swing.tree DefaultMutableTreeNode getUserObject

Introduction

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

Prototype

public Object getUserObject() 

Source Link

Document

Returns this node's user object.

Usage

From source file:GUI.MainWindow.java

private void deleteAffectedHosts() {

    System.out.println("==deleteAffectedHost");
    // Get reference to visible vulnerability
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) VulnTree.getLastSelectedPathComponent();
    Vulnerability vuln = (Vulnerability) node.getUserObject();

    // Build the list of hosts to delete
    int[] rows = this.VulnAffectedHostsTable.getSelectedRows();
    for (int i = 0; i < rows.length; i++) {
        int row = this.VulnAffectedHostsTable.convertRowIndexToModel(rows[i]);
        Object obj = this.VulnAffectedHostsTable.getModel().getValueAt(row, 0);
        if (obj instanceof Host) {
            Host host = (Host) obj;/*from ww w  .  j ava 2  s  .c  o  m*/
            System.out.println("To Delete: " + host + ":" + host.getPortnumber());
            vuln.deleteAffectedHost(host);
        }
    }

    // update the GUI
    DefaultTableModel dtm = (DefaultTableModel) this.VulnAffectedHostsTable.getModel();
    // Clear the existing table
    dtm.setRowCount(0);

    // Set affected hosts into table
    Enumeration enums = vuln.getAffectedHosts().elements();
    while (enums.hasMoreElements()) {
        Object obj2 = enums.nextElement();
        if (obj2 instanceof Host) {
            Host host2 = (Host) obj2;
            Vector vecrow = host2.getAsVector(); // Gets the first two columns from the host
            dtm.addRow(vecrow);
        }
    }
}

From source file:GUI.MainWindow.java

private void EditRiskButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditRiskButtonActionPerformed
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) VulnTree.getLastSelectedPathComponent();
    if (node == null) {
        return;//from w ww . jav  a2s  .co  m
    }
    Object obj = node.getUserObject();
    if (obj instanceof Vulnerability) {
        // Show Edit Risk Window
        CVSSv2Calculator riskWindow = new CVSSv2Calculator(this, true, this.VulnTree, node);
        riskWindow.setVisible(true);
    }

}

From source file:GUI.MainWindow.java

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

    DefaultMutableTreeNode node = ((DefaultMutableTreeNode) VulnTree.getLastSelectedPathComponent());
    if (node == null) {
        return;/*from w w  w.  j a  va2  s  . c o m*/
    }

    Object obj = node.getUserObject();
    if (!(obj instanceof Vulnerability)) {
        return; // here be monsters, most likely in the merge tree
    }

    Vulnerability vuln = (Vulnerability) obj;
    vuln.deleteAllIds();

}

From source file:GUI.MainWindow.java

/**
 * The vulnTree has had a selection event fire. This will find the last
 * selected node and display that on the right pane. It will also update the
 * count of selected node label at the bottom if more than one has been
 * selected./*from  w w  w .  j a  v a  2  s. c om*/
 *
 * @param evt
 */
private void VulnTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_VulnTreeValueChanged

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.VulnTree.getLastSelectedPathComponent();
    if (node == null) {
        return;
    }
    Object obj = node.getUserObject();
    if (node.isLeaf() && obj instanceof Vulnerability) {
        // this is a vulnerability we should update the UI to show the contents
        showVulnerability((Vulnerability) obj);
    }

    int number_of_nodes = this.VulnTree.getSelectionCount();
    this.ExtraInfoLabel.setText("Number of nodes selected: " + number_of_nodes);

}

From source file:GUI.MainWindow.java

public void deleteReferences(JTree tree, JList list) {

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    if (node == null) {
        return;/*  w  w  w .  ja  va 2 s  .c  o m*/
    }

    Object obj = node.getUserObject();
    if (!(obj instanceof Vulnerability)) {
        return;
    }

    // Get currently selected vulnerability
    Vulnerability vuln = (Vulnerability) obj;
    DefaultListModel dlm = (DefaultListModel) list.getModel();
    List selected = list.getSelectedValuesList();
    for (Object ref_obj : selected) {
        if (ref_obj instanceof Reference) {
            Reference ref = (Reference) ref_obj;
            vuln.deleteReference(ref);
            dlm.removeElement(ref_obj);
            System.out.println("Deleted Reference: " + ref);
        } else {
            System.out.println("Somehow the references list contained a non-Regerence object");
        }
    }
}

From source file:eu.crisis_economics.abm.dashboard.Page_Parameters.java

private static int calculateNumberOfRuns(DefaultMutableTreeNode combinationRoot) {
    int result = 1;
    @SuppressWarnings("rawtypes")
    final Enumeration combinationElements = combinationRoot.children();

    while (combinationElements.hasMoreElements()) {
        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) combinationElements.nextElement();
        final ParameterInfo info = ((ParameterInATree) node.getUserObject()).info;

        if (info instanceof SubmodelInfo)
            result *= calculateNumberOfRuns(node);
        else//from  w w w .  ja  v  a 2  s  . co m
            result *= info.getMultiplicity();
    }

    return result;
}

From source file:GUI.MainWindow.java

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

    System.out.println("Edit Hostname Selected");
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.VulnTree.getLastSelectedPathComponent();
    if (node == null) {
        return;/* ww w  .  jav  a  2 s  .  co  m*/
    }

    Object vuln_obj = node.getUserObject();
    if (!(vuln_obj instanceof Vulnerability)) {
        return;
    }

    Vulnerability current = (Vulnerability) vuln_obj;

    int row = this.VulnAffectedHostsTable.getSelectedRow();
    row = this.VulnAffectedHostsTable.convertRowIndexToModel(row);
    Object obj = this.VulnAffectedHostsTable.getModel().getValueAt(row, 0);
    if (obj instanceof Host) {
        Host previous = (Host) obj;

        JTextField hostname = new JTextField();
        hostname.setText(previous.getHostname());
        Object[] message = { "Hostname:", hostname };

        String new_hostname = null;
        while (new_hostname == null) {
            int option = JOptionPane.showConfirmDialog(null, message, "Modify Hostname",
                    JOptionPane.OK_CANCEL_OPTION);
            if (option == JOptionPane.OK_OPTION) {
                new_hostname = hostname.getText();
                Host modified = previous.clone(); // Clone the previous one
                modified.setHostname(new_hostname);
                new TreeUtils().modifyHostname((DefaultMutableTreeNode) this.VulnTree.getModel().getRoot(),
                        previous, modified);
                //current.modifyAffectedHost(previous, modified); // Update the Vulnerability object
                // Update the affected hosts table
                DefaultTableModel dtm = (DefaultTableModel) this.VulnAffectedHostsTable.getModel();
                // Clear the existing table
                dtm.setRowCount(0);

                // Set affected hosts into table
                Enumeration enums = current.getAffectedHosts().elements();
                while (enums.hasMoreElements()) {
                    Object obj2 = enums.nextElement();
                    if (obj instanceof Host) {
                        Host host = (Host) obj2;
                        Vector row2 = host.getAsVector(); // Gets the first two columns from the host
                        dtm.addRow(row2);
                    }
                }

            } else {
                return; // user cancelled or closed the prompt
            }
        }

    }

}

From source file:GUI.MainWindow.java

public void addAffectedHost() {

    System.out.println("==addAffectedHost");

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.VulnTree.getLastSelectedPathComponent();
    if (node == null) {
        return;/*from ww  w. j av  a 2  s .co  m*/
    }

    Vulnerability vuln = (Vulnerability) node.getUserObject();
    //ManageAffectedHosts.setVisible(true) ;

    // Old way of doing it
    JTextField ip_address = new JTextField();
    JTextField hostname = new JTextField();
    JTextField port_number = new JTextField();
    port_number.setText("0");
    JTextField protocol = new JTextField();
    protocol.setText("tcp");

    Object[] message = { "IP Address:", ip_address, "Hostname:", hostname, "Port Number:", port_number,
            "Protocol:", protocol };

    String new_ip = null;
    String new_hostname = null;
    String new_port_number = null;
    String new_protocol = null;

    while (new_ip == null || new_hostname == null || new_port_number == null || new_protocol == null) {
        int option = JOptionPane.showConfirmDialog(null, message, "Add affected host",
                JOptionPane.OK_CANCEL_OPTION);
        if (option == JOptionPane.OK_OPTION) {

            new_ip = ip_address.getText();
            new_hostname = hostname.getText();
            new_port_number = port_number.getText();
            new_protocol = protocol.getText();
            Host host = new Host();
            host.setIp_address(new_ip);
            host.setHostname(new_hostname);
            host.setPortnumber(new_port_number);
            host.setProtocol(new_protocol);
            vuln.addAffectedHost(host);
            DefaultTableModel dtm = (DefaultTableModel) this.VulnAffectedHostsTable.getModel();
            dtm.addRow(host.getAsVector());
            dtm.fireTableDataChanged();

        } else {
            return; // End the infinite loop
        }

    }

}

From source file:GUI.MainWindow.java

public void addReference(JTree tree, JList list, Reference current) {
    DefaultMutableTreeNode node = ((DefaultMutableTreeNode) tree.getLastSelectedPathComponent());
    if (node == null) {
        return;/*ww w.  java 2s. c o m*/
    }

    Object obj = node.getUserObject();
    if (!(obj instanceof Vulnerability)) {
        return; // here be monsters, most likely in the merge tree
    }
    Vulnerability vuln = (Vulnerability) obj;
    DefaultListModel dlm = (DefaultListModel) list.getModel();
    // Build Input Field and display it
    JTextField description = new JTextField();
    JTextField url = new JTextField();

    // If current is not null then pre-set the description and risk
    if (current != null) {
        description.setText(current.getDescription());
        url.setText(current.getUrl());
    }

    JLabel error = new JLabel(
            "A valid URL needs to be supplied including the protocol i.e. http://www.github.com");
    error.setForeground(Color.red);
    Object[] message = { "Description:", description, "URL:", url };

    String url_string = null;
    Reference newref = null;
    while (url_string == null) {
        int option = JOptionPane.showConfirmDialog(null, message, "Add Reference",
                JOptionPane.OK_CANCEL_OPTION);
        if (option == JOptionPane.OK_OPTION) {
            System.out.println("User clicked ok, validating data");
            String ref_desc = description.getText();
            String ref_url = url.getText();
            if (!ref_desc.equals("") || !ref_url.equals("")) {
                // Both have values
                // Try to validate URL
                try {

                    URL u = new URL(url.getText());
                    u.toURI();
                    url_string = url.getText(); // Causes loop to end with a valid url

                } catch (MalformedURLException ex) {
                    url_string = null;
                    //ex.printStackTrace();
                } catch (URISyntaxException ex) {
                    url_string = null;
                    //ex.printStackTrace();
                }

            }

        } else if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
            System.out.println("User clicked cancel/close");
            return; // ends the loop without making any chages
        }

        if (url_string == null) {
            // We need to show an error saying that the url failed to parse
            Object[] message2 = { error, "Description:", description, "URL:", url };
            message = message2;

        }
    }

    // If you get here there is a valid reference URL and description
    Reference ref = new Reference(description.getText(), url.getText());

    if (current == null) {
        // Add it to the vuln
        vuln.addReference(ref);
        // Add it to the GUI 
        dlm.addElement(ref);
        System.out.println("Valid reference added: " + ref);
    } else {
        // Modify it in the vuln
        vuln.modifyReference(current, ref);
        // Update the GUI
        dlm.removeElement(current);
        dlm.addElement(ref);
        System.out.println("Valid reference modified: " + ref);
    }

}

From source file:edu.harvard.i2b2.previousquery.ui.PreviousQueryPanel.java

public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) event.getPath().getLastPathComponent();

    if (node.getUserObject().getClass().getSimpleName().equalsIgnoreCase("QueryResultData")) {
        QueryResultData rdata = (QueryResultData) node.getUserObject();
        if (rdata.patientCount().equalsIgnoreCase("0")) {

            final JPanel parent = this;
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(parent, "There are no patients in this set.", "Message",
                            JOptionPane.INFORMATION_MESSAGE);
                }/*from w w  w. j  a  v  a2 s  .  c  o m*/
            });

            return;
        }
    }
}