Example usage for javax.swing.tree DefaultMutableTreeNode add

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

Introduction

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

Prototype

public void add(MutableTreeNode newChild) 

Source Link

Document

Removes newChild from its parent and makes it a child of this node by adding it to the end of this node's child array.

Usage

From source file:org.zaproxy.zap.control.AddOnRunIssuesUtils.java

/**
 * Creates a scrollable tree with the given add-ons as root nodes and its issues as child nodes.
 *
 * @param availableAddOns the add-ons that are available, used to create check the running issues
 * @param addOnsNotRunnable the add-ons with running issues that will be shown in the tree
 * @return the tree wrapper in a {@code JSCrollPane}
 *///  ww w  .  j a  v  a  2s. c o m
private static JScrollPane createScrollableTreeAddOnsNotRunnable(final AddOnCollection availableAddOns,
        AddOn... addOnsNotRunnable) {
    AddOnSearcher addOnSearcher = new AddOnSearcher() {

        @Override
        public AddOn searchAddOn(String id) {
            return availableAddOns.getAddOn(id);
        }
    };

    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("");
    for (AddOn addOn : addOnsNotRunnable) {
        DefaultMutableTreeNode addOnNode = new DefaultMutableTreeNode(addOn.getName());
        AddOn.AddOnRunRequirements requirements = addOn.calculateRunRequirements(availableAddOns.getAddOns());
        List<String> issues = getUiRunningIssues(requirements, addOnSearcher);

        if (issues.isEmpty()) {
            issues.addAll(getUiExtensionsRunningIssues(requirements, addOnSearcher));
        }

        for (String issue : issues) {
            addOnNode.add(new DefaultMutableTreeNode(issue));
        }
        rootNode.add(addOnNode);
    }

    JXTree tree = new JXTree(new DefaultTreeModel(rootNode));
    tree.setVisibleRowCount(5);
    tree.setEditable(false);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.expandAll();

    return new JScrollPane(tree);
}

From source file:org.zaproxy.zap.extension.ascan.CustomScanDialog.java

private void setTech() {
    Context context = this.getSelectedContext();

    TechSet ts = new TechSet(Tech.builtInTech);
    Iterator<Tech> iter = ts.getIncludeTech().iterator();

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(
            Constant.messages.getString("ascan.custom.tab.tech.node"));
    Tech tech;/*w  w w  .  j  a v  a 2 s .  c o m*/
    DefaultMutableTreeNode parent;
    DefaultMutableTreeNode node;
    while (iter.hasNext()) {
        tech = iter.next();
        if (tech.getParent() != null) {
            parent = techToNodeMap.get(tech.getParent());
        } else {
            parent = null;
        }
        if (parent == null) {
            parent = root;
        }
        node = new DefaultMutableTreeNode(tech.getUiName());
        parent.add(node);
        techToNodeMap.put(tech, node);
    }

    techModel = new DefaultTreeModel(root);
    techTree.setModel(techModel);
    techTree.expandAll();
    // Default to everything set
    TreePath rootTp = new TreePath(root);
    techTree.checkSubTree(rootTp, true);
    techTree.setCheckBoxEnabled(rootTp, false);

    if (context != null) {
        TechSet techSet = context.getTechSet();
        Iterator<Entry<Tech, DefaultMutableTreeNode>> iter2 = techToNodeMap.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<Tech, DefaultMutableTreeNode> nodeEntry = iter2.next();
            TreePath tp = this.getTechPath(nodeEntry.getValue());
            if (techSet.includes(nodeEntry.getKey())) {
                this.getTechTree().check(tp, true);
            }
        }
    }
}

From source file:org.zaproxy.zap.extension.customFire.TechnologyTreePanel.java

public TechnologyTreePanel(String nameRootNode) {
    setLayout(new BorderLayout());

    techToNodeMap = new HashMap<>();
    techTree = new JCheckBoxScriptsTree() {

        private static final long serialVersionUID = 1L;

        @Override/*  w  ww.j  ava2s.  co m*/
        protected void setExpandedState(TreePath path, boolean state) {
            // Ignore all collapse requests; collapse events will not be fired
            if (state) {
                super.setExpandedState(path, state);
            }
        }

    };
    // Initialise the structure based on all the tech we know about
    TechSet ts = new TechSet(Tech.builtInTech);
    Iterator<Tech> iter = ts.getIncludeTech().iterator();

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(nameRootNode);
    Tech tech;
    DefaultMutableTreeNode parent;
    DefaultMutableTreeNode node;
    while (iter.hasNext()) {
        tech = iter.next();
        if (tech.getParent() != null) {
            parent = techToNodeMap.get(tech.getParent());
        } else {
            parent = null;
        }
        if (parent == null) {
            parent = root;
        }
        node = new DefaultMutableTreeNode(tech.getUiName());
        parent.add(node);
        techToNodeMap.put(tech, node);
    }

    techTree.setModel(new DefaultTreeModel(root));
    techTree.expandAll();
    techTree.setCheckBoxEnabled(new TreePath(root), false);
    reset();

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(techTree);
    scrollPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));

    add(scrollPane, BorderLayout.CENTER);
}

From source file:org.zaproxy.zap.view.ContextTechnologyPanel.java

private JCheckBoxTree getTechTree() {
    if (techTree == null) {
        techTree = new JCheckBoxTree() {
            private static final long serialVersionUID = 1L;

            @Override/*from ww w . j  ava  2 s. c o m*/
            protected void setExpandedState(TreePath path, boolean state) {
                // Ignore all collapse requests; collapse events will not be fired
                if (state) {
                    super.setExpandedState(path, state);
                }
            }
        };
        // Initialise the structure based on all the tech we know about
        TechSet ts = new TechSet(Tech.builtInTech);
        Iterator<Tech> iter = ts.getIncludeTech().iterator();

        DefaultMutableTreeNode root = new DefaultMutableTreeNode(
                Constant.messages.getString("context.technology.tree.root"));
        Tech tech;
        DefaultMutableTreeNode parent;
        DefaultMutableTreeNode node;
        while (iter.hasNext()) {
            tech = iter.next();
            if (tech.getParent() != null) {
                parent = techToNodeMap.get(tech.getParent());
            } else {
                parent = null;
            }
            if (parent == null) {
                parent = root;
            }
            node = new DefaultMutableTreeNode(tech.getUiName());
            parent.add(node);
            techToNodeMap.put(tech, node);
        }

        model = new DefaultTreeModel(root);
        techTree.setModel(model);
        techTree.expandAll();
        techTree.setCheckBoxEnabled(new TreePath(root), false);

    }
    return techTree;
}

From source file:org.zaproxy.zap.view.TechnologyTreePanel.java

public TechnologyTreePanel(String nameRootNode) {
    setLayout(new BorderLayout());

    techToNodeMap = new HashMap<>();
    techTree = new JCheckBoxTree() {

        private static final long serialVersionUID = 1L;

        @Override/*from www  .  ja v a  2  s  . c o m*/
        protected void setExpandedState(TreePath path, boolean state) {
            // Ignore all collapse requests; collapse events will not be fired
            if (state) {
                super.setExpandedState(path, state);
            }
        }
    };
    // Initialise the structure based on all the tech we know about
    TechSet ts = new TechSet(Tech.builtInTech);
    Iterator<Tech> iter = ts.getIncludeTech().iterator();

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(nameRootNode);
    Tech tech;
    DefaultMutableTreeNode parent;
    DefaultMutableTreeNode node;
    while (iter.hasNext()) {
        tech = iter.next();
        if (tech.getParent() != null) {
            parent = techToNodeMap.get(tech.getParent());
        } else {
            parent = null;
        }
        if (parent == null) {
            parent = root;
        }
        node = new DefaultMutableTreeNode(tech.getUiName());
        parent.add(node);
        techToNodeMap.put(tech, node);
    }

    techTree.setModel(new DefaultTreeModel(root));
    techTree.expandAll();
    techTree.setCheckBoxEnabled(new TreePath(root), false);
    reset();

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(techTree);
    scrollPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));

    add(scrollPane, BorderLayout.CENTER);
}

From source file:plptool.gui.ProjectDriver.java

/**
 * GUI: update the develop window to reflect the current state of the
 * project driver (open file, etc.)// ww  w .ja  va 2s. co  m
 *
 * @param commitCurrentAsm Whether to commit currently open asm file
 * in the editor before refreshing.
 * @return PLP_OK
 */
public int refreshProjectView(boolean commitCurrentAsm) {
    Msg.D("Project view refresh...", 3, this);

    if (plpfile == null) {
        g_dev.disableBuildControls();
        g_dev.catchyPLP();

        g_dev.getProjectTree()
                .setModel(new DefaultTreeModel(new DefaultMutableTreeNode("No project file open.")));

        return Constants.PLP_OK;
    }

    if (commitCurrentAsm)
        updateAsm(open_asm, g_dev.getEditorText());

    updateWindowTitle();

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(plpfile.getName());
    DefaultMutableTreeNode srcRoot = new DefaultMutableTreeNode("Source Files");
    DefaultMutableTreeNode metaRoot = new DefaultMutableTreeNode("Meta Information");
    root.add(srcRoot);
    root.add(metaRoot);
    for (int i = 0; i < asms.size(); i++)
        srcRoot.add(new DefaultMutableTreeNode(i + ": " + asms.get(i).getAsmFilePath()));

    Scanner metaScanner = new Scanner(meta);
    metaScanner.findWithinHorizon("DIRTY=", 0);
    int meta_dirty = metaScanner.nextInt();
    metaRoot.add(new DefaultMutableTreeNode("meta.DIRTY=" + meta_dirty));
    metaRoot.add(new DefaultMutableTreeNode("ISA=" + ArchRegistry.getStringID(arch.getID())));

    g_dev.getProjectTree().setModel(new DefaultTreeModel(root));
    for (int i = 0; i < g_dev.getProjectTree().getRowCount(); i++)
        g_dev.getProjectTree().expandRow(i);

    if (!asms.get(open_asm).getAsmString().equals(g_dev.getEditorText()))
        g_dev.setEditorText(asms.get(open_asm).getAsmString());

    g_dev.getEditor().setEnabled(true);
    g_dev.getEditor().setVisible(true);
    g_dev.getEditor().setCaretPosition(0);
    g_dev.enableBuildControls();

    String header = asms.get(open_asm).getAsmFilePath();

    if (open_asm == 0)
        header += " <main program>";

    g_dev.setCurFile(header);

    CallbackRegistry.callback(CallbackRegistry.GUI_VIEW_REFRESH, commitCurrentAsm);
    Msg.D("Done.", 3, this);
    return Constants.PLP_OK;
}

From source file:Proiect.uploadFTP.java

public void actionFTP() {
    adressf.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent e) {
            InetAddress thisIp;/*w  ww .j  av  a2s .com*/
            try {
                thisIp = InetAddress.getLocalHost();
                titleFTP.setText("Connection: " + thisIp.getHostAddress() + " -> " + adressf.getText());
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            }
        }
    });

    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            saveState();
            uploadFTP.dispose();
            tree.dispose();
        }
    });

    connect.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            FTPClient client = new FTPClient();
            FileInputStream fis = null;
            String pass = String.valueOf(passf.getPassword());
            try {
                if (filename == null) {
                    status.setText("File does not exist!");
                } else {
                    // Server address
                    client.connect(adressf.getText());
                    // Login credentials
                    client.login(userf.getText(), pass);
                    if (client.isConnected()) {
                        status.setText("Succesfull transfer!");
                        // File type
                        client.setFileType(FTP.BINARY_FILE_TYPE);
                        // File location
                        File file = new File(filepath);
                        fis = new FileInputStream(file);
                        // Change the folder on the server
                        client.changeWorkingDirectory(folderf.getText());
                        // Save the file on the server
                        client.storeFile(filename, fis);
                    } else {
                        status.setText("Transfer failed!");
                    }
                }
                client.logout();
            } catch (IOException e1) {
                Encrypter.printException(e1);
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    client.disconnect();
                } catch (IOException e1) {
                    Encrypter.printException(e1);
                }
            }
        }
    });

    browsef.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int retval = chooserf.showOpenDialog(chooserf);
            if (retval == JFileChooser.APPROVE_OPTION) {
                status.setText("");
                filename = chooserf.getSelectedFile().getName().toString();
                filepath = chooserf.getSelectedFile().getPath();
                filenf.setText(chooserf.getSelectedFile().getName().toString());
            }
        }
    });

    adv.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            tree.setSize(220, uploadFTP.getHeight());
            tree.setLocation(uploadFTP.getX() + 405, uploadFTP.getY());
            tree.setResizable(false);
            tree.setIconImage(Toolkit.getDefaultToolkit()
                    .getImage(getClass().getClassLoader().getResource("assets/ico.png")));
            tree.setUndecorated(true);
            tree.getRootPane().setBorder(BorderFactory.createLineBorder(Encrypter.color_black, 2));
            tree.setVisible(true);
            tree.setLayout(new BorderLayout());

            JLabel labeltree = new JLabel("Server documents");
            labeltree.setOpaque(true);
            labeltree.setBackground(Encrypter.color_light);
            labeltree.setBorder(BorderFactory.createMatteBorder(8, 10, 10, 0, Encrypter.color_light));
            labeltree.setForeground(Encrypter.color_blue);
            labeltree.setFont(Encrypter.font16);

            JButton refresh = new JButton("");
            ImageIcon refresh_icon = getImageIcon("assets/icons/refresh.png");
            refresh.setIcon(refresh_icon);
            refresh.setBackground(Encrypter.color_light);
            refresh.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
            refresh.setForeground(Encrypter.color_black);
            refresh.setFont(Encrypter.font16);
            refresh.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

            final FTPClient client = new FTPClient();
            DefaultMutableTreeNode top = new DefaultMutableTreeNode(adressf.getText());
            DefaultMutableTreeNode files = null;
            DefaultMutableTreeNode leaf = null;

            final JTree tree_view = new JTree(top);
            tree_view.setForeground(Encrypter.color_black);
            tree_view.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
            tree_view.putClientProperty("JTree.lineStyle", "None");
            tree_view.setBackground(Encrypter.color_light);
            JScrollPane scrolltree = new JScrollPane(tree_view);
            scrolltree.setBackground(Encrypter.color_light);
            scrolltree.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));

            UIManager.put("Tree.textBackground", Encrypter.color_light);
            UIManager.put("Tree.selectionBackground", Encrypter.color_blue);
            UIManager.put("Tree.selectionBorderColor", Encrypter.color_blue);

            tree_view.updateUI();

            final String pass = String.valueOf(passf.getPassword());
            try {
                client.connect(adressf.getText());
                client.login(userf.getText(), pass);
                client.enterLocalPassiveMode();
                if (client.isConnected()) {
                    try {
                        FTPFile[] ftpFiles = client.listFiles();
                        for (FTPFile ftpFile : ftpFiles) {
                            files = new DefaultMutableTreeNode(ftpFile.getName());
                            top.add(files);
                            if (ftpFile.getType() == FTPFile.DIRECTORY_TYPE) {
                                FTPFile[] ftpFiles1 = client.listFiles(ftpFile.getName());
                                for (FTPFile ftpFile1 : ftpFiles1) {
                                    leaf = new DefaultMutableTreeNode(ftpFile1.getName());
                                    files.add(leaf);
                                }
                            }
                        }
                    } catch (IOException e1) {
                        Encrypter.printException(e1);
                    }
                    client.disconnect();
                } else {
                    status.setText("Failed connection!");
                }
            } catch (IOException e1) {
                Encrypter.printException(e1);
            } finally {
                try {
                    client.disconnect();
                } catch (IOException e1) {
                    Encrypter.printException(e1);
                }
            }

            tree.add(labeltree, BorderLayout.NORTH);
            tree.add(scrolltree, BorderLayout.CENTER);
            tree.add(refresh, BorderLayout.SOUTH);

            uploadFTP.addComponentListener(new ComponentListener() {

                public void componentMoved(ComponentEvent e) {
                    tree.setLocation(uploadFTP.getX() + 405, uploadFTP.getY());
                }

                public void componentShown(ComponentEvent e) {
                }

                public void componentResized(ComponentEvent e) {
                }

                public void componentHidden(ComponentEvent e) {
                }
            });

            uploadFTP.addWindowListener(new WindowListener() {
                public void windowActivated(WindowEvent e) {
                    tree.toFront();
                }

                public void windowOpened(WindowEvent e) {
                }

                public void windowIconified(WindowEvent e) {
                }

                public void windowDeiconified(WindowEvent e) {
                }

                public void windowDeactivated(WindowEvent e) {
                }

                public void windowClosing(WindowEvent e) {
                }

                public void windowClosed(WindowEvent e) {
                }
            });

            refresh.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    tree.dispose();
                    tree.setVisible(true);
                }
            });
        }
    });

}

From source file:ropes.MainWindow.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    //TODO display loader animation while liberys are loading
    DefaultTreeModel model = (DefaultTreeModel) jTree_fileList.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();

    final JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.setMultiSelectionEnabled(true);/*  w  w  w. j a  v a2 s .c o  m*/

    int returnVal = fc.showOpenDialog(MainWindow.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File[] files = fc.getSelectedFiles();
        for (File file : files) {

            if (file.isFile()) {
                addFileToList(file);
            } else if (file.isDirectory()) {
                //adds the parent to tree
                root.add(new DefaultMutableTreeNode(file));
                //adds all the subfolders and files to the parent
                addFileRecursive(file.toString());

            }

        }

    }

}

From source file:ropes.MainWindow.java

private void addFileToList(File path) {
    DefaultTreeModel model = (DefaultTreeModel) jTree_fileList.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    //get perent directory to see if such node exist
    File perentDir = path.getParentFile();

    DefaultMutableTreeNode node = findNode(root, perentDir.toString());
    if (node != null)
        node.add(new DefaultMutableTreeNode(path));
    else {//from  w  ww .j  ava  2s .c  o m
        root.add(new DefaultMutableTreeNode(path));
    }
    //update jtree
    model.reload(root);

}

From source file:se.nawroth.asciidoc.browser.AsciidocBrowserApplication.java

private void addFileWrapperChildren(final DefaultMutableTreeNode parentNode, final Object... parentPath) {
    FileWrapper parent = (FileWrapper) parentNode.getUserObject();
    for (FileWrapper child : getChildren(parent)) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(child);
        parentNode.add(node);
        Object[] path = Arrays.copyOf(parentPath, parentPath.length + 1);
        path[parentPath.length] = node;//ww w  .j a va  2s.c  o m
        paths.put(child, path);
        addFileWrapperChildren(node, path);
    }
}