001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ant.freeform.ui;
043:
044: import java.awt.Image;
045: import java.awt.event.ActionEvent;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.util.ArrayList;
049: import java.util.Arrays;
050: import java.util.Collection;
051: import java.util.HashMap;
052: import java.util.HashSet;
053: import java.util.Iterator;
054: import java.util.List;
055: import java.util.Map;
056: import java.util.Set;
057: import java.util.logging.Level;
058: import java.util.logging.Logger;
059: import javax.swing.AbstractAction;
060: import javax.swing.Action;
061: import javax.swing.JSeparator;
062: import javax.swing.event.ChangeEvent;
063: import javax.swing.event.ChangeListener;
064: import org.netbeans.api.project.Project;
065: import org.netbeans.api.project.ProjectUtils;
066: import org.netbeans.api.project.SourceGroup;
067: import org.netbeans.api.project.Sources;
068: import org.openide.ErrorManager;
069: import org.openide.filesystems.FileObject;
070: import org.openide.filesystems.FileStateInvalidException;
071: import org.openide.filesystems.FileStatusEvent;
072: import org.openide.filesystems.FileStatusListener;
073: import org.openide.filesystems.FileSystem;
074: import org.openide.filesystems.FileUtil;
075: import org.openide.nodes.FilterNode;
076: import org.openide.nodes.Node;
077: import org.openide.util.Lookup;
078: import org.openide.util.RequestProcessor;
079: import org.openide.util.WeakListeners;
080: import org.openide.util.lookup.Lookups;
081:
082: /**A wrapper node for project's root node that adds CVS badges+Project/Actions.
083: *
084: * This class should be moved into projectuiapi (org.netbeans.spi.project.ui.support)
085: * after some cleanup and adding tests.
086: *
087: * The intent is to make it package private and create a factory which will wrap
088: * provided node (project's logical view) with this wrapper to serve the CVS annotations.
089: *
090: * @author Jan Lahoda
091: */
092: public final class ProjectNodeWrapper extends FilterNode implements
093: Runnable, FileStatusListener, ChangeListener,
094: PropertyChangeListener {
095:
096: private Set<FileObject> files;
097: private Map<FileSystem, FileStatusListener> fileSystemListeners;
098: private RequestProcessor.Task task;
099: private final Object privateLock = new Object();
100: private boolean iconChange;
101: private boolean nameChange;
102: private ChangeListener sourcesListener;
103: private Map<SourceGroup, PropertyChangeListener> groupsListeners;
104:
105: public static final Action GENERIC_PROJECTS_ACTIONS_MARKER = new AbstractAction() {
106: public void actionPerformed(ActionEvent e) {
107: }
108: };
109:
110: public ProjectNodeWrapper(Node toWrap) {
111: super (toWrap);
112: setProjectFiles();
113: }
114:
115: public Action[] getActions(boolean context) {
116: Action[] actions = super .getActions(context);
117:
118: List<Action> result = new ArrayList<Action>();
119:
120: for (int cntr = 0; cntr < actions.length; cntr++) {
121: if (actions[cntr] != GENERIC_PROJECTS_ACTIONS_MARKER) {
122: result.add(actions[cntr]);
123: } else {
124: // honor 57874 contact:
125: Lookup lookup = Lookups.forPath("Projects/Actions");
126: Iterator<? extends Object> it = lookup.lookupAll(
127: Object.class).iterator();
128: while (it.hasNext()) {
129: Object next = it.next();
130: if (next instanceof Action) {
131: result.add((Action) next);
132: } else if (next instanceof JSeparator) {
133: result.add(null);
134: }
135: }
136: }
137: }
138:
139: return result.toArray(new Action[result.size()]);
140: }
141:
142: protected final void setProjectFiles() {
143: Project prj = getLookup().lookup(Project.class);
144:
145: if (prj != null) {
146: setProjectFiles(prj);
147: } else {
148: ErrorManager
149: .getDefault()
150: .log(
151: ErrorManager.INFORMATIONAL,
152: "Node: "
153: + getOriginal()
154: + " wrapped with ProjectNodeWrapper, but does not contain a Project in the lookup!");
155: }
156: }
157:
158: protected final void setProjectFiles(Project project) {
159: Sources sources = ProjectUtils.getSources(project); // returns singleton
160: if (sourcesListener == null) {
161: sourcesListener = WeakListeners.change(this , sources);
162: sources.addChangeListener(sourcesListener);
163: }
164: setGroups(Arrays.asList(sources
165: .getSourceGroups(Sources.TYPE_GENERIC)));
166: }
167:
168: private final void setGroups(Collection<SourceGroup> groups) {
169: if (groupsListeners != null) {
170: for (Map.Entry<SourceGroup, PropertyChangeListener> entry : groupsListeners
171: .entrySet()) {
172: entry.getKey().removePropertyChangeListener(
173: entry.getValue());
174: }
175: }
176: groupsListeners = new HashMap<SourceGroup, PropertyChangeListener>();
177: Set<FileObject> roots = new HashSet<FileObject>();
178: for (SourceGroup group : groups) {
179: PropertyChangeListener pcl = WeakListeners.propertyChange(
180: this , group);
181: groupsListeners.put(group, pcl);
182: group.addPropertyChangeListener(pcl);
183: FileObject fo = group.getRootFolder();
184: roots.add(fo);
185: }
186: setFiles(roots);
187: }
188:
189: protected final void setFiles(Set<FileObject> files) {
190: if (fileSystemListeners != null) {
191: for (Map.Entry<FileSystem, FileStatusListener> entry : fileSystemListeners
192: .entrySet()) {
193: entry.getKey().removeFileStatusListener(
194: entry.getValue());
195: }
196: }
197:
198: fileSystemListeners = new HashMap<FileSystem, FileStatusListener>();
199: this .files = files;
200: if (files == null)
201: return;
202:
203: Set<FileSystem> hookedFileSystems = new HashSet<FileSystem>();
204: for (FileObject fo : files) {
205: try {
206: FileSystem fs = fo.getFileSystem();
207: if (hookedFileSystems.contains(fs)) {
208: continue;
209: }
210: hookedFileSystems.add(fs);
211: FileStatusListener fsl = FileUtil
212: .weakFileStatusListener(this , fs);
213: fs.addFileStatusListener(fsl);
214: fileSystemListeners.put(fs, fsl);
215: } catch (FileStateInvalidException e) {
216: Logger
217: .getLogger(ProjectNodeWrapper.class.getName())
218: .log(
219: Level.INFO,
220: "Cannot get " + fo
221: + " filesystem, ignoring...", e); // NOI18N
222: }
223: }
224: }
225:
226: // public String getDisplayName() {
227: // String s = super.getDisplayName();
228: //
229: // if (files != null && files.iterator().hasNext()) {
230: // try {
231: // FileObject fo = (FileObject) files.iterator().next();
232: // s = fo.getFileSystem().getStatus().annotateName(s, files);
233: // } catch (FileStateInvalidException e) {
234: // ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
235: // }
236: // }
237: //
238: // return s;
239: // }
240: //
241: // public String getHtmlDisplayName() {
242: // if (files != null && files.iterator().hasNext()) {
243: // try {
244: // FileObject fo = (FileObject) files.iterator().next();
245: // FileSystem.Status stat = fo.getFileSystem().getStatus();
246: // if (stat instanceof FileSystem.HtmlStatus) {
247: // FileSystem.HtmlStatus hstat = (FileSystem.HtmlStatus) stat;
248: //
249: // String result = hstat.annotateNameHtml(
250: // super.getHtmlDisplayName(), files);
251: //
252: // //Make sure the super string was really modified
253: // if (result != null && !result.equals(getDisplayName())) {
254: // return result;
255: // }
256: // }
257: // } catch (FileStateInvalidException e) {
258: // ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
259: // }
260: // }
261: // return super.getHtmlDisplayName();
262: // }
263:
264: public Image getIcon(int type) {
265: Image img = super .getIcon(type);
266:
267: if (files != null && files.iterator().hasNext()) {
268: try {
269: FileObject fo = files.iterator().next();
270: img = fo.getFileSystem().getStatus().annotateIcon(img,
271: type, files);
272: } catch (FileStateInvalidException e) {
273: ErrorManager.getDefault().notify(
274: ErrorManager.INFORMATIONAL, e);
275: }
276: }
277:
278: return img;
279: }
280:
281: public Image getOpenedIcon(int type) {
282: Image img = super .getOpenedIcon(type);
283:
284: if (files != null && files.iterator().hasNext()) {
285: try {
286: FileObject fo = files.iterator().next();
287: img = fo.getFileSystem().getStatus().annotateIcon(img,
288: type, files);
289: } catch (FileStateInvalidException e) {
290: ErrorManager.getDefault().notify(
291: ErrorManager.INFORMATIONAL, e);
292: }
293: }
294:
295: return img;
296: }
297:
298: public void run() {
299: boolean fireIcon;
300: boolean fireName;
301: synchronized (privateLock) {
302: fireIcon = iconChange;
303: fireName = nameChange;
304: iconChange = false;
305: nameChange = false;
306: }
307: if (fireIcon) {
308: fireIconChange();
309: fireOpenedIconChange();
310: }
311: if (fireName) {
312: fireDisplayNameChange(null, null);
313: }
314: }
315:
316: public void annotationChanged(FileStatusEvent event) {
317: if (task == null) {
318: task = RequestProcessor.getDefault().create(this );
319: }
320:
321: synchronized (privateLock) {
322: if ((iconChange == false && event.isIconChange())
323: || (nameChange == false && event.isNameChange())) {
324: for (FileObject fo : files) {
325: if (event.hasChanged(fo)) {
326: iconChange |= event.isIconChange();
327: nameChange |= event.isNameChange();
328: }
329: }
330: }
331: }
332:
333: task.schedule(50); // batch by 50 ms
334: }
335:
336: // sources change
337: public void stateChanged(ChangeEvent e) {
338: setProjectFiles();
339: }
340:
341: // group change
342: public void propertyChange(PropertyChangeEvent evt) {
343: setProjectFiles();
344: }
345:
346: }
|