org.openide.nodes.NodeListener Java Examples

The following examples show how to use org.openide.nodes.NodeListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FormEditorSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void registerNodeListener() {
    if (formDataObject.isValid()) {
        Node node = formDataObject.getNodeDelegate();
        multiviewTC.setIcon(node.getIcon(BeanInfo.ICON_COLOR_16x16));
        if (nodeListener == null) {
            NodeListener listener = new NodeAdapter() {
                @Override
                public void propertyChange(final PropertyChangeEvent ev) {
                    Mutex.EVENT.writeAccess(new Runnable() {
                        @Override
                        public void run() {
                            if (Node.PROP_ICON.equals(ev.getPropertyName())) {
                                if (formDataObject.isValid() && (multiviewTC != null)) {
                                    multiviewTC.setIcon(formDataObject.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16));
                                }
                            }
                        }
                    });
                }
            };
            node.addNodeListener(org.openide.nodes.NodeOp.weakNodeListener(listener, node));
            nodeListener = listener;
        }
    }
}
 
Example #2
Source File: PropertiesRowModel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Object getValueFor(Object node, int column) {
    Node n = Visualizer.findNode(node);
    if (n == null) {
        throw new IllegalStateException("TreeNode must be VisualizerNode but was: " + node + " of class " + node.getClass().getName());
    }
    PropertyChangeListener cacheEntry = nodesListenersCache.get (n);
    if (cacheEntry == null) {
        PropertyChangeListener p = WeakListeners.propertyChange(pcl, n);
        nodesListenersCache.put(n, p);
        n.addPropertyChangeListener(p);
        NodeListener l = WeakListeners.create(NodeListener.class, nl, n);
        n.addNodeListener(l);
    }
    Node.Property theRealProperty = getPropertyFor(n, prop[column]);
    return theRealProperty;
}
 
Example #3
Source File: CookieAction2Test.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testNodeListenersDetachedAtFinalizeIssue58065() throws Exception {
    CookieNode node = new CookieNode();
    SimpleCookieAction2 sca = new SimpleCookieAction2();
    Action action = sca.createContextAwareInstance(node.getLookup());
    
    class NodeListenerMemoryFilter implements MemoryFilter {
        public int numofnodelisteners = 0;
        public boolean reject(Object obj) {
            numofnodelisteners += (obj instanceof NodeListener)?1:0;
            return !((obj instanceof EventListenerList) | (obj instanceof Object[]));
        }
    }
    NodeListenerMemoryFilter filter = new NodeListenerMemoryFilter();
    assertSize("",Arrays.asList( new Object[] {node} ),1000000,filter);
    assertTrue("Node is expected to have a NodeListener attached", filter.numofnodelisteners > 0);
    
    Reference actionref = new WeakReference(sca);
    sca = null;
    action = null;
    assertGC("CookieAction is supposed to be GCed", actionref);
    
    NodeListenerMemoryFilter filter2 = new NodeListenerMemoryFilter();
    assertSize("",Arrays.asList( new Object[] {node} ),1000000,filter2);
    assertEquals("Node is expected to have no NodeListener attached", 0, filter2.numofnodelisteners);
}
 
Example #4
Source File: ActionFilterNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initChildren() {
    Node node = getOriginal();
    node.addNodeListener(WeakListeners.create(NodeListener.class, this, node));
    final boolean leaf = node.isLeaf();
    if (!leaf) {
        setChildren(children);
    }
}
 
Example #5
Source File: DataEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Annotates the editor with icon from the data object and also sets 
 * appropriate selected node. But only in the case the data object is valid.
 * This implementation also listen to display name and icon changes of the
 * node and keeps editor top component up-to-date. If you override this
 * method and not call super, please note that you will have to keep things
 * synchronized yourself. 
 *
 * @param editor the editor that has been created and should be annotated
 */
@Override
protected void initializeCloneableEditor (CloneableEditor editor) {
    // Prevention to bug similar to #17134. Don't call getNodeDelegate
    // on invalid data object. Top component should be discarded later.
    if(obj.isValid()) {
        Node ourNode = obj.getNodeDelegate();
        editor.setActivatedNodes (new Node[] { ourNode });
        editor.setIcon(ourNode.getIcon (java.beans.BeanInfo.ICON_COLOR_16x16));
        NodeListener nl = new DataNodeListener(editor);
        ourNode.addNodeListener(org.openide.nodes.NodeOp.weakNodeListener (nl, ourNode));
        nodeL = nl;
    }
}
 
Example #6
Source File: TopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Attaches itself to a given node.
*/
private void attach(Node n) {
    synchronized (top) {
        node = new WeakReference<Node>(n);
        nodeL = WeakListeners.create(NodeListener.class, this, n);
        n.addNodeListener(nodeL);
        top.attachNodeName(this);
        top.setActivatedNodes(new Node[] { n });
        top.setName(n.getDisplayName());
    }
}
 
Example #7
Source File: FavoritesNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected NodeListener createNodeListener() {
    return new NodeAdapter(this) {
        @Override
        protected void propertyChange(FilterNode fn, PropertyChangeEvent ev) {
            super.propertyChange(fn, ev);
            if (Node.PROP_LEAF.equals(ev.getPropertyName())) {
                RP.post(ProjectFilterNode.this);
            }
        }
    };
}
 
Example #8
Source File: PhysicalView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected NodeListener createNodeListener() {
    return new NodeAdapter(this) {
        @Override
        protected void propertyChange(FilterNode fn, PropertyChangeEvent ev) {
            super.propertyChange(fn, ev);
            if (Node.PROP_LEAF.equals(ev.getPropertyName())) {
                Node orig = getOriginal();
                setChildren(orig.isLeaf() ? Children.LEAF : new ProjectBadgingChildren(orig));
            }
        }
    };
}
 
Example #9
Source File: TreeTableView.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@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 #10
Source File: DataFolderTimeOrderTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testLastModifiedOrderUpdatedAfterFileIsTouched() throws Exception {
    aa.setSortMode(DataFolder.SortMode.LAST_MODIFIED);

    Node n = aa.getNodeDelegate().cloneNode();
    Node[] nodes = n.getChildren().getNodes(true);
    assertEquals ("Two nodes", 2, nodes.length);

    waitEvents();
    assertEquals("Sort mode not changed and children not refreshed: " + events, 2, events.size());
    assertTrue(DataFolder.PROP_SORT_MODE + " change not fired", events.contains(DataFolder.PROP_SORT_MODE));
    assertTrue(DataFolder.PROP_CHILDREN + " change not fired", events.contains(DataFolder.PROP_CHILDREN));
    assertEquals("Y.txt", nodes[0].getName()); // Y is newer
    assertEquals("X.txt", nodes[1].getName()); // X is older
    events.clear();

    final FileObject orig = lfs.findResource("AA/Y.txt");
    final FileObject touch = lfs.findResource("AA/X.txt");

    // After touching, X.txt will be newer than Y.txt.
    TestFileUtils.touch(FileUtil.toFile(touch), FileUtil.toFile(orig));
    // It's not enough to wait only for DataFolder event
    // because of number of RP tasks run before node children are updated
    // must wait for reorder fired by node itself.
    final CountDownLatch barrier = new CountDownLatch(1);
    NodeListener nodeList = new NodeAdapter() {

        @Override
        public void childrenReordered (NodeReorderEvent ev) {
            barrier.countDown();
        }
      
    };
    n.addNodeListener(nodeList);
    try {
        touch.refresh();
        waitEvents();
        // wait for node reorder event
        barrier.await(10, TimeUnit.SECONDS);
    } finally {
        n.removeNodeListener(nodeList);
    }
    assertEquals(0, barrier.getCount());
    assertTrue(DataFolder.PROP_CHILDREN + " change not fired", events.contains(DataFolder.PROP_CHILDREN));

    Node[] newNodes = n.getChildren().getNodes(true);
    assertEquals("Node " + nodes[1].getName() + " expected first.", newNodes[0], nodes[1]);
    assertEquals("Node " + nodes[0].getName() + " expected second.", newNodes[1], nodes[0]);
}
 
Example #11
Source File: Tab.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private NodeListener rcListener () {
    if (rcListener == null) {
        rcListener = new RootContextListener();
    }
    return rcListener;
}
 
Example #12
Source File: ProjectsRootNode.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NonNull
final Node logicalViewForProject(
        @NonNull final Project project,
        final Union2<LogicalViewProvider,org.openide.util.Pair<Sources,SourceGroup[]>> data,
        final boolean[] projectInLookup) {
    Node node;            
    if (!data.hasFirst()) {
        LOG.log(
                Level.WARNING,
                "Warning - project of {0} in {1} failed to supply a LogicalViewProvider in its lookup",  // NOI18N
                new Object[]{
                    project.getClass(),
                    FileUtil.getFileDisplayName(project.getProjectDirectory())
                });
        final Sources sources = data.second().first();
        final SourceGroup[] groups = data.second().second();
        sources.removeChangeListener(this);
        sources.addChangeListener(this);
        if (groups.length > 0) {
            node = PhysicalView.createNodeForSourceGroup(groups[0], project);
        } else {
            node = Node.EMPTY;
        }
    } else {
        final LogicalViewProvider lvp = data.first();
        node = lvp.createLogicalView();
        if (!project.equals(node.getLookup().lookup(Project.class))) {
            // Various actions, badging, etc. are not going to work.
            LOG.log(
                    Level.WARNING,
                    "Warning - project {0} failed to supply itself in the lookup of the root node of its own logical view",  // NOI18N
                    ProjectUtils.getInformation(project).getName());
            //#114664
            if (projectInLookup != null) {
                projectInLookup[0] = false;
            }
        }
    }                        
    node.addNodeListener(WeakListeners.create(NodeListener.class, this, node));
    return node;
}