KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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