Java Code Examples for org.openide.nodes.Node.getParentNode()

The following are Jave code examples for showing how to use getParentNode() of the org.openide.nodes.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: OptionsAction.java   Source Code and License Vote up 6 votes
public HelpCtx getHelpCtx () {
    HelpCtx defaultHelp = new HelpCtx (HELP_ID);
    HelpCtx help = org.openide.explorer.ExplorerUtils.getHelpCtx (
        getExplorerManager ().getSelectedNodes (),
        defaultHelp
    );
    // bugfix #23551, add help id to subnodes of Templates category
    // this check prevents mixed help ids on more selected nodes
    if (!defaultHelp.equals (help)) {
        // try if selected node isn't template
        Node node = getExplorerManager ().getSelectedNodes ()[0];
        HelpCtx readHelpId = getHelpId (node);
        if (readHelpId != null) return readHelpId;
        
        // next bugfix #23551, children have same helpId as parent if no specific is declared
        while (node != null && !TEMPLATES_DISPLAY_NAME.equals (node.getDisplayName ())) {
            readHelpId = getHelpId (node);
            if (readHelpId != null) return readHelpId;
            node = node.getParentNode ();
        }
        if (node != null && TEMPLATES_DISPLAY_NAME.equals (node.getDisplayName ())) {
            return new HelpCtx ("org.netbeans.core.actions.OptionsAction$TemplatesSubnode"); // NOI18N
        }
    }
    return help;
}
 
Example 2
Project: incubator-netbeans   File: ProjectsRootNodePhysicalViewTest.java   Source Code and License Vote up 6 votes
@RandomlyFails // NB-Core-Build #3939: "Can be garbage collected when closed" involving TimedWeakReference
public void testBehaviourOfProjectsLogicNode() throws InterruptedException {
    Node n = doBehaviourOfProjectsNode();
    
    Project p = n.getLookup().lookup(Project.class);
    assertNotNull("Project is in the node", p);
    
    WeakReference<Project> ref = new WeakReference<Project>(p);
    p = null;
    
    // keep just parent
    n = n.getParentNode();
    
    try {
        assertGC("Cannot be garbage collected while open", ref);
        throw new IllegalStateException("Cannot be GCed");
    } catch (AssertionFailedError ok) {
        // ok
    }
    
    OpenProjectList.getDefault().close(new Project[] { ref.get() }, true);
    
    assertGC("Can be garbage collected when closed", ref);
}
 
Example 3
Project: incubator-netbeans   File: TreeModelRoot.java   Source Code and License Vote up 6 votes
private Object initExpandCollapseNotify(TreeExpansionEvent event) {
    Node node = Visualizer.findNode(event.getPath ().getLastPathComponent());
    Object obj = node.getLookup().lookup(Object.class);
    Object actOn;
    node = node.getParentNode();
    if (node == null) {
        actOn = new Integer(0);
    } else {
        Children ch = node.getChildren();
        if (ch instanceof TreeModelNode.TreeModelChildren) {
            actOn = ((TreeModelNode.TreeModelChildren) ch).getTreeDepth();
        } else {
            actOn = ch;
        }
    }
    Models.CompoundModel model = getModel();
    if (model != null) {
        DefaultTreeExpansionManager.get(model).setChildrenToActOn(actOn);
    }
    return obj;
}
 
Example 4
Project: incubator-netbeans   File: UICommonUtils.java   Source Code and License Vote up 6 votes
private static boolean hasParentAmongNodes(final Node[] nodes,
                                           final int idx) {
    Node node;

    node = nodes[idx].getParentNode();
    while (null != node) {
        for (int i = 0; i < nodes.length; i++) {
            if (i == idx) {
                continue;
            }
            if (node == nodes[i]) {
                return true;
            }
        }
        node = node.getParentNode();
    }
    return false;
}
 
Example 5
Project: incubator-netbeans   File: OutlineTable.java   Source Code and License Vote up 6 votes
/** Converts path of strings to TreePath if exists null otherwise
 */
private TreePath stringPath2TreePath (String[] sp) {
    ExplorerManager em = ExplorerManager.find (this);
    try {
        Node n = NodeOp.findPath (em.getRootContext (), sp); 
        
        // Create the tree path
        TreeNode tns[] = new TreeNode [sp.length + 1];
        
        for (int i = sp.length; i >= 0; i--) {
            tns[i] = Visualizer.findVisualizer (n);
            n = n.getParentNode ();
        }                
        return new TreePath (tns);
    } catch (NodeNotFoundException e) {
        return null;
    }
}
 
Example 6
Project: incubator-netbeans   File: JUnitTestCreatorProvider.java   Source Code and License Vote up 6 votes
private static boolean hasParentAmongNodes(final Node[] nodes,
                                           final int idx) {
    Node node;

    node = nodes[idx].getParentNode();
    while (null != node) {
        for (int i = 0; i < nodes.length; i++) {
            if (i == idx) {
                continue;
            }
            if (node == nodes[i]) {
                return true;
            }
        }
        node = node.getParentNode();
    }
    return false;
}
 
Example 7
Project: incubator-netbeans   File: FileTreeView.java   Source Code and License Vote up 5 votes
private static Node[] getChildrenInDisplayedOrder(Node parent,
        OutlineView outlineView) {

    Outline outline = outlineView.getOutline();
    Node[] unsortedChildren = parent.getChildren().getNodes(true);
    int rows = outlineView.getOutline().getRowCount();
    int start = findRowIndexInOutline(parent, outline, rows);
    if (start == -1 && parent != ExplorerManager.find(outlineView).getRootContext()) {
        return unsortedChildren;
    }
    List<Node> children = new LinkedList<Node>();
    for (int j = start + 1; j < rows; j++) {
        int childModelIndex = outline.convertRowIndexToModel(j);
        if (childModelIndex == -1) {
            continue;
        }
        Object childObject = outline.getModel().getValueAt(
                childModelIndex, 0);
        Node childNode = Visualizer.findNode(childObject);
        if (childNode.getParentNode() == parent) {
            children.add(childNode);
        } else if (children.size() == unsortedChildren.length) {
            break;
        }
    }
    return children.toArray(new Node[children.size()]);
}
 
Example 8
Project: incubator-netbeans   File: ContextView.java   Source Code and License Vote up 5 votes
private void updateForSelection() {
    Node[] nodes = explorerManager.getSelectedNodes();
    if (nodes.length == 0) {
        displayNoFileSelected();
    } else if (nodes.length == 1) {
        Node n = nodes[0];
        MatchingObject mo = n.getLookup().lookup(MatchingObject.class);
        if (mo != null) {
            displayFile(mo, -1);
        } else {
            Node parent = n.getParentNode();
            TextDetail td = n.getLookup().lookup(TextDetail.class);
            if (td != null && parent != null) {
                mo = parent.getLookup().lookup(
                        MatchingObject.class);
                if (mo != null) {
                    // TODO pass TextDetail directly
                    int index = -1;
                    for (int i = 0; i < mo.getTextDetails().size(); i++) {
                        if (mo.getTextDetails().get(i) == td) {
                            index = i;
                            break;
                        }
                    }
                    displayFile(mo, index);
                }
            } else {
                displayNoFileSelected();
            }
        }
    } else {
        displayMultipleItemsSelected();
    }
}
 
Example 9
Project: incubator-netbeans   File: DefaultSearchResultsDisplayer.java   Source Code and License Vote up 5 votes
@Override
public boolean isRelevantNode(Node node) {
    if (node == null) {
        return false;
    } else {
        Node parent = node.getParentNode();
        return node.isLeaf() && parent != null
                && parent.getParentNode() != null;
    }
}
 
Example 10
Project: incubator-netbeans   File: UIJavaUtils.java   Source Code and License Vote up 5 votes
/**
 */
public static TestMethodNode getTestMethodNode(Node node) {
    while (!(node instanceof TestMethodNode)) {
        node = node.getParentNode();
    }
    return (TestMethodNode) node;
}
 
Example 11
Project: incubator-netbeans   File: Nodes.java   Source Code and License Vote up 5 votes
private static boolean isParent(Node parent, Node child) {
    if (NodeOp.isSon(parent, child)) {
        return true;
    }

    Node p = child.getParentNode();

    if (p == null) {
        return false;
    }

    return isParent(parent, p);
}
 
Example 12
Project: incubator-netbeans   File: ListView.java   Source Code and License Vote up 5 votes
public void valueChanged(ListSelectionEvent e) {
    int curSize = model.getSize();
    int[] indices = list.getSelectedIndices();

    // bugfix #24193, check if the nodes in selection are in the view's root context
    List<Node> ll = new ArrayList<Node>(indices.length);

    for (int i = 0; i < indices.length; i++) {
        if (indices[i] < curSize) {
            Node n = Visualizer.findNode(model.getElementAt(indices[i]));

            if ((n == manager.getRootContext()) || (n.getParentNode() != null)) {
                ll.add(n);
            }
        } else {
            // something went wrong?
            updateSelection();

            return;
        }
    }

    Node[] nodes = ll.toArray(new Node[ll.size()]);

    // forwarding TO E.M., so we won't listen to its cries for a while
    manager.removePropertyChangeListener(wlpc);
    manager.removeVetoableChangeListener(wlvc);

    try {
        selectionChanged(nodes, manager);
    } catch (PropertyVetoException ex) {
        // selection vetoed - restore previous selection
        updateSelection();
    } finally {
        manager.addPropertyChangeListener(wlpc);
        manager.addVetoableChangeListener(wlvc);
    }
}
 
Example 13
Project: incubator-netbeans   File: DebugTreeView.java   Source Code and License Vote up 5 votes
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    Color background = null;
    if (value instanceof TreeNode) {
        try {
            java.lang.reflect.Field fnode = value.getClass().getDeclaredField("node");
            fnode.setAccessible(true);
            value = fnode.get(value);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    if (value instanceof Node) {
        Node node = (Node) value;
        DVThread thread;
        do {
            thread = node.getLookup().lookup(DVThread.class);
            if (thread == null) {
                node = node.getParentNode();
            }
        } while (thread == null && node != null);
        if (thread != null) {
            DVThread currentThread = getCurrentThread();
            boolean isHighlighted = focusedThread != null && thread == focusedThread && node == value;
            boolean isCurrent = currentThread == thread;
            if (isHighlighted || isCurrent) {
                background = isHighlighted ? highlightColor : currentThreadColor;
            }
        }
    }
    Component component = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    if (background != null) {
        component.setBackground(background);
        if (component instanceof JComponent) {
            ((JComponent) component).setOpaque(true);
        }
    }
    return component;
}
 
Example 14
Project: incubator-netbeans   File: FileObjectSearchGroup.java   Source Code and License Vote up 5 votes
/** Tests if the node has parent. Helper method. */
private static boolean hasParent(Node node, Node[] nodes) {
    for (Node parent = node.getParentNode(); parent != null; parent = parent.getParentNode()) {
        for (Node n : nodes) {
            if (n.equals(parent)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 15
Project: incubator-netbeans   File: TreeView.java   Source Code and License Vote up 5 votes
/** Checks whether given Node is a subnode of rootContext.
* @return true if specified Node is under current rootContext
*/
private boolean isUnderRoot(Node rootContext, Node node) {
    while (node != null) {
        if (node.equals(rootContext)) {
            return true;
        }

        node = node.getParentNode();
    }

    return false;
}
 
Example 16
Project: incubator-netbeans   File: TreeModelNode.java   Source Code and License Vote up 5 votes
private Integer getTreeDepth() {
    Node p = getParentNode();
    if (p == null) {
        return 0;
    } else if (depth != null) {
        return depth;
    } else {
        int d = 1;
        while ((p = p.getParentNode()) != null) d++;
        depth = new Integer(d);
        return depth;
    }
}
 
Example 17
Project: incubator-netbeans   File: TemplatesPanel.java   Source Code and License Vote up 5 votes
static DataObject createDuplicateFromNode (Node n) {
    DataObject source = getDOFromNode (n);
    try {
        Node parent = n.getParentNode ();
        DataObject target = source.copy(source.getFolder());
        FileObject srcFo = source.getPrimaryFile();
        FileObject targetFo = target.getPrimaryFile();
        setTemplateAttributes(targetFo, srcFo);
        if (parent != null) {
            Node duplicateNode = null;
            for (Node k : parent.getChildren ().getNodes (true)) {
                if (k.getName ().startsWith (targetFo.getName ())) {
                    duplicateNode = k;
                    break;
                }
            }
            if (duplicateNode != null) {
                final Node finalNode = duplicateNode;
                SwingUtilities.invokeLater (new Runnable () {
                    @Override public void run() {
                        try {
                            manager.setSelectedNodes (new Node [] { finalNode });
                            view.invokeInplaceEditing ();
                        } catch (PropertyVetoException ex) {
                            Logger.getLogger (TemplatesPanel.class.getName ()).log (Level.INFO, ex.getLocalizedMessage (), ex);
                        }
                    }
                });
            }
        }
        return target;
    } catch (IOException ioe) {
        Logger.getLogger(TemplatesPanel.class.getName()).log(Level.WARNING, null, ioe);
    }
    return null;
}
 
Example 18
Project: incubator-netbeans   File: Browser.java   Source Code and License Vote up 5 votes
private int getNodeLevel(Node node) {
    int level = 0;
    while(node!=null) {
        node = node.getParentNode();
        level++;
    }
    return level;
}
 
Example 19
Project: incubator-netbeans   File: TreeTableView.java   Source Code and License Vote up 4 votes
@Override
protected NodeListener createNodeListener() {
    return new FilterNode.NodeAdapter(this) {
        @Override
        protected void propertyChange(FilterNode fn, PropertyChangeEvent ev) {
            super.propertyChange(fn, ev);
            if (ev.getPropertyName().equals(Node.PROP_LEAF)) {
                final org.openide.nodes.Children[] newChildren = new org.openide.nodes.Children[1];
                Children.MUTEX.readAccess(new Runnable() {

                    @Override
                    public void run() {
                        boolean origIsLeaf = getOriginal().isLeaf();
                        boolean thisIsLeaf = isLeaf();
                        if (origIsLeaf && !thisIsLeaf) {
                            newChildren[0] = Children.LEAF;
                        } else if (!origIsLeaf && thisIsLeaf) {
                            newChildren[0] = new SortedChildren(getOriginal());
                        }
                    }
                });

                if (newChildren[0] != null) {
                    Children.MUTEX.postWriteRequest(
                            new Runnable() {

                                @Override
                                public void run() {
                                    setChildren(newChildren[0]);
                                }
                            }
                    );
                }
            }
            if (ev.getPropertyName().equals(Node.PROP_PARENT_NODE)) {
                final Node node = (Node)ev.getSource();
                if (node.getParentNode() == null) {
                    original2filter.remove(node);
                }
            }
        }
    };
}
 
Example 20
Project: incubator-netbeans   File: ProjectTab.java   Source Code and License Vote up 4 votes
private void performChange(Node [] selectedNodes) {
    String text = "";
    Node projectNode = null;
    if( selectedNodes != null && selectedNodes.length > 0 ) {
        Node selectedNode = selectedNodes[0];
        Node originallySelectedNode = selectedNodes[0];
        Node rootNode = ProjectTab.this.manager.getRootContext();
        while ( selectedNode.getParentNode() != null && !selectedNode.getParentNode().equals(rootNode)) {
            selectedNode = selectedNode.getParentNode();
        }
        projectNode = selectedNode;
        //Tests whether other selected items have same project owner
        if( selectedNodes.length > 1 ) {
            for ( int i = 1; i < selectedNodes.length; i ++) {
                selectedNode = selectedNodes[i];                        
                while ( !selectedNode.getParentNode().equals(rootNode) ) {
                    selectedNode = selectedNode.getParentNode();
                }
                if ( !projectNode.equals(selectedNode) ) {
                    projectNode = null;
                    text = Bundle.MSG_nodes_from_more_projects();
                    break;
                }
            }
        }
        if ( projectNode != null ) {
            ProjectTab.this.btv.showOrHideNodeSelectionProjectPanel(projectNode, originallySelectedNode);
            text = projectNode.getDisplayName();
        }
    } else {
        text = Bundle.MSG_none_node_selected();
    }
    if ( this.actualProjectLabel != null ) {
        this.actualProjectLabel.setText(text);
        setSelectionLabelProperties(projectNode);
    } else {
        this.actualProjectLabel = new JLabel(text);
        setSelectionLabelProperties(projectNode);
        this.selectionsProjectPanel.add(actualProjectLabel);
    }
}