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.wwidesigner.gui.StudyView.java

@Override
protected void initializeComponents() {
    // create file tree
    tree = new JTree() {
        @Override//  w  w w .  j  a  va  2s .c  om
        public String getToolTipText(MouseEvent e) {
            String tip = null;
            TreePath path = tree.getPathForLocation(e.getX(), e.getY());
            if (path != null) {
                if (path.getPathCount() == 3) // it is a leaf
                {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                    if (node instanceof TreeNodeWithToolTips) {
                        tip = ((TreeNodeWithToolTips) node).getToolTip();
                    }
                }
            }
            return tip == null ? getToolTipText() : tip;
        }
    };
    // Show tooltips for the Study view, and let them persist for 8 seconds.
    ToolTipManager.sharedInstance().registerComponent(tree);
    ToolTipManager.sharedInstance().setDismissDelay(8000);
    // If a Study view node doesn't fit in the pane, expand it when hovering
    // over it.
    ExpandedTipUtils.install(tree);
    tree.setRootVisible(false);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    tree.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            TreePath path = tree.getPathForLocation(e.getX(), e.getY());
            if (path != null) {
                if (path.getPathCount() == 3) // it is a leaf
                {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();
                    String category = (String) parentNode.getUserObject();
                    study.setCategorySelection(category, (String) node.getUserObject());
                    if (StudyModel.INSTRUMENT_CATEGORY_ID.equals(category)
                            || StudyModel.TUNING_CATEGORY_ID.equals(category)) {
                        try {
                            study.validHoleCount();
                        } catch (Exception ex) {
                            showException(ex);
                        }
                    }
                }
                updateView();
            }
        }
    });
    JScrollPane scrollPane = new JScrollPane(tree);
    scrollPane.setPreferredSize(new Dimension(225, 100));
    add(scrollPane);

    Preferences myPreferences = getApplication().getPreferences();
    String modelName = myPreferences.get(OptimizationPreferences.STUDY_MODEL_OPT,
            OptimizationPreferences.NAF_STUDY_NAME);
    setStudyModel(modelName);
    study.setPreferences(myPreferences);

    getApplication().getEventManager().subscribe(WIDesigner.FILE_OPENED_EVENT_ID, this);
    getApplication().getEventManager().subscribe(WIDesigner.FILE_CLOSED_EVENT_ID, this);
    getApplication().getEventManager().subscribe(WIDesigner.FILE_SAVED_EVENT_ID, this);
    getApplication().getEventManager().subscribe(WIDesigner.WINDOW_RENAMED_EVENT_ID, this);
}

From source file:org.netxilia.server.rest.HomeResource.java

/**
 * walk the tree and dump the node to the tree
 * /*ww w  .  j  a  v a 2 s.  c o  m*/
 * @param node
 * @param treeview
 */
@SuppressWarnings("unchecked")
private void buildTreeView(DefaultMutableTreeNode node, StringBuilder treeview) {
    // <ul id="example" class="filetree">
    // <li><span class="folder">Folder 1</span>
    // <ul>
    // <li><span class="file">Item 1.1</span></li>
    // </ul>
    // </li>
    // <li><span class="folder">Folder 2</span>

    TreeViewData viewData = (TreeViewData) node.getUserObject();
    if (node.isLeaf()) {
        if (viewData != null) {
            treeview.append("<li><span id='").append(viewData.getId()).append("' class='")
                    .append(viewData.getCssClass()).append("'>").append(viewData.getName())
                    .append("</span></li>");
        }
    } else {
        if (viewData != null) {
            if (viewData.isCollapsed()) {
                treeview.append("<li class='closed'>");
            } else {
                treeview.append("<li>");
            }
            treeview.append("<span class='folder ").append(viewData.getCssClass()).append("'>")
                    .append(viewData.getName()).append("</span><ul>");
        }
        Enumeration<DefaultMutableTreeNode> childrenEnum = node.children();
        while (childrenEnum.hasMoreElements()) {
            buildTreeView(childrenEnum.nextElement(), treeview);
        }
        if (viewData != null) {
            treeview.append("</ul></li>");
        }
    }
}

From source file:dotaSoundEditor.Controls.ItemPanel.java

private void currentTreeSelectionChanged(TreeSelectionEvent evt) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) currentTree.getLastSelectedPathComponent();
    if (node == null) {
        return;/*  ww w .j  ava 2  s . co m*/
    }
    //get highest parent of selected node
    while (node.getParent() != null && !node.getParent().equals(node.getRoot())) {
        node = (DefaultMutableTreeNode) node.getParent();
    }

    NamedItem clickedItem = new NamedItem(node.getUserObject().toString(), this.getCurrentScriptString());
    fillImageFrame(clickedItem);
    itemLabel.setText("Item: " + clickedItem.getFriendlyName());

}

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

protected Project getSelectedProject() {
    DefaultMutableTreeNode node = getLastSelectedNode();
    return (Project) node.getUserObject();
}

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

protected File getSelectedFile() {
    DefaultMutableTreeNode node = getLastSelectedNode();
    File hierarchyFile = (File) node.getUserObject();
    File loadedFile = mapper.getFile(hierarchyFile);
    loadedFile.setProject(getProjectForSelectedFile());
    return loadedFile;
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

private TreeSelectionListener getTreeSelectionListener(final TreeNode root) {
    return new TreeSelectionListener() {
        @Override// w  ww  .ja va  2 s  . co  m
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();

            if (selectedNode == null) {

            } else if (selectedNode.isLeaf() && root.getChildCount() > 0) {
                final CodeInfo codeInfo = (CodeInfo) selectedNode.getUserObject();
                final Document windowEditorDocument = windowEditor.getDocument();

                writeToDocument(codeInfo, windowEditorDocument);

                final List<Integer> linesForFolding = codeInfo.getLineNumbers();
                linesForFolding.add(windowEditorDocument.getLineCount() + 1);
                java.util.Collections.sort(linesForFolding);
                addFoldings(windowEditorDocument, linesForFolding);
            }
        }
    };
}

From source file:com.yosanai.java.aws.console.panel.InstancesPanel.java

private void trInstancesValueChanged(javax.swing.event.TreeSelectionEvent evt) {// GEN-FIRST:event_trInstancesValueChanged
    Object nodeObj = evt.getPath().getLastPathComponent();
    if (null != nodeObj) {
        if (nodeObj instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) nodeObj;
            Object userObject = treeNode.getUserObject();
            if (userObject instanceof InstanceObjectWrapper) {
                InstanceObjectWrapper wrapper = (InstanceObjectWrapper) userObject;
                instanceTableModel.setObject(wrapper.instance);
                tblInstances.setModel(instanceTableModel);
            } else {
                tblInstances.setModel(instancesTableModel);
            }/*from   w ww . ja  v  a2  s  . c  o  m*/

        }
    }
}

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

private Project getProjectForSelectedFile() {
    DefaultMutableTreeNode node = getLastSelectedNode();
    TreeNode[] path = node.getPath();
    DefaultMutableTreeNode projectNode = (DefaultMutableTreeNode) path[1];
    return (Project) projectNode.getUserObject();
}

From source file:edu.ucla.stat.SOCR.chart.ChartTree.java

/**
 * Receives notification of tree selection events and updates the demo 
 * display accordingly./*from w w  w .j  a  va 2  s.co m*/
 * 
 * @param event  the event.
 */
public void valueChanged(TreeSelectionEvent event) {
    TreePath path = event.getPath();
    Object obj = path.getLastPathComponent();
    if (obj != null) {
        DefaultMutableTreeNode n = (DefaultMutableTreeNode) obj;
        Object userObj = n.getUserObject();
        if (userObj instanceof DemoDescription) {
            //                DemoDescription dd = (DemoDescription) userObj;
            //SwingUtilities.invokeLater(new DisplayDemo(this, dd));
        } else {
            /* this.chartContainer.removeAll();
                this.chartContainer.add(createNoDemoSelectedPanel());
                this.displayPanel.validate();
                displayDescription("select.html");*/
        }
    }
    System.out.println(obj);
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java

public void addField() {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) availableFieldsComp.getTree()
            .getLastSelectedPathComponent();
    if (node == null || !node.isLeaf() || !(node.getUserObject() instanceof DataObjDataFieldWrapper)) {
        return; // not really a field that can be added, just empty or a string
    }/*from  w w w.j a va  2s .c o  m*/

    Object obj = node.getUserObject();
    if (obj instanceof DataObjDataFieldWrapper) {
        DataObjDataFieldWrapper wrapper = (DataObjDataFieldWrapper) obj;
        String sep = sepText.getText();
        if (StringUtils.isNotEmpty(sep)) {
            try {
                DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument();
                if (doc.getLength() > 0) {
                    doc.insertString(doc.getLength(), sep, null);
                }
            } catch (BadLocationException ble) {
            }

        }
        insertFieldIntoTextEditor(wrapper);
        setHasChanged(true);
    }
}