KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > projects > SettingChildren


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
20 package org.netbeans.core.projects;
21
22 import org.openide.NotifyDescriptor;
23 import org.openide.cookies.InstanceCookie;
24 import org.openide.loaders.DataObject;
25 import org.openide.loaders.DataFolder;
26 import org.openide.loaders.DataShadow;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileSystem;
29 import org.openide.filesystems.FileStateInvalidException;
30 import org.openide.filesystems.Repository;
31 import org.openide.util.NbBundle;
32 import org.openide.util.Utilities;
33 import org.netbeans.beaninfo.editors.ListImageEditor;
34 import java.awt.Image JavaDoc;
35 import java.beans.PropertyEditor JavaDoc;
36 import java.lang.reflect.InvocationTargetException JavaDoc;
37 import java.lang.ref.WeakReference JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Arrays JavaDoc;
40 import java.util.List JavaDoc;
41 import javax.swing.Action JavaDoc;
42 import org.openide.actions.ToolsAction;
43 import org.openide.nodes.FilterNode;
44 import org.openide.nodes.Node;
45 import org.openide.nodes.PropertySupport;
46 import org.openide.util.Exceptions;
47 import org.openide.util.actions.SystemAction;
48
49 /** Filters nodes under the session node (displayed in Options dialog), adds special
50  * properties to Nodes of particular settings to show/edit positions wher the
51  * setting is defined on DefaultFileSystem.
52  *
53  * @author Vitezslav Stejskal
54  */

55 public final class SettingChildren extends FilterNode.Children {
56
57     /** Name of Node.Property showing status of Session layer according to the setting */
58     public static final String JavaDoc PROP_LAYER_SESSION = "Session-Layer"; // NOI18N
59
/** Name of Node.Property showing status of Modules layer according to the setting */
60     public static final String JavaDoc PROP_LAYER_MODULES = "Modules-Layer"; // NOI18N
61

62     public SettingChildren (Node original) {
63         super (original);
64     }
65
66     protected Node copyNode (Node node) {
67         boolean filter = false;
68         try {
69             DataObject d = (DataObject) node.getCookie (DataObject.class);
70             if (d != null) {
71                 InstanceCookie.Of inst = (InstanceCookie.Of)d.getCookie(InstanceCookie.Of.class);
72                 if (inst != null && (inst.instanceOf(Node.class) || inst.instanceOf(Node.Handle.class))) {
73                     // This is just a node, not a real setting. E.g. ModuleNode, LoaderPoolNode. As such,
74
// it itself should not display any origin information, it would make no sense. However
75
// its children might have a legitimate DataObject cookie from the SFS.
76
d = null;
77                 }
78             }
79             DataFolder folder = (DataFolder) node.getCookie (DataFolder.class);
80             FileSystem fs = d == null || folder != null ? null : d.getPrimaryFile ().getFileSystem ();
81             filter = fs == null ? false : fs.equals (Repository.getDefault ().getDefaultFileSystem ());
82         } catch (FileStateInvalidException e) {
83             // ignore
84
}
85
86         return filter ? new SettingFilterNode (node) :
87             node.isLeaf() ? node.cloneNode() : new TrivialFilterNode(node);
88     }
89     
90     private static Action JavaDoc[] removeActions(Action JavaDoc[] allActions, Action JavaDoc[] toDeleteActions) {
91         Action JavaDoc[] retVal = allActions;
92         List JavaDoc<Action JavaDoc> actions = new ArrayList JavaDoc<Action JavaDoc>(Arrays.asList(allActions)); // to be mutable
93
for (int i = 0; i < toDeleteActions.length; i++) {
94             Action JavaDoc a = toDeleteActions[i];
95             if (actions.contains(a)) {
96                 actions.remove(a);
97                 retVal = actions.toArray(new Action JavaDoc[actions.size()]);
98             }
99         }
100         return retVal;
101     }
102     
103     private static final class TrivialFilterNode extends FilterNode {
104         public TrivialFilterNode(Node n) {
105             super(n, new SettingChildren(n));
106         }
107         // #17920: Index cookie works only when equality works
108
public boolean equals(Object JavaDoc o) {
109             return this == o || getOriginal().equals(o) || (o != null && o.equals(getOriginal()));
110         }
111         public int hashCode() {
112             return getOriginal().hashCode();
113         }
114         public Action JavaDoc[] getActions(boolean context) {
115             return removeActions(super.getActions(context), new Action JavaDoc[] {SystemAction.get(ToolsAction.class)});
116         }
117         public String JavaDoc getHtmlDisplayName() {
118             return null;
119         }
120     }
121
122     /** Property allowing display/manipulation of setting status for one specific layer. */
123     public static class FileStateProperty extends PropertySupport<Integer JavaDoc> {
124         static final int ACTION_DEFINE = 1;
125         static final int ACTION_REVERT = 2;
126         static final int ACTION_DELETE = 3;
127         
128         private FileObject primaryFile = null;
129         private int layer;
130
131         public FileStateProperty (String JavaDoc name) {
132             this (null, 0, name, true);
133         }
134
135         public FileStateProperty (FileObject primaryFile, int layer, String JavaDoc name, boolean readonly) {
136             super (name, Integer JavaDoc.class,
137                 NbBundle.getMessage (FileStateProperty.class, "LBL_FSP_" + name), // NOI18N
138
NbBundle.getMessage (FileStateProperty.class, "LBL_FSP_Desc_" + name), // NOI18N
139
true, !readonly);
140             
141             this.primaryFile = primaryFile;
142             this.layer = layer;
143
144             setValue (ListImageEditor.PROP_VALUES, new Integer JavaDoc [] {
145                 FileStateManager.FSTATE_DEFINED,
146                 FileStateManager.FSTATE_IGNORED,
147                 FileStateManager.FSTATE_INHERITED,
148                 FileStateManager.FSTATE_UNDEFINED,
149             });
150
151             setValue (ListImageEditor.PROP_IMAGES, new Image JavaDoc [] {
152                 Utilities.loadImage ("org/netbeans/core/resources/setting-defined.gif"), // NOI18N
153
Utilities.loadImage ("org/netbeans/core/resources/setting-ignored.gif"), // NOI18N
154
Utilities.loadImage ("org/netbeans/core/resources/setting-inherited.gif"), // NOI18N
155
Utilities.loadImage ("org/openide/resources/actions/empty.gif") // NOI18N
156
});
157         }
158
159         public boolean canWrite () {
160             if (!super.canWrite ())
161                 return false;
162             
163             Integer JavaDoc val = null;
164             try {
165                 val = getValue();
166             } catch (Exception JavaDoc e) {
167                 // ignore it, will be handled later
168
}
169             
170             return val != null &&
171                 val != FileStateManager.FSTATE_DEFINED &&
172                 (layer != FileStateManager.LAYER_MODULES || val != FileStateManager.FSTATE_UNDEFINED);
173         }
174
175         public Integer JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
176             return FileStateManager.getDefault().getFileState(primaryFile, layer);
177         }
178
179         public void setValue(Integer JavaDoc val) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
180             FileStateManager fsm = FileStateManager.getDefault ();
181             int action = val;
182             
183             try {
184                 switch (action) {
185                     case ACTION_DEFINE:
186                     case ACTION_REVERT:
187                         boolean go = true;
188
189                         for (int i = 0; i < layer; i++) {
190                             int state = fsm.getFileState (primaryFile, i);
191                             if (state == FileStateManager.FSTATE_DEFINED) {
192                                 // warn user, that above defined files will be removed
193

194                                 NotifyDescriptor nd = new NotifyDescriptor.Confirmation (
195                                     NbBundle.getMessage (SettingChildren.class, "MSG_ask_remove_above_defined_files"), // NOI18N
196
NotifyDescriptor.YES_NO_OPTION);
197
198                                 Object JavaDoc answer = org.openide.DialogDisplayer.getDefault ().notify (nd);
199                                 if (answer.equals (NotifyDescriptor.NO_OPTION))
200                                     go = false;
201
202                                 break;
203                             }
204                         }
205
206                         if (go)
207                             fsm.define (primaryFile, layer, action == ACTION_REVERT);
208
209                         break;
210
211                     case ACTION_DELETE:
212                         fsm.delete (primaryFile, layer);
213                         break;
214
215                     default:
216                         throw new IllegalArgumentException JavaDoc ("Required file state change isn't allowed. Action=" + action); // NOI18N
217
}
218             } catch (java.io.IOException JavaDoc e) {
219                 Exceptions.printStackTrace(e);
220             }
221         }
222
223         public PropertyEditor JavaDoc getPropertyEditor () {
224             return new FileStateEditor ();
225         }
226         
227         public String JavaDoc getShortDescription () {
228             Integer JavaDoc val = null;
229             String JavaDoc s = null;
230
231             if (primaryFile != null) {
232                 try {
233                     val = getValue();
234                 } catch (Exception JavaDoc e) {
235                     // ignore it, will be handled later
236
}
237
238                 switch (val == null ? -1 : val) {
239                     case FileStateManager.FSTATE_DEFINED:
240                         s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_defined");
241                         break;
242                     case FileStateManager.FSTATE_IGNORED:
243                         s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_ignored");
244                         break;
245                     case FileStateManager.FSTATE_INHERITED:
246                         s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_inherited");
247                         break;
248                     case FileStateManager.FSTATE_UNDEFINED:
249                         s = NbBundle.getMessage (SettingChildren.class, "LBL_fstate_undefined");
250                         break;
251                 }
252             }
253             else {
254                 s = super.getShortDescription ();
255             }
256             return s == null || s.length () == 0 ? null : s;
257         }
258     }
259
260     /** Filter node used for adding special status related properties to setting nodes. */
261     private static final class SettingFilterNode extends FilterNode {
262         private FSL weakL = null;
263         
264         public SettingFilterNode (Node original) {
265             super (original);
266         // need to keep the values in this FilterNode, not delegates
267
disableDelegation(DELEGATE_SET_VALUE | DELEGATE_GET_VALUE);
268
269             FileObject pf = ((DataObject) getCookie (DataObject.class)).getPrimaryFile ();
270             weakL = new FSL (this);
271             FileStateManager.getDefault ().addFileStatusListener (weakL, pf);
272
273             specialProp (new FileStateProperty (pf, FileStateManager.LAYER_SESSION, PROP_LAYER_SESSION, false));
274             specialProp (new FileStateProperty (pf, FileStateManager.LAYER_MODULES, PROP_LAYER_MODULES, false));
275         }
276         
277         /* @return the display name of the original node
278         */

279         public String JavaDoc getDisplayName() {
280             String JavaDoc retVal = null;
281             DataObject dobj= (DataObject) getCookie (DataObject.class);
282             if (dobj != null && dobj instanceof DataShadow) {
283                 DataShadow dsh = (DataShadow)dobj;
284                 Node origNode = dsh.getOriginal().getNodeDelegate();
285                 if (origNode != null) {
286                     retVal = origNode.getDisplayName();
287                 }
288             }
289             return (retVal != null) ? retVal : super.getDisplayName();
290         }
291         
292         /** Registers special property.
293          */

294         private void specialProp (Node.Property p) {
295             setValue (p.getName (), p);
296         }
297      
298         // #17920: Index cookie works only when equality works
299
public boolean equals(Object JavaDoc o) {
300             return this == o || getOriginal().equals(o) || (o != null && o.equals(getOriginal()));
301         }
302         public int hashCode() {
303             return getOriginal().hashCode();
304         }
305         // #24766 Exclude Customize Bean action.
306
/** Overrides superclass method, excludes the ToolsAction from the node. */
307         public Action JavaDoc[] getActions(boolean context) {
308             return removeActions(super.getActions(context), new Action JavaDoc[] {SystemAction.get(ToolsAction.class)});
309         }
310
311         private static class FSL implements FileStateManager.FileStatusListener {
312             WeakReference JavaDoc<SettingFilterNode> node = null;
313             public FSL (SettingFilterNode sfn) {
314                 node = new WeakReference JavaDoc<SettingFilterNode> (sfn);
315             }
316             public void fileStatusChanged (FileObject mfo) {
317                 SettingFilterNode n = node.get ();
318                 if (n == null) {
319                     FileStateManager.getDefault ().removeFileStatusListener (this, null);
320                     return;
321                 }
322                 
323                 n.firePropertyChange (PROP_LAYER_SESSION, null, null);
324                 n.firePropertyChange (PROP_LAYER_MODULES, null, null);
325             }
326         }
327     }
328 }
329
Popular Tags