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-2007 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.ArrayList;
046: import java.util.Collections;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.LinkedHashMap;
050: import java.util.List;
051: import java.util.Set;
052: import javax.swing.event.ChangeListener;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.modules.apisupport.project.NbModuleProject;
055: import org.netbeans.modules.apisupport.project.layers.LayerNode;
056: import org.netbeans.modules.apisupport.project.layers.LayerUtils;
057: import org.netbeans.modules.apisupport.project.metainf.ServiceNodeHandler;
058: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
059: import org.netbeans.spi.project.ui.support.NodeFactory;
060: import org.netbeans.spi.project.ui.support.NodeList;
061: import org.openide.ErrorManager;
062: import org.openide.filesystems.FileChangeAdapter;
063: import org.openide.filesystems.FileChangeListener;
064: import org.openide.filesystems.FileEvent;
065: import org.openide.filesystems.FileObject;
066: import org.openide.filesystems.FileRenameEvent;
067: import org.openide.filesystems.FileStateInvalidException;
068: import org.openide.filesystems.FileSystem;
069: import org.openide.loaders.DataObject;
070: import org.openide.loaders.DataObjectNotFoundException;
071: import org.openide.nodes.Children;
072: import org.openide.nodes.FilterNode;
073: import org.openide.nodes.Node;
074: import org.openide.util.NbBundle;
075: import org.openide.util.RequestProcessor;
076: import org.openide.util.Utilities;
077:
078: /**
079: *
080: * @author mkleint
081: */
082: public class ImportantFilesNodeFactory implements NodeFactory {
083:
084: /** Package private for unit tests. */
085: static final String IMPORTANT_FILES_NAME = "important.files"; // NOI18N
086:
087: /** Package private for unit tests only. */
088: static final RequestProcessor RP = new RequestProcessor();
089:
090: public ImportantFilesNodeFactory() {
091: }
092:
093: public NodeList createNodes(Project p) {
094: return new ImpFilesNL(p);
095: }
096:
097: private static class ImpFilesNL implements NodeList<String> {
098: private Project project;
099:
100: public ImpFilesNL(Project p) {
101: project = p;
102: }
103:
104: public List<String> keys() {
105: return Collections.singletonList(IMPORTANT_FILES_NAME);
106: }
107:
108: public void addChangeListener(ChangeListener l) {
109: //ignore, doesn't change
110: }
111:
112: public void removeChangeListener(ChangeListener l) {
113: //ignore, doesn't change
114: }
115:
116: public Node node(String key) {
117: assert key.equals(IMPORTANT_FILES_NAME);
118: if (project instanceof NbModuleProject) {
119: return new ImportantFilesNode((NbModuleProject) project);
120: }
121: if (project instanceof SuiteProject) {
122: return new ImportantFilesNode(
123: new SuiteImportantFilesChildren(
124: (SuiteProject) project));
125: }
126: return null;
127: }
128:
129: public void addNotify() {
130: }
131:
132: public void removeNotify() {
133: }
134: }
135:
136: /**
137: * Show node "Important Files" with various config and docs files beneath it.
138: */
139: static final class ImportantFilesNode extends AnnotatedNode {
140:
141: private static final String DISPLAY_NAME = NbBundle.getMessage(
142: ModuleLogicalView.class, "LBL_important_files");
143:
144: public ImportantFilesNode(NbModuleProject project) {
145: super (new ImportantFilesChildren(project));
146: }
147:
148: ImportantFilesNode(Children ch) {
149: super (ch);
150: }
151:
152: public @Override
153: String getName() {
154: return IMPORTANT_FILES_NAME;
155: }
156:
157: private Image getIcon(boolean opened) {
158: Image badge = Utilities
159: .loadImage(
160: "org/netbeans/modules/apisupport/project/resources/config-badge.gif",
161: true);
162: return Utilities.mergeImages(UIUtil
163: .getTreeFolderIcon(opened), badge, 8, 8);
164: }
165:
166: public @Override
167: String getDisplayName() {
168: return annotateName(DISPLAY_NAME);
169: }
170:
171: public @Override
172: String getHtmlDisplayName() {
173: return computeAnnotatedHtmlDisplayName(DISPLAY_NAME,
174: getFiles());
175: }
176:
177: public @Override
178: Image getIcon(int type) {
179: return annotateIcon(getIcon(false), type);
180: }
181:
182: public @Override
183: Image getOpenedIcon(int type) {
184: return annotateIcon(getIcon(true), type);
185: }
186:
187: }
188:
189: public static Node createLayerNode(Project prj) {
190: LayerUtils.LayerHandle handle = LayerUtils.layerForProject(prj);
191: if (handle != null && handle.getLayerFile() != null) {
192: return new SpecialFileNode(new LayerNode(handle), null);
193: }
194: return null;
195: }
196:
197: /**
198: * Actual list of important files.
199: */
200: private static final class ImportantFilesChildren extends
201: Children.Keys<Object> {
202:
203: private List<Object> visibleFiles = null;
204: private FileChangeListener fcl;
205:
206: /** Abstract location to display name. */
207: private static final java.util.Map<String, String> FILES = new LinkedHashMap<String, String>();
208: static {
209: FILES.put("${manifest.mf}", NbBundle.getMessage(
210: ModuleLogicalView.class, "LBL_module_manifest"));
211: FILES.put("${javadoc.arch}", NbBundle.getMessage(
212: ModuleLogicalView.class, "LBL_arch_desc"));
213: FILES.put("${javadoc.apichanges}", NbBundle.getMessage(
214: ModuleLogicalView.class, "LBL_api_changes"));
215: FILES.put("${javadoc.overview}", NbBundle.getMessage(
216: ModuleLogicalView.class, "LBL_javadoc_overview"));
217: FILES.put("build.xml", NbBundle.getMessage(
218: ModuleLogicalView.class, "LBL_build.xml"));
219: FILES.put("nbproject/project.xml", NbBundle.getMessage(
220: ModuleLogicalView.class, "LBL_project.xml"));
221: FILES.put("nbproject/project.properties", NbBundle
222: .getMessage(ModuleLogicalView.class,
223: "LBL_project.properties"));
224: FILES.put("nbproject/private/private.properties", NbBundle
225: .getMessage(ModuleLogicalView.class,
226: "LBL_private.properties"));
227: FILES.put("nbproject/suite.properties", NbBundle
228: .getMessage(ModuleLogicalView.class,
229: "LBL_suite.properties"));
230: FILES.put("nbproject/private/suite-private.properties",
231: NbBundle.getMessage(ModuleLogicalView.class,
232: "LBL_suite-private.properties"));
233: FILES.put("nbproject/platform.properties", NbBundle
234: .getMessage(ModuleLogicalView.class,
235: "LBL_platform.properties"));
236: FILES.put("nbproject/private/platform-private.properties",
237: NbBundle.getMessage(ModuleLogicalView.class,
238: "LBL_platform-private.properties"));
239: }
240:
241: private final NbModuleProject project;
242:
243: public ImportantFilesChildren(NbModuleProject project) {
244: this .project = project;
245: }
246:
247: protected @Override
248: void addNotify() {
249: super .addNotify();
250: attachListeners();
251: refreshKeys();
252: }
253:
254: protected @Override
255: void removeNotify() {
256: setKeys(Collections.<String> emptyList());
257: visibleFiles = null;
258: removeListeners();
259: super .removeNotify();
260: }
261:
262: protected Node[] createNodes(Object key) {
263: if (key instanceof String) {
264: String loc = (String) key;
265: String locEval = project.evaluator().evaluate(loc);
266: FileObject file = project.getHelper()
267: .resolveFileObject(locEval);
268: try {
269: Node orig = DataObject.find(file).getNodeDelegate();
270: return new Node[] { new SpecialFileNode(orig, FILES
271: .get(loc)) };
272: } catch (DataObjectNotFoundException e) {
273: throw new AssertionError(e);
274: }
275: } else if (key instanceof LayerUtils.LayerHandle) {
276: return new Node[] {/* #68240 */new SpecialFileNode(
277: new LayerNode((LayerUtils.LayerHandle) key),
278: null) };
279: } else if (key instanceof ServiceNodeHandler) {
280: return new Node[] { ((ServiceNodeHandler) key)
281: .createServiceRootNode() };
282: } else {
283: throw new AssertionError(key);
284: }
285: }
286:
287: private void refreshKeys() {
288: Set<FileObject> files = new HashSet<FileObject>();
289: List<Object> newVisibleFiles = new ArrayList<Object>();
290: LayerUtils.LayerHandle handle = LayerUtils
291: .layerForProject(project);
292: FileObject layerFile = handle.getLayerFile();
293: if (layerFile != null) {
294: newVisibleFiles.add(handle);
295: files.add(layerFile);
296: }
297: for (String loc : FILES.keySet()) {
298: String locEval = project.evaluator().evaluate(loc);
299: if (locEval == null) {
300: newVisibleFiles.remove(loc); // XXX why?
301: continue;
302: }
303: FileObject file = project.getHelper()
304: .resolveFileObject(locEval);
305: if (file != null) {
306: newVisibleFiles.add(loc);
307: files.add(file);
308: }
309: }
310: newVisibleFiles.add(project.getLookup().lookup(
311: ServiceNodeHandler.class));
312: if (!newVisibleFiles.equals(visibleFiles)) {
313: visibleFiles = newVisibleFiles;
314: RP.post(new Runnable() { // #72471
315: public void run() {
316: setKeys(visibleFiles);
317: }
318: });
319: ((ImportantFilesNode) getNode()).setFiles(files);
320: }
321: }
322:
323: private void attachListeners() {
324: try {
325: if (fcl == null) {
326: fcl = new FileChangeAdapter() {
327: public @Override
328: void fileRenamed(FileRenameEvent fe) {
329: refreshKeys();
330: }
331:
332: public @Override
333: void fileDataCreated(FileEvent fe) {
334: refreshKeys();
335: }
336:
337: public @Override
338: void fileDeleted(FileEvent fe) {
339: refreshKeys();
340: }
341: };
342: project.getProjectDirectory().getFileSystem()
343: .addFileChangeListener(fcl);
344: }
345: } catch (FileStateInvalidException e) {
346: assert false : e;
347: }
348: }
349:
350: private void removeListeners() {
351: if (fcl != null) {
352: try {
353: project.getProjectDirectory().getFileSystem()
354: .removeFileChangeListener(fcl);
355: } catch (FileStateInvalidException e) {
356: assert false : e;
357: }
358: fcl = null;
359: }
360: }
361:
362: }
363:
364: /**
365: * Node to represent some special file in a project.
366: * Mostly just a wrapper around the normal data node.
367: */
368: static final class SpecialFileNode extends FilterNode {
369:
370: private final String displayName;
371:
372: public SpecialFileNode(Node orig, String displayName) {
373: super (orig);
374: this .displayName = displayName;
375: }
376:
377: public @Override
378: String getDisplayName() {
379: if (displayName != null) {
380: return displayName;
381: } else {
382: return super .getDisplayName();
383: }
384: }
385:
386: public @Override
387: boolean canRename() {
388: return false;
389: }
390:
391: public @Override
392: boolean canDestroy() {
393: return false;
394: }
395:
396: public @Override
397: boolean canCut() {
398: return false;
399: }
400:
401: public @Override
402: String getHtmlDisplayName() {
403: String result = null;
404: DataObject dob = getLookup().lookup(DataObject.class);
405: if (dob != null) {
406: Set<FileObject> files = dob.files();
407: result = computeAnnotatedHtmlDisplayName(
408: getDisplayName(), files);
409: }
410: return result;
411: }
412:
413: }
414:
415: /**
416: * Annotates <code>htmlDisplayName</code>, if it is needed, and returns the
417: * result; <code>null</code> otherwise.
418: */
419: private static String computeAnnotatedHtmlDisplayName(
420: final String htmlDisplayName,
421: final Set<? extends FileObject> files) {
422:
423: String result = null;
424: if (files != null && files.iterator().hasNext()) {
425: try {
426: FileObject fo = (FileObject) files.iterator().next();
427: FileSystem.Status stat = fo.getFileSystem().getStatus();
428: if (stat instanceof FileSystem.HtmlStatus) {
429: FileSystem.HtmlStatus hstat = (FileSystem.HtmlStatus) stat;
430:
431: String annotated = hstat.annotateNameHtml(
432: htmlDisplayName, files);
433:
434: // Make sure the super string was really modified (XXX why?)
435: if (!htmlDisplayName.equals(annotated)) {
436: result = annotated;
437: }
438: }
439: } catch (FileStateInvalidException e) {
440: ErrorManager.getDefault().notify(
441: ErrorManager.INFORMATIONAL, e);
442: }
443: }
444: return result;
445: }
446:
447: /**
448: * Actual list of important files.
449: */
450: private static final class SuiteImportantFilesChildren extends
451: Children.Keys<String> {
452:
453: private List<String> visibleFiles = new ArrayList<String>();
454: private FileChangeListener fcl;
455:
456: /** Abstract location to display name. */
457: private static final java.util.Map<String, String> FILES = new LinkedHashMap<String, String>();
458: static {
459: FILES.put("master.jnlp", NbBundle.getMessage(
460: SuiteLogicalView.class, "LBL_jnlp_master"));
461: FILES.put("build.xml", NbBundle.getMessage(
462: SuiteLogicalView.class, "LBL_build.xml"));
463: FILES.put("nbproject/project.properties", NbBundle
464: .getMessage(SuiteLogicalView.class,
465: "LBL_project.properties"));
466: FILES.put("nbproject/private/private.properties", NbBundle
467: .getMessage(SuiteLogicalView.class,
468: "LBL_private.properties"));
469: FILES.put("nbproject/platform.properties", NbBundle
470: .getMessage(SuiteLogicalView.class,
471: "LBL_platform.properties"));
472: FILES.put("nbproject/private/platform-private.properties",
473: NbBundle.getMessage(SuiteLogicalView.class,
474: "LBL_platform-private.properties"));
475: }
476:
477: private final SuiteProject project;
478:
479: public SuiteImportantFilesChildren(SuiteProject project) {
480: this .project = project;
481: }
482:
483: protected @Override
484: void addNotify() {
485: super .addNotify();
486: attachListeners();
487: refreshKeys();
488: }
489:
490: protected @Override
491: void removeNotify() {
492: setKeys(Collections.<String> emptySet());
493: removeListeners();
494: super .removeNotify();
495: }
496:
497: protected Node[] createNodes(String loc) {
498: String locEval = project.getEvaluator().evaluate(loc);
499: FileObject file = project.getHelper().resolveFileObject(
500: locEval);
501:
502: try {
503: Node orig = DataObject.find(file).getNodeDelegate();
504: return new Node[] { new SpecialFileNode(orig, FILES
505: .get(loc)) };
506: } catch (DataObjectNotFoundException e) {
507: throw new AssertionError(e);
508: }
509: }
510:
511: private void refreshKeys() {
512: List<String> newVisibleFiles = new ArrayList<String>();
513: Iterator it = FILES.keySet().iterator();
514: Set<FileObject> files = new HashSet<FileObject>();
515: while (it.hasNext()) {
516: String loc = (String) it.next();
517: String locEval = project.getEvaluator().evaluate(loc);
518: if (locEval == null) {
519: continue;
520: }
521: FileObject file = project.getHelper()
522: .resolveFileObject(locEval);
523: if (file != null) {
524: newVisibleFiles.add(loc);
525: files.add(file);
526: }
527: }
528: if (!isInitialized()
529: || !newVisibleFiles.equals(visibleFiles)) {
530: visibleFiles = newVisibleFiles;
531: RP.post(new Runnable() { // #72471
532: public void run() {
533: setKeys(visibleFiles);
534: }
535: });
536: ((ImportantFilesNode) getNode()).setFiles(files); // #72439
537: }
538: }
539:
540: private void attachListeners() {
541: try {
542: if (fcl == null) {
543: fcl = new FileChangeAdapter() {
544: public @Override
545: void fileRenamed(FileRenameEvent fe) {
546: refreshKeys();
547: }
548:
549: public @Override
550: void fileDataCreated(FileEvent fe) {
551: refreshKeys();
552: }
553:
554: public @Override
555: void fileDeleted(FileEvent fe) {
556: refreshKeys();
557: }
558: };
559: project.getProjectDirectory().getFileSystem()
560: .addFileChangeListener(fcl);
561: }
562: } catch (FileStateInvalidException e) {
563: assert false : e;
564: }
565: }
566:
567: private void removeListeners() {
568: if (fcl != null) {
569: try {
570: project.getProjectDirectory().getFileSystem()
571: .removeFileChangeListener(fcl);
572: } catch (FileStateInvalidException e) {
573: assert false : e;
574: }
575: fcl = null;
576: }
577: }
578:
579: }
580:
581: }
|