Example usage for javax.swing.tree TreePath getLastPathComponent

List of usage examples for javax.swing.tree TreePath getLastPathComponent

Introduction

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

Prototype

public Object getLastPathComponent() 

Source Link

Document

Returns the last element of this path.

Usage

From source file:FileTreeFrame.java

public void valueForPathChanged(TreePath path, Object value) {
    File oldFile = (File) path.getLastPathComponent();
    String fileParentPath = oldFile.getParent();
    String newFileName = (String) value;
    File targetFile = new File(fileParentPath, newFileName);
    oldFile.renameTo(targetFile);//from   w w w . ja  v a 2  s . co  m
    File parent = new File(fileParentPath);
    int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) };
    Object[] changedChildren = { targetFile };
    fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);

}

From source file:it.unibas.spicygui.controllo.tree.ActionViewAllVirtualNode.java

public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < albero.getRowCount(); i++) {
        TreePath path = albero.getPathForRow(i);
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path.getLastPathComponent();
        TreeNodeAdapter adapter = (TreeNodeAdapter) treeNode.getUserObject();
        adapter.toggleFlagVirtual();/*  w w  w. ja  va  2  s. com*/
    }
    albero.updateUI();
}

From source file:MyData.java

public Main() {
    super();//from ww w.  j a v  a  2s  .  c  o  m
    JTree tree = new JTree(getRootNode()) {
        public boolean isPathEditable(TreePath path) {
            Object comp = path.getLastPathComponent();
            if (comp instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) comp;
                Object userObject = node.getUserObject();
                if (userObject instanceof Main) {
                    return true;
                }
            }
            return false;
        }
    };
    QuestionCellRenderer renderer = new QuestionCellRenderer();
    tree.setCellRenderer(renderer);
    QuestionCellEditor editor = new QuestionCellEditor();
    tree.setCellEditor(editor);
    tree.setEditable(true);
    JScrollPane jsp = new JScrollPane(tree);
    getContentPane().add(jsp);
}

From source file:it.unibas.spicygui.controllo.tree.ActionViewAllProvenance.java

public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < albero.getRowCount(); i++) {
        TreePath path = albero.getPathForRow(i);
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path.getLastPathComponent();
        TreeNodeAdapter adapter = (TreeNodeAdapter) treeNode.getUserObject();
        adapter.setProvenance(!visualizzati);
    }/*  ww w  .  j  a v a2  s  .  c om*/
    visualizzati = !visualizzati;
    albero.updateUI();
}

From source file:it.unibas.spicygui.controllo.tree.ActionViewAllSkolemFunctors.java

public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < albero.getRowCount(); i++) {
        TreePath path = albero.getPathForRow(i);
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path.getLastPathComponent();
        TreeNodeAdapter adapter = (TreeNodeAdapter) treeNode.getUserObject();
        adapter.setSkolem(!visualizzati);
    }/* w w w .  j av a  2 s  .  c  o  m*/
    visualizzati = !visualizzati;
    albero.updateUI();
}

From source file:it.unibas.spicygui.controllo.tree.ActionViewOID.java

public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < albero.getRowCount(); i++) {
        TreePath path = albero.getPathForRow(i);
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path.getLastPathComponent();
        TreeNodeAdapter adapter = (TreeNodeAdapter) treeNode.getUserObject();
        adapter.setOids(!visualizzati);/*from  ww w  .ja v a  2  s.c o m*/
    }
    visualizzati = !visualizzati;
    albero.updateUI();
}

From source file:Main.java

private Element getHyperlinkElement(MouseEvent event) {
    Point p = event.getPoint();/*from  w  w  w  . ja va  2 s  . c  om*/
    int selRow = tree.getRowForLocation(p.x, p.y);
    TreeCellRenderer r = tree.getCellRenderer();
    if (selRow == -1 || r == null) {
        return null;
    }
    TreePath path = tree.getPathForRow(selRow);
    Object lastPath = path.getLastPathComponent();
    Component rComponent = r.getTreeCellRendererComponent(tree, lastPath, tree.isRowSelected(selRow),
            tree.isExpanded(selRow), tree.getModel().isLeaf(lastPath), selRow, true);

    if (rComponent instanceof JEditorPane == false) {
        return null;
    }
    Rectangle pathBounds = tree.getPathBounds(path);
    JEditorPane editor = (JEditorPane) rComponent;
    editor.setBounds(tree.getRowBounds(selRow));
    p.translate(-pathBounds.x, -pathBounds.y);
    int pos = editor.getUI().viewToModel(editor, p);
    if (pos >= 0 && editor.getDocument() instanceof HTMLDocument) {
        HTMLDocument hdoc = (HTMLDocument) editor.getDocument();
        Element elem = hdoc.getCharacterElement(pos);
        if (elem.getAttributes().getAttribute(HTML.Tag.A) != null) {
            return elem;
        }
    }
    return null;
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/* ww w.  j  a  v a 2 s . c  om*/

    model = new DefaultTreeModel(top);
    tree = new JTree(model);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    JScrollPane treeView = new JScrollPane(tree);
    add(treeView);

    btnAdd.addActionListener(e -> {
        TreePath treePath = tree.getSelectionPath();
        if (treePath != null) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child " + (++childCount));
            model.insertNodeInto(child, node, node.getChildCount());
        }
    });
    add(btnAdd, BorderLayout.SOUTH);
}

From source file:Main.java

private void myPopupEvent(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    JTree tree = (JTree) e.getSource();
    TreePath path = tree.getPathForLocation(x, y);
    if (path == null)
        return;/*from   www. j  a v  a2  s .c om*/

    DefaultMutableTreeNode rightClickedNode = (DefaultMutableTreeNode) path.getLastPathComponent();

    TreePath[] selectionPaths = tree.getSelectionPaths();

    boolean isSelected = false;
    if (selectionPaths != null) {
        for (TreePath selectionPath : selectionPaths) {
            if (selectionPath.equals(path)) {
                isSelected = true;
            }
        }
    }
    if (!isSelected) {
        tree.setSelectionPath(path);
    }
    if (rightClickedNode.isLeaf()) {
        JPopupMenu popup = new JPopupMenu();
        final JMenuItem refreshMenuItem = new JMenuItem("refresh");
        refreshMenuItem.addActionListener(ev -> System.out.println("refresh!"));
        popup.add(refreshMenuItem);
        popup.show(tree, x, y);
    }
}

From source file:SimpleClient.java

public void valueChanged(TreeSelectionEvent e) {
    TreePath path = e.getNewLeadSelectionPath();
    if (path != null) {
        Object o = path.getLastPathComponent();
        if (o instanceof FolderTreeNode) {
            FolderTreeNode node = (FolderTreeNode) o;
            Folder folder = node.getFolder();

            try {
                if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
                    SimpleClient.fv.setFolder(folder);
                }//  w  w w.j a v  a 2s  . c  o m
            } catch (MessagingException me) {
            }
        }
    }
}