KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.awt.Image JavaDoc;
23 import java.awt.datatransfer.DataFlavor JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
26 import java.beans.BeanInfo JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import org.openide.ErrorManager;
30 import org.openide.loaders.DataNode;
31 import org.openide.nodes.FilterNode;
32 import org.openide.nodes.Node;
33 import org.openide.text.ActiveEditorDrop;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.Lookup;
36 import org.openide.util.NbBundle;
37 import org.openide.util.Utilities;
38 import org.openide.util.datatransfer.ExTransferable;
39 import org.openide.util.lookup.AbstractLookup;
40 import org.openide.util.lookup.InstanceContent;
41
42
43
44
45 /**
46  *
47  * @author Libor Kotouc
48  */

49 public final class PaletteItemNode extends FilterNode {
50     
51     private static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0];
52
53     private String JavaDoc name;
54     private String JavaDoc bundleName;
55     private String JavaDoc displayNameKey;
56     private String JavaDoc className;
57     private String JavaDoc tooltipKey;
58     private String JavaDoc icon16URL;
59     private String JavaDoc icon32URL;
60     
61     private String JavaDoc displayName;
62     private String JavaDoc description;
63     private Image JavaDoc icon16;
64     private Image JavaDoc icon32;
65     
66     PaletteItemNode(DataNode original,
67                     String JavaDoc name,
68                     String JavaDoc bundleName,
69                     String JavaDoc displayNameKey,
70                     String JavaDoc className,
71                     String JavaDoc tooltipKey,
72                     String JavaDoc icon16URL,
73                     String JavaDoc icon32URL,
74                     InstanceContent content)
75     {
76         super(original, Children.LEAF, new AbstractLookup(content));
77         
78         content.add( this );
79         this.name = name;
80         this.bundleName = bundleName;
81         this.displayNameKey = displayNameKey;
82         this.className = className;
83         this.tooltipKey = tooltipKey;
84         this.icon16URL = icon16URL;
85         this.icon32URL = icon32URL;
86     }
87  
88     PaletteItemNode(DataNode original,
89                     String JavaDoc name,
90                     String JavaDoc displayName,
91                     String JavaDoc tooltip,
92                     String JavaDoc icon16URL,
93                     String JavaDoc icon32URL,
94                     InstanceContent content)
95     {
96         super(original, Children.LEAF, new AbstractLookup(content));
97         
98         content.add( this );
99         this.name = name;
100         this.bundleName = bundleName;
101         assert null != displayName;
102         this.displayName = displayName;
103         this.description = tooltip;
104         if( null == this.description )
105             description = displayName;
106         this.icon16URL = icon16URL;
107         this.icon32URL = icon32URL;
108     }
109     
110     public String JavaDoc getName() {
111         return name;
112     }
113
114     public String JavaDoc getDisplayName() {
115         if (displayName == null)
116             displayName = _getDisplayName(bundleName, displayNameKey, className);
117         
118         return displayName;
119     }
120
121     public String JavaDoc getShortDescription() {
122         if (description == null)
123             description = _getShortDescription(bundleName, tooltipKey, className, displayNameKey);
124         
125         return description;
126     }
127
128     public Image JavaDoc getIcon(int type) {
129
130         Image JavaDoc icon = null;
131         
132         if (type == BeanInfo.ICON_COLOR_16x16 || type == BeanInfo.ICON_MONO_16x16) {
133             if (icon16 == null) {
134                 icon16 = _getIcon(icon16URL);
135                 if (icon16 == null)
136                     icon16 = Utilities.loadImage("org/netbeans/modules/palette/resources/unknown16.gif"); // NOI18N
137
}
138             icon = icon16;
139         }
140         else if (type == BeanInfo.ICON_COLOR_32x32 || type == BeanInfo.ICON_MONO_32x32) {
141             if (icon32 == null) {
142                 icon32 = _getIcon(icon32URL);
143                 if (icon32 == null)
144                     icon32 = Utilities.loadImage("org/netbeans/modules/palette/resources/unknown32.gif"); // NOI18N
145
}
146             icon = icon32;
147         }
148         
149         return icon;
150     }
151     
152     public boolean canRename() {
153         return false;
154     }
155
156     // TODO properties
157
public Node.PropertySet[] getPropertySets() {
158         return NO_PROPERTIES;
159     }
160
161     public Transferable JavaDoc clipboardCopy() throws IOException JavaDoc {
162
163         ExTransferable t = ExTransferable.create( super.clipboardCopy() );
164         
165         Lookup lookup = getLookup();
166         ActiveEditorDrop drop = (ActiveEditorDrop) lookup.lookup(ActiveEditorDrop.class);
167         ActiveEditorDropTransferable s = new ActiveEditorDropTransferable(drop);
168         t.put(s);
169
170         //do not allow external DnD flavors otherwise some items may get interpreted
171
//as an external file dropped into the editor window
172
return new NoExternalDndTransferable( t );
173     }
174
175     public Transferable JavaDoc drag() throws IOException JavaDoc {
176         return clipboardCopy();
177     }
178
179     private static class ActiveEditorDropTransferable extends ExTransferable.Single {
180         
181         private ActiveEditorDrop drop;
182
183         ActiveEditorDropTransferable(ActiveEditorDrop drop) {
184             super(ActiveEditorDrop.FLAVOR);
185             
186             this.drop = drop;
187         }
188                
189         public Object JavaDoc getData () {
190             return drop;
191         }
192         
193     }
194     
195     public String JavaDoc _getDisplayName(
196             String JavaDoc bundleName,
197             String JavaDoc displayNameKey,
198             String JavaDoc instanceName)
199     {
200
201         String JavaDoc displayName = null;
202         try {
203             displayName = NbBundle.getBundle(bundleName).getString(displayNameKey);
204
205             if (displayName == null && displayNameKey != null)
206                 displayName = displayNameKey;
207
208             if (displayName == null) {//derive name from the instance name
209
if (instanceName != null && instanceName.trim().length() > 0) {
210                     int dotIndex = instanceName.lastIndexOf('.'); // NOI18N
211
displayName = instanceName.substring(dotIndex);
212                 }
213             }
214
215             if (displayName == null) // no name derived from the item
216
displayName = name;
217
218         }
219         catch (Exception JavaDoc ex) {
220             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
221         }
222
223         return (displayName == null ? "" : displayName);
224     }
225
226     public String JavaDoc _getShortDescription(
227             String JavaDoc bundleName,
228             String JavaDoc tooltipKey,
229             String JavaDoc instanceName,
230             String JavaDoc displayNameKey)
231     {
232
233         String JavaDoc tooltip = null;
234         try {
235             tooltip = NbBundle.getBundle(bundleName).getString(tooltipKey);
236
237             if (tooltip == null && tooltipKey != null)
238                 tooltip = tooltipKey;
239
240             if (tooltip == null) {//derive name from instance name
241
if (instanceName != null && instanceName.trim().length() > 0) {
242                     int dotIndex = instanceName.indexOf('.'); // NOI18N
243
tooltip = instanceName.substring(0, dotIndex).replace('-', '.'); // NOI18N
244
}
245             }
246
247             if (tooltip == null) // no tooltip derived from the item
248
tooltip = _getDisplayName(bundleName, displayNameKey, instanceName);
249
250         }
251         catch (Exception JavaDoc ex) {
252             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
253         }
254
255         return (tooltip == null ? "" : tooltip);
256     }
257
258     public Image JavaDoc _getIcon(String JavaDoc iconURL) {
259
260         Image JavaDoc icon = null;
261         try {
262             icon = Utilities.loadImage(iconURL);
263         }
264         catch (Exception JavaDoc ex) {
265             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
266         }
267
268         return icon;
269     }
270     
271     public HelpCtx getHelpCtx() {
272         DataNode dn = (DataNode) getOriginal();
273         Object JavaDoc helpId = dn.getDataObject().getPrimaryFile().getAttribute("helpId"); //NOI18N
274
return (helpId == null ? super.getHelpCtx() : new HelpCtx(helpId.toString()));
275     }
276     
277     /**
278      * Transferable wrapper that does not allow DataFlavors for external drag and drop
279      * (FileListFlavor and URI list flavors)
280      */

281     private static class NoExternalDndTransferable implements Transferable JavaDoc {
282         private Transferable JavaDoc t;
283         private DataFlavor JavaDoc uriListFlavor;
284         public NoExternalDndTransferable( Transferable JavaDoc t ) {
285             this.t = t;
286         }
287     
288         public DataFlavor JavaDoc[] getTransferDataFlavors() {
289             DataFlavor JavaDoc[] flavors = t.getTransferDataFlavors();
290             if( t.isDataFlavorSupported( DataFlavor.javaFileListFlavor )
291                 || t.isDataFlavorSupported( getUriListFlavor() ) ) {
292                 ArrayList JavaDoc<DataFlavor JavaDoc> tmp = new ArrayList JavaDoc<DataFlavor JavaDoc>( flavors.length );
293                 for( int i=0; i<flavors.length; i++ ) {
294                     if( isDataFlavorSupported( flavors[i] ) )
295                         tmp.add( flavors[i] );
296                 }
297                 flavors = tmp.toArray( new DataFlavor JavaDoc[tmp.size()] );
298             }
299             return flavors;
300         }
301
302         public boolean isDataFlavorSupported( DataFlavor JavaDoc flavor ) {
303             if( DataFlavor.javaFileListFlavor.equals( flavor ) || getUriListFlavor().equals( flavor ) )
304                 return false;
305             return t.isDataFlavorSupported(flavor);
306         }
307
308         public Object JavaDoc getTransferData( DataFlavor JavaDoc flavor ) throws UnsupportedFlavorException JavaDoc, IOException JavaDoc {
309             if( !isDataFlavorSupported(flavor) )
310                 throw new UnsupportedFlavorException JavaDoc( flavor );
311             return t.getTransferData( flavor );
312         }
313         
314         private DataFlavor JavaDoc getUriListFlavor () {
315             if( null == uriListFlavor ) {
316                 try {
317                     uriListFlavor = new DataFlavor JavaDoc("text/uri-list;class=java.lang.String");
318                 } catch (ClassNotFoundException JavaDoc ex) {
319                     //cannot happen
320
throw new AssertionError JavaDoc(ex);
321                 }
322             }
323             return uriListFlavor;
324         }
325     }
326 }
327
Popular Tags