KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > ActionFilterNode


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 package org.netbeans.modules.web.project.ui;
20
21
22 import java.net.URL JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.swing.Action JavaDoc;
28
29 import org.openide.ErrorManager;
30 import org.openide.actions.EditAction;
31 import org.openide.actions.FindAction;
32 import org.openide.loaders.DataObject;
33 import org.openide.actions.OpenAction;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileStateInvalidException;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.nodes.FilterNode;
38 import org.openide.nodes.Node;
39 import org.openide.util.Lookup;
40 import org.openide.util.actions.SystemAction;
41 import org.openide.util.lookup.Lookups;
42 import org.openide.util.lookup.ProxyLookup;
43
44 import org.netbeans.api.project.FileOwnerQuery;
45 import org.netbeans.api.project.Project;
46 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
47 import org.netbeans.spi.project.support.ant.AntProjectHelper;
48 import org.netbeans.spi.project.support.ant.EditableProperties;
49 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
50 import org.netbeans.spi.project.support.ant.ReferenceHelper;
51
52 import org.netbeans.modules.web.project.UpdateHelper;
53 import org.netbeans.modules.web.project.classpath.ClassPathSupport;
54 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
55
56 /**
57  * This class decorates package nodes and file nodes under the Libraries Nodes.
58  * It removes all actions from these nodes except of file node's {@link OpenAction}
59  * and package node's {@link FindAction} It also adds the {@link ShowJavadocAction}
60  * to both file and package nodes. It also adds {@link RemoveClassPathRootAction} to
61  * class path roots.
62  */

63 class ActionFilterNode extends FilterNode {
64
65     private static final int MODE_ROOT = 1;
66     private static final int MODE_PACKAGE = 2;
67     private static final int MODE_FILE = 3;
68     private static final int MODE_FILE_CONTENT = 4;
69
70     private final int mode;
71     private Action JavaDoc[] actionCache;
72
73     /**
74      * Creates new ActionFilterNode for class path root
75      * @param original the original node
76      * @param helper used for implementing {@link RemoveClassPathRootAction.Removable} or null if
77      * the node should not have the {@link RemoveClassPathRootAction}
78      * @param classPathId ant property name of classpath to which these classpath root belongs or null if
79      * the node should not have the {@link RemoveClassPathRootAction}
80      * @param entryId ant property name of this classpath root or null if
81      * the node should not have the {@link RemoveClassPathRootAction}
82      * @return ActionFilterNode
83      */

84     static ActionFilterNode create (Node original, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String JavaDoc classPathId, String JavaDoc entryId, String JavaDoc webModuleElementName) {
85         DataObject dobj = (DataObject) original.getLookup().lookup(DataObject.class);
86         assert dobj != null;
87         FileObject root = dobj.getPrimaryFile();
88         Lookup lkp = new ProxyLookup (new Lookup[] {original.getLookup(), helper == null ?
89             Lookups.singleton (new JavadocProvider(root,root)) :
90             Lookups.fixed (new Object JavaDoc[] {new Removable (helper, eval, refHelper, classPathId, entryId, webModuleElementName),
91             new JavadocProvider(root,root)})});
92         return new ActionFilterNode (original, helper == null ? MODE_PACKAGE : MODE_ROOT, root, lkp);
93     }
94
95
96
97     private ActionFilterNode (Node original, int mode, FileObject cpRoot, FileObject resource) {
98         this (original, mode, cpRoot,
99             new ProxyLookup(new Lookup[] {original.getLookup(),Lookups.singleton(new JavadocProvider(cpRoot,resource))}));
100     }
101
102     private ActionFilterNode (Node original, int mode) {
103         super (original, new ActionFilterChildren (original, mode, null));
104         this.mode = mode;
105     }
106
107     private ActionFilterNode (Node original, int mode, FileObject root, Lookup lkp) {
108         super (original, new ActionFilterChildren (original, mode,root),lkp);
109         this.mode = mode;
110     }
111
112     public Action JavaDoc[] getActions(boolean context) {
113         Action JavaDoc[] result = initActions();
114         return result;
115     }
116
117
118     public Action JavaDoc getPreferredAction() {
119         if (mode == MODE_FILE) {
120             Action JavaDoc[] actions = initActions();
121             if (actions.length > 0 && (actions[0] instanceof OpenAction || actions[0] instanceof EditAction)) {
122                 return actions[0];
123             }
124         }
125         return null;
126     }
127
128     private Action JavaDoc[] initActions () {
129         if (actionCache == null) {
130             List JavaDoc result = new ArrayList JavaDoc(2);
131             if (mode == MODE_FILE) {
132                 Action JavaDoc[] superActions = super.getActions(false);
133                 for (int i=0; i<superActions.length; i++) {
134                     if (superActions[i] instanceof OpenAction || superActions[i] instanceof EditAction) {
135                         result.add (superActions[i]);
136                     }
137                 }
138                 result.add (SystemAction.get(ShowJavadocAction.class));
139             }
140             else if (mode == MODE_PACKAGE || mode == MODE_ROOT) {
141                 result.add (SystemAction.get(ShowJavadocAction.class));
142                 Action JavaDoc[] superActions = super.getActions(false);
143                 for (int i=0; i<superActions.length; i++) {
144                     if (superActions[i] instanceof FindAction) {
145                         result.add (superActions[i]);
146                     }
147                 }
148                 if (mode == MODE_ROOT) {
149                     result.add (SystemAction.get(RemoveClassPathRootAction.class));
150                 }
151             }
152             actionCache = (Action JavaDoc[]) result.toArray(new Action JavaDoc[result.size()]);
153         }
154         return actionCache;
155     }
156
157     private static class ActionFilterChildren extends Children {
158
159         private final int mode;
160         private final FileObject cpRoot;
161
162         ActionFilterChildren (Node original, int mode, FileObject cpRooot) {
163             super (original);
164             this.mode = mode;
165             this.cpRoot = cpRooot;
166         }
167
168         protected Node[] createNodes(Node n) {
169             switch (mode) {
170                 case MODE_ROOT:
171                 case MODE_PACKAGE:
172                     DataObject dobj = (DataObject) n.getCookie(org.openide.loaders.DataObject.class);
173                     if (dobj == null) {
174                         assert false : "DataNode without DataObject in Lookup"; //NOI18N
175
return new Node[0];
176                     }
177                     else if (dobj.getPrimaryFile().isFolder()) {
178                         return new Node[] {new ActionFilterNode (n, MODE_PACKAGE,cpRoot,dobj.getPrimaryFile())};
179                     }
180                     else {
181                         return new Node[] {new ActionFilterNode (n, MODE_FILE,cpRoot,dobj.getPrimaryFile())};
182                     }
183                 case MODE_FILE:
184                 case MODE_FILE_CONTENT:
185                     return new Node[] {new ActionFilterNode (n, MODE_FILE_CONTENT)};
186                 default:
187                     assert false : "Unknown mode"; //NOI18N
188
return new Node[0];
189             }
190         }
191     }
192
193     private static class JavadocProvider implements ShowJavadocAction.JavadocProvider {
194
195         private final FileObject cpRoot;
196         private final FileObject resource;
197
198         JavadocProvider (FileObject cpRoot, FileObject resource) {
199             this.cpRoot = cpRoot;
200             this.resource = resource;
201         }
202
203         public boolean hasJavadoc() {
204             try {
205                 return resource != null && JavadocForBinaryQuery.findJavadoc(cpRoot.getURL()).getRoots().length>0;
206             } catch (FileStateInvalidException fsi) {
207                 return false;
208             }
209         }
210
211         public void showJavadoc() {
212             try {
213                 String JavaDoc relativeName = FileUtil.getRelativePath(cpRoot,resource);
214                 URL JavaDoc[] urls = JavadocForBinaryQuery.findJavadoc(cpRoot.getURL()).getRoots();
215                 URL JavaDoc pageURL;
216                 if (relativeName.length()==0) {
217                     pageURL = ShowJavadocAction.findJavadoc ("overview-summary.html",urls); //NOI18N
218
if (pageURL == null) {
219                         pageURL = ShowJavadocAction.findJavadoc ("index.html",urls); //NOI18N
220
}
221                 }
222                 else if (resource.isFolder()) {
223                     //XXX Are the names the same also in the localized javadoc?
224
pageURL = ShowJavadocAction.findJavadoc ("package-summary.html",urls); //NOI18N
225
}
226                 else {
227                     String JavaDoc javadocFileName = relativeName.substring(0,relativeName.lastIndexOf('.'))+".html"; //NOI18Ns
228
pageURL = ShowJavadocAction.findJavadoc (javadocFileName,urls);
229                 }
230                 ShowJavadocAction.showJavaDoc(pageURL,relativeName.replace('/','.')); //NOI18N
231
} catch (FileStateInvalidException fsi) {
232                 ErrorManager.getDefault().notify (fsi);
233             }
234         }
235     }
236
237    private static class Removable implements RemoveClassPathRootAction.Removable {
238
239        private final UpdateHelper helper;
240        private final String JavaDoc classPathId;
241        private final String JavaDoc entryId;
242        private final String JavaDoc webModuleElementName;
243        private final ClassPathSupport cs;
244
245        Removable (UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper, String JavaDoc classPathId, String JavaDoc entryId, String JavaDoc webModuleElementName) {
246            this.helper = helper;
247            this.classPathId = classPathId;
248            this.entryId = entryId;
249            this.webModuleElementName = webModuleElementName;
250            
251            this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(),
252                                         WebProjectProperties.WELL_KNOWN_PATHS,
253                                         WebProjectProperties.LIBRARY_PREFIX,
254                                         WebProjectProperties.LIBRARY_SUFFIX,
255                                         WebProjectProperties.ANT_ARTIFACT_PREFIX );
256
257        }
258
259
260        public boolean canRemove () {
261             //Allow to remove only entries from PROJECT_PROPERTIES, same behaviour as the project customizer
262
EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
263             return props.getProperty (classPathId) != null;
264         }
265
266        public Project remove() {
267            // Different implementation than j2seproject's one, because
268
// we need to remove the library entry from project.xml
269

270            // The caller has write access to ProjectManager
271
// and ensures the project will be saved.
272

273             boolean removed = false;
274             EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
275             String JavaDoc raw = props.getProperty (classPathId);
276             List JavaDoc resources = cs.itemsList( raw, webModuleElementName );
277             for (Iterator JavaDoc i = resources.iterator(); i.hasNext();) {
278                 ClassPathSupport.Item item = (ClassPathSupport.Item)i.next();
279                 if (entryId.equals(WebProjectProperties.getAntPropertyName(item.getReference()))) {
280                     i.remove();
281                     removed = true;
282                 }
283             }
284             if (removed) {
285                 String JavaDoc[] itemRefs = cs.encodeToStrings(resources.iterator(), webModuleElementName);
286                 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); //Reread the properties, PathParser changes them
287
props.setProperty (classPathId, itemRefs);
288                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
289                 //update lib references in private properties
290
EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
291                 ArrayList JavaDoc l = new ArrayList JavaDoc ();
292                 l.addAll(resources);
293                 l.addAll(cs.itemsList(props.getProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES));
294                 WebProjectProperties.storeLibrariesLocations(l.iterator(), privateProps);
295                 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps);
296                return FileOwnerQuery.getOwner(helper.getAntProjectHelper().getProjectDirectory());
297            } else {
298                return null;
299            }
300        }
301    }
302 }
303
Popular Tags