Example usage for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode

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

Introduction

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

Prototype

public DefaultMutableTreeNode(Object userObject) 

Source Link

Document

Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.

Usage

From source file:Importers.ExcelImporter.java

@Override
public DefaultMutableTreeNode readFile(File file) {
    System.out.println("==ExcelImporter=readFile: " + file.getAbsolutePath());
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("vulns");
    try {//from  ww  w .j a v a  2s .co  m

        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row;
        HSSFCell cell;

        int rows; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        int cols = 0; // No of columns
        int tmp = 0;

        // This trick ensures that we get the data properly even if it doesn't start from first few rows
        for (int i = 0; i < 10 || i < rows; i++) {
            row = sheet.getRow(i);
            if (row != null) {
                tmp = sheet.getRow(i).getPhysicalNumberOfCells();
                if (tmp > cols) {
                    cols = tmp;
                }
            }
        }

        for (int r = 1; r < rows; r++) {
            row = sheet.getRow(r);
            if (row != null) {

                // Create a new vuln
                Vulnerability vuln = new Vulnerability();
                vuln.setTitle("NEW");
                vuln.setIs_custom_risk(true);
                vuln.setRisk_category("None");

                for (int c = 0; c < cols; c++) {
                    cell = row.getCell(c);
                    if (cell != null) {
                        // Your code here
                        String value = cell.getStringCellValue();
                        switch (c) {
                        case 1:// title
                            vuln.setTitle(value);
                            break;
                        case 2: // Risk
                            CellStyle style = cell.getCellStyle();
                            short colorIdx = style.getFillForegroundColor();
                            HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
                            HSSFColor color = palette.getColor(colorIdx);
                            String cc = color.getHexString();
                            System.out.println(cc);
                            if (cc.equalsIgnoreCase("8080:8080:0")) {
                                vuln.setRisk_category("Critical");
                            } else if (cc.equalsIgnoreCase("FFFF:0:0")) {
                                vuln.setRisk_category("High");
                            } else if (cc.equalsIgnoreCase("FFFF:6666:0")) {
                                vuln.setRisk_category("Medium");
                            } else if (cc.equalsIgnoreCase("F2F2:ECEC:0")) {
                                vuln.setRisk_category("Low");
                            } else if (cc.equalsIgnoreCase("0:0:FFFF")) {
                                vuln.setRisk_category("Info");
                            }

                            break;
                        case 3:// cvss string
                            System.out.println(value);
                            if (value.equalsIgnoreCase("No CVSS Vector")) {
                                vuln.setIs_custom_risk(true);
                            } else {
                                vuln.setIs_custom_risk(false);
                                vuln.setCvss_vector_string("CVSS2#" + value);
                            }
                            break;
                        case 4://Description
                            vuln.setDescription(value);
                            break;
                        case 5://Recommendation
                            vuln.setRecommendation(value);
                            break;
                        case 6://Affected Hosts
                            try {
                                String[] lines = value.split("\n");

                                for (String line : lines) {
                                    String[] bits = line.split(" ");
                                    Host host = new Host();
                                    host.setIp_address(bits[0]);
                                    String portprotocol = bits[2];
                                    host.setPortnumber(portprotocol.split("/")[0]);
                                    host.setProtocol(portprotocol.split("/")[1]);
                                    vuln.addAffectedHost(host);
                                }
                            } catch (Exception ex) {
                                ;
                            }
                            break;
                        }

                    }
                }
                System.out.println(vuln);

                root.add(new DefaultMutableTreeNode(vuln));
            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return root;
}

From source file:Main.java

public void load(boolean isAlpha) {
    rootNode.removeAllChildren();//  w  w w .j a v  a 2  s.  c  om

    String[] values;
    if (isAlpha)
        values = new String[] { "A", "A1", "A2", "B", "B1", "B2" };
    else
        values = new String[] { "10", "11", "12", "20", "21", "22" };

    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(values[0]);
    DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode(values[1]);
    childNode.add(leafNode);
    leafNode = new DefaultMutableTreeNode(values[2]);
    childNode.add(leafNode);

    rootNode.add(childNode);

    childNode = new DefaultMutableTreeNode(values[3]);
    leafNode = new DefaultMutableTreeNode(values[4]);
    childNode.add(leafNode);
    leafNode = new DefaultMutableTreeNode(values[5]);
    childNode.add(leafNode);
    rootNode.add(childNode);

    nodeStructureChanged(rootNode);
}

From source file:UIDefaultsTreeModel.java

public UIDefaultsTreeModel() {
    innerModel = new DefaultTreeModel(rootNode);
    innerModel.insertNodeInto(colorNode, rootNode, 0);
    innerModel.insertNodeInto(borderNode, rootNode, 1);
    innerModel.insertNodeInto(fontNode, rootNode, 2);
    innerModel.insertNodeInto(iconNode, rootNode, 3);
    innerModel.insertNodeInto(otherNode, rootNode, 4);
    UIDefaults defaults = UIManager.getDefaults();
    Enumeration elems = defaults.keys();
    String keyName;/*from  ww w  . j  a va 2  s  . com*/
    Object valueForKey;
    while (elems.hasMoreElements()) {
        DefaultMutableTreeNode newKeyNode;
        DefaultMutableTreeNode newValueNode;
        try {
            keyName = elems.nextElement().toString();
            valueForKey = defaults.get(keyName);

            newKeyNode = new DefaultMutableTreeNode(keyName);
            newValueNode = new DefaultMutableTreeNode(valueForKey);

            if (valueForKey instanceof java.awt.Color) {
                innerModel.insertNodeInto(newKeyNode, colorNode, 0);
            } else if (valueForKey instanceof javax.swing.border.Border) {
                innerModel.insertNodeInto(newKeyNode, borderNode, 0);
            } else if (valueForKey instanceof java.awt.Font) {
                innerModel.insertNodeInto(newKeyNode, fontNode, 0);
            } else if (valueForKey instanceof javax.swing.Icon) {
                innerModel.insertNodeInto(newKeyNode, iconNode, 0);
            } else {
                innerModel.insertNodeInto(newKeyNode, otherNode, 0);
            }
            innerModel.insertNodeInto(newValueNode, newKeyNode, 0);
        } catch (NullPointerException e) {
        }
    }
}

From source file:LocationSensitiveDemo.java

private static DefaultTreeModel getDefaultTreeModel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("things");
    DefaultMutableTreeNode parent;
    DefaultMutableTreeNode nparent;

    parent = new DefaultMutableTreeNode("colors");
    root.add(parent);/*from w ww .  ja v  a 2s  .com*/
    parent.add(new DefaultMutableTreeNode("red"));
    parent.add(new DefaultMutableTreeNode("yellow"));
    parent.add(new DefaultMutableTreeNode("green"));
    parent.add(new DefaultMutableTreeNode("blue"));
    parent.add(new DefaultMutableTreeNode("purple"));

    parent = new DefaultMutableTreeNode("names");
    root.add(parent);
    nparent = new DefaultMutableTreeNode("men");
    nparent.add(new DefaultMutableTreeNode("jack"));
    nparent.add(new DefaultMutableTreeNode("kieran"));
    nparent.add(new DefaultMutableTreeNode("william"));
    nparent.add(new DefaultMutableTreeNode("jose"));

    parent.add(nparent);
    nparent = new DefaultMutableTreeNode("women");
    nparent.add(new DefaultMutableTreeNode("jennifer"));
    nparent.add(new DefaultMutableTreeNode("holly"));
    nparent.add(new DefaultMutableTreeNode("danielle"));
    nparent.add(new DefaultMutableTreeNode("tara"));
    parent.add(nparent);

    parent = new DefaultMutableTreeNode("sports");
    root.add(parent);
    parent.add(new DefaultMutableTreeNode("basketball"));
    parent.add(new DefaultMutableTreeNode("soccer"));
    parent.add(new DefaultMutableTreeNode("football"));

    nparent = new DefaultMutableTreeNode("hockey");
    parent.add(nparent);
    nparent.add(new DefaultMutableTreeNode("ice hockey"));
    nparent.add(new DefaultMutableTreeNode("roller hockey"));
    nparent.add(new DefaultMutableTreeNode("floor hockey"));
    nparent.add(new DefaultMutableTreeNode("road hockey"));

    parent = new DefaultMutableTreeNode("food");
    root.add(parent);
    parent.add(new DefaultMutableTreeNode("pizza"));
    parent.add(new DefaultMutableTreeNode("wings"));
    parent.add(new DefaultMutableTreeNode("pasta"));
    nparent = new DefaultMutableTreeNode("fruit");
    parent.add(nparent);
    nparent.add(new DefaultMutableTreeNode("bananas"));
    nparent.add(new DefaultMutableTreeNode("apples"));
    nparent.add(new DefaultMutableTreeNode("grapes"));
    nparent.add(new DefaultMutableTreeNode("pears"));
    return new DefaultTreeModel(root);
}

From source file:StAXEventTreeViewer.java

public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file)
        throws XMLStreamException, FileNotFoundException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
            StartDocument startDocument = (StartDocument) event;
            DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion());
            current.add(version);/*from  w ww  .  j a  va 2 s . c  om*/

            current.add(new DefaultMutableTreeNode(startDocument.isStandalone()));
            current.add(new DefaultMutableTreeNode(startDocument.standaloneSet()));
            current.add(new DefaultMutableTreeNode(startDocument.encodingSet()));
            current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme()));
            break;
        case XMLStreamConstants.START_ELEMENT:
            StartElement startElement = (StartElement) event;
            QName elementName = startElement.getName();

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart());
            current.add(element);
            current = element;

            if (!elementName.getNamespaceURI().equals("")) {
                String prefix = elementName.getPrefix();
                if (prefix.equals("")) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI());
                current.add(namespace);
            }

            for (Iterator it = startElement.getAttributes(); it.hasNext();) {
                Attribute attr = (Attribute) it.next();
                DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name="
                        + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')");
                String attURI = attr.getName().getNamespaceURI();
                if (!attURI.equals("")) {
                    String attPrefix = attr.getName().getPrefix();
                    if (attPrefix.equals("")) {
                        attPrefix = "[None]";
                    }
                    attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI));
                }
                current.add(attribute);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            Characters characters = (Characters) event;
            if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) {
                String data = characters.getData();
                if (data.length() != 0) {
                    current.add(new DefaultMutableTreeNode(characters.getData()));
                }
            }
            break;
        case XMLStreamConstants.DTD:
            DTD dtde = (DTD) event;
            current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration()));
        default:
            System.out.println(event.getClass().getName());
        }
    }
}

From source file:org.syncope.console.commons.RoleTreeBuilder.java

private void populateSubtree(final DefaultMutableTreeNode subRoot, final List<RoleTO> roles) {

    RoleTO role = (RoleTO) subRoot.getUserObject();

    DefaultMutableTreeNode child;
    for (RoleTO subRoleTO : getChildRoles(role.getId(), roles)) {
        child = new DefaultMutableTreeNode(subRoleTO);
        subRoot.add(child);/*from w  ww .  j  a va 2  s. c o m*/
        populateSubtree(child, roles);
    }
}

From source file:com.mindcognition.mindraider.ui.swing.explorer.NotebooksTree.java

public NotebooksTree() {
    // TODO bundle
    notebookUris = new HashSet<String>();
    notebooksRootNode = new DefaultMutableTreeNode(new NotebookNodeUserObject("Outlines", 0, null));

    notebooksTreeModel = new DefaultTreeModel(notebooksRootNode);
    setModel(notebooksTreeModel);//from w ww  .  j a v  a2 s.c om

    setEditable(false);
    getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    addTreeExpansionListener(new TreeExpansionListenerImplementation());
    addTreeWillExpandListener(new TreeWillExpandListenerImplementation());
    setShowsRootHandles(true);

    setCellRenderer(new OutlinesTreeCellRenderer());

    // tree node selection listener
    addTreeSelectionListener(new NotebooksTreeSelectionListener(this));

    reloadModel();
}

From source file:Importers.ImportReportCompiler.java

@Override
public DefaultMutableTreeNode readFile(File importFile) {
    System.out.println("==ImportReportCompiler=readFile");
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("vulns");
    try {// w  w  w  .  j a va 2 s . c  o m

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(importFile);
        doc.normalize();

        NodeList vulns_list = doc.getElementsByTagName("vuln");
        for (int i = 0; i < vulns_list.getLength(); i++) {
            Node vuln_node = vulns_list.item(i);
            Vulnerability vuln = getVuln(vuln_node);
            root.add(new DefaultMutableTreeNode(vuln));
        }

    } catch (ParserConfigurationException ex) {
        Logger.getLogger(NessusV2XMLImporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXException ex) {
        Logger.getLogger(NessusV2XMLImporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(NessusV2XMLImporter.class.getName()).log(Level.SEVERE, null, ex);
    }

    return root;
}

From source file:AncestorTree.java

public boolean addAncestors(DefaultMutableTreeNode node) {
    if (node.getChildCount() > 0)
        return false;

    Object obj = node.getUserObject();
    if (obj == null)
        return false;
    node.add(new DefaultMutableTreeNode(new IconData(ICON_MALE, "Father of: " + obj.toString())));
    node.add(new DefaultMutableTreeNode(new IconData(ICON_FEMALE, "Mother of: " + obj.toString())));
    return true;// www.java2 s. c o  m
}

From source file:MyData.java

protected MutableTreeNode getRootNode() {
    DefaultMutableTreeNode root, child;
    MyData question;//from w  w  w.  j av a 2 s .c  o m
    root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < questions.length; i++) {
        question = new MyData(questions[i]);
        child = new DefaultMutableTreeNode(question);
        root.add(child);
    }
    return root;
}