Example usage for javax.swing.event TreeModelEvent getTreePath

List of usage examples for javax.swing.event TreeModelEvent getTreePath

Introduction

In this page you can find the example usage for javax.swing.event TreeModelEvent getTreePath.

Prototype

public TreePath getTreePath() 

Source Link

Document

For all events, except treeStructureChanged, returns the parent of the changed nodes.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JTree Demo");
    f.setSize(260, 240);/*w w w.ja v  a2  s. c om*/
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("A");
    root.add(aNode);

    DefaultMutableTreeNode bNode = new DefaultMutableTreeNode("B");
    aNode.add(bNode);

    DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("C");
    aNode.add(cNode);

    cNode.add(new DefaultMutableTreeNode("D"));
    cNode.add(new DefaultMutableTreeNode("E"));

    DefaultMutableTreeNode fNode = new DefaultMutableTreeNode("F");
    root.add(fNode);

    DefaultMutableTreeNode gNode = new DefaultMutableTreeNode("G");
    fNode.add(gNode);
    fNode.add(new DefaultMutableTreeNode("H"));
    gNode.add(new DefaultMutableTreeNode("I"));

    JTree jtree = new JTree(root);
    jtree.setEditable(true);

    TreeSelectionModel tsm = jtree.getSelectionModel();
    tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    jtree.addTreeExpansionListener(new TreeExpansionListener() {
        public void treeExpanded(TreeExpansionEvent tee) {
            TreePath tp = tee.getPath();
            System.out.println("Expansion: " + tp.getLastPathComponent());
        }

        public void treeCollapsed(TreeExpansionEvent tee) {
            TreePath tp = tee.getPath();
            System.out.println("Collapse: " + tp.getLastPathComponent());
        }
    });

    jtree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent tse) {
            TreePath tp = tse.getPath();
            System.out.println("Selection event: " + tp.getLastPathComponent());
        }
    });

    jtree.getModel().addTreeModelListener(new TreeModelListener() {
        public void treeNodesChanged(TreeModelEvent tme) {
            TreePath tp = tme.getTreePath();
            Object[] children = tme.getChildren();
            DefaultMutableTreeNode changedNode;
            if (children != null)
                changedNode = (DefaultMutableTreeNode) children[0];
            else
                changedNode = (DefaultMutableTreeNode) tp.getLastPathComponent();

            System.out.println("Model change path: " + tp + "New data: " + changedNode.getUserObject());

        }

        public void treeNodesInserted(TreeModelEvent tme) {
        }

        public void treeNodesRemoved(TreeModelEvent tme) {
        }

        public void treeStructureChanged(TreeModelEvent tme) {
        }
    });

    f.add(new JScrollPane(jtree));
    f.setVisible(true);
}

From source file:MyTreeModelListener.java

public void treeNodesChanged(TreeModelEvent e) {
    DefaultMutableTreeNode node;/*  www  . ja  v a  2s  .  co m*/
    node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent());
    try {
        int index = e.getChildIndices()[0];
        node = (DefaultMutableTreeNode) (node.getChildAt(index));
    } catch (NullPointerException exc) {
    }
    System.out.println("New value: " + node.getUserObject());
}

From source file:MyTreeModelListener.java

public void treeNodesChanged(TreeModelEvent e) {
    DefaultMutableTreeNode node;//from  w  ww.java 2s  .  c om
    node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent());

    try {
        int index = e.getChildIndices()[0];
        node = (DefaultMutableTreeNode) (node.getChildAt(index));
    } catch (NullPointerException exc) {
    }

    System.out.println("The user has finished editing the node.");
    System.out.println("New value: " + node.getUserObject());
}

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

public void treeNodesChanged(TreeModelEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent());

    /*//from w  w w  . j a v a2s  . co m
     * If the event lists children, then the changed node is the child
     * of the node we've already gotten. Otherwise, the changed node and
     * the specified node are the same.
     */
    // TODO
    try {
        int index = e.getChildIndices()[0];
        node = (DefaultMutableTreeNode) (node.getChildAt(index));

        // TODO RENAME NOT IMPLEMENTED investigate how is the node stored into the user object
        /*            
                    if (node.getLevel() == LEVEL_LABELS) {
        MindRaider.folderCustodian.rename(
                (String) getTreeNodeToResourceUriMap().get(node),
                (String) node.getUserObject());
                    } else {
        if (node.getLevel() == LEVEL_NOTEBOOKS) {
            MindRaider.notebookCustodian.rename(
                    (String) getTreeNodeToResourceUriMap()
                            .get(node), (String) node
                            .getUserObject());
            MindRaider.profile.setActiveNotebookUri(null);
            MindRaider.notebookCustodian.loadNotebook(new URI(
                    (String) getTreeNodeToResourceUriMap()
                            .get(node)));
            NotebookJPanel.getInstance().refresh();
        }
                    }
        */
    } catch (Exception ex) {
        logger.debug("Unable to found node!", ex);
    }

    logger.debug("New value: " + node.getUserObject());
}

From source file:net.sf.mzmine.modules.visualization.ida.IDABottomPanel.java

@Override
public void treeNodesChanged(TreeModelEvent event) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) event.getTreePath().getLastPathComponent();
    if (node.getUserObject() instanceof PeakList)
        rebuildPeakListSelector();/*from w w w . j a v a 2 s.c om*/
}

From source file:net.sf.mzmine.modules.visualization.ida.IDABottomPanel.java

@Override
public void treeNodesInserted(TreeModelEvent event) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) event.getTreePath().getLastPathComponent();
    if (node.getUserObject() instanceof PeakList)
        rebuildPeakListSelector();/*w w  w .  j av a2s .  c  om*/
}

From source file:net.sf.mzmine.modules.visualization.ida.IDABottomPanel.java

@Override
public void treeNodesRemoved(TreeModelEvent event) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) event.getTreePath().getLastPathComponent();
    if (node.getUserObject() instanceof PeakList)
        rebuildPeakListSelector();//from  w  w w .j  av a  2s.com
}

From source file:net.sf.mzmine.modules.visualization.ida.IDABottomPanel.java

@Override
public void treeStructureChanged(TreeModelEvent event) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) event.getTreePath().getLastPathComponent();
    if (node.getUserObject() instanceof PeakList)
        rebuildPeakListSelector();// ww  w .  ja v  a  2 s .c o  m
}

From source file:cz.lidinsky.editor.Editor.java

/**
 *  Add a link component -> tree to each new node.
 *  And select last inserted node./*from  www  .ja  va 2s.c om*/
 */
public void treeNodesInserted(TreeModelEvent e) {
    // add a link component -> tree
    TreePath parentPath = e.getTreePath();
    Node<GuiObject> parent = (Node<GuiObject>) parentPath.getLastPathComponent();
    int[] indexes = e.getChildIndices();
    for (int i : indexes) {
        Node<GuiObject> child = parent.getChild(i);
        setComponent2TreeLink(child);
    }
    // select last inserted node
    Node<GuiObject> child = parent.getChild(indexes[indexes.length - 1]);
    guiStructureTree.setSelectionPath(parentPath.pathByAddingChild(child));
}

From source file:hr.fer.zemris.vhdllab.view.explorer.ProjectExplorerView.java

@Override
protected JComponent createControl() {
    CommandManager commandManager = getActiveWindow().getCommandManager();
    commandManager.createCommandGroup("hierarchyMenu", new String[] { hierarchyXUsesYCommand.getId(),
            hierarchyXUsedByYCommand.getId(), hierarchyFlatCommand.getId() });
    CommandGroup commandGroup = commandManager.createCommandGroup("treePopupMenu",
            new String[] { "newMenu", "separator", openCommand.getId(), "separator", compileCommand.getId(),
                    simulateCommand.getId(), viewVhdlCommand.getId(), "separator", deleteCommand.getId(),
                    "separator", "hierarchyMenu" });
    popupMenu = commandGroup.createPopupMenu();

    root = new DefaultMutableTreeNode("vhdllab-root");
    model = new DefaultTreeModel(root);
    model.addTreeModelListener(new TreeModelAdapter() {
        @Override//ww w  .  j a va  2  s .  com
        public void treeNodesInserted(TreeModelEvent e) {
            Object[] children = e.getChildren();
            if (children.length > 0) {
                Object child = children[children.length - 1];
                TreePath path = e.getTreePath().pathByAddingChild(child);
                tree.setSelectionPath(path);
            }
        }
    });
    tree = new JTree(model);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    // tree is expanded manually in OpenFileOrShowPopupMenuListener
    tree.setToggleClickCount(0);
    tree.setCellRenderer(new WorkspaceCellRenderer());
    tree.addMouseListener(new OpenFileOrShowPopupMenuListener());
    tree.addTreeSelectionListener(new CommandGuard());
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    JPanel hierarchyPanel = new JPanel();
    hierarchyPanel.add(createHierarchyButton(hierarchyXUsesYCommand));
    hierarchyPanel.add(createHierarchyButton(hierarchyXUsedByYCommand));
    hierarchyPanel.add(createHierarchyButton(hierarchyFlatCommand));
    JPanel borderHierarchyPanel = new JPanel(new BorderLayout());
    borderHierarchyPanel.add(hierarchyPanel, BorderLayout.WEST);

    JPanel control = new JPanel(new BorderLayout());
    control.add(borderHierarchyPanel, BorderLayout.NORTH);
    control.add(new JScrollPane(tree), BorderLayout.CENTER);

    return control;
}