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 54150
Collapse All | Expand All

(-)src/org/netbeans/modules/masterfs/MasterFileSystem.java (-4 / +116 lines)
Lines 14-19 Link Here
14
14
15
package org.netbeans.modules.masterfs;
15
package org.netbeans.modules.masterfs;
16
16
17
import org.netbeans.modules.masterfs.providers.AnnotationProvider;
17
import org.openide.filesystems.*;
18
import org.openide.filesystems.*;
18
import org.openide.util.NbBundle;
19
import org.openide.util.NbBundle;
19
import org.openide.util.actions.SystemAction;
20
import org.openide.util.actions.SystemAction;
Lines 213-218 Link Here
213
    }
214
    }
214
215
215
    public SystemAction[] getActions(java.util.Set foSet) {
216
    public SystemAction[] getActions(java.util.Set foSet) {
217
        SystemAction[] some = status.getActions (foSet);
218
        if (some != null) {
219
            return some;
220
        }
221
        
216
        SyncSection.getDefault().enterSection();
222
        SyncSection.getDefault().enterSection();
217
        try {
223
        try {
218
            //check if all fileobjects come from the same filesystem
224
            //check if all fileobjects come from the same filesystem
Lines 312-318 Link Here
312
    }
318
    }
313
319
314
    
320
    
315
    private static final class StatusImpl implements FileSystem.HtmlStatus {
321
    private static final class StatusImpl implements FileSystem.HtmlStatus,
322
    org.openide.util.LookupListener, org.openide.filesystems.FileStatusListener {
323
        /** result with providers */
324
        private org.openide.util.Lookup.Result annotationProviders;
325
        private Collection previousProviders;
326
        {
327
            annotationProviders = org.openide.util.Lookup.getDefault ().lookup (
328
                new org.openide.util.Lookup.Template (AnnotationProvider.class)
329
            );
330
            annotationProviders.addLookupListener (this);
331
            resultChanged (null);
332
        }
333
        
334
        public void resultChanged (org.openide.util.LookupEvent ev) {
335
            java.util.Collection now = annotationProviders.allInstances ();
336
            java.util.Collection add;
337
            
338
            if (previousProviders != null) {
339
                add = new HashSet (now);
340
                add.removeAll (previousProviders);
341
                
342
                previousProviders.removeAll (now);
343
                java.util.Iterator it = previousProviders.iterator ();
344
                while (it.hasNext ()) {
345
                    AnnotationProvider ap = (AnnotationProvider)it.next ();
346
                    ap.removeFileStatusListener (this);
347
                }
348
            
349
            } else {
350
                add = now;
351
            }
352
353
            
354
            
355
            java.util.Iterator it = add.iterator ();
356
            while (it.hasNext ()) {
357
                AnnotationProvider ap = (AnnotationProvider)it.next ();
358
                try {
359
                    ap.addFileStatusListener (this);
360
                } catch (java.util.TooManyListenersException ex) {
361
                    org.openide.ErrorManager.getDefault ().notify (ex);
362
                }
363
            }
364
            
365
            previousProviders = now;
366
        }
367
368
        public SystemAction[] getActions(java.util.Set foSet) {
369
            
370
            javax.swing.Action[] retVal = null;
371
            java.util.Iterator it = annotationProviders.allInstances ().iterator ();
372
            while (retVal == null && it.hasNext ()) {
373
                AnnotationProvider ap = (AnnotationProvider)it.next ();
374
                retVal = ap.actions (foSet);
375
            }
376
            if (retVal != null) {
377
                // right now we handle just SystemAction, it can be changed if necessary
378
                SystemAction[] ret = new SystemAction[retVal.length];
379
                for (int i = 0; i < retVal.length; i++) {
380
                    if (retVal[i] instanceof SystemAction) {
381
                        ret[i] = (SystemAction)retVal[i];
382
                    }
383
                }
384
                return ret;
385
            }
386
            return null;
387
        }
388
        
389
        public void annotationChanged (org.openide.filesystems.FileStatusEvent ev) {
390
            if (ev.getSource () != MasterFileSystem.getDefault ()) {
391
                throw new IllegalStateException ("The source must be master fs and not : " + ev.getSource ()); // NOI18N
392
            }
393
            MasterFileSystem.getDefault ().fireFileStatusChanged (ev);
394
        }
395
        
316
        private FileSystem getDelegateFileSystem (Set files) {
396
        private FileSystem getDelegateFileSystem (Set files) {
317
            FileSystem retVal = null;
397
            FileSystem retVal = null;
318
           Iterator it = files.iterator();
398
           Iterator it = files.iterator();
Lines 325-331 Link Here
325
        }
405
        }
326
        
406
        
327
        public Image annotateIcon(Image icon, int iconType, Set files) {
407
        public Image annotateIcon(Image icon, int iconType, Set files) {
328
            Image retVal = icon;            
408
            Image retVal = null;            
409
            
410
            Iterator it = annotationProviders.allInstances ().iterator ();
411
            while (retVal == null && it.hasNext ()) {
412
                AnnotationProvider ap = (AnnotationProvider)it.next ();
413
                retVal = ap.annotateIcon (icon, iconType, files);
414
            }
415
            if (retVal != null) {
416
                return retVal;
417
            }
418
            
419
            
420
            retVal = icon;
329
            FileSystem fs = getDelegateFileSystem(files);                        
421
            FileSystem fs = getDelegateFileSystem(files);                        
330
            if (fs != null) {
422
            if (fs != null) {
331
                Set transformedSet = new LazySet (files);
423
                Set transformedSet = new LazySet (files);
Lines 336-342 Link Here
336
        }
428
        }
337
429
338
        public String annotateName(String name, Set files) {
430
        public String annotateName(String name, Set files) {
339
            String retVal = name;
431
            String retVal = null;
432
            Iterator it = annotationProviders.allInstances ().iterator ();
433
            while (retVal == null && it.hasNext ()) {
434
                AnnotationProvider ap = (AnnotationProvider)it.next ();
435
                retVal = ap.annotateName (name, files);
436
            }
437
            if (retVal != null) {
438
                return retVal;
439
            }
440
            retVal = name;
441
            
340
            Set transformedSet = new LazySet (files);
442
            Set transformedSet = new LazySet (files);
341
            FileSystem fs = getDelegateFileSystem(files);                        
443
            FileSystem fs = getDelegateFileSystem(files);                        
342
            if (fs != null) {
444
            if (fs != null) {
Lines 346-352 Link Here
346
        }
448
        }
347
449
348
        public String annotateNameHtml(String name, Set files) {
450
        public String annotateNameHtml(String name, Set files) {
349
            String retVal = name;
451
            String retVal = null;
452
            Iterator it = annotationProviders.allInstances ().iterator ();
453
            while (retVal == null && it.hasNext ()) {
454
                AnnotationProvider ap = (AnnotationProvider)it.next ();
455
                retVal = ap.annotateNameHtml (name, files);
456
            }
457
            if (retVal != null) {
458
                return retVal;
459
            }
460
            retVal = name;
461
            
350
            FileSystem fs = getDelegateFileSystem(files);                        
462
            FileSystem fs = getDelegateFileSystem(files);                        
351
            if (fs != null) {                
463
            if (fs != null) {                
352
                if (fs != null && fs.getStatus() instanceof FileSystem.HtmlStatus) {
464
                if (fs != null && fs.getStatus() instanceof FileSystem.HtmlStatus) {
(-)src/org/netbeans/modules/masterfs/providers/AnnotationProvider.java (+121 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.modules.masterfs.providers;
15
16
import java.io.IOException;
17
18
/** Can provide status and actions for FileObjects. Register it 
19
 * in META-INF/services/org.netbeans.modules.masterfs.providers.AnnotationProvider
20
 * file.
21
 *
22
 * @author Jaroslav Tulach
23
 */
24
public abstract class AnnotationProvider extends Object {
25
    /** listeners */
26
    private org.openide.filesystems.FileStatusListener listener;
27
    /** lock for modification of listeners */
28
    private static Object LOCK = new Object ();
29
    
30
    
31
    /** Annotate the name of a file cluster.
32
    * @param name the name suggested by default
33
    * @param files an immutable set of {@link FileObject}s belonging to this filesystem
34
    * @return the annotated name or null if this provider does not know how to annotate these files
35
    */
36
    public abstract String annotateName (String name, java.util.Set files);
37
38
    /** Annotate the icon of a file cluster.
39
     * <p>Please do <em>not</em> modify the original; create a derivative icon image,
40
     * using a weak-reference cache if necessary.
41
    * @param icon the icon suggested by default
42
    * @param iconType an icon type from {@link java.beans.BeanInfo}
43
    * @param files an immutable set of {@link FileObject}s belonging to this filesystem
44
    * @return the annotated icon or null if some other provider shall anotate the icon
45
    */
46
    public abstract java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set files);
47
    
48
    /** Annotate a name such that the returned value contains HTML markup.
49
     * The return value less the html content should typically be the same 
50
     * as the return value from <code>annotateName()</code>.  This is used,
51
     * for example, by VCS filesystems to de&euml;phasize the status information
52
     * included in the file name by using a light grey font color. 
53
     * <p>
54
     * For consistency with <code>Node.getHtmlDisplayName()</code>, 
55
     * filesystems that proxy other filesystems (and so must implement
56
     * this interface to supply HTML annotations) should return null if
57
     * the filesystem they proxy does not provide an implementation of
58
     * HTMLStatus.
59
     *
60
     * @see org.openide.awt.HtmlRenderer
61
     * @see <a href="@OPENIDE/LOADERS@/org/openide/loaders/DataNode.html#getHtmlDisplayName()"><code>DataNode.getHtmlDisplayName()</code></a>
62
     * @see org.openide.nodes.Node#getHtmlDisplayName
63
     **/
64
    public abstract String annotateNameHtml (String name, java.util.Set files);
65
66
    /** Provides actions that should be added to given set of files.
67
     * @return null or array of actions for these files.
68
     */
69
    public abstract javax.swing.Action[] actions (java.util.Set files);
70
    
71
    //
72
    // Listener support
73
    //
74
    
75
76
    /** Registers FileStatusListener to receive events.
77
    * The implementation registers the listener only when getStatus () is 
78
    * overriden to return a special value.
79
    *
80
    * @param listener The listener to register.
81
    */
82
    public final void addFileStatusListener (
83
        org.openide.filesystems.FileStatusListener listener
84
    ) throws java.util.TooManyListenersException {
85
        synchronized (LOCK) {
86
            if (this.listener != null) {
87
                throw new java.util.TooManyListenersException ();
88
            }
89
            this.listener = listener;
90
        }
91
    }
92
93
    /** Removes FileStatusListener from the list of listeners.
94
     *@param listener The listener to remove.
95
     */
96
    public final void removeFileStatusListener (
97
        org.openide.filesystems.FileStatusListener listener
98
    ) {
99
        synchronized (LOCK) {
100
            if (this.listener == listener) {
101
                this.listener = null;
102
            }
103
        }
104
    }
105
106
    /** Notifies all registered listeners about change of status of some files.
107
    *
108
    * @param event The event to be fired
109
    */
110
    protected final void fireFileStatusChanged(org.openide.filesystems.FileStatusEvent event) {
111
        org.openide.filesystems.FileStatusListener l;
112
        synchronized (LOCK) {
113
            l = this.listener;
114
        }
115
        if (l != null) {
116
            l.annotationChanged (event);
117
        }
118
    }    
119
}
120
121
(-)test/unit/src/org/netbeans/modules/masterfs/providers/AnnotationProviderTest.java (+288 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.modules.masterfs.providers;
15
16
import junit.framework.*;
17
import java.io.IOException;
18
19
/** Check the behaviour of masterfs's annation provider.
20
 *
21
 * @author Jaroslav Tulach
22
 */
23
public class AnnotationProviderTest extends org.netbeans.junit.NbTestCase {
24
    private AnnoProv a1, a2, a3, a4;
25
    
26
    private org.openide.filesystems.FileSystem fs;
27
    private org.openide.filesystems.FileObject fo;
28
    
29
    public AnnotationProviderTest (String testName) {
30
        super (testName);
31
    }
32
    
33
    static {
34
        System.setProperty ("org.openide.util.Lookup", "org.netbeans.modules.masterfs.providers.AnnotationProviderTest$Lkp"); // NOI18N
35
    }
36
    
37
    // TODO add test methods here. The name must begin with 'test'. For example:
38
    // public void testHello() {}
39
40
    protected void setUp () throws java.lang.Exception {
41
        assertEquals (Lkp.class, org.openide.util.Lookup.getDefault ().getClass ());
42
        
43
        // we need some masterfs fileobject
44
        fs = org.netbeans.modules.masterfs.MasterFileSystem.settingsFactory (null);
45
        fo = fs.getRoot ();
46
        assertEquals (org.netbeans.modules.masterfs.MasterFileSystem.class, fo.getFileSystem ().getClass ());
47
        
48
        
49
        a1 = new AnnoProv ();
50
        a2 = new AnnoProv ();
51
        a3 = new AnnoProv ();
52
        a4 = new AnnoProv ();
53
        
54
        Lkp.ic.add (a1);
55
        Lkp.ic.add (a2);
56
        Lkp.ic.add (a3);
57
        Lkp.ic.add (a4);
58
    }
59
60
    protected void tearDown () throws java.lang.Exception {
61
        Lkp.ic.remove (a1);
62
        Lkp.ic.remove (a2);
63
        Lkp.ic.remove (a3);
64
        Lkp.ic.remove (a4);
65
    }
66
67
    public static junit.framework.Test suite () {
68
        junit.framework.TestSuite suite = new junit.framework.TestSuite(AnnotationProviderTest.class);
69
        
70
        return suite;
71
    }
72
73
    public void testAnnotateName () {
74
        
75
        a2.returnName = "MyName";
76
        
77
        String r = fs.getStatus ().annotateName ("Kuk", java.util.Collections.singleton (fo));
78
        
79
        assertEquals ("My name returned", a2.returnName, r);
80
        assertEquals ("Kuk", a1.queriedName);
81
        assertEquals ("Kuk", a2.queriedName);
82
        assertNull ("Not queried at all", a3.queriedName);
83
        assertNull ("Not queried at all", a4.queriedName);
84
        
85
        assertTrue ("fo is the file", a1.filesName.contains (fo));
86
        assertTrue ("fo is the file", a2.filesName.contains (fo));
87
        
88
        assertNull (a1.filesActions);
89
        assertNull (a1.filesHtml);
90
        assertNull (a1.filesIcon);
91
        
92
        assertNull (a2.filesActions);
93
        assertNull (a2.filesHtml);
94
        assertNull (a2.filesIcon);
95
    }
96
97
    public void testAnnotateIcon () {
98
        
99
        int t = java.awt.image.BufferedImage.TYPE_BYTE_GRAY;
100
        java.awt.image.BufferedImage my = new java.awt.image.BufferedImage (10, 10, t);
101
        java.awt.image.BufferedImage his = new java.awt.image.BufferedImage (20, 20, t);
102
        a2.returnIcon = his;
103
        
104
        java.awt.Image r = fs.getStatus ().annotateIcon (my, 0, java.util.Collections.singleton (fo));
105
        
106
        assertEquals ("My name returned", his, r);
107
        assertSame (my, a1.queriedIcon);
108
        assertSame (my, a2.queriedIcon);
109
        assertNull ("Not queried at all", a3.queriedIcon);
110
        assertNull ("Not queried at all", a4.queriedIcon);
111
        
112
        
113
        assertNull (a1.filesActions);
114
        assertNull (a1.filesHtml);
115
        assertNull (a1.filesName);
116
        
117
        assertNull (a2.filesActions);
118
        assertNull (a2.filesHtml);
119
        assertNull (a2.filesName);
120
    }
121
122
    public void testAnnotateNameHtml () {
123
        
124
        a2.returnHtml = "MyName";
125
        
126
        Object o = fs.getStatus ();
127
        assertTrue ("Must be HtmlStatus", o instanceof org.openide.filesystems.FileSystem.HtmlStatus);
128
        org.openide.filesystems.FileSystem.HtmlStatus status = (org.openide.filesystems.FileSystem.HtmlStatus)o;
129
        
130
        
131
        
132
        String r = status.annotateNameHtml ("Kuk", java.util.Collections.singleton (fo));
133
        
134
        assertEquals ("My name returned", a2.returnHtml, r);
135
        assertEquals ("Kuk", a1.queriedHtml);
136
        assertEquals ("Kuk", a2.queriedHtml);
137
        assertNull ("Not queried at all", a3.queriedHtml);
138
        assertNull ("Not queried at all", a4.queriedHtml);
139
        
140
        assertTrue ("fo is the file", a1.filesHtml.contains (fo));
141
        assertTrue ("fo is the file", a2.filesHtml.contains (fo));
142
        
143
        assertNull (a1.filesActions);
144
        assertNull (a1.filesName);
145
        assertNull (a1.filesIcon);
146
        
147
        assertNull (a2.filesActions);
148
        assertNull (a2.filesName);
149
        assertNull (a2.filesIcon);
150
    }
151
152
    public void testActionsWorksOnSystemActionsCorrectly () {
153
        
154
        a2.returnActions = new javax.swing.Action[] {
155
            org.openide.actions.OpenAction.get (org.openide.actions.OpenAction.class)
156
        };
157
        
158
        java.util.Set s = java.util.Collections.singleton (fo);
159
        javax.swing.Action[] actions = fs.getActions (s);
160
        
161
        assertEquals ("Actions has the same size", a2.returnActions.length, actions.length);
162
        assertEquals ("And that is one", 1, actions.length);
163
        assertEquals ("First elem is same", a2.returnActions[0], actions[0]);
164
        assertEquals ("a1 queried", s, a1.filesActions);
165
        assertEquals ("a2 queried", s, a2.filesActions);
166
        assertNull ("Not queried at all", a3.filesActions);
167
        assertNull ("Not queried at all", a4.filesActions);
168
        
169
        assertNull (a1.filesHtml);
170
        assertNull (a1.filesName);
171
        assertNull (a1.filesIcon);
172
        
173
        assertNull (a2.filesHtml);
174
        assertNull (a2.filesName);
175
        assertNull (a2.filesIcon);
176
    }
177
178
    public void testListeningCapabilities () {
179
        class L implements org.openide.filesystems.FileStatusListener {
180
            public org.openide.filesystems.FileStatusEvent ev;
181
            
182
            public void annotationChanged (org.openide.filesystems.FileStatusEvent ev) {
183
                assertNull (this.ev);
184
                this.ev = ev;
185
            }
186
        }
187
        L l = new L ();
188
        
189
        try {
190
            fs.addFileStatusListener (l);
191
            
192
            a2.returnName = "xyz";
193
            String r = fs.getStatus().annotateName ("my name", java.util.Collections.singleton (fo));
194
            assertEquals (r, a2.returnName);
195
            
196
            org.openide.filesystems.FileStatusEvent ev;
197
            ev = new org.openide.filesystems.FileStatusEvent (fs, false, true);
198
            a3.fireFileStatusChanged (ev);
199
            
200
            assertEquals ("Our listener was called", ev, l.ev);
201
            l.ev = null;            
202
            Lkp.ic.remove (a3);
203
            
204
            a3.fireFileStatusChanged (ev);
205
            assertNull ("Now our listener was not called", l.ev);
206
            
207
        } finally {
208
            fs.removeFileStatusListener (l);
209
        }
210
    }
211
212
    private class AnnotationProviderImpl extends AnnotationProvider {
213
214
        public java.lang.String annotateName (java.lang.String name, java.util.Set files) {
215
            return null;
216
        }
217
218
        public java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set files) {
219
            return null;
220
        }
221
222
        public java.lang.String annotateNameHtml (java.lang.String name, java.util.Set files) {
223
            return null;
224
        }
225
226
        public javax.swing.Action[] actions (java.util.Set files) {
227
            return null;
228
        }
229
    }
230
231
232
    public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
233
        static org.openide.util.lookup.InstanceContent ic;
234
        
235
        public Lkp () {
236
            this (new org.openide.util.lookup.InstanceContent ());
237
        }
238
        
239
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
240
            super (ic);
241
            this.ic = ic;
242
        }
243
    } // end of Lkp
244
245
    static final class AnnoProv extends AnnotationProvider {
246
        public String returnHtml;
247
        public String returnName;
248
        public java.awt.Image returnIcon;
249
        public javax.swing.Action[] returnActions;
250
251
        public String queriedHtml;
252
        public String queriedName;
253
        public java.awt.Image queriedIcon;
254
255
        public java.util.Set filesHtml;
256
        public java.util.Set filesName;
257
        public java.util.Set filesIcon;
258
        public java.util.Set filesActions;
259
        
260
        public String annotateNameHtml (String name, java.util.Set files) {
261
            assertNull (queriedHtml);
262
            queriedHtml = name;
263
            filesHtml = files;
264
            return returnHtml;
265
        }
266
267
        public String annotateName (String name, java.util.Set files) {
268
            assertNull (queriedName);
269
            queriedName = name;
270
            filesName = files;
271
            return returnName;
272
        }
273
274
        public java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set files) {
275
            assertNull (queriedIcon);
276
            queriedIcon = icon;
277
            filesIcon = files;
278
            return returnIcon;
279
        }
280
281
        public javax.swing.Action[] actions (java.util.Set files) {
282
            assertNull (filesActions);
283
            filesActions = files;
284
            return returnActions;
285
        }
286
        
287
    }
288
}

Return to bug 54150