KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > group > GroupNode


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
21 package org.netbeans.modules.group;
22
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.List JavaDoc;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileStateInvalidException;
31 import org.openide.loaders.DataNode;
32 import org.openide.loaders.DataObject;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Node;
35 import org.openide.nodes.NodeTransfer;
36 import org.openide.nodes.PropertySupport;
37 import org.openide.nodes.Sheet;
38 import org.openide.util.NbBundle;
39 import org.openide.util.datatransfer.ExTransferable;
40 import org.openide.util.datatransfer.PasteType;
41
42 /** Node for group shadow data object. */
43 public class GroupNode extends DataNode implements PropertyChangeListener JavaDoc {
44
45     /** base name for this node's icons */
46     static final String JavaDoc GS_ICON_BASE
47             = "org/netbeans/modules/group/resources/groupShadow"; //NOI18N
48

49     /** format for display name. */
50     private static MessageFormat JavaDoc groupFormat;
51
52
53     /**
54      * Creates a folder node for a given group shadow.
55      *
56      * @param group group shadow to create a node for
57      * @param children children to use for the node
58      */

59     public GroupNode(final GroupShadow group, Children children) {
60         super(group, children);
61         setIconBase(GS_ICON_BASE);
62     }
63
64     /**
65      */

66     public String JavaDoc getDisplayName() {
67         if (groupFormat == null) {
68             String JavaDoc fmtString = NbBundle.getMessage(
69                     GroupNode.class,
70                     "FMT_groupShadowName"); //NOI18N
71
groupFormat = new MessageFormat JavaDoc(fmtString);
72         }
73
74         String JavaDoc name = getName();
75         DataObject dataObject = getDataObject();
76         FileObject fileObject = dataObject.getPrimaryFile();
77         String JavaDoc defaultDisplayName = super.getDisplayName();
78         String JavaDoc displayName = groupFormat.format(
79                 new Object JavaDoc[] {name != null ? name : "", //NOI18N
80
"", //NOI18N
81
fileObject.toString(),
82                               "", //NOI18N
83
defaultDisplayName != null
84                                     ? defaultDisplayName
85                                     : ""}); //NOI18N
86
try {
87             displayName = fileObject.getFileSystem().getStatus().annotateName(
88                     displayName,
89                     dataObject.files());
90
91         } catch (FileStateInvalidException e) {
92             /* OK, so the display name will not be annotated */
93         }
94         return displayName;
95         // return super.getDisplayName() + GroupShadow.getLocalizedString("PROP_group"); // " (group)"; // NOI18N
96
}
97
98     /**
99      * Returns a group this node represents.
100      *
101      * @return <code>GroupShadow</code> represented by this node
102      */

103     private GroupShadow getGroup() {
104         return (GroupShadow) getCookie(DataObject.class);
105     }
106
107     /**
108      * Initializes a sheet of this node's properties.
109      *
110      * @return the initialized sheet
111      */

112     protected Sheet createSheet() {
113         Sheet s = super.createSheet();
114         updateSheet(s);
115         return s;
116     }
117
118     /** Listens to GroupShadow dataobject and updates Expert properties
119      * list visibility (on "template" property change). */

120     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
121         String JavaDoc propertyName = evt.getPropertyName();
122         if (propertyName == null) {
123             return;
124         }
125         if (propertyName.equals(DataObject.PROP_TEMPLATE)) {
126             Object JavaDoc oldValue = evt.getOldValue();
127             Object JavaDoc newValue = evt.getNewValue();
128             if (newValue != null && !newValue.equals(oldValue)) {
129                 updateSheet(getSheet());
130             }
131         } else if (propertyName.equals(GroupShadow.PROP_USE_PATTERN)) {
132             updateSheet(getSheet());
133         }
134     }
135
136     /** Conditionally fills the set */
137     private void fillExpertSet(Sheet.Set set){
138         final DataObject obj = getDataObject();
139         Node.Property p;
140
141         // put properties to set
142
try {
143             /*X
144               p = new PropertySupport.Reflection (obj, Boolean.TYPE, "getShowLinks", "setShowLinks");
145               p.setName(GroupShadow.PROP_SHOW_LINKS);
146               p.setDisplayName(GroupShadow.getLocalizedString("PROP_showlinks"));
147               p.setShortDescription(GroupShadow.getLocalizedString("HINT_showlinks"));
148               ss.put(p);
149             */

150             if (obj.isTemplate()){
151                 
152                 String JavaDoc dispName;
153                 String JavaDoc descr;
154                 final boolean isReadOnly = obj.getPrimaryFile().isReadOnly();
155                 
156                 /* property Create All: */
157                 p = new PropertySupport.Reflection(
158                         obj,
159                         Boolean.TYPE,
160                         "getTemplateAll", //NOI18N
161
isReadOnly ? null : "setTemplateAll"); //NOI18N
162
p.setName(GroupShadow.PROP_TEMPLATE_ALL);
163                 p.setDisplayName(NbBundle.getMessage(
164                         GroupNode.class,
165                         "PROP_templateall")); //NOI18N
166
p.setShortDescription(NbBundle.getMessage(
167                         GroupNode.class,
168                         "HINT_templateall")); //NOI18N
169
set.put(p);
170
171                 /* property Naming Pattern: */
172                 class TemplatePatternProperty
173                         extends PropertySupport.Reflection {
174                     public TemplatePatternProperty(DataObject obj)
175                             throws NoSuchMethodException JavaDoc {
176                         super(obj, String JavaDoc.class,
177                               "getTemplatePattern", //NOI18N
178
obj.getPrimaryFile().isReadOnly()
179                                       ? null
180                                       : "setTemplatePattern"); //NOI18N
181
}
182                     public boolean canWrite() {
183                         return GroupNode.this.getGroup().isUsePattern();
184                     }
185                 }
186                 p = new TemplatePatternProperty(obj);
187                 p.setName(GroupShadow.PROP_TEMPLATE_PATTERN);
188                 p.setDisplayName(NbBundle.getMessage(
189                         GroupNode.class,
190                         "PROP_templatePattern")); //NOI18N
191
p.setShortDescription(NbBundle.getMessage(
192                         GroupNode.class,
193                         "HINT_templatePattern")); //NOI18N
194
set.put(p);
195
196                 /* property Use Naming Pattern: */
197                 p = new PropertySupport.Reflection(
198                         obj,
199                         Boolean.TYPE,
200                         "isUsePattern", //NOI18N
201
"setUsePattern"); //NOI18N
202
p.setName(GroupShadow.PROP_USE_PATTERN);
203                 p.setDisplayName(NbBundle.getMessage(
204                         GroupNode.class,
205                         "PROP_UsePattern")); //NOI18N
206
p.setShortDescription(NbBundle.getMessage(
207                         GroupNode.class,
208                         "HINT_UsePattern")); //NOI18N
209
set.put(p);
210             }
211         } catch (Exception JavaDoc ex) {
212             throw new InternalError JavaDoc();
213         }
214     }
215
216     /**
217      * Creates, updates or removes an expert property set of this node's
218      * property sheet, according to the current template status of the group
219      * shadow. If the group shadow is not a template, its expert property set
220      * is removed (if it exists). If the group shadow is a templte, its
221      * expert property sheet is updated (if it already exists) or created.
222      *
223      * @param sheet property sheet whose expert property set is to be
224      * updated
225      */

226     private void updateSheet(Sheet sheet){
227         if (getDataObject().isTemplate()) {
228             Sheet.Set set = sheet.get(Sheet.EXPERT);
229             if (set == null) {
230                 set = Sheet.createExpertSet();
231                 fillExpertSet(set);
232                 sheet.put(set);
233             } else {
234                 fillExpertSet(set);
235             }
236         } else {
237             sheet.remove(Sheet.EXPERT);
238         }
239     }
240
241     /** Augments the default behaviour to test for {@link NodeTransfer#nodeCutFlavor} and
242      * {@link NodeTransfer#nodeCopyFlavor}
243      * with the {@link DataObject}. If there is such a flavor then adds
244      * the cut and copy flavors. Also, if there is a copy flavor and the
245      * data object is a template, adds an instantiate flavor.
246      *
247      * @param t transferable to use
248      * @param s list of {@link PasteType}s
249      */

250     protected void createPasteTypes(Transferable JavaDoc t, List JavaDoc s) {
251         super.createPasteTypes(t, s);
252         if (getDataObject().getPrimaryFile().isReadOnly()) {
253             return;
254         }
255
256         DataObject objToPaste = null;
257
258         /* Get the DataObject being dragged/pasted: */
259         objToPaste = (DataObject) NodeTransfer.cookie(
260                   t,
261                   NodeTransfer.CLIPBOARD_COPY | NodeTransfer.CLIPBOARD_CUT,
262                   DataObject.class);
263
264         if (objToPaste != null && objToPaste.isCopyAllowed()) {
265             // copy and cut
266
s.add(new Paste("PT_copy", objToPaste, false)); //NOI18N
267
}
268     }
269
270
271     /** Paste type for data objects. */
272     private class Paste extends PasteType {
273         
274         /** resource bundle key of this paste type's name */
275         private String JavaDoc nameBundleKey;
276         /** data object to be pasted */
277         private DataObject obj;
278         /**
279          * indicates whether the clipboard should be cleared after the paste
280          * action is finished
281          */

282         private boolean clearClipboard;
283
284         
285         /**
286          * Creates a paste type for a given data object.
287          *
288          * @param nameBundleKey resource bundle key of the paste type's name
289          * @param obj data object to be pasted
290          * @param clear <code>true</code> if the clipboard should be cleared
291          * after the paste action is finished,
292          * <code>false</code> otherwise
293          */

294         public Paste(String JavaDoc nameBundleKey, DataObject obj, boolean clear) {
295             this.nameBundleKey = nameBundleKey;
296             this.obj = obj;
297             this.clearClipboard = clear;
298         }
299
300         
301         /** */
302         public String JavaDoc getName() {
303             return NbBundle.getMessage(GroupNode.class, nameBundleKey);
304         }
305
306         /** */
307         public final Transferable JavaDoc paste() throws IOException JavaDoc {
308             handle(obj);
309             return clearClipboard ? ExTransferable.EMPTY : null;
310         }
311
312         /**
313          * Actually performs the paste action.
314          *
315          * @param objToPaste data object to be pasted to this node's
316          * <code>GroupShadow</code>
317          * @exception java.io.IOException
318          * if an error occured while reading the current
319          * or writing a modified list of contained files' names
320          */

321         public void handle(DataObject objToPaste) throws IOException JavaDoc {
322             List JavaDoc list = getGroup().readLinks();
323             String JavaDoc name = GroupShadow.getLinkName(objToPaste.getPrimaryFile());
324             if (!list.contains(name)) {
325                 list.add(name);
326             }
327             getGroup().writeLinks(list);
328         }
329     }
330
331 }
332
Popular Tags