KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > AnnotatedNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileStateInvalidException;
29 import org.openide.filesystems.FileStatusEvent;
30 import org.openide.filesystems.FileStatusListener;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.nodes.AbstractNode;
34 import org.openide.nodes.Children;
35 import org.openide.util.Lookup;
36 import org.openide.util.RequestProcessor;
37
38 class AnnotatedNode extends AbstractNode implements Runnable JavaDoc, FileStatusListener {
39     
40     private Set JavaDoc files;
41     private Set JavaDoc fileSystemListeners;
42     private RequestProcessor.Task task;
43     private volatile boolean iconChange;
44     private volatile boolean nameChange;
45     private boolean forceAnnotation;
46     
47     protected AnnotatedNode(Children children) {
48         super(children, null);
49     }
50     
51     protected AnnotatedNode(Children children, Lookup lookup) {
52         super(children, lookup);
53     }
54     
55     protected final void setFiles(final Set JavaDoc files) {
56         fileSystemListeners = new HashSet JavaDoc();
57         this.files = files;
58         if (files == null) {
59             return;
60         }
61         Iterator JavaDoc it = files.iterator();
62         Set JavaDoc hookedFileSystems = new HashSet JavaDoc();
63         while (it.hasNext()) {
64             FileObject fo = (FileObject) it.next();
65             try {
66                 FileSystem fs = fo.getFileSystem();
67                 if (hookedFileSystems.contains(fs)) {
68                     continue;
69                 }
70                 hookedFileSystems.add(fs);
71                 FileStatusListener fsl = FileUtil.weakFileStatusListener(this, fs);
72                 fs.addFileStatusListener(fsl);
73                 fileSystemListeners.add(fsl);
74             } catch (FileStateInvalidException e) {
75                 ErrorManager err = ErrorManager.getDefault();
76                 err.annotate(e, "Cannot get " + fo + " filesystem, ignoring..."); // NOI18N
77
err.notify(ErrorManager.INFORMATIONAL, e);
78             }
79         }
80     }
81     
82     protected final Set JavaDoc<FileObject> getFiles() {
83         return files;
84     }
85     
86     protected void setForceAnnotation(boolean forceAnnotation) {
87         this.forceAnnotation = forceAnnotation;
88     }
89     
90     protected final Image JavaDoc annotateIcon(final Image JavaDoc img, final int type) {
91         Image JavaDoc annotatedImg = img;
92         if (files != null && files.iterator().hasNext()) {
93             try {
94                 FileObject fo = (FileObject) files.iterator().next();
95                 annotatedImg = fo.getFileSystem().getStatus().annotateIcon(img, type, files);
96             } catch (FileStateInvalidException e) {
97                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
98             }
99         }
100         return annotatedImg;
101     }
102     
103     protected final String JavaDoc annotateName(final String JavaDoc name) {
104         String JavaDoc annotatedName = name;
105         if (files != null && files.iterator().hasNext()) {
106             try {
107                 FileObject fo = (FileObject) files.iterator().next();
108                 annotatedName = fo.getFileSystem().getStatus().annotateName(name, files);
109             } catch (FileStateInvalidException e) {
110                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
111             }
112         }
113         return annotatedName;
114     }
115     
116     public final void annotationChanged(FileStatusEvent event) {
117         if (task == null) {
118             task = RequestProcessor.getDefault().create(this);
119         }
120         
121         boolean changed = false;
122         if (forceAnnotation || ((iconChange == false && event.isIconChange()) || (nameChange == false && event.isNameChange()))) {
123             Iterator JavaDoc it = files.iterator();
124             while (it.hasNext()) {
125                 FileObject fo = (FileObject) it.next();
126                 if (event.hasChanged(fo)) {
127                     iconChange |= event.isIconChange();
128                     nameChange |= event.isNameChange();
129                     changed = true;
130                 }
131             }
132         }
133         
134         if (changed) {
135             task.schedule(50); // batch by 50 ms
136
}
137     }
138     
139     public final void run() {
140         if (forceAnnotation || iconChange) {
141             fireIconChange();
142             fireOpenedIconChange();
143             iconChange = false;
144         }
145         if (forceAnnotation || nameChange) {
146             fireDisplayNameChange(null, null);
147             nameChange = false;
148         }
149     }
150     
151 }
152
Popular Tags