KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > looks > SampleLook


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.spi.looks;
21
22 import java.beans.*;
23 import java.util.List JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import java.awt.Component JavaDoc;
29
30 import org.openide.nodes.*;
31 import org.openide.util.HelpCtx;
32 import org.openide.util.datatransfer.NewType;
33 import org.openide.util.datatransfer.PasteType;
34 import org.openide.util.Lookup;
35 import org.netbeans.spi.looks.*;
36
37 /** Class used for testing. It takes the values from the SampleRepObject.
38  *
39  *
40  * @author Petr Hrebejk
41  */

42 public class SampleLook extends Look {
43             
44
45     private SampleListener listener = new SampleListener();
46     
47     private GoldenValue goldenValues[] = null;
48
49     public SampleLook( String JavaDoc name ) {
50         super( name );
51     }
52
53     public SampleLook( String JavaDoc name, GoldenValue[] goldenValues ) {
54         this( name );
55         this.goldenValues = goldenValues;
56     }
57         
58     // Methods for the look itself ---------------------------------------------
59

60     /** The human presentable name of the look.
61      * @return human presentable name
62      */

63     public String JavaDoc getDisplayName() {
64         return getName() + " - DisplayName";
65     }
66     
67     public void attachTo(Object JavaDoc representedObject) {
68         
69         Lookup lookup = null;
70         
71         if ( representedObject instanceof SampleRepObject ) {
72             // Remember that attachTo was called
73
((SampleRepObject)representedObject).attach();
74          
75             
76             ((SampleRepObject)representedObject).addPropertyChangeListener( listener );
77         }
78         else if ( goldenValues != null ) {
79             lookup = (Lookup)GoldenValue.get( ProxyLook.GET_LOOKUP_ITEMS, goldenValues );
80         }
81         
82     }
83     
84     public void detachFrom( Object JavaDoc representedObject ) {
85         if ( representedObject instanceof SampleRepObject ) {
86             // Remember that attachTo was called
87
((SampleRepObject)representedObject).detach(); // Mark that detach was called
88
((SampleRepObject)representedObject).removePropertyChangeListener( listener ); // Remove the listener
89
}
90     }
91     
92     // Methods for FUNCTIONALITY EXTENSIONS ------------------------------------
93

94     public Collection JavaDoc getLookupItems(Object JavaDoc representedObject, Lookup oldEnv ) {
95         if ( goldenValues != null ) {
96             return (Collection JavaDoc)GoldenValue.get( ProxyLook.GET_LOOKUP_ITEMS, goldenValues );
97         }
98         else {
99             return (Collection JavaDoc)((SampleRepObject)representedObject).getValue( ProxyLook.GET_LOOKUP_ITEMS );
100         }
101     }
102     
103     
104     // Methods for STYLE -------------------------------------------------------
105

106     public String JavaDoc getDisplayName(Object JavaDoc representedObject, Lookup env ) {
107         return (String JavaDoc)get( ProxyLook.GET_DISPLAY_NAME, representedObject );
108     }
109     
110     public String JavaDoc getName(Object JavaDoc representedObject, Lookup env ) {
111         return (String JavaDoc)get( ProxyLook.GET_NAME, representedObject );
112     }
113
114
115     public void rename(Object JavaDoc representedObject, String JavaDoc newName, Lookup env ) {
116         if ( representedObject instanceof SampleRepObject ) {
117             ((SampleRepObject)representedObject).setName();
118         }
119     }
120     
121     public String JavaDoc getShortDescription(Object JavaDoc representedObject, Lookup env ) {
122         return (String JavaDoc)get( ProxyLook.GET_SHORT_DESCRIPTION, representedObject );
123     }
124     
125     public java.awt.Image JavaDoc getIcon(Object JavaDoc representedObject, int type, Lookup env ) {
126         return (java.awt.Image JavaDoc)get( ProxyLook.GET_ICON, representedObject );
127     }
128     
129     public java.awt.Image JavaDoc getOpenedIcon(Object JavaDoc representedObject, int type, Lookup env ) {
130         return (java.awt.Image JavaDoc)get( ProxyLook.GET_OPENED_ICON, representedObject );
131     }
132     
133     public HelpCtx getHelpCtx(Object JavaDoc representedObject, Lookup env ) {
134         return (HelpCtx)get( ProxyLook.GET_HELP_CTX, representedObject );
135     }
136     
137     // Methods for CHILDREN ----------------------------------------------------
138

139     public List JavaDoc getChildObjects(Object JavaDoc representedObject, Lookup env ) {
140         return (List JavaDoc)get( ProxyLook.GET_CHILD_OBJECTS, representedObject );
141     }
142     
143     public boolean isLeaf(Object JavaDoc representedObject, Lookup env ) {
144         return get( ProxyLook.GET_CHILD_OBJECTS, representedObject ) == null;
145     }
146         
147     // Methods for ACTIONS & NEW TYPES -----------------------------------------
148

149     public NewType[] getNewTypes(Object JavaDoc representedObject, Lookup env ) {
150         return (NewType[])get( ProxyLook.GET_NEW_TYPES, representedObject );
151     }
152     
153     public Action JavaDoc[] getActions(Object JavaDoc representedObject, Lookup env ) {
154         return (Action JavaDoc[])get( ProxyLook.GET_ACTIONS, representedObject );
155     }
156     
157     public Action JavaDoc[] getContextActions(Object JavaDoc representedObject, Lookup env ) {
158         return (Action JavaDoc[])get( ProxyLook.GET_CONTEXT_ACTIONS, representedObject );
159     }
160     
161     public Action JavaDoc getDefaultAction(Object JavaDoc representedObject, Lookup env ) {
162         return (Action JavaDoc)get( ProxyLook.GET_DEFAULT_ACTION, representedObject );
163     }
164     
165     // Methods for PROPERTIES AND CUSTOMIZER -----------------------------------
166

167     public Node.PropertySet[] getPropertySets(Object JavaDoc representedObject, Lookup env ) {
168         return (Node.PropertySet[])get( ProxyLook.GET_PROPERTY_SETS, representedObject );
169     }
170     
171     public Component JavaDoc getCustomizer(Object JavaDoc representedObject, Lookup env ) {
172         return (java.awt.Component JavaDoc)get( ProxyLook.GET_CUSTOMIZER, representedObject );
173     }
174     
175     public boolean hasCustomizer(Object JavaDoc representedObject, Lookup env ) {
176         return ((Boolean JavaDoc)get( ProxyLook.HAS_CUSTOMIZER, representedObject )).booleanValue();
177     }
178     
179     // Methods for CLIPBOARD OPERATIONS ----------------------------------------
180

181     public boolean canRename(Object JavaDoc representedObject, Lookup env ) {
182         return ((Boolean JavaDoc)get( ProxyLook.CAN_RENAME, representedObject )).booleanValue();
183     }
184     
185     public boolean canDestroy(Object JavaDoc representedObject, Lookup env ) {
186         return ((Boolean JavaDoc)get( ProxyLook.CAN_DESTROY, representedObject )).booleanValue();
187     }
188     
189     public boolean canCopy(Object JavaDoc representedObject, Lookup env ) {
190         return ((Boolean JavaDoc)get( ProxyLook.CAN_COPY, representedObject )).booleanValue();
191     }
192     
193     public boolean canCut(Object JavaDoc representedObject, Lookup env ) {
194         return ((Boolean JavaDoc)get( ProxyLook.CAN_CUT, representedObject )).booleanValue();
195     }
196     
197     public PasteType[] getPasteTypes(Object JavaDoc representedObject, Transferable JavaDoc t, Lookup env ) {
198         return (PasteType[])get( ProxyLook.GET_PASTE_TYPES, representedObject );
199     }
200     
201     public PasteType getDropType(Object JavaDoc representedObject, Transferable JavaDoc t, int action, int index, Lookup env ) {
202         return (PasteType)get( ProxyLook.GET_DROP_TYPE, representedObject );
203     }
204     
205     public Transferable JavaDoc clipboardCopy(Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
206         return (Transferable JavaDoc)get( ProxyLook.CLIPBOARD_COPY, representedObject );
207     }
208     
209     public Transferable JavaDoc clipboardCut(Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
210         return (Transferable JavaDoc)get( ProxyLook.CLIPBOARD_CUT, representedObject );
211     }
212     
213     public Transferable JavaDoc drag(Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
214         return (Transferable JavaDoc)get( ProxyLook.DRAG, representedObject );
215     }
216           
217     public void destroy(Object JavaDoc representedObject, Lookup env ) throws IOException JavaDoc {
218         if ( representedObject instanceof SampleRepObject ) {
219             ((SampleRepObject)representedObject).destroy();
220         }
221     }
222         
223     // Private/protected helper methods ----------------------------------------
224

225     /** Returns the value by key. Most of the keys are those used in ProxyLook
226      * to mark the methods, but there are some additional to test attachTo(),
227      * rename() and destroy().
228      */

229     protected Object JavaDoc get( long key, Object JavaDoc representedObject ) {
230         
231         if ( goldenValues != null ) {
232             return GoldenValue.get( key, goldenValues );
233         }
234         
235         /*
236         if ( substitute instanceof Look.NodeSubstitute ) {
237             substitute = ((Look.NodeSubstitute)substitute).getRepresentedObject();
238         }
239         */

240         if ( representedObject instanceof SampleRepObject ) {
241             return ((SampleRepObject)representedObject).getValue( key );
242         }
243         else {
244             return null;
245         }
246     }
247         
248     // Innerclasses ------------------------------------------------------------
249

250     public class SampleListener implements PropertyChangeListener {
251             
252         // Kind of strange translation of events
253

254         public void propertyChange(PropertyChangeEvent evt) {
255             
256             try {
257                 long ev = Long.parseLong( evt.getPropertyName() );
258
259                 fireChange( evt.getSource(), ev );
260             }
261             catch ( NumberFormatException JavaDoc e ) {
262                 if ( SampleRepObject.DESTROY.equals( evt.getPropertyName() ) ) {
263                     fireChange( evt.getSource(), Look.DESTROY );
264                 }
265                 else {
266                     firePropertyChange( evt.getSource(), evt.getPropertyName() );
267                 }
268             }
269         }
270         
271     }
272     
273     
274 }
275
Popular Tags