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:hr.fer.zemris.vhdllab.view.explorer.ProjectExplorerView.java

protected boolean isProject(DefaultMutableTreeNode node) {
    if (node == null) {
        return false;
    }/*from   w w  w.  j av a2  s  . c  o  m*/
    return node.getUserObject() instanceof Project;
}

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

protected boolean isFile(DefaultMutableTreeNode node) {
    if (node == null) {
        return false;
    }//from  ww w.  ja v a2s.  c  o  m
    return node.getUserObject() instanceof File;
}

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

@Override
public void projectDeleted(Project project) {
    for (int i = 0; i < root.getChildCount(); i++) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
        if (project.equals(node.getUserObject())) {
            root.remove(i);/*ww  w .j ava2 s .  co m*/
            model.nodesWereRemoved(root, new int[] { i }, new Object[] { node });
            break;
        }
    }
}

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

private DefaultMutableTreeNode getNodeFor(Project project) {
    for (int i = 0; i < root.getChildCount(); i++) {
        DefaultMutableTreeNode projectNode = (DefaultMutableTreeNode) root.getChildAt(i);
        if (project.equals(projectNode.getUserObject())) {
            return projectNode;
        }/*from w  ww.  j a v a 2  s  . com*/
    }
    return null;
}

From source file:com.openbravo.pos.admin.RolesViewTree.java

public DefaultMutableTreeNode searchNode(String nodeStr, DefaultMutableTreeNode tNode) {
    DefaultMutableTreeNode node = null;
    Enumeration e = tNode.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        node = (DefaultMutableTreeNode) e.nextElement();
        if (nodeStr.equals(node.getUserObject().toString())) {
            return node;
        }//from   w w  w . j  a  va  2 s  . c  om
    }
    return null;
}

From source file:it.isislab.dmason.tools.batch.BatchWizard.java

private static void createXML(String saveFile) {
    XStream xstream = new XStream(new DomDriver("UTF-8"));

    xstream.processAnnotations(Batch.class);
    xstream.processAnnotations(Param.class);
    xstream.processAnnotations(ParamRange.class);
    xstream.processAnnotations(ParamFixed.class);
    xstream.processAnnotations(ParamDistributionExponential.class);
    xstream.processAnnotations(ParamDistributionNormal.class);
    xstream.processAnnotations(ParamDistributionUniform.class);
    xstream.processAnnotations(ParamList.class);

    ArrayList<Param> paramSim = new ArrayList<Param>();
    for (int i = 0; i < simParams.getChildCount(); i++) {

        // if(top.getChildAt(i) instanceof ParamFixed)
        // {//from  w w w. j a v  a2s .  co  m
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) simParams.getChildAt(i);
        paramSim.add((Param) node.getUserObject());
        // }

    }

    ArrayList<Param> paramGen = new ArrayList<Param>();
    for (int i = 0; i < generalParams.getChildCount(); i++) {

        // if(top.getChildAt(i) instanceof ParamFixed)
        // {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) generalParams.getChildAt(i);
        paramGen.add((Param) node.getUserObject());
        // }

    }

    int numOfWorker = Integer.parseInt(textFieldNumberOfWorkers.getText());
    boolean balanced = checkBoxLoadBalancing.isSelected();
    Batch b = new Batch(paramSim, paramGen, simulationFile.getName(), numOfWorker, balanced);
    String xml = xstream.toXML(b);

    try {
        FileWriter fstream = new FileWriter(saveFile);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
        xstream.toXML(b, out);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // FileOutputStream fs = new
    // FileOutputStream("generated_simulation.xml");

    // xstream.toXML(b, fs);

}

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

private void updateHierarchy(DefaultMutableTreeNode parentNode, Hierarchy hierarchy,
        HierarchyNode hierarchyNode) {//w ww  .j  ava2  s .  c o m
    Map<Object, DefaultMutableTreeNode> map = new HashMap<Object, DefaultMutableTreeNode>(
            parentNode.getChildCount());
    for (int i = 0; i < parentNode.getChildCount(); i++) {
        DefaultMutableTreeNode c = (DefaultMutableTreeNode) parentNode.getChildAt(i);
        map.put(c.getUserObject(), c);
    }

    Iterator<HierarchyNode> iterator = getHierarchyIterator(hierarchy, hierarchyNode);
    while (iterator.hasNext()) {
        HierarchyNode next = iterator.next();
        File file = next.getFile();
        DefaultMutableTreeNode nextParentNode;
        if (map.containsKey(file)) {
            nextParentNode = map.remove(file);
        } else {
            nextParentNode = (DefaultMutableTreeNode) insertNode(parentNode, file);
        }
        updateHierarchy(nextParentNode, hierarchy, next);
    }

    // model.nodesWereRemoved expected ordered indices
    Map<Integer, DefaultMutableTreeNode> sortedMap = new TreeMap<Integer, DefaultMutableTreeNode>();
    for (DefaultMutableTreeNode n : map.values()) {
        int index = parentNode.getIndex(n);
        sortedMap.put(Integer.valueOf(index), n);
    }

    // construct arguments for model.nodesWereRemoved
    int[] childIndices = new int[sortedMap.size()];
    Object[] removedChildren = new Object[sortedMap.size()];
    int i = 0;
    for (Entry<Integer, DefaultMutableTreeNode> entry : sortedMap.entrySet()) {
        childIndices[i] = entry.getKey();
        removedChildren[i] = entry.getValue();
        i++;
        parentNode.remove(entry.getValue());
    }
    model.nodesWereRemoved(parentNode, childIndices, removedChildren);
}

From source file:com.pironet.tda.AbstractDumpParser.java

/**
 * create a category entry for a category (categories are "Monitors", "Threads waiting", e.g.). A ThreadInfo
 * instance will be created with the passed information.
 *
 * @param category  the category the node should be added to.
 * @param title     the title of the new node
 * @param info      the info part of the new node
 * @param content   the content part of the new node
 * @param lineCount the line count of the thread stack, 0 if not applicable for this element.
 * @see ThreadInfo//from   w  w  w.  j  av a 2  s  .  c om
 */
protected void addToCategory(DefaultMutableTreeNode category, String title, StringBuffer info, String content,
        int lineCount, boolean parseTokens) {
    DefaultMutableTreeNode threadInfo = new DefaultMutableTreeNode(
            new ThreadInfo(title, info != null ? info.toString() : null, content, lineCount,
                    parseTokens ? getThreadTokens(title) : null));
    ((Category) category.getUserObject()).addToCatNodes(threadInfo);
}

From source file:com.sec.ose.osi.ui.frm.main.identification.JTreeAllFiles.java

private TreePath getTreePathByFilePathString(DefaultMutableTreeNode rootNode, String selectedFilePath) {
    TreePath tp = null;/*from  w ww. j av  a  2  s.  com*/
    if (selectedFilePath != null) {
        DefaultMutableTreeNode node = null;
        Enumeration<?> enumer = rootNode.breadthFirstEnumeration();
        String sFilePath = "";
        DefaultTreeModel m_model = new DefaultTreeModel(rootNode);
        TreeNode[] nodes = null;
        while (enumer.hasMoreElements()) {
            node = (DefaultMutableTreeNode) enumer.nextElement();
            if (node.getUserObject().equals("/"))
                continue;
            sFilePath = ((FileNodeInfo) node.getUserObject()).getFilePath();
            if (sFilePath.equals(selectedFilePath)) {
                nodes = m_model.getPathToRoot(node);
                tp = new TreePath(nodes);
                break;
            }
        }
    }
    return tp;
}

From source file:net.sf.taverna.t2.workbench.views.results.workflow.RenderedResultComponent.java

/**
 * Update the component based on the node selected from the
 * ResultViewComponent tree./*  ww w  .  ja va 2  s  .  c  o  m*/
 */
public void updateResult() {
    if (recognisedRenderersForMimeType == null)
        recognisedRenderersForMimeType = new ArrayList<>();
    if (otherRenderers == null)
        otherRenderers = new ArrayList<>();

    // Enable the combo box
    renderersComboBox.setEnabled(true);

    /*
     * Update the 'save result' buttons appropriately as the result node had
     * changed
     */
    for (int i = 0; i < saveButtonsPanel.getComponents().length; i++) {
        JButton saveButton = (JButton) saveButtonsPanel.getComponent(i);
        SaveIndividualResultSPI action = (SaveIndividualResultSPI) saveButton.getAction();
        // Update the action with the new result reference
        action.setResultReference(path);
        saveButton.setEnabled(true);
    }

    if (DataBundles.isValue(path) || DataBundles.isReference(path)) {
        // Enable refresh button
        refreshButton.setEnabled(true);

        List<MimeType> mimeTypes = new ArrayList<>();
        try (InputStream inputstream = getInputStream(path)) {
            mimeTypes.addAll(getMimeTypes(inputstream));
        } catch (IOException e) {
            logger.warn("Error getting mimetype", e);
        }

        if (mimeTypes.isEmpty())
            // If MIME types is empty - add "plain/text" MIME type
            mimeTypes.add(new MimeType("text/plain"));
        else if (mimeTypes.size() == 1 && mimeTypes.get(0).toString().equals("chemical/x-fasta")) {
            /*
             * If MIME type is recognised as "chemical/x-fasta" only then
             * this might be an error from MIME magic (i.e., sometimes it
             * recognises stuff that is not "chemical/x-fasta" as
             * "chemical/x-fasta" and then Seq Vista renderer is used that
             * causes errors) - make sure we also add the renderers for
             * "text/plain" and "text/xml" as it is most probably just
             * normal xml text and push the "chemical/x-fasta" to the bottom
             * of the list.
             */
            mimeTypes.add(0, new MimeType("text/plain"));
            mimeTypes.add(1, new MimeType("text/xml"));
        }

        for (MimeType mimeType : mimeTypes) {
            List<Renderer> renderersList = rendererRegistry.getRenderersForMimeType(mimeType.toString());
            for (Renderer renderer : renderersList)
                if (!recognisedRenderersForMimeType.contains(renderer))
                    recognisedRenderersForMimeType.add(renderer);
        }
        // if there are no renderers then force text/plain
        if (recognisedRenderersForMimeType.isEmpty())
            recognisedRenderersForMimeType = rendererRegistry.getRenderersForMimeType("text/plain");

        /*
         * Add all other available renderers that are not recognised to be
         * able to handle the MIME type of the result
         */
        otherRenderers = new ArrayList<>(rendererRegistry.getRenderers());
        otherRenderers.removeAll(recognisedRenderersForMimeType);

        mimeList = new String[recognisedRenderersForMimeType.size() + otherRenderers.size()];
        rendererList = new ArrayList<>();

        /*
         * First add the ones that can handle the MIME type of the result
         * item
         */
        for (int i = 0; i < recognisedRenderersForMimeType.size(); i++) {
            mimeList[i] = recognisedRenderersForMimeType.get(i).getType();
            rendererList.add(recognisedRenderersForMimeType.get(i));
        }
        // Then add the other renderers just in case
        for (int i = 0; i < otherRenderers.size(); i++) {
            mimeList[recognisedRenderersForMimeType.size() + i] = otherRenderers.get(i).getType();
            rendererList.add(otherRenderers.get(i));
        }

        renderersComboBox.setModel(new DefaultComboBoxModel<String>(mimeList));

        if (mimeList.length > 0) {
            int index = 0;

            // Find the index of the current MIME type for this output port.
            for (int i = 0; i < mimeList.length; i++)
                if (mimeList[i].equals(lastUsedMIMEtype)) {
                    index = i;
                    break;
                }

            int previousindex = renderersComboBox.getSelectedIndex();
            renderersComboBox.setSelectedIndex(index);
            /*
             * force rendering as setSelectedIndex will not fire an
             * itemstatechanged event if previousindex == index and we still
             * need render the result as we may have switched from a
             * different result item in a result list but the renderer index
             * stayed the same
             */
            if (previousindex == index)
                renderResult(); // draw the rendered result component
        }

    } else if (DataBundles.isError(path)) {
        // Disable refresh button
        refreshButton.setEnabled(false);

        // Hide wrap text check box - only works for actual data
        wrapTextCheckBox.setVisible(false);

        // Reset the renderers as we have an error item
        recognisedRenderersForMimeType = null;
        otherRenderers = null;

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Error Trace");

        try {
            ErrorDocument errorDocument = DataBundles.getError(path);
            try {
                buildErrorDocumentTree(root, errorDocument);
            } catch (IOException e) {
                logger.warn("Error building error document tree", e);
            }
        } catch (IOException e) {
            logger.warn("Error getting the error document", e);
        }

        JTree errorTree = new JTree(root);
        errorTree.setCellRenderer(new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                    boolean expanded, boolean leaf, int row, boolean hasFocus) {
                Component renderer = null;
                if (value instanceof DefaultMutableTreeNode) {
                    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
                    Object userObject = treeNode.getUserObject();
                    if (userObject instanceof ErrorDocument)
                        renderer = renderErrorDocument(tree, selected, expanded, leaf, row, hasFocus,
                                (ErrorDocument) userObject);
                }
                if (renderer == null)
                    renderer = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row,
                            hasFocus);
                if (renderer instanceof JLabel) {
                    JLabel label = (JLabel) renderer;
                    label.setIcon(null);
                }
                return renderer;
            }

            private Component renderErrorDocument(JTree tree, boolean selected, boolean expanded, boolean leaf,
                    int row, boolean hasFocus, ErrorDocument errorDocument) {
                return super.getTreeCellRendererComponent(tree,
                        "<html>" + escapeHtml(errorDocument.getMessage()) + "</html>", selected, expanded, leaf,
                        row, hasFocus);
            }
        });

        renderersComboBox.setModel(new DefaultComboBoxModel<>(new String[] { ERROR_DOCUMENT }));
        renderedResultPanel.removeAll();
        renderedResultPanel.add(errorTree, CENTER);
        repaint();
    }
}