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.apisupport.project.ui;
043:
044: import java.awt.Image;
045: import java.util.HashSet;
046: import java.util.Set;
047: import org.openide.ErrorManager;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileStateInvalidException;
050: import org.openide.filesystems.FileStatusEvent;
051: import org.openide.filesystems.FileStatusListener;
052: import org.openide.filesystems.FileSystem;
053: import org.openide.filesystems.FileUtil;
054: import org.openide.nodes.AbstractNode;
055: import org.openide.nodes.Children;
056: import org.openide.util.Lookup;
057: import org.openide.util.RequestProcessor;
058:
059: class AnnotatedNode extends AbstractNode implements Runnable,
060: FileStatusListener {
061:
062: private Set<FileObject> files;
063: private Set<FileStatusListener> fileSystemListeners;
064: private RequestProcessor.Task task;
065: private volatile boolean iconChange;
066: private volatile boolean nameChange;
067: private boolean forceAnnotation;
068:
069: protected AnnotatedNode(Children children) {
070: super (children, null);
071: }
072:
073: protected AnnotatedNode(Children children, Lookup lookup) {
074: super (children, lookup);
075: }
076:
077: protected final void setFiles(final Set<FileObject> files) {
078: fileSystemListeners = new HashSet<FileStatusListener>();
079: this .files = files;
080: if (files == null) {
081: return;
082: }
083: Set<FileSystem> hookedFileSystems = new HashSet<FileSystem>();
084: for (FileObject fo : files) {
085: try {
086: FileSystem fs = fo.getFileSystem();
087: if (hookedFileSystems.contains(fs)) {
088: continue;
089: }
090: hookedFileSystems.add(fs);
091: FileStatusListener fsl = FileUtil
092: .weakFileStatusListener(this , fs);
093: fs.addFileStatusListener(fsl);
094: fileSystemListeners.add(fsl);
095: } catch (FileStateInvalidException e) {
096: ErrorManager err = ErrorManager.getDefault();
097: err.annotate(e, "Cannot get " + fo
098: + " filesystem, ignoring..."); // NOI18N
099: err.notify(ErrorManager.INFORMATIONAL, e);
100: }
101: }
102: }
103:
104: protected final Set<FileObject> getFiles() {
105: return files;
106: }
107:
108: protected void setForceAnnotation(boolean forceAnnotation) {
109: this .forceAnnotation = forceAnnotation;
110: }
111:
112: protected final Image annotateIcon(final Image img, final int type) {
113: Image annotatedImg = img;
114: if (files != null && files.iterator().hasNext()) {
115: try {
116: FileObject fo = files.iterator().next();
117: annotatedImg = fo.getFileSystem().getStatus()
118: .annotateIcon(img, type, files);
119: } catch (FileStateInvalidException e) {
120: ErrorManager.getDefault().notify(
121: ErrorManager.INFORMATIONAL, e);
122: }
123: }
124: return annotatedImg;
125: }
126:
127: protected final String annotateName(final String name) {
128: String annotatedName = name;
129: if (files != null && files.iterator().hasNext()) {
130: try {
131: FileObject fo = files.iterator().next();
132: annotatedName = fo.getFileSystem().getStatus()
133: .annotateName(name, files);
134: } catch (FileStateInvalidException e) {
135: ErrorManager.getDefault().notify(
136: ErrorManager.INFORMATIONAL, e);
137: }
138: }
139: return annotatedName;
140: }
141:
142: public final void annotationChanged(FileStatusEvent event) {
143: if (task == null) {
144: task = RequestProcessor.getDefault().create(this );
145: }
146:
147: boolean changed = false;
148: if (forceAnnotation
149: || ((iconChange == false && event.isIconChange()) || (nameChange == false && event
150: .isNameChange()))) {
151: for (FileObject fo : files) {
152: if (event.hasChanged(fo)) {
153: iconChange |= event.isIconChange();
154: nameChange |= event.isNameChange();
155: changed = true;
156: }
157: }
158: }
159:
160: if (changed) {
161: task.schedule(50); // batch by 50 ms
162: }
163: }
164:
165: public final void run() {
166: if (forceAnnotation || iconChange) {
167: fireIconChange();
168: fireOpenedIconChange();
169: iconChange = false;
170: }
171: if (forceAnnotation || nameChange) {
172: fireDisplayNameChange(null, null);
173: nameChange = false;
174: }
175: }
176:
177: }
|