KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > CategoryNode


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.modules.palette;
21 import java.awt.datatransfer.Transferable JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import org.netbeans.spi.palette.PaletteActions;
27 import org.netbeans.spi.palette.PaletteController;
28 import org.netbeans.spi.palette.PaletteFilter;
29 import org.openide.DialogDisplayer;
30 import org.openide.NotifyDescriptor;
31
32 import org.openide.filesystems.FileObject;
33 import org.openide.loaders.DataFolder;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.DataShadow;
36 import org.openide.nodes.FilterNode;
37 import org.openide.nodes.Node;
38 import org.openide.util.HelpCtx;
39 import org.openide.util.Utilities;
40 import org.openide.util.Lookup;
41 import org.openide.util.LookupEvent;
42 import org.openide.util.datatransfer.PasteType;
43 import org.openide.util.lookup.AbstractLookup;
44 import org.openide.util.lookup.InstanceContent;
45 import org.openide.util.lookup.ProxyLookup;
46
47 /**
48  * A node for palette category.
49  *
50  * @author S. Aubrecht
51  */

52 class CategoryNode extends FilterNode {
53     
54     static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0];
55     
56     static final String JavaDoc CAT_NAME = "categoryName"; // NOI18N
57

58     private Action JavaDoc[] actions;
59
60     CategoryNode( Node originalNode, Lookup lkp ) {
61         this( originalNode, new InstanceContent(), lkp );
62     }
63     
64     private CategoryNode( Node originalNode, InstanceContent content, Lookup lkp ) {
65         super( originalNode,
66                new Children( originalNode, lkp ),
67                new ProxyLookup( new Lookup[] { lkp, new AbstractLookup(content), originalNode.getLookup() } ) );
68         
69         DataFolder folder = (DataFolder)originalNode.getCookie( DataFolder.class );
70         if( null != folder ) {
71             content.add( new DataFolder.Index( folder, this ) );
72             FileObject fob = folder.getPrimaryFile();
73             Object JavaDoc catName = fob.getAttribute( CAT_NAME );
74             if (catName instanceof String JavaDoc)
75                 setDisplayName((String JavaDoc)catName);
76         }
77         content.add( this );
78     }
79     
80     // -------
81

82     public String JavaDoc getDisplayName() {
83
84         String JavaDoc retValue = null;
85         DataFolder folder = (DataFolder)getCookie( DataFolder.class );
86         if( null != folder ) {
87             FileObject fob = folder.getPrimaryFile();
88             Object JavaDoc catName = fob.getAttribute( CAT_NAME );
89             if (catName instanceof String JavaDoc)
90                 retValue = catName.toString();
91         }
92         if( null == retValue ) {
93             retValue = super.getDisplayName();
94         }
95         // XXX poor impl; should not depend on org.openide.loaders.Bundle#FMT_shadowName:
96
if( null != retValue && retValue.indexOf("\u2192") > 0 ) {
97             DataShadow shadow = (DataShadow)getCookie( DataShadow.class );
98             if( null != shadow ) {
99                 DataObject dobj = shadow.getOriginal();
100                 if( null != dobj ) {
101                     Node origNode = dobj.getNodeDelegate();
102                     if( null != origNode && null != origNode.getDisplayName() ) {
103                         retValue = origNode.getDisplayName();
104                     }
105                 }
106             }
107         }
108         return retValue;
109     }
110
111     public void setDisplayName( String JavaDoc displayName ) {
112         try {
113             DataFolder folder = (DataFolder)getCookie( DataFolder.class );
114             if( null != folder ) {
115                 FileObject fo = folder.getPrimaryFile();
116                 fo.setAttribute( CAT_NAME, displayName );
117             }
118         } catch (java.io.IOException JavaDoc ex) {
119             RuntimeException JavaDoc e = new IllegalArgumentException JavaDoc();
120             org.openide.ErrorManager.getDefault().annotate(e, ex);
121             throw e;
122         }
123         super.setDisplayName( displayName );
124     }
125
126     public String JavaDoc getShortDescription() {
127         return getDisplayName();
128     }
129
130     public Action JavaDoc[] getActions(boolean context) {
131         if (actions == null) {
132             Node n = getParentNode();
133             actions = new Action JavaDoc[] {
134                 new Utils.PasteItemAction( this ),
135                 null,
136                 new Utils.NewCategoryAction( n ),
137                 null,
138                 new Utils.DeleteCategoryAction(this),
139                 new Utils.RenameCategoryAction(this),
140                 null,
141                 new Utils.SortItemsAction(this),
142                 null,
143                 new Utils.SortCategoriesAction( n ),
144                 null,
145                 new Utils.RefreshPaletteAction()
146             };
147         }
148         PaletteActions customActions = (PaletteActions)getParentNode().getLookup().lookup( PaletteActions.class );
149         if( null != customActions ) {
150             return Utils.mergeActions( actions, customActions.getCustomCategoryActions( getLookup() ) );
151         }
152         return actions;
153     }
154
155     public Node.PropertySet[] getPropertySets() {
156         return NO_PROPERTIES;
157     }
158
159     public boolean canDestroy() {
160         return !Utils.isReadonly( getOriginal() );
161     }
162
163     public HelpCtx getHelpCtx() {
164         return Utils.getHelpCtx( this, super.getHelpCtx() );
165     }
166     
167     private static class Children extends FilterNode.Children {
168
169         private Lookup lkp;
170         private PaletteFilter filter;
171         
172         public Children(Node original, Lookup lkp) {
173             super(original);
174             this.lkp = lkp;
175             this.filter = (PaletteFilter)lkp.lookup( PaletteFilter.class );
176         }
177
178         protected Node copyNode(Node node) {
179             return new ItemNode( node );
180         }
181         
182         protected Node[] createNodes(Node key) {
183             if( null == filter || filter.isValidItem( key.getLookup() ) ) {
184                 return new Node[] { copyNode(key) };
185             }
186
187             return null;
188         }
189         
190         public void resultChanged(LookupEvent ev) {
191             Node[] nodes = original.getChildren().getNodes();
192             List JavaDoc<Node> empty = Collections.emptyList();
193             setKeys( empty );
194             setKeys( nodes );
195         }
196     }
197
198     /** Checks category name if it is valid and if there's already not
199      * a category with the same name.
200      * @param name name to be checked
201      * @param namedNode node which name is checked or null if it doesn't exist yet
202      * @return true if the name is OK
203      */

204     static boolean checkCategoryName( Node parentNode, String JavaDoc name, Node namedNode) {
205         boolean invalid = false;
206         if (name == null || "".equals(name)) // NOI18N
207
invalid = true;
208         else // name should not start with . or contain only spaces
209
for (int i=0, n=name.length(); i < n; i++) {
210                 char ch = name.charAt(i);
211                 if (ch == '.' || (ch == ' ' && i+1 == n)) {
212                     invalid = true;
213                     break;
214                 }
215                 else if (ch != ' ')
216                     break;
217             }
218
219         if (invalid) {
220             DialogDisplayer.getDefault().notify(
221                 new NotifyDescriptor.Message(MessageFormat.format(
222                       Utils.getBundleString("ERR_InvalidName"), // NOI18N
223
new Object JavaDoc[] { name }),
224                       NotifyDescriptor.INFORMATION_MESSAGE));
225             return false;
226         }
227
228         Node[] nodes = parentNode.getChildren().getNodes();
229         for (int i=0; i < nodes.length; i++)
230             if (name.equals(nodes[i].getName()) && nodes[i] != namedNode) {
231                 DialogDisplayer.getDefault().notify(
232                     new NotifyDescriptor.Message(MessageFormat.format(
233                           Utils.getBundleString("FMT_CategoryExists"), // NOI18N
234
new Object JavaDoc[] { name }),
235                           NotifyDescriptor.INFORMATION_MESSAGE));
236                 return false;
237             }
238
239         return true;
240     }
241
242     /** Converts category name to name that can be used as name of folder
243      * for the category (restricted even to package name).
244      */

245     static String JavaDoc convertCategoryToFolderName( FileObject paletteFO,
246                                                        String JavaDoc name,
247                                                        String JavaDoc currentName)
248     {
249         if (name == null || "".equals(name)) // NOI18N
250
return null;
251
252         int i;
253         int n = name.length();
254         StringBuffer JavaDoc nameBuff = new StringBuffer JavaDoc(n);
255
256         char ch = name.charAt(0);
257         if (Character.isJavaIdentifierStart(ch)) {
258             nameBuff.append(ch);
259             i = 1;
260         }
261         else {
262             nameBuff.append('_');
263             i = 0;
264         }
265
266         while (i < n) {
267             ch = name.charAt(i);
268             if (Character.isJavaIdentifierPart(ch))
269                 nameBuff.append(ch);
270             i++;
271         }
272
273         String JavaDoc fName = nameBuff.toString();
274         if ("_".equals(fName)) // NOI18N
275
fName = "Category"; // NOI18N
276
if (fName.equals(currentName))
277             return fName;
278
279         // having the base name, make sure it is not used yet
280
String JavaDoc freeName = null;
281         boolean nameOK = false;
282
283         for (i=0; !nameOK; i++) {
284             freeName = i > 0 ? fName + "_" + i : fName; // NOI18N
285

286             if (Utilities.isWindows()) {
287                 nameOK = true;
288                 java.util.Enumeration JavaDoc en = paletteFO.getChildren(false);
289                 while (en.hasMoreElements()) {
290                     FileObject fo = (FileObject)en.nextElement();
291                     String JavaDoc fn = fo.getName();
292                     String JavaDoc fe = fo.getExt();
293
294                     // case-insensitive on Windows
295
if ((fe == null || "".equals(fe)) && fn.equalsIgnoreCase(freeName)) { // NOI18N
296
nameOK = false;
297                         break;
298                     }
299                 }
300             }
301             else nameOK = paletteFO.getFileObject(freeName) == null;
302         }
303         return freeName;
304     }
305
306     public PasteType getDropType(Transferable JavaDoc t, int action, int index) {
307         if( t.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) )
308             return super.getDropType(t, action, index);
309         return null;
310     }
311 }
312
Popular Tags