KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28 import java.awt.datatransfer.Transferable JavaDoc;
29 import java.awt.datatransfer.StringSelection JavaDoc;
30
31 import javax.swing.JPanel JavaDoc;
32
33
34 import org.openide.nodes.*;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.Lookup;
37 import org.openide.util.actions.SystemAction;
38 import org.openide.util.actions.CallableSystemAction;
39 import org.openide.util.datatransfer.NewType;
40 import org.openide.util.datatransfer.PasteType;
41
42 import org.netbeans.spi.looks.ProxyLook;
43 import org.netbeans.api.nodes2looks.LookNodeValuesTest;
44
45 /** Class for comparing desirable data against real results. It also contains
46  * some testing values which can be used as test values.
47  */

48 public class GoldenValue {
49     
50     long key;
51     Object JavaDoc result;
52
53     public GoldenValue(long key, Object JavaDoc result) {
54         this.key = key;
55         this.result = result;
56     }
57
58     public static boolean isOK( long key, Object JavaDoc result, GoldenValue[] items) {
59         for( int i = 0; i < items.length; i++ ) {
60             if ( key == items[i].key ) {
61                 if ( result == items[i].result ) {
62                     return true;
63                 }
64                 else if ( result == null ) {
65                     return false;
66                 }
67                 else if ( result.equals( items[i].result ) ){
68                     return true;
69                 }
70                 else if ( key == ProxyLook.GET_LOOKUP_ITEMS ) {
71                     return compareLookupItems( (Collection JavaDoc)result, (Collection JavaDoc)items[i].result );
72                 }
73                 else if ( result.getClass().isArray() &&
74                           items[i].result.getClass().isArray() &&
75                           Arrays.equals( (Object JavaDoc[])result, (Object JavaDoc[])items[i].result ) ) {
76                     return true;
77                 }
78                 else {
79                     return false;
80                 }
81             }
82         }
83
84         return false;
85     }
86     
87     public static Object JavaDoc get( long key, GoldenValue[] items) {
88         for( int i = 0; i < items.length; i++ ) {
89             if ( key == items[i].key ) {
90                 return items[i].result;
91             }
92         }
93
94         return null;
95     }
96     
97     // Private methods ---------------------------------------------------------
98

99     private static boolean compareLookupItems( Collection JavaDoc c1, Collection JavaDoc c2 ) {
100         if ( c1.size() != c2.size() ) {
101             return false;
102         }
103         
104         Iterator JavaDoc it1 = c1.iterator();
105         Iterator JavaDoc it2 = c2.iterator();
106         
107         while( it1.hasNext() ) {
108             Lookup.Item li1 = (Lookup.Item)it1.next();
109             Lookup.Item li2 = (Lookup.Item)it2.next();
110             
111             if ( !li1.getDisplayName().equals( li2.getDisplayName() ) ) {
112                 return false;
113             }
114             
115             if ( !li1.getId().equals( li2.getId() ) ) {
116                 return false;
117             }
118             
119             if ( !li1.getInstance().equals( li2.getInstance() ) ) {
120                 return false;
121             }
122             
123             if ( !li1.getType().equals( li2.getType() ) ) {
124                 return false;
125             }
126             
127         }
128         
129         return true;
130     }
131
132     // This method can be used in other tests as well.
133
public static GoldenValue[] createGoldenValues() {
134
135         return new GoldenValue[] {
136             new GoldenValue( ProxyLook.GET_DISPLAY_NAME,
137                 "DisplayName" ),
138
139             new GoldenValue( ProxyLook.GET_NAME,
140                 "Name" ),
141
142             new GoldenValue( ProxyLook.GET_SHORT_DESCRIPTION,
143                 "ShortDescription" ),
144
145             new GoldenValue( ProxyLook.GET_ICON,
146                 new BufferedImage JavaDoc( 16, 16, BufferedImage.TYPE_INT_RGB ) ),
147
148             new GoldenValue( ProxyLook.GET_OPENED_ICON,
149                 new BufferedImage JavaDoc( 16, 16, BufferedImage.TYPE_INT_RGB ) ),
150
151             new GoldenValue( ProxyLook.GET_HELP_CTX,
152                 new HelpCtx( LookNodeValuesTest.class ) ),
153
154             new GoldenValue( ProxyLook.GET_CHILD_OBJECTS,
155                 Arrays.asList( new String JavaDoc[] {
156                     "Child 1",
157                     "Child 2"
158                 }) ),
159
160             new GoldenValue( ProxyLook.GET_NEW_TYPES,
161                 new NewType[] {
162                     new NewType() {
163                         public void create() { }
164                     },
165
166                     new NewType() {
167                         public void create() { }
168                     }
169                 } ),
170
171             new GoldenValue( ProxyLook.GET_ACTIONS,
172                 new SystemAction[] {
173                     (SystemAction)org.openide.util.SharedClassObject.findObject( TestingAction1.class ),
174                     (SystemAction)org.openide.util.SharedClassObject.findObject( TestingAction2.class )
175                 } ),
176
177             new GoldenValue( ProxyLook.GET_CONTEXT_ACTIONS,
178                 new SystemAction[] {
179                     (SystemAction)org.openide.util.SharedClassObject.findObject( TestingAction3.class ),
180                     (SystemAction)org.openide.util.SharedClassObject.findObject( TestingAction4.class )
181                 } ),
182
183             new GoldenValue( ProxyLook.GET_DEFAULT_ACTION,
184                 SystemAction.get( TestingAction5.class ) ),
185
186             new GoldenValue( ProxyLook.GET_PROPERTY_SETS,
187                 new Node.PropertySet[] {
188                     new Sheet.Set(),
189                     new Sheet.Set()
190                 }),
191
192             new GoldenValue( ProxyLook.GET_CUSTOMIZER,
193                 new JPanel JavaDoc() ),
194
195             new GoldenValue( ProxyLook.HAS_CUSTOMIZER,
196                 Boolean.TRUE ),
197
198             new GoldenValue( ProxyLook.CAN_RENAME,
199                 Boolean.TRUE ),
200
201             new GoldenValue( ProxyLook.CAN_DESTROY,
202                 Boolean.TRUE ),
203
204             new GoldenValue( ProxyLook.CAN_COPY,
205                 Boolean.TRUE ),
206
207             new GoldenValue( ProxyLook.CAN_CUT,
208                 Boolean.TRUE ),
209
210             new GoldenValue( ProxyLook.GET_PASTE_TYPES,
211                 new PasteType[] {
212                     new PasteType() {
213                         public Transferable JavaDoc paste() { return null; }
214                     },
215
216                     new PasteType() {
217                         public Transferable JavaDoc paste() { return null; }
218                     }
219                 }),
220
221             new GoldenValue( ProxyLook.GET_DROP_TYPE,
222                 new PasteType() {
223                     public Transferable JavaDoc paste() { return null; }
224                 }),
225
226             new GoldenValue( ProxyLook.CLIPBOARD_COPY,
227                 new StringSelection JavaDoc( "ClipboardCopy" ) ),
228
229             new GoldenValue( ProxyLook.CLIPBOARD_CUT,
230                 new StringSelection JavaDoc( "ClipboardCut" ) ),
231
232             new GoldenValue( ProxyLook.DRAG,
233                 new StringSelection JavaDoc( "Drag" ) ),
234
235             new GoldenValue( ProxyLook.GET_LOOKUP_ITEMS,
236                 createGoldenLookupItems() )
237
238
239         };
240     }
241
242     public static Collection JavaDoc createGoldenLookupItems() {
243
244         ArrayList JavaDoc lookupItems = new ArrayList JavaDoc();
245
246         lookupItems.add(
247             new TestLookupItem(
248                 new org.openide.cookies.SaveCookie() {
249                     public void save() {}
250                     
251                     public String JavaDoc toString() {
252                         return "Tetst SaveCookie";
253                     }
254                 }
255             )
256         );
257
258         lookupItems.add(
259             new TestLookupItem(
260                 new org.openide.cookies.CloseCookie() {
261                     public boolean close() { return false; }
262                     
263                     public String JavaDoc toString() {
264                         return "Tetst CloseCookie";
265                     }
266                 }
267             )
268         );
269
270         lookupItems.add( new TestLookupItem( "HoHo" ) );
271
272         return lookupItems;
273     }
274     
275     // Sample data -------------------------------------------------------------
276

277     public static class TestingAction extends CallableSystemAction {
278
279         public HelpCtx getHelpCtx() {
280             return null;
281         }
282         
283         public String JavaDoc getName() {
284             return this.getClass().getName();
285         }
286         
287         public void performAction() {
288         }
289         
290     }
291     
292     public static class TestingAction1 extends TestingAction {
293     }
294     
295     public static class TestingAction2 extends TestingAction {
296     }
297     
298     public static class TestingAction3 extends TestingAction {
299     }
300     
301     public static class TestingAction4 extends TestingAction {
302     }
303     
304     public static class TestingAction5 extends TestingAction {
305     }
306     
307     public static class TestingAction6 extends TestingAction {
308     }
309
310     public static class TestLookupItem extends Lookup.Item {
311
312         private String JavaDoc id;
313         private Object JavaDoc instance;
314
315         public TestLookupItem( Object JavaDoc instance ) {
316             this( instance, null );
317         }
318
319         public TestLookupItem( Object JavaDoc instance, String JavaDoc id ) {
320             this.id = id;
321             this.instance = instance;
322         }
323
324         public String JavaDoc getDisplayName() {
325             return getId();
326         }
327
328         public String JavaDoc getId() {
329             return id == null ? instance.toString() : id;
330         }
331
332         public Object JavaDoc getInstance() {
333             return instance;
334         }
335
336         public Class JavaDoc getType() {
337             return instance.getClass();
338         }
339         
340         public boolean equals( Object JavaDoc object ) {
341             if ( object instanceof Lookup.Item ) {
342                 return instance == ((Lookup.Item)object).getInstance();
343             }
344             return false;
345         }
346         
347         public int hashCode() {
348             return instance.hashCode();
349         }
350
351     }
352
353 }
354  
Popular Tags