Example usage for javax.swing.tree DefaultTreeModel DefaultTreeModel

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

Introduction

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

Prototype

@ConstructorProperties({ "root" })
public DefaultTreeModel(TreeNode root) 

Source Link

Document

Creates a tree in which any node can have children.

Usage

From source file:hu.bme.mit.sette.snippetbrowser.SnippetBrowser.java

private void initialized() {
    DefaultTreeModel model = new DefaultTreeModel(new SnippetProjectTreeNode(snippetProject));
    treeSnippets.setModel(model);/* w  w  w.j a v a 2s.  c  o  m*/
    treeSnippets.addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            if (treeSnippets.getSelectionCount() == 1) {
                TreeNodeBase<?, ?> treeNode = (TreeNodeBase<?, ?>) treeSnippets.getSelectionPath()
                        .getLastPathComponent();
                txtrInfo.setText(treeNode.getDescription());
            } else if (treeSnippets.getSelectionCount() > 1) {
                int projectContainerCnt = 0;
                int projectSnippetCnt = 0;

                projectContainerCnt = snippetProject.getModel().getContainers().size();

                for (SnippetContainer container : snippetProject.getModel().getContainers()) {
                    projectSnippetCnt += container.getSnippets().size();
                }

                int containerCnt = 0;
                int containerSnippetCnt = 0;
                int snippetCnt = 0;

                for (TreePath path : treeSnippets.getSelectionPaths()) {
                    Object node = path.getLastPathComponent();

                    if (node instanceof SnippetContainerTreeNode) {
                        containerCnt++;

                        SnippetContainerTreeNode obj = (SnippetContainerTreeNode) node;
                        containerSnippetCnt += obj.getContainer().getSnippets().size();
                    } else if (node instanceof SnippetTreeNode) {
                        snippetCnt++;
                    }
                }

                String[] lines = new String[3];

                lines[0] = String.format("Project contains %d container(s) with %d snippet(s)",
                        projectContainerCnt, projectSnippetCnt);
                lines[1] = String.format("Selected %d container(s) (%d snippet(s))", containerCnt,
                        containerSnippetCnt);
                lines[2] = String.format("Selected %d snippet(s)", snippetCnt);

                txtrInfo.setText(StringUtils.join(lines, '\n'));
            } else {
                txtrInfo.setText("[No selection]");
            }
        }
    });
}

From source file:uk.co.markfrimston.tasktree.TaskTree.java

public TaskTree(String filePath) {
    this.filePath = filePath;

    this.root = new DefaultMutableTreeNode("root");
    this.treeModel = new DefaultTreeModel(root);
}

From source file:net.sf.housekeeper.swing.CategoriesView.java

private void refresh() {
    LOG.debug("Refreshing view");
    final Category root = (Category) categoryManager.getCategories().get(0);
    final DefaultMutableTreeNode rootNode = createNode(root);
    final DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    tree.setModel(treeModel);//  ww  w .ja v a  2  s . c o m
    tree.setSelectionRow(0);
}

From source file:nosqltools.JSONUtilities.java

public DefaultTreeModel makeJtreeModel(String filename) {
    DefaultMutableTreeNode root = makeJtree(filename, JSONParsedData);
    DefaultTreeModel dt = new DefaultTreeModel(root);

    return dt;/*from w  w w. j  av  a 2s.c  om*/
}

From source file:SAXTreeValidator.java

/**
 * <p> This will construct the tree using Swing. </p>
 *
 * @param filename <code>String</code> path to XML document.
 *///from   w ww  .  j  ava2 s  .c o  m
public void init(String xmlURI) throws IOException, SAXException {
    DefaultMutableTreeNode base = new DefaultMutableTreeNode("XML Document: " + xmlURI);

    // Build the tree model
    defaultTreeModel = new DefaultTreeModel(base);
    jTree = new JTree(defaultTreeModel);

    // Construct the tree hierarchy
    buildTree(defaultTreeModel, base, xmlURI);

    // Display the results
    getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);
}

From source file:org.spf4j.ui.TSDBViewJInternalFrame.java

/**
 * Creates new form TSDBViewJInternalFrame
 *///w w  w .j a  v a 2  s .  c  o m
public TSDBViewJInternalFrame(final String databaseFile) throws IOException {
    super(databaseFile);
    initComponents();
    tsDb = new TimeSeriesDatabase(databaseFile, null);
    Collection<TSTable> columnsInfo = tsDb.getTSTables();
    Map<String, DefaultMutableTreeNode> gNodes = new HashMap<>();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(databaseFile);
    long startDate = System.currentTimeMillis();
    for (TSTable info : columnsInfo) {
        String groupName = info.getTableName();
        long tableStart = tsDb.readStartDate(groupName);
        if (tableStart < startDate) {
            startDate = tableStart;
        }
        Pair<String, String> pair = Pair.from(groupName);
        if (pair == null) {
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(groupName);
            for (String colName : info.getColumnNames()) {
                child.add(new DefaultMutableTreeNode(colName));
            }
            root.add(child);
        } else {
            groupName = pair.getFirst();
            DefaultMutableTreeNode gNode = gNodes.get(groupName);
            if (gNode == null) {
                gNode = new DefaultMutableTreeNode(groupName);
                gNodes.put(groupName, gNode);
                root.add(gNode);
            }
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(pair.getSecond());
            for (String colName : info.getColumnNames()) {
                child.add(new DefaultMutableTreeNode(colName));
            }
            gNode.add(child);
        }
    }
    measurementTree.setModel(new DefaultTreeModel(root));
    measurementTree.setVisible(true);
    this.startDate.setValue(new DateTime().toDate());
}

From source file:net.pms.newgui.SelectRenderers.java

public void build() {
    JPanel checkPanel = new JPanel();
    checkPanel.applyComponentOrientation(ComponentOrientation.getOrientation(PMS.getLocale()));
    add(checkPanel, BorderLayout.LINE_START);
    allRenderers = new SearchableMutableTreeNode(Messages.getString("GeneralTab.13"));

    Pattern pattern = Pattern.compile("^\\s*([^\\s]*) ?([^\\s].*?)?\\s*$");
    for (String renderer : RendererConfiguration.getAllRenderersNames()) {
        Matcher match = pattern.matcher(renderer);
        if (match.find()) {
            // Find or create group or single name renderer
            SearchableMutableTreeNode node = null;
            try {
                node = allRenderers.findChild(match.group(1));
            } catch (IllegalChildException e) {
            }//from w w w.  j av  a  2 s  .co  m
            if (node == null) {
                node = new SearchableMutableTreeNode(match.group(1));
                allRenderers.add(node);
            }
            // Find or create subgroup/name
            if (match.groupCount() > 1 && match.group(2) != null) {
                SearchableMutableTreeNode subNode = null;
                try {
                    subNode = node.findChild(match.group(2));
                } catch (IllegalChildException e) {
                }
                if (subNode != null) {
                    LOGGER.warn("Renderer {} found twice, ignoring repeated entry", renderer);
                } else {
                    subNode = new SearchableMutableTreeNode(match.group(2));
                    node.add(subNode);
                }
            }
        } else {
            LOGGER.warn("Can't parse renderer name \"{}\"", renderer);
        }
    }

    SrvTree = new JTree(new DefaultTreeModel(allRenderers));
    checkTreeManager = new CheckTreeManager(SrvTree);
    checkPanel.add(new JScrollPane(SrvTree));
    checkPanel.setSize(400, 500);
}

From source file:br.upe.ecomp.dosa.view.mainwindow.MainWindowActions.java

/**
 * Default constructor.//from w  ww. j  av a 2s . c  o  m
 */
public MainWindowActions() {
    super();
    setTitle("Dynamic Optimization System Analyzer");

    createPopupMenus();
    updateToolBarButtonsState();

    initResultsTab();

    tree.setModel(new DefaultTreeModel(null));
    table.setModel(new DefaultTableModel());
}

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  w  w.  ja  va 2 s.  c o m
    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:com.mindcognition.mindraider.ui.swing.trash.TrashJPanel.java

/**
 * Constructor.//from w  w w .j  a v  a  2  s.  c o m
 */
private TrashJPanel() {
    treeNodeToResourceUriMap = new HashMap();

    rootNode = new DefaultMutableTreeNode(Messages.getString("TrashJPanel.notebookArchive"));
    treeModel = new DefaultTreeModel(rootNode);
    treeModel.addTreeModelListener(new MyTreeModelListener());

    tree = new JTree(treeModel);
    tree.setEditable(false);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.addTreeExpansionListener(this);
    tree.addTreeWillExpandListener(this);
    tree.setShowsRootHandles(true);
    tree.putClientProperty("JTree.lineStyle", "Angled");

    // tree rendered
    // TODO implement own renderer in order to tooltips
    tree.setCellRenderer(new TrashTreeCellRenderer(IconsRegistry.getImageIcon("trashFull.png"),
            IconsRegistry.getImageIcon("explorerNotebookIcon.png")));

    setLayout(new BorderLayout());

    // control panel
    JToolBar tp = new JToolBar();
    tp.setLayout(new GridLayout(1, 6));
    undoButton = new JButton("", IconsRegistry.getImageIcon("trashUndo.png"));
    undoButton.setEnabled(false);
    undoButton.setToolTipText("Restore Outline");
    undoButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node == null) {
                return;
            }
            new RestoreNotebookJDialog((String) treeNodeToResourceUriMap.get(node), "Restore Outline",
                    "Restore", true);
        }
    });
    tp.add(undoButton);

    deleteButton = new JButton("", IconsRegistry.getImageIcon("explorerDeleteSmall.png"));
    deleteButton.setToolTipText("Delete Outline");
    deleteButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node == null) {
                return;
            }

            int result = JOptionPane.showConfirmDialog(MindRaider.mainJFrame,
                    "Do you really want to DELETE this Outline?", "Delete Outline", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                MindRaider.labelCustodian.deleteOutline((String) treeNodeToResourceUriMap.get(node));
                refresh();
                ExplorerJPanel.getInstance().refresh();
            }
        }
    });
    tp.add(deleteButton);

    emptyButton = new JButton("", IconsRegistry.getImageIcon("trashEmpty.png"));

    emptyButton.setToolTipText(Messages.getString("TrashJPanel.emptyArchive"));
    emptyButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int result = JOptionPane.showConfirmDialog(MindRaider.mainJFrame,
                    "Do you really want to DELETE all discarded Outlines?", "Empty Trash",
                    JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                final SwingWorker worker = new SwingWorker() {

                    public Object construct() {
                        ProgressDialogJFrame progressDialogJFrame = new ProgressDialogJFrame("Empty Trash",
                                "<html><br>&nbsp;&nbsp;<b>Deleting:</b>&nbsp;&nbsp;</html>");
                        try {
                            ResourceDescriptor[] resourceDescriptors = MindRaider.labelCustodian
                                    .getDiscardedOutlineDescriptors();
                            if (resourceDescriptors != null) {
                                for (int i = 0; i < resourceDescriptors.length; i++) {
                                    MindRaider.labelCustodian.deleteOutline(resourceDescriptors[i].getUri());
                                }
                                refresh();
                            }
                        } finally {
                            if (progressDialogJFrame != null) {
                                progressDialogJFrame.dispose();
                            }
                        }
                        return null;
                    }
                };
                worker.start();
            }
        }
    });
    tp.add(emptyButton);

    add(tp, BorderLayout.NORTH);

    // add the tree
    JScrollPane scrollPane = new JScrollPane(tree);
    add(scrollPane);
    // build the whole tree
    buildTree();
    // click handler
    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (node == null) {
                return;
            }
            logger.debug("Tree selection path: " + node.getPath()[node.getLevel()]);

            enableDisableToolbarButtons(node.getLevel());
        }
    });
}