KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

275            // The caller has write access to ProjectManager
276
// and ensures the project will be saved.
277

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