KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > ComboTest


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 /*
21  * PropertySheetTest.java
22  *
23  * Created on August 24, 2001, 4:25 PM
24  */

25
26 package org.openide.explorer.propertysheet;
27
28 import java.awt.Color JavaDoc;
29 import java.awt.Component JavaDoc;
30 import java.awt.DisplayMode JavaDoc;
31 import java.awt.Font JavaDoc;
32 import java.awt.Graphics JavaDoc;
33 import java.awt.GraphicsEnvironment JavaDoc;
34 import java.awt.Image JavaDoc;
35 import java.awt.KeyboardFocusManager JavaDoc;
36 import java.awt.Point JavaDoc;
37 import java.awt.Rectangle JavaDoc;
38 import java.awt.event.KeyEvent JavaDoc;
39 import java.awt.event.MouseEvent JavaDoc;
40 import java.awt.event.WindowAdapter JavaDoc;
41 import java.awt.event.WindowEvent JavaDoc;
42 import java.awt.image.BufferedImage JavaDoc;
43 import org.openide.*;
44 import org.openide.nodes.*;
45 import org.openide.explorer.propertysheet.*;
46 import org.openide.explorer.propertysheet.editors.*;
47 import java.beans.*;
48 import java.lang.reflect.*;
49 import javax.swing.*;
50 import javax.swing.ImageIcon JavaDoc;
51 import junit.framework.*;
52 import junit.textui.TestRunner;
53 import org.netbeans.junit.*;
54 import org.openide.util.Lookup;
55 import org.openide.util.Utilities;
56
57
58 /** Tests property marking functionality and the ability of a Property to
59  * provide a "postSetAction" action hint, which will be run if the user
60  * successfully changes the property value.
61  */

62 public class ComboTest extends NbTestCase {
63     private static boolean setup=false;
64     
65     static {
66         registerPropertyEditors();
67     }
68     
69     public static void registerPropertyEditors () {
70         //org.netbeans.core.startup.Main.registerPropertyEditors();
71
}
72     
73     public ComboTest(String JavaDoc name) {
74         super(name);
75     }
76  
77     static SheetTable tb=null;
78     static JFrame jf=null;
79 /*
80  * This test creates a Property, Editor and Node. First test checks if initialized
81  * editor contains the same value as property. The second checks if the property
82  * value is changed if the same change will be done in the editor.
83  */

84     protected void setUp() throws Exception JavaDoc {
85         if (setup) return;
86
87         try {
88
89             tp = new TProperty("oh", true);
90             tp1 = new TProperty2("the", true);
91             postSetAction = new PostSetAction();
92             
93             tn = new TNode();
94 // PropUtils.forceRadioButtons=true;
95
final PropertySheet ps = new PropertySheet();
96             
97             //ensure no stored value in preferences:
98
ps.setCurrentNode(tn);
99             sleep();
100             ps.setSortingMode(PropertySheet.UNSORTED);
101             
102             jf = new JFrame();
103             jf.getContentPane().add (ps);
104             jf.setLocation (20,20);
105             jf.setSize (300, 400);
106             new WaitWindow(jf);
107             tb = ps.table;
108
109             SwingUtilities.invokeLater(new Runnable JavaDoc() {
110                 public void run() {
111                     try {
112                     ps.setSortingMode(ps.SORTED_BY_NAMES);
113                     }catch (Exception JavaDoc e){}
114                 }
115             });
116             
117             
118             
119         } catch (Exception JavaDoc e) {
120             e.printStackTrace();
121             fail("FAILED - Exception thrown "+e.getClass().toString());
122         } finally {
123             setup = true;
124         }
125     }
126     
127     public void tearDown() {
128         jf.hide();
129         jf.dispose();
130     }
131     
132     
133     
134     static boolean checkGraphicsEnvironment() {
135         if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) {
136             System.err.println("Cannot run test in a headless environment");
137         }
138         DisplayMode JavaDoc dm =
139             GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
140         int i = dm.getBitDepth();
141         if (i == dm.BIT_DEPTH_MULTI || i >= 16) {
142             return true;
143         }
144         return false;
145     }
146     
147     public void testFoo () throws Exception JavaDoc {
148     }
149     
150      
151     
152     private static class WaitWindow extends WindowAdapter JavaDoc {
153         boolean shown=false;
154         public WaitWindow (JFrame f) {
155             f.addWindowListener(this);
156             f.show();
157             if (!shown) {
158                 synchronized(this) {
159                     try {
160                             wait(5000);
161                     } catch (Exception JavaDoc e) {}
162                 }
163             }
164         }
165         
166         public void windowOpened(WindowEvent JavaDoc e) {
167             shown = true;
168             synchronized(this) {
169                 notifyAll();
170                 ((JFrame) e.getSource()).removeWindowListener(this);
171             }
172         }
173     }
174     
175     private static final int SLEEP_LENGTH=1000;
176     private void sleep() {
177          //useful when running interactively
178

179         try {
180 // Thread.currentThread().sleep(800);
181
//jf.getTreeLock().wait();
182
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
183                 public void run() {
184                     System.currentTimeMillis();
185                 }
186             });
187             //jf.getTreeLock().wait();
188
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
189                 public void run() {
190                     System.currentTimeMillis();
191                 }
192             });
193         } catch (Exception JavaDoc e) {
194         }
195         
196         
197     }
198     
199     
200     private static Color JavaDoc checkColor=null;
201     private Exception JavaDoc throwMe=null;
202     
203     private static int count=0;
204     /** Asserts that a pixel at a given position in an image matches a
205      * pixel in a given position in a component */

206     private synchronized void assertPixelFromImage(final Image JavaDoc i, final Component JavaDoc c, final int imageX, final int imageY, final int compX, final int compY) throws Exception JavaDoc {
207         final BufferedImage JavaDoc bi = i instanceof BufferedImage JavaDoc ? (BufferedImage JavaDoc) i : toBufferedImage(i);
208         throwMe = null;
209         sleep();
210         
211         int rgb = bi.getRGB(imageX, imageY);
212         Color JavaDoc color = new Color JavaDoc (rgb);
213        
214         
215         //uncomment the code below for diagnosing painting problems
216
//and seeing which pixel you'return really checking
217
JFrame jf = new JFrame("assertPixelFromImage " + count + " (look for the yellow line)") {
218             public void paint (Graphics JavaDoc g) {
219                 new ImageIcon JavaDoc (bi).paintIcon(this, g, 25, 25);
220                 g.setColor (Color.YELLOW);
221                 g.drawLine(imageX+20, imageY+25, imageX+25, imageY+25);
222             }
223         };
224         jf.setLocation (500,500);
225         jf.setSize (100,100);
226         jf.show();
227         
228         try {
229             assertPixel(c, color, compX, compY);
230         } catch (Exception JavaDoc e) {
231             throwMe = e;
232         }
233         if (throwMe != null) {
234             throw throwMe;
235         }
236     }
237     
238     private Exception JavaDoc throwMe2=null;
239     private synchronized void assertPixel(final Component JavaDoc c, final Color JavaDoc toMatch, final int x, final int y) throws Exception JavaDoc {
240         sleep();
241         throwMe2 = null;
242         if (true) {
243             doAssertPixel(c, toMatch, x, y);
244             return;
245         }
246         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
247             public void run() {
248                 try {
249                     doAssertPixel(c, toMatch, x, y);
250                 } catch (Exception JavaDoc e) {
251                     throwMe2 = e;
252                 }
253             }
254         });
255         if (throwMe2 != null) {
256             throw throwMe2;
257         }
258     }
259     
260     private synchronized void doAssertPixel(final Component JavaDoc c, final Color JavaDoc toMatch, final int x, final int y) throws Exception JavaDoc {
261         final BufferedImage JavaDoc bi = new BufferedImage JavaDoc (700, 700, BufferedImage.TYPE_INT_RGB);
262
263         sleep();
264         ((JComponent) c).paintAll(bi.getGraphics());
265         sleep();
266         int[] cArr = new int[3];
267         bi.getData().getPixel(x, y, cArr);
268         checkColor = new Color JavaDoc (cArr[0], cArr[1], cArr[2]);
269
270         
271         //uncomment the code below for diagnosing painting problems
272
//and seeing which pixel you'return really checking
273
JFrame jf = new JFrame("Assert pixel test " + count + " (look for the yellow line)") {
274             public void paint (Graphics JavaDoc g) {
275                 new ImageIcon JavaDoc (bi).paintIcon(this, g, 25, 25);
276                 g.setColor (Color.YELLOW);
277                 g.drawLine(x+20, y+25, x+25, y+25);
278             }
279         };
280         jf.setLocation (400,400);
281         jf.setSize (500,500);
282         jf.show();
283         count++;
284         
285         assertEquals("Color at " + x + "," + y + " does not match", toMatch, checkColor);
286                 
287     }
288
289     
290     private void clickOn (final SheetTable tb, final int row, final int col) throws Exception JavaDoc {
291         SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
292             public void run() {
293                 Rectangle JavaDoc r = tb.getCellRect(row, col, false);
294                 Point JavaDoc toClick = r.getLocation();
295                 toClick.x += 15;
296                 toClick.y +=3;
297                 MouseEvent JavaDoc me = new MouseEvent JavaDoc (tb, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
298                 tb.dispatchEvent(me);
299             }
300         });
301         sleep();
302     }
303
304     private void releaseKey (final Component JavaDoc target, final int key) throws Exception JavaDoc {
305         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
306             public void run() {
307                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc (target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key);
308                 target.dispatchEvent(ke);
309             }
310         });
311         sleep();
312     }
313     
314     private void pressKey (final Component JavaDoc target, final int key) throws Exception JavaDoc {
315         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
316             public void run() {
317                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key);
318                 target.dispatchEvent(ke);
319             }
320         });
321         sleep();
322     }
323         
324     private void typeKey (final Component JavaDoc target, final int key) throws Exception JavaDoc {
325         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
326             public void run() {
327                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc (target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key);
328                 target.dispatchEvent(ke);
329             }
330         });
331         sleep();
332     }
333     
334     //Node definition
335
public class TNode extends AbstractNode {
336         //create Node
337
public TNode() {
338             super (Children.LEAF);
339             setName("TNode"); // or, super.setName if needed
340
setDisplayName("TNode");
341         }
342         //clone existing Node
343
public Node cloneNode() {
344             return new TNode();
345         }
346         
347         public void addProp (Node.Property p) {
348             props.put(p);
349             this.firePropertyChange(PROP_PROPERTY_SETS, null, null);
350             this.firePropertySetsChange(null, null);
351         }
352         
353         Sheet sheet=null;
354         Sheet.Set props=null;
355         // Create a property sheet:
356
protected Sheet createSheet() {
357             sheet = super.createSheet();
358             // Make sure there is a "Properties" set:
359
props = sheet.get(Sheet.PROPERTIES);
360             if (props == null) {
361                 props = Sheet.createPropertiesSet();
362                 sheet.put(props);
363             }
364             props.put(tp);
365             props.put(tp1);
366             return sheet;
367         }
368         // Method firing changes
369
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
370             firePropertyChange(s,o1,o2);
371         }
372     }
373     
374     public static void main(String JavaDoc args[]) {
375         LookAndFeel lf = UIManager.getLookAndFeel();
376         TestRunner.run(suite ());
377     }
378     
379     public static Test suite() {
380         return new ComboTestSuite ();
381     }
382     
383     private static final class ComboTestSuite extends NbTestSuite {
384         public ComboTestSuite () {
385             super (ComboTest.class);
386         }
387         
388         public void run (final TestResult tr) {
389             super.run (tr);
390         }
391     }
392
393     // Property definition
394
public class TProperty extends PropertySupport {
395         private Object JavaDoc myValue = "Value";
396         // Create new Property
397
public TProperty(String JavaDoc name, boolean isWriteable) {
398             super(name, String JavaDoc.class, name, "", true, isWriteable);
399         }
400         // get property value
401
public Object JavaDoc getValue() {
402             return myValue;
403         }
404         // set property value
405
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException {
406             Object JavaDoc oldVal = myValue;
407             myValue = value;
408             tn.fireMethod(getName(), oldVal, myValue);
409         }
410         // get the property editor
411
public PropertyEditor getPropertyEditor() {
412             return new TEditor();
413         }
414         
415         public Object JavaDoc getValue(String JavaDoc key) {
416             if ("nameIcon".equals(key)) {
417                 return new NameIcon();
418             } else if ("valueIcon".equals(key)) {
419                 return new ValueIcon();
420             } else if ("postSetAction".equals(key)) {
421                 return postSetAction;
422             }
423             return super.getValue(key);
424         }
425     }
426     
427     // Editor definition
428
public class TEditor extends PropertyEditorSupport {
429         PropertyEnv env;
430         
431         // Create new TEditor
432
public TEditor() {
433         }
434         
435         /*
436          * This method is called by the IDE to pass
437          * the environment to the property editor.
438          */

439         public void attachEnv(PropertyEnv env) {
440             this.env = env;
441         }
442         
443         // Set that this Editor doesn't support custom Editor
444
public boolean supportsCustomEditor() {
445             return false;
446         }
447         
448         // Set the Property value threw the Editor
449
public void setValue(Object JavaDoc newValue) {
450             super.setValue(newValue);
451         }
452         
453         public String JavaDoc getAsText() {
454             return getValue() == null ? "null" : getValue().toString();
455         }
456     }
457     
458
459     public class TagsEditor extends PropertyEditorSupport implements ExPropertyEditor {
460         PropertyEnv env;
461         
462         public TagsEditor() {
463         }
464         
465         public String JavaDoc[] getTags() {
466             return new String JavaDoc[] {"a","b","c","d","Value"};
467         }
468         
469         public void attachEnv(PropertyEnv env) {
470             this.env = env;
471             
472             env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE);
473         }
474         
475         public boolean supportsCustomEditor() {
476             return false;
477         }
478         
479         public void setValue(Object JavaDoc newValue) {
480             super.setValue(newValue);
481         }
482     }
483     
484     // Property definition
485
public class TProperty2 extends PropertySupport {
486         private Object JavaDoc myValue = "Value";
487         // Create new Property
488
public TProperty2(String JavaDoc name, boolean isWriteable) {
489             super(name, Object JavaDoc.class, name, "", true, isWriteable);
490         }
491         // get property value
492
public Object JavaDoc getValue() {
493             return myValue;
494         }
495         // set property value
496
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException {
497             Object JavaDoc oldVal = myValue;
498             myValue = value;
499             tn.fireMethod(getName(), oldVal, myValue);
500         }
501         // get the property editor
502
public PropertyEditor getPropertyEditor() {
503             return new TagsEditor();
504         }
505         
506         public Object JavaDoc getValue(String JavaDoc key) {
507             if ("canEditAsText".equals(key)) {
508                 return Boolean.TRUE;
509             } else {
510                 return super.getValue(key);
511             }
512         }
513     }
514     
515     // Property definition
516
public class TProperty3 extends PropertySupport {
517         private Boolean JavaDoc myValue = Boolean.FALSE;
518         // Create new Property
519
public TProperty3(String JavaDoc name, boolean isWriteable) {
520             super(name, Boolean JavaDoc.class, name, "", true, isWriteable);
521         }
522         // get property value
523
public Object JavaDoc getValue() {
524             return myValue;
525         }
526         
527         // set property value
528
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException {
529             Object JavaDoc oldVal = myValue;
530             myValue = (Boolean JavaDoc) value;
531             tn.fireMethod(getName(), oldVal, myValue);
532         }
533         
534         public Object JavaDoc getValue(String JavaDoc key) {
535             if ("nameIcon".equals(key)) {
536                 return new NameIcon();
537             } else if ("valueIcon".equals(key)) {
538                 return new ValueIcon();
539             } else if ("postSetAction".equals(key)) {
540                 return postSetAction;
541             }
542             return super.getValue(key);
543         }
544         
545         public PropertyEditor getPropertyEditor() {
546             return new WrapperEx(super.getPropertyEditor());
547         }
548     
549         public class WrapperEx implements ExPropertyEditor {
550             private PropertyEditor orig;
551             public WrapperEx (PropertyEditor orig) {
552                 this.orig = orig;
553             }
554
555
556             public void attachEnv(PropertyEnv env) {
557                 env.setState(myValue == Boolean.FALSE ? env.STATE_INVALID : env.STATE_VALID);
558             }
559
560             public void addPropertyChangeListener(PropertyChangeListener listener) {
561                 orig.addPropertyChangeListener(listener);
562             }
563
564             public String JavaDoc getAsText() {
565                 return orig.getAsText();
566             }
567
568             public java.awt.Component JavaDoc getCustomEditor() {
569                 return orig.getCustomEditor();
570             }
571
572             public String JavaDoc getJavaInitializationString() {
573                 return orig.getJavaInitializationString();
574             }
575
576             public String JavaDoc[] getTags() {
577                 return orig.getTags();
578             }
579
580             public Object JavaDoc getValue() {
581                 return orig.getValue();
582             }
583
584             public boolean isPaintable() {
585                 return orig.isPaintable();
586             }
587
588             public void paintValue(java.awt.Graphics JavaDoc gfx, java.awt.Rectangle JavaDoc box) {
589                 orig.paintValue(gfx, box);
590             }
591
592             public void removePropertyChangeListener(PropertyChangeListener listener) {
593                 orig.removePropertyChangeListener(listener);
594             }
595
596             public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
597                 orig.setAsText(text);
598             }
599
600             public void setValue(Object JavaDoc value) {
601                 orig.setValue(value);
602             }
603
604             public boolean supportsCustomEditor() {
605                 return orig.supportsCustomEditor();
606             }
607         }
608     }
609     
610     public class BadEditor extends PropertyEditorSupport implements ExPropertyEditor {
611         PropertyEnv env;
612         
613         public BadEditor() {
614         }
615         
616         public String JavaDoc[] getTags() {
617             //return new String[] {"a","b","c","d","Value"};
618
return null;
619         }
620         
621         public void attachEnv(PropertyEnv env) {
622             this.env = env;
623             env.setState(env.STATE_INVALID);
624         }
625         
626         public boolean supportsCustomEditor() {
627             return false;
628         }
629         
630         public void setValue(Object JavaDoc newValue) {
631             super.setValue(newValue);
632         }
633     }
634     
635     private class NameIcon implements Icon JavaDoc {
636         
637         public int getIconHeight() {
638             return 12;
639         }
640         
641         public int getIconWidth() {
642             return 12;
643         }
644         
645         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
646             Color JavaDoc col = g.getColor();
647             try {
648                 g.setColor(Color.BLUE);
649                 g.drawRect(x, y, getIconWidth(), getIconHeight());
650                 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5);
651             } finally {
652                 g.setColor(col);
653             }
654         }
655         
656     }
657     
658     private class ValueIcon implements Icon JavaDoc {
659         
660         public int getIconHeight() {
661             return 12;
662         }
663         
664         public int getIconWidth() {
665             return 12;
666         }
667         
668         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
669             Color JavaDoc col = g.getColor();
670             try {
671                 g.setColor(Color.GREEN);
672                 g.drawRect(x, y, getIconWidth(), getIconHeight());
673                 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5);
674             } finally {
675                 g.setColor(col);
676             }
677         }
678     }
679     
680     private class PostSetAction extends AbstractAction {
681         boolean performed = false;
682         
683         public void assertPerformed() {
684             assertTrue ("Action was not performed", performed);
685             performed = false;
686         }
687         
688         public void assertNotPerformed() {
689             assertTrue ("Action should not be performed before an appropriate event is triggered", !performed);
690         }
691         
692         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
693             performed = true;
694         }
695         
696     }
697     
698     
699     private static TNode tn;
700     private static TProperty tp;
701     private static TProperty2 tp1;
702     private static TEditor te;
703     private static PostSetAction postSetAction;
704     private static String JavaDoc initEditorValue;
705     private static String JavaDoc initPropertyValue;
706     private static String JavaDoc postChangePropertyValue;
707     private static String JavaDoc postChangeEditorValue;
708     
709     //Shamelessly stolen from util.IconManager
710
private static final BufferedImage JavaDoc toBufferedImage(Image JavaDoc img) {
711         // load the image
712
new javax.swing.ImageIcon JavaDoc(img);
713         java.awt.image.BufferedImage JavaDoc rep = createBufferedImage(img.getWidth(null), img.getHeight(null));
714         java.awt.Graphics JavaDoc g = rep.createGraphics();
715         g.drawImage(img, 0, 0, null);
716         g.dispose();
717         img.flush();
718         return rep;
719     }
720     
721     /** Creates BufferedImage 16x16 and Transparency.BITMASK */
722     private static final java.awt.image.BufferedImage JavaDoc createBufferedImage(int width, int height) {
723         java.awt.image.ColorModel JavaDoc model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().
724                                           getDefaultScreenDevice().getDefaultConfiguration().getColorModel(java.awt.Transparency.BITMASK);
725         java.awt.image.BufferedImage JavaDoc buffImage = new java.awt.image.BufferedImage JavaDoc(model,
726                 model.createCompatibleWritableRaster(width, height), model.isAlphaPremultiplied(), null);
727         return buffImage;
728     }
729     
730
731     
732 }
733
Popular Tags