KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.j2seproject.ui;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList 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.java.j2seproject.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.api.project.ProjectManager;
46 import org.netbeans.spi.project.support.ant.AntProjectHelper;
47 import org.netbeans.spi.project.support.ant.EditableProperties;
48 import org.netbeans.spi.project.support.ant.PropertyUtils;
49 import org.netbeans.modules.java.j2seproject.UpdateHelper;
50
51 /**
52  * This class decorates package nodes and file nodes under the Libraries Nodes.
53  * It removes all actions from these nodes except of file node's {@link OpenAction}
54  * and package node's {@link FindAction} It also adds the {@link ShowJavadocAction}
55  * to both file and package nodes. It also adds {@link RemoveClassPathRootAction} to
56  * class path roots.
57  */

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

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