KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > NodeProxyLook


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.api.nodes2looks;
21
22 import java.awt.Component JavaDoc;
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.util.Arrays JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import org.netbeans.spi.looks.Look;
32 import org.openide.nodes.Node;
33 import org.openide.nodes.NodeEvent;
34 import org.openide.nodes.NodeListener;
35 import org.openide.nodes.NodeMemberEvent;
36 import org.openide.nodes.NodeReorderEvent;
37 import org.openide.util.HelpCtx;
38 import org.openide.util.NbBundle;
39 import org.openide.util.Lookup;
40 import org.openide.util.datatransfer.NewType;
41 import org.openide.util.datatransfer.PasteType;
42
43 /**
44  * Utility Class which allows to use LookNodes on existing Nodes.
45  * The class assumes that represented object is a Node. If there is
46  * Node.Interior representing other object than a ndoe the look delegates
47  * to {DefaultLook#INSTANCE} i.e. returns neutral values.<BR>
48  * Recomended usage of this class is to be used as delegatee.
49  * <CODE>
50  * public class MyNodeLook extends AcceptorLook {
51  *
52  * public MyNodeLook() {
53  * super( new NodeProxyLook(), MyNode.Class );
54  * // set mask or do other initializations
55  * }
56  * }
57  * </CODE>
58  *
59  * @author Petr Hrebejk
60  */

61 final class NodeProxyLook extends Look {
62
63     private static final Lookup.Template TEMPLATE_ALL =
64         new Lookup.Template( Object JavaDoc.class );
65
66     private NodeEventTranslator eventTranslator = new NodeEventTranslator();
67
68     /** Creates new NodeProxySupport */
69     public NodeProxyLook( String JavaDoc name ) {
70         super( name );
71         // nodeListener = new NodeEventTranslator( this );
72
}
73
74     // Methods of look itself --------------------------------------------------
75

76     /** Specifies the node to which the Look delegates. The default
77      * implementation returns <CODE>substitute.getRepresentedObject()</CODE>
78      * casted to Node in case the represented object is a Node in other cases
79      * it returns null. If you override this method you can provide delegation
80      * nodes for different types of object. (E.g. by calling
81      * dataObject.getNodeDelegate() ).
82      */

83     protected Node getNodeDelegate( Object JavaDoc representedObject ) {
84
85         if ( representedObject instanceof Node ) {
86             return (Node)representedObject;
87         }
88         else {
89             return null;
90         }
91     }
92
93     // General methods ---------------------------------------------------------
94

95     public void attachTo(Object JavaDoc representedObject) {
96
97         if ( representedObject instanceof Node ) {
98             Node dn = (Node)representedObject;
99             // Register listener to the node to pass events forward
100
dn.addNodeListener( eventTranslator );
101             dn.addPropertyChangeListener( eventTranslator.propertyChangeListener );
102         }
103         else {
104             throw new IllegalArgumentException JavaDoc( "Represented object has to be a node" );
105         }
106     }
107
108
109     public void detachFrom( Object JavaDoc representedObject ) {
110         if ( representedObject instanceof Node ) {
111             Node dn = (Node)representedObject;
112             // Register listener to the node to pass events forward
113
dn.removeNodeListener( eventTranslator );
114             dn.removePropertyChangeListener( eventTranslator.propertyChangeListener );
115         }
116     }
117
118     // Methods for FUNCTIONALITY EXTENSIONS ------------------------------------
119

120     public Collection JavaDoc getLookupItems(Object JavaDoc representedObject, Lookup oldEnv ) {
121         if ( representedObject instanceof Node ) {
122             Node dn = (Node)representedObject;
123             Lookup lookup = dn.getLookup();
124             Lookup.Result result = lookup.lookup( TEMPLATE_ALL );
125             return result.allItems();
126         }
127         else {
128             throw new IllegalArgumentException JavaDoc( "Represented object has to be a node: " + representedObject );
129         }
130     }
131
132     // Methods for STYLE -------------------------------------------------------
133

134     public String JavaDoc getDisplayName( Object JavaDoc representedObject, Lookup env ) {
135         Node dn = getNodeDelegate( representedObject );
136         if (dn == null) {
137             return null;
138         } else {
139             return dn.getDisplayName();
140         }
141     }
142
143     public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
144         Node dn = getNodeDelegate( representedObject );
145         if (dn == null) {
146             return null;
147         } else {
148             return dn.getName();
149         }
150     }
151
152     public void rename( Object JavaDoc representedObject, String JavaDoc newName, Lookup env ) {
153         Node dn = getNodeDelegate( representedObject );
154         if (dn != null) {
155             dn.setName( newName );
156         }
157     }
158
159     public String JavaDoc getShortDescription ( Object JavaDoc representedObject, Lookup env ) {
160         Node dn = getNodeDelegate( representedObject );
161         if (dn == null) {
162             return null;
163         } else {
164             return dn.getShortDescription();
165         }
166     }
167
168     public java.awt.Image JavaDoc getIcon( Object JavaDoc representedObject, int type, Lookup env ) {
169         Node dn = getNodeDelegate( representedObject );
170         if (dn == null) {
171             return null;
172         } else {
173             return dn.getIcon( type );
174         }
175     }
176
177     public java.awt.Image JavaDoc getOpenedIcon( Object JavaDoc representedObject, int type, Lookup env ) {
178         Node dn = getNodeDelegate( representedObject );
179         if (dn == null) {
180             return null;
181         } else {
182             return dn.getOpenedIcon( type );
183         }
184     }
185
186     public HelpCtx getHelpCtx( Object JavaDoc representedObject, Lookup env ) {
187         Node dn = getNodeDelegate( representedObject );
188         if (dn == null) {
189             return null;
190         } else {
191             return dn.getHelpCtx();
192         }
193     }
194
195     // Methods for CHILDREN ----------------------------------------------------
196

197     public List JavaDoc getChildObjects( Object JavaDoc representedObject, Lookup env ) {
198         Node dn = getNodeDelegate( representedObject );
199         if (dn == null) {
200             return null;
201         } else {
202            return Arrays.asList( dn.getChildren().getNodes() );
203         }
204     }
205
206     public boolean isLeaf( Object JavaDoc representedObject, Lookup env ) {
207         Node dn = getNodeDelegate( representedObject );
208         if (dn == null) {
209             return true;
210         } else {
211            return dn.isLeaf();
212         }
213     }
214
215     // Methods for ACTIONS & NEW TYPES -----------------------------------------
216

217     public NewType[] getNewTypes( Object JavaDoc representedObject, Lookup env ) {
218         Node dn = getNodeDelegate( representedObject );
219         if ( dn == null ) {
220             return null;
221         }
222         else {
223             return dn.getNewTypes();
224         }
225     }
226
227     public Action JavaDoc[] getActions(Object JavaDoc representedObject, Lookup env ) {
228         Node dn = getNodeDelegate( representedObject );
229         if ( dn == null ) {
230             return null;
231         }
232         else {
233             return dn.getActions(false);
234         }
235     }
236
237     public Action JavaDoc[] getContextActions(Object JavaDoc representedObject, Lookup env ) {
238         Node dn = getNodeDelegate( representedObject );
239         if ( dn == null ) {
240             return null;
241         }
242         else {
243             return dn.getActions(true);
244         }
245     }
246
247     public Action JavaDoc getDefaultAction(Object JavaDoc representedObject, Lookup env ) {
248         Node dn = getNodeDelegate( representedObject );
249         if ( dn == null ) {
250             return null;
251         }
252         else {
253             return dn.getDefaultAction();
254         }
255     }
256
257     // Methods for PROPERTIES AND CUSTOMIZER -----------------------------------
258

259     public Node.PropertySet[] getPropertySets( Object JavaDoc representedObject, Lookup env ) {
260         Node dn = getNodeDelegate( representedObject );
261         if ( dn == null ) {
262             return null;
263         }
264         else {
265             return dn.getPropertySets();
266         }
267     }
268
269     public Component JavaDoc getCustomizer(Object JavaDoc representedObject, Lookup env ) {
270         Node dn = getNodeDelegate( representedObject );
271         if ( dn == null || !dn.hasCustomizer() ) {
272             return null;
273         }
274         else {
275             return dn.getCustomizer();
276         }
277     }
278
279     public boolean hasCustomizer( Object JavaDoc representedObject, Lookup env ) {
280         Node dn = getNodeDelegate( representedObject );
281         return dn != null && dn.hasCustomizer ();
282     }
283
284     // Methods for CLIPBOARD OPERATIONS ----------------------------------------
285

286     public boolean canRename( Object JavaDoc representedObject, Lookup env ) {
287         Node dn = getNodeDelegate( representedObject );
288         if ( dn == null ) {
289             return false;
290         }
291         else {
292             return dn.canRename();
293         }
294     }
295
296     public boolean canDestroy( Object JavaDoc representedObject, Lookup env ) {
297         Node dn = getNodeDelegate( representedObject );
298         if ( dn == null ) {
299             return false;
300         }
301         else {
302             return dn.canDestroy();
303         }
304     }
305
306     public boolean canCopy( Object JavaDoc representedObject, Lookup env ) {
307         Node dn = getNodeDelegate( representedObject );
308         if ( dn == null ) {
309             return false;
310         }
311         else {
312             return dn.canCopy();
313         }
314     }
315
316     public boolean canCut( Object JavaDoc representedObject, Lookup env ) {
317         Node dn = getNodeDelegate( representedObject );
318         if ( dn == null ) {
319             return false;
320         }
321         else {
322             return dn.canCut();
323         }
324     }
325
326     public PasteType[] getPasteTypes( Object JavaDoc representedObject, Transferable JavaDoc t, Lookup env ) {
327         Node dn = getNodeDelegate( representedObject );
328         if ( dn == null ) {
329             return null;
330         }
331         else {
332             return dn.getPasteTypes( t );
333         }
334     }
335
336     public PasteType getDropType( Object JavaDoc representedObject, Transferable JavaDoc t, int action, int index, Lookup env ) {
337         Node dn = getNodeDelegate( representedObject );
338         if ( dn == null ) {
339             return null;
340         }
341         else {
342             return dn.getDropType( t, action, index );
343         }
344     }
345
346     public Transferable JavaDoc clipboardCopy( Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
347         Node dn = getNodeDelegate( representedObject );
348         if ( dn == null ) {
349             return null;
350         }
351         else {
352             return dn.clipboardCopy();
353         }
354     }
355
356     public Transferable JavaDoc clipboardCut( Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
357         Node dn = getNodeDelegate( representedObject );
358         if ( dn == null ) {
359             return null;
360         }
361         else {
362             return dn.clipboardCut();
363         }
364     }
365
366     public Transferable JavaDoc drag( Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
367         Node dn = getNodeDelegate( representedObject );
368         if ( dn == null ) {
369             return null;
370         }
371         else {
372             return dn.drag();
373         }
374     }
375
376     public void destroy( Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
377         Node dn = getNodeDelegate( representedObject );
378         if ( dn != null ) {
379             dn.destroy();
380         }
381     }
382
383     /** The human presentable name of the look.
384      * @return human presentable name
385      */

386     public String JavaDoc getDisplayName() {
387         return NbBundle.getMessage (NodeProxyLook.class, "LAB_NodeProxyLook"); // NOI18N
388
}
389
390
391     // Innerclasses ------------------------------------------------------------
392

393     private class NodeEventTranslator implements NodeListener {
394
395         private NodePropertyChangeTranslator propertyChangeListener = new NodePropertyChangeTranslator();
396
397         /*
398         public void unregister( Object representedObject ) {
399             ((Node)representedObject).removeNodeListener( this );
400             ((Node)representedObject).removePropertyChangeListener( propertyChangeListener );
401         }
402         */

403
404         public void childrenAdded( NodeMemberEvent e ) {
405             fireChange( e.getSource(), Look.GET_CHILD_OBJECTS );
406         }
407
408         public void childrenRemoved( NodeMemberEvent e ) {
409             fireChange( e.getSource(), Look.GET_CHILD_OBJECTS );
410         }
411
412         public void childrenReordered( NodeReorderEvent e ) {
413             fireChange( e.getSource(), Look.GET_CHILD_OBJECTS );
414         }
415
416         public void nodeDestroyed( NodeEvent e ) {
417             fireChange( e.getSource(), Look.DESTROY );
418         }
419
420         public void propertyChange( PropertyChangeEvent JavaDoc e ) {
421
422             if ( Node.PROP_NAME.equals( e.getPropertyName() ) ) {
423                 fireChange( e.getSource(), Look.GET_NAME );
424             }
425             else if ( Node.PROP_DISPLAY_NAME.equals( e.getPropertyName() ) ) {
426                 fireChange( e.getSource(), Look.GET_DISPLAY_NAME );
427             }
428             else if ( Node.PROP_SHORT_DESCRIPTION.equals( e.getPropertyName() ) ) {
429                 fireChange( e.getSource(), Look.GET_SHORT_DESCRIPTION );
430             }
431             else if ( Node.PROP_ICON.equals( e.getPropertyName() ) ) {
432                 fireChange( e.getSource(), Look.GET_ICON );
433             }
434             else if ( Node.PROP_OPENED_ICON.equals( e.getPropertyName() ) ) {
435                 fireChange( e.getSource(), Look.GET_OPENED_ICON );
436             }
437             // else if ( Node.PROP_PARENT_NODE.equals( e.getPropertyName() ) ) {
438
// property
439
// }
440
else if ( Node.PROP_PROPERTY_SETS.equals( e.getPropertyName() ) ) {
441                 fireChange( e.getSource(), Look.GET_PROPERTY_SETS );
442             }
443             else if ( Node.PROP_LEAF.equals( e.getPropertyName() ) ) {
444                 fireChange( e.getSource(), GET_CHILD_OBJECTS );
445             }
446             else if ( Node.PROP_COOKIE.equals( e.getPropertyName() ) ) {
447                 fireChange( e.getSource(), GET_LOOKUP_ITEMS );
448             }
449         }
450
451         private class NodePropertyChangeTranslator implements PropertyChangeListener JavaDoc {
452
453
454             public void propertyChange( PropertyChangeEvent JavaDoc e ) {
455                 firePropertyChange( e.getSource(), e.getPropertyName() );
456             }
457         }
458     }
459
460 }
461
Popular Tags