Example usage for javax.swing.tree DefaultMutableTreeNode getUserObject

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

Introduction

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

Prototype

public Object getUserObject() 

Source Link

Document

Returns this node's user object.

Usage

From source file:com.autentia.wuija.widget.TreeComponent.java

private void expand(DefaultMutableTreeNode node, boolean expand) {
    if (!node.isLeaf()) {
        ((IceUserObject) node.getUserObject()).setExpanded(expand);
        final Enumeration nodes = node.children();
        while (nodes.hasMoreElements()) {
            final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) nodes.nextElement();
            expand(childNode, expand);//ww  w  .j  av a2s . co  m
        }
    }
}

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

public void actionPerformed(ActionEvent e) {
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) albero.getLastSelectedPathComponent();
    TreeNodeAdapter adapter = (TreeNodeAdapter) treeNode.getUserObject();
    INode iNode = adapter.getINode();//  w ww  . j  a v  a  2 s.co  m
    PathExpression pathExpression = pathGenerator.generatePathFromRoot(iNode);
    if (iNode.isExcluded()) {
        if (!dataSource.getExclusions().contains(pathExpression)) {
            DialogDisplayer.getDefault()
                    .notify(new NotifyDescriptor.Message(
                            NbBundle.getMessage(Costanti.class, Costanti.INCLUDE_NODES_WARNING),
                            DialogDescriptor.INFORMATION_MESSAGE));
            StatusDisplayer.getDefault()
                    .setStatusText(NbBundle.getMessage(Costanti.class, Costanti.INCLUDE_NODES_WARNING));
            return;
        }
        includeNodes.include(iNode);
        dataSource.getExclusions().remove(pathExpression);
    } else {
        if (!chiediConferma()) {
            return;
        }
        excludeNodes.exclude(iNode);
        if (dataSource.getInclusions().isEmpty()) {
            dataSource.addInclusion(pathGenerator.generatePathFromRoot(dataSource.getIntermediateSchema()));
        }
        dataSource.addExclusion(pathExpression);
    }
    albero.updateUI();
}

From source file:CheckBoxNodeTreeSample.java

public boolean isCellEditable(EventObject event) {
    boolean returnValue = false;
    if (event instanceof MouseEvent) {
        MouseEvent mouseEvent = (MouseEvent) event;
        TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
        if (path != null) {
            Object node = path.getLastPathComponent();
            if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
                DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                Object userObject = treeNode.getUserObject();
                returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
            }/*from  w  w  w. ja v  a  2 s.  c o  m*/
        }
    }
    return returnValue;
}

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

public void actionPerformed(ActionEvent e) {
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) albero.getLastSelectedPathComponent();
    TreeTopComponentAdapter adapter = (TreeTopComponentAdapter) treeNode.getUserObject();
    TopComponent tc = adapter.getTopComponent();
    try {/*  w ww. j av  a 2 s . c o m*/
        if (tc.isOpened()) {
            tc.requestVisible();
            return;
        }

        if (tc instanceof InstancesTopComponent) {
            Lookups.forPath("Azione").lookup(ActionViewInstances.class)
                    .myPerformAction((InstancesTopComponent) tc);
        } else if (tc instanceof TransformationTopComponent) {
            Lookups.forPath("Azione").lookup(ActionViewTransformation.class)
                    .myPerformAction(((TransformationTopComponent) tc).getScenario());
        } else if (tc instanceof XQueryTopComponent) {
            Lookups.forPath("Azione").lookup(ActionGenerateXQuery.class)
                    .myPerformAction(((XQueryTopComponent) tc).getScenario());
        } else if (tc instanceof SqlTopComponent) {
            Lookups.forPath("Azione").lookup(ActionGenerateSql.class)
                    .myPerformAction(((SqlTopComponent) tc).getScenario());
        } else if (tc instanceof TGDListTopComponent) {
            Lookups.forPath("Azione").lookup(ActionViewTGDs.class)
                    .myPerformAction(((TGDListTopComponent) tc).getScenario());
        } else if (tc instanceof TGDCorrespondencesTopComponent) {
            Lookups.forPath("Azione").lookup(ActionViewTGD.class)
                    .myPerformAction(((TGDCorrespondencesTopComponent) tc).getScenario());
        } else {
            tc.open();
            tc.requestVisible();
        }
    } catch (IllegalMappingTaskException exception) {
        DialogDisplayer.getDefault()
                .notify(new NotifyDescriptor.Message(exception.getMessage(), DialogDescriptor.ERROR_MESSAGE));
    }
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TreeNode root = getNodes();/*w  ww  . j a  v a2s. c  om*/
    DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);
    tree.setRootVisible(false);

    JButton add = new JButton("add new");
    add.addActionListener(e -> {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selectedNode == null) {
            return;
        }
        MyObject obj = (MyObject) selectedNode.getUserObject();
        MyObject newChild = new MyObject(obj.name + "-" + (obj.childs.size() + 1));
        obj.childs.add(newChild);
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newChild);
        model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
        TreeNode[] nodes = model.getPathToRoot(newNode);
        TreePath path = new TreePath(nodes);
        tree.scrollPathToVisible(path);
    });

    JButton print = new JButton("print childs");
    print.addActionListener(e -> {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selectedNode == null) {
            return;
        }
        MyObject obj = (MyObject) selectedNode.getUserObject();
        System.out.println(obj.childs);
    });
    JPanel btns = new JPanel();
    btns.add(add);
    btns.add(print);

    add(new JScrollPane(tree));
    add(btns, BorderLayout.SOUTH);
    pack();
    setVisible(true);
}

From source file:ClassTree.java

/**
 * Finds an object in the tree.//from ww w . ja  v  a2s  .  c om
 * @param obj the object to find
 * @return the node containing the object or null if the object is not present in the tree
 */
@SuppressWarnings("unchecked")
public DefaultMutableTreeNode findUserObject(Object obj) {
    // find the node containing a user object
    Enumeration<TreeNode> e = (Enumeration<TreeNode>) root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        if (node.getUserObject().equals(obj))
            return node;
    }
    return null;
}

From source file:ClassTree.java

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    // get the user object
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
    Class<?> c = (Class<?>) node.getUserObject();

    // the first time, derive italic font from plain font
    if (plainFont == null) {
        plainFont = getFont();// w  ww  .ja  v a2 s .c  o  m
        // the tree cell renderer is sometimes called with a label that has a null font
        if (plainFont != null)
            italicFont = plainFont.deriveFont(Font.ITALIC);
    }

    // set font to italic if the class is abstract, plain otherwise
    if ((c.getModifiers() & Modifier.ABSTRACT) == 0)
        setFont(plainFont);
    else
        setFont(italicFont);
    return this;
}

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

private INode getNodeSelected() {
    TreePath treePath = jTree.getSelectionPath();
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
    TreeNodeAdapter adapter = (TreeNodeAdapter) treeNode.getUserObject();
    return adapter.getINode();
}

From source file:edu.clemson.cs.nestbed.client.gui.ProgramTreeTransferHandler.java

protected Transferable createTransferable(JComponent c) {
    log.debug("createTransferable() called.");

    Transferable transferable = null;

    if (c instanceof JTree) {
        source = (JTree) c;//  w w  w .ja  va2  s  .com

        DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) source.getSelectionPath().getLastPathComponent();
        Object o = dmtn.getUserObject();

        if (o instanceof Program) {
            log.debug("Transfering a Program");

            Program program = (Program) o;
            transferable = new ProgramTransferable(program);
        } else if (o instanceof ProgramSymbol) {
            log.debug("Transfering a ProgramSymbol");

            ProgramSymbol programSymbol = (ProgramSymbol) o;
            transferable = new ProgramSymbolTransferable(programSymbol);
        } else if (o instanceof ProgramMessageSymbol) {
            log.debug("Transfering a ProgramMessageSymbol");

            ProgramMessageSymbol programMessageSymbol = (ProgramMessageSymbol) o;

            transferable = new ProgramMessageSymbolTransferable(programMessageSymbol);
        } else {
            log.error("Selected value is not a Program");
        }
    }

    return transferable;
}

From source file:TreeUtil.java

public void addChildNode(DefaultMutableTreeNode parent, String name) {
    String realName = parent.getUserObject().toString() + "." + name;
    addNode(realName);/*from ww w  .  j  a va2 s .c  o m*/
}