KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > 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.ruby.railsprojects.ui;
20
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import org.netbeans.api.project.FileOwnerQuery;
29 import org.netbeans.api.project.Project;
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.project.ProjectManager;
45 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
46 import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties;
47 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils;
48 import org.netbeans.modules.ruby.railsprojects.UpdateHelper;
49 import org.netbeans.modules.ruby.railsprojects.ui.customizer.RailsProjectProperties;
50
51
52 /**
53  * This class decorates package nodes and file nodes under the Libraries Nodes.
54  * It removes all actions from these nodes except of file node's {@link OpenAction}
55  * and package node's {@link FindAction} It also adds the {@link ShowRDocAction}
56  * to both file and package nodes. It also adds {@link RemoveClassPathRootAction} to
57  * class path roots.
58  */

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

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