This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 45361
Collapse All | Expand All

(-)src/org/openide/nodes/FilterNode.java (+21 lines)
Lines 708-713 Link Here
708
        return original.getCookie (type);
708
        return original.getCookie (type);
709
    }
709
    }
710
710
711
    /** This method is called from NodeLookup and if it is then
712
     * this means somebody overriden the getCookie method.
713
     */
714
    void lookupCookieItems (Class clazz, Set/*Lookup.Item*/ toAdd) {
715
        if (Node.Cookie.class.isAssignableFrom (clazz)) {
716
            Object c = getCookie (clazz);
717
            if (c != null) {
718
                toAdd.add (new NodeLookup.LookupItem (c));
719
            }
720
        } else {
721
            Lookup.Result res = getOriginal ().getLookup ().lookup (new Lookup.Template (clazz));
722
            java.util.Iterator it = res.allItems ().iterator ();
723
            while (it.hasNext ()) {
724
                Lookup.Item item = (Lookup.Item)it.next ();
725
                if (!Node.class.isAssignableFrom (item.getType ())) {
726
                    toAdd.add (new NodeLookup.DelegatePair (item));
727
                }
728
            }
729
        }
730
    }
731
711
    /** If this is FilterNode without any changes (subclassed, changed children)
732
    /** If this is FilterNode without any changes (subclassed, changed children)
712
    * and the original provides handle, stores them and
733
    * and the original provides handle, stores them and
713
    * returns a new handle for the proxy.
734
    * returns a new handle for the proxy.
(-)src/org/openide/nodes/Node.java (-1 / +22 lines)
Lines 628-633 Link Here
628
        return null;
628
        return null;
629
    }
629
    }
630
    
630
    
631
    /** Lookups items from lookup. Use only from NodeLookup!!!! 
632
     * Is reimplemented in FilterNode
633
     * @param toAdd adds all items that satisfy that template
634
     */
635
    void lookupCookieItems (Class c, Set/*Lookup.Item*/ toAdd) {
636
        Lookup l = internalLookup (true);
637
        if (l != null && ! (l instanceof NodeLookup)) {
638
            java.util.Iterator it = getLookup ().lookup (
639
                new org.openide.util.Lookup.Template (c)
640
            ).allItems ().iterator ();
641
            while (it.hasNext ()) {
642
                toAdd.add (it.next ());
643
            }
644
        } else {
645
            Object o = getCookie (c);
646
            if (o != null) {
647
                toAdd.add (new NodeLookup.LookupItem (o));
648
            }
649
        }
650
    }
651
    
631
    /** Obtains a Lookup represeting additional content of this Node.
652
    /** Obtains a Lookup represeting additional content of this Node.
632
     * If the lookup was provided in a constructor, it is returned here,
653
     * If the lookup was provided in a constructor, it is returned here,
633
     * if not, a lookup based on the content of <link>getCookie</link> 
654
     * if not, a lookup based on the content of <link>getCookie</link> 
Lines 1417-1423 Link Here
1417
    
1438
    
1418
    
1439
    
1419
    /** template for changes in cookies */
1440
    /** template for changes in cookies */
1420
    private static final Lookup.Template TEMPL_COOKIE = new Lookup.Template (Node.Cookie.class);
1441
    static final Lookup.Template TEMPL_COOKIE = new Lookup.Template (Object.class);
1421
    /** Lock for initialication */
1442
    /** Lock for initialication */
1422
    private static final Object INIT_LOCK = new Object();
1443
    private static final Object INIT_LOCK = new Object();
1423
    
1444
    
(-)src/org/openide/nodes/NodeLookup.java (-17 / +116 lines)
Lines 54-76 Link Here
54
     * @param colleciton to put Pair into if found
54
     * @param colleciton to put Pair into if found
55
     */
55
     */
56
    private static void addCookie (Node node, Class c, Collection collection) {
56
    private static void addCookie (Node node, Class c, Collection collection) {
57
        Object res;
57
        java.util.HashSet toAdd = new java.util.HashSet ();
58
        AbstractLookup.Pair pair; 
58
        AbstractLookup.Pair pair; 
59
        Object prev = CookieSet.entryQueryMode (c);
59
        Object prev = CookieSet.entryQueryMode (c);
60
        try {
60
        try {
61
            res = node.getCookie (c);
61
            node.lookupCookieItems (c, toAdd);
62
        } finally {
62
        } finally {
63
            pair = CookieSet.exitQueryMode (prev);
63
            pair = CookieSet.exitQueryMode (prev);
64
        }
64
        }
65
65
66
        if (pair == null) {
66
        if (pair != null) {
67
            if (res == null) {
67
            toAdd.clear ();
68
                return;
68
            toAdd.add (pair);
69
            }
70
            pair = new LookupItem (res);
71
        }
69
        }
72
70
73
        collection.add (pair);
71
        java.util.Iterator it = toAdd.iterator ();
72
        while (it.hasNext ()) {
73
            collection.add (it.next ());
74
        }
74
    }
75
    }
75
    
76
    
76
77
Lines 78-95 Link Here
78
     * @param template the template 
79
     * @param template the template 
79
     */
80
     */
80
    protected final void beforeLookup (Template template) {
81
    protected final void beforeLookup (Template template) {
81
        Class type = template.getType ();
82
        if (NO_COOKIE_CHANGE.get () == this) {
83
            // no beforeLookup mangling we just want all the pairs see X1
84
            return;
85
        }
82
        
86
        
83
        if (type == Object.class) {
87
        Class origType = template.getType ();
88
        
89
        Class type;
90
        if (origType == Object.class) {
84
            type = Node.Cookie.class;
91
            type = Node.Cookie.class;
92
        } else {
93
            type = origType;
85
        }
94
        }
86
95
87
        if (Node.Cookie.class.isAssignableFrom (type)) {
96
        if (Node.Cookie.class.isAssignableFrom (type)) {
88
            if (!queriedCookieClasses.contains (type)) {
97
            boolean queriedCookieClassesChanged;
89
                synchronized (this) {
98
            synchronized (this) {
90
                    queriedCookieClasses.add (type);
99
                queriedCookieClassesChanged = this.queriedCookieClasses.add (type);
91
                }
100
            }
92
                
101
            
102
            if (queriedCookieClassesChanged) {
93
                Object prev = NO_COOKIE_CHANGE.get ();
103
                Object prev = NO_COOKIE_CHANGE.get ();
94
                try {
104
                try {
95
                    NO_COOKIE_CHANGE.set (node);
105
                    NO_COOKIE_CHANGE.set (node);
Lines 98-103 Link Here
98
                    NO_COOKIE_CHANGE.set (prev);
108
                    NO_COOKIE_CHANGE.set (prev);
99
                }
109
                }
100
            }
110
            }
111
            
112
            if (type == origType) {
113
                // e.g. we really searched for cookie, 
114
                return; // and do not do the special treatment of FilterNode
115
                // as the cookie was either found or not and in such
116
                // case it is not supposed to be found
117
            }
118
        } 
119
        
120
        if (node instanceof FilterNode && !Node.Cookie.class.isAssignableFrom (origType)) {
121
            // still ask
122
            boolean queriedCookieClassesChanged;
123
            synchronized (this) {
124
                queriedCookieClassesChanged = this.queriedCookieClasses.add (type);
125
            }
126
            
127
            if (queriedCookieClassesChanged) {
128
                Object prev = NO_COOKIE_CHANGE.get ();
129
                try {
130
                    NO_COOKIE_CHANGE.set (node);
131
                    addCookie (node, origType, this);
132
                } finally {
133
                    NO_COOKIE_CHANGE.set (prev);
134
                }            
135
            }
101
        }
136
        }
102
    }
137
    }
103
    /** See #40734 and NodeLookupTest and CookieActionIsTooSlowTest. 
138
    /** See #40734 and NodeLookupTest and CookieActionIsTooSlowTest. 
Lines 113-122 Link Here
113
        // if it is cookie change, do the rescan, try to keep order
148
        // if it is cookie change, do the rescan, try to keep order
114
        synchronized (this) {
149
        synchronized (this) {
115
            java.util.HashSet toQuery = new java.util.HashSet (queriedCookieClasses);
150
            java.util.HashSet toQuery = new java.util.HashSet (queriedCookieClasses);
116
            Iterator it = lookup (new Template (Node.Cookie.class)).allItems ().iterator ();
151
            Iterator it;
152
            Object prev = NO_COOKIE_CHANGE.get ();
153
            try {
154
                NO_COOKIE_CHANGE.set (this);
155
                it = lookup (Node.TEMPL_COOKIE).allItems ().iterator ();
156
            } finally {
157
                NO_COOKIE_CHANGE.set (prev);
158
            }
117
            instances.add (new LookupItem (node));
159
            instances.add (new LookupItem (node));
118
            while (it.hasNext()) {
160
            while (it.hasNext()) {
119
                Item item = (Item)it.next ();
161
                Item item = (Item)it.next ();
162
                
163
                if (item instanceof DelegatePair) {
164
                    // we are filter node then
165
                    org.openide.util.Lookup del = ((FilterNode)node).getOriginal ().getLookup ();
166
                    org.openide.util.Lookup.Item found = del.lookupItem (new org.openide.util.Lookup.Template (
167
                        item.getType (), item.getId (), item.getInstance ()
168
                    ));
169
                    if (found != null) {
170
                        instances.add (new DelegatePair (found));
171
                    }
172
                    continue;
173
                }
174
                
120
                Class c = item.getType ();
175
                Class c = item.getType ();
121
                toQuery.remove (c);
176
                toQuery.remove (c);
122
                addCookie (node, c, instances);
177
                addCookie (node, c, instances);
Lines 190-196 Link Here
190
    }
245
    }
191
246
192
    /** Simple Pair to hold cookies and nodes */
247
    /** Simple Pair to hold cookies and nodes */
193
    private static class LookupItem extends AbstractLookup.Pair {
248
    static class LookupItem extends AbstractLookup.Pair {
194
        private Object instance;
249
        private Object instance;
195
250
196
        public LookupItem( Object instance ) {
251
        public LookupItem( Object instance ) {
Lines 232-236 Link Here
232
            return c.isInstance (instance);
287
            return c.isInstance (instance);
233
        }
288
        }
234
    } // End of LookupItem class
289
    } // End of LookupItem class
290
291
    /** Simple Pair that delegates to another pair */
292
    static class DelegatePair extends AbstractLookup.Pair {
293
        private org.openide.util.Lookup.Item item;
294
295
        public DelegatePair ( org.openide.util.Lookup.Item item) {
296
            this.item = item;
297
        }
298
299
        public String getDisplayName() {
300
            return item.getDisplayName ();
301
        }
302
303
        public String getId () {
304
            return item.getId ();
305
        }
306
307
        public Object getInstance() {
308
            return item.getInstance ();
309
        }
310
311
        public Class getType() {
312
            return item.getType ();
313
        }
314
        
315
        public boolean equals( Object object ) {
316
            if ( object instanceof DelegatePair ) {
317
                return item.equals (((DelegatePair)object).item);
318
            }
319
            return false;
320
        }
321
        
322
        public int hashCode() {
323
            return item.hashCode() + 777;
324
        }
325
        
326
        protected boolean creatorOf(Object obj) {
327
            return obj == getInstance ();
328
        }
329
        
330
        protected boolean instanceOf(Class c) {
331
            return c.isAssignableFrom (getType ());
332
        }
333
    } // End of DelegatePair class
235
    
334
    
236
}
335
}
(-)test/unit/src/org/openide/nodes/NodeLookupTest.java (-3 / +9 lines)
Lines 60-65 Link Here
60
60
61
    public void testChangesAreFiredFromLookupThruFilterNodeWithOverWrittenGetCookie () {
61
    public void testChangesAreFiredFromLookupThruFilterNodeWithOverWrittenGetCookie () {
62
        final Node.Cookie myInstance = new Node.Cookie () { };
62
        final Node.Cookie myInstance = new Node.Cookie () { };
63
        final Node.Cookie notMyInstance = new Node.Cookie () { };
63
        final ArrayList queries = new ArrayList ();
64
        final ArrayList queries = new ArrayList ();
64
        
65
        
65
        InstanceContent ic = new InstanceContent ();
66
        InstanceContent ic = new InstanceContent ();
Lines 68-73 Link Here
68
            public Node.Cookie getCookie (Class clazz) {
69
            public Node.Cookie getCookie (Class clazz) {
69
                queries.add (clazz);
70
                queries.add (clazz);
70
                
71
                
72
                if (clazz == notMyInstance.getClass ()) {
73
                    return null;
74
                }
75
                
71
                if (clazz == myInstance.getClass ()) {
76
                if (clazz == myInstance.getClass ()) {
72
                    return myInstance;
77
                    return myInstance;
73
                }
78
                }
Lines 78-86 Link Here
78
        checkGetNodesDoesNotInitializeLookup (node, queries);
83
        checkGetNodesDoesNotInitializeLookup (node, queries);
79
        checkInstanceInGetCookie (new Node.Cookie () {}, ic, node);
84
        checkInstanceInGetCookie (new Node.Cookie () {}, ic, node);
80
        checkInstanceInGetLookup (new Node.Cookie () {}, ic, node, true);
85
        checkInstanceInGetLookup (new Node.Cookie () {}, ic, node, true);
81
        // by overwriting the FilterNode.getCookie we disable enhanced support
86
        checkInstanceInGetLookup (notMyInstance, ic, node, false);
82
        // for non-cookie objects in original lookup
87
        // by overwriting the FilterNode.getCookie we say the lookup
83
        checkInstanceInGetLookup ("Some string", ic, node, false);
88
        // to provide all non-cookie content from its delegate
89
        checkInstanceInGetLookup ("Some string", ic, node, true);
84
        
90
        
85
        assertEquals ("It is possible to get myInstance from getCookie", myInstance, node.getCookie (myInstance.getClass ()));
91
        assertEquals ("It is possible to get myInstance from getCookie", myInstance, node.getCookie (myInstance.getClass ()));
86
        assertEquals ("It also possible to get it from getLookup", myInstance, node.getLookup ().lookup (myInstance.getClass ()));
92
        assertEquals ("It also possible to get it from getLookup", myInstance, node.getLookup ().lookup (myInstance.getClass ()));

Return to bug 45361