KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.explorer.propertysheet;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.GraphicsEnvironment JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.KeyboardFocusManager JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.Transparency JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.awt.image.BufferedImage JavaDoc;
33 import java.awt.image.ColorModel JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.beans.PropertyEditor JavaDoc;
36 import java.beans.PropertyEditorManager JavaDoc;
37 import java.beans.PropertyEditorSupport JavaDoc;
38 import java.lang.reflect.InvocationTargetException JavaDoc;
39 import javax.swing.AbstractAction JavaDoc;
40 import javax.swing.Icon JavaDoc;
41 import javax.swing.ImageIcon JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.JFrame JavaDoc;
44 import javax.swing.JTextField JavaDoc;
45 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow;
46 import org.openide.nodes.AbstractNode;
47 import org.openide.nodes.Children;
48 import org.openide.nodes.Node;
49 import org.openide.nodes.PropertySupport;
50 import org.openide.nodes.Sheet;
51
52 /** Tests property marking functionality and the ability of a Property to
53  * provide a "postSetAction" action hint, which will be run if the user
54  * successfully changes the property value.
55  */

56 public class PropertyMarkingTest extends GraphicsTestCase {
57     private static boolean setup=false;
58     
59     static {
60         String JavaDoc[] syspesp = PropertyEditorManager.getEditorSearchPath();
61         String JavaDoc[] nbpesp = new String JavaDoc[] {
62             "org.netbeans.beaninfo.editors", // NOI18N
63
"org.openide.explorer.propertysheet.editors", // NOI18N
64
};
65         String JavaDoc[] allpesp = new String JavaDoc[syspesp.length + nbpesp.length];
66         System.arraycopy(nbpesp, 0, allpesp, 0, nbpesp.length);
67         System.arraycopy(syspesp, 0, allpesp, nbpesp.length, syspesp.length);
68         PropertyEditorManager.setEditorSearchPath(allpesp);
69     }
70     
71     public PropertyMarkingTest(String JavaDoc name) {
72         super(name);
73     }
74     
75     static SheetTable tb=null;
76     static JFrame JavaDoc jf=null;
77 /*
78  * This test creates a Property, Editor and Node. First test checks if initialized
79  * editor contains the same value as property. The second checks if the property
80  * value is changed if the same change will be done in the editor.
81  */

82     protected void setUp() throws Exception JavaDoc {
83         if (setup) return;
84         PropUtils.forceRadioButtons=false;
85         
86         try {
87             
88             tp = new TProperty("oh", true);
89             tp1 = new TProperty2("the", true);
90             tp2 = new TProperty2("pretty", true);
91             tp3 = new TProperty2("pictures",true);
92             tp4 = new TProperty3("I can create",true);
93             postSetAction = new PostSetAction();
94             
95             tn = new TNode();
96             
97             final PropertySheet ps = new PropertySheet();
98             
99             //ensure no stored value in preferences:
100
ps.setCurrentNode(tn);
101             sleep();
102             ps.setSortingMode(PropertySheet.UNSORTED);
103             
104             jf = new JFrame JavaDoc();
105             jf.getContentPane().add(ps);
106             jf.setLocation(20,20);
107             jf.setSize(300, 400);
108             new WaitWindow(jf);
109             tb = ps.table;
110             
111             ps.setSortingMode(ps.SORTED_BY_NAMES);
112             jf.repaint();
113             
114             
115         } catch (Exception JavaDoc e) {
116             e.printStackTrace();
117             fail("FAILED - Exception thrown "+e.getClass().toString());
118         } finally {
119             setup = true;
120         }
121     }
122     
123     public void testPostSetAction() throws Exception JavaDoc {
124         if (!canSafelyRunFocusTests()) {
125             System.err.println("Cannot run post set " +
126                     "action test - testing machine's focus behavior is not sane");
127             return;
128         }
129         pressCell(tb, 1, 1);
130         
131         sleep();
132         requestFocus(tb);
133         System.err.println("WILL TYPE MIAOU");
134         JComponent JavaDoc focusOwner = (JComponent JavaDoc)KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
135         assertTrue("Focus should be on a JTextField, not " + focusOwner, focusOwner instanceof JTextField JavaDoc);
136         
137         typeString("MIAOU", focusOwner);
138         
139         sleep();
140         sleep();
141         System.err.println("MIAOU TYPED");
142         
143         postSetAction.assertNotPerformed();
144         System.err.println("ACTION NOT PERFORMED AS IT SHOULD BE");
145         
146         pressKey(tb, KeyEvent.VK_ENTER);
147         System.err.println("SENDING ENTER TO " + tb);
148         Thread.currentThread().sleep(1000);
149         
150         sleep();
151         
152         System.err.println("TYPING ENTER TO " + tb);
153         typeKey(tb, KeyEvent.VK_ENTER);
154         sleep();
155         sleep();
156         
157         System.err.println("POST SET ACTION PERFORMED: " + postSetAction.performed);
158         postSetAction.assertPerformed();
159         click(tb, 1, 0);
160     }
161     
162     /*
163     public void testNameIconDrawn() throws Exception {
164         if (!canSafelyRunPixelTests()) {
165             //This test will fail on 8 bit displays
166             return;
167         }
168         Rectangle r = tb.getCellRect (1, 0, false);
169         assertColorOnComponent(tb, Color.BLUE, r.x+7, r.y+7);
170     }
171      
172      
173     public void testValueIconDrawn() throws Exception {
174         if (!canSafelyRunPixelTests()) {
175             //This test will fail on 8 bit displays
176             return;
177         }
178         Rectangle r = tb.getCellRect (1, 1, false);
179         assertColorOnComponent(tb, Color.GREEN, r.x+7, r.y+7);
180     }
181      
182      
183     public void testErrorIconDrawn() throws Exception {
184         if (!canSafelyRunPixelTests()) {
185             //This test will fail on 8 bit displays
186             return;
187         }
188         Image i = loadImage("org/openide/resources/propertysheet/invalid.gif");
189         Rectangle r = tb.getCellRect (3, 1, false);
190         assertPixelFromImage(i, tb, 9 - PropUtils.getTextMargin(), 6, r.x+7, r.y+7);
191 // assertPixelFromImage(i, tb, 12 - PropUtils.getTextMargin(), 11, r.x+12, r.y+12);
192 // assertPixelFromImage(i, tb, 5 - PropUtils.getTextMargin(), 4, r.x+6, r.y+5);
193     }
194      */

195     
196     
197     //Node definition
198
public class TNode extends AbstractNode {
199         //create Node
200
public TNode() {
201             super(Children.LEAF);
202             setName("TNode"); // or, super.setName if needed
203
setDisplayName("TNode");
204         }
205         //clone existing Node
206
public Node cloneNode() {
207             return new TNode();
208         }
209         
210         public void addProp(Node.Property p) {
211             props.put(p);
212             this.firePropertyChange(PROP_PROPERTY_SETS, null, null);
213             this.firePropertySetsChange(null, null);
214         }
215         
216         Sheet sheet=null;
217         Sheet.Set props=null;
218         // Create a property sheet:
219
protected Sheet createSheet() {
220             sheet = super.createSheet();
221             // Make sure there is a "Properties" set:
222
props = sheet.get(Sheet.PROPERTIES);
223             if (props == null) {
224                 props = Sheet.createPropertiesSet();
225                 sheet.put(props);
226             }
227             props.put(tp);
228             props.put(tp1);
229             props.put(tp2);
230             props.put(tp3);
231             props.put(tp4);
232             return sheet;
233         }
234         // Method firing changes
235
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
236             firePropertyChange(s,o1,o2);
237         }
238     }
239     
240     // Property definition
241
public class TProperty extends PropertySupport {
242         private Object JavaDoc myValue = "Value";
243         // Create new Property
244
public TProperty(String JavaDoc name, boolean isWriteable) {
245             super(name, String JavaDoc.class, name, "", true, isWriteable);
246         }
247         // get property value
248
public Object JavaDoc getValue() {
249             return myValue;
250         }
251         // set property value
252
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
253             Object JavaDoc oldVal = myValue;
254             myValue = value;
255             tn.fireMethod(getName(), oldVal, myValue);
256         }
257         // get the property editor
258
public PropertyEditor JavaDoc getPropertyEditor() {
259             return new TEditor();
260         }
261         
262         public Object JavaDoc getValue(String JavaDoc key) {
263             if ("nameIcon".equals(key)) {
264                 return new NameIcon();
265             } else if ("valueIcon".equals(key)) {
266                 return new ValueIcon();
267             } else if ("postSetAction".equals(key)) {
268                 return postSetAction;
269             }
270             return super.getValue(key);
271         }
272     }
273     
274     // Editor definition
275
public class TEditor extends PropertyEditorSupport JavaDoc {
276         PropertyEnv env;
277         
278         // Create new TEditor
279
public TEditor() {
280         }
281         
282         /*
283          * This method is called by the IDE to pass
284          * the environment to the property editor.
285          */

286         public void attachEnv(PropertyEnv env) {
287             this.env = env;
288         }
289         
290         // Set that this Editor doesn't support custom Editor
291
public boolean supportsCustomEditor() {
292             return false;
293         }
294         
295         // Set the Property value threw the Editor
296
public void setValue(Object JavaDoc newValue) {
297             super.setValue(newValue);
298         }
299         
300         public String JavaDoc getAsText() {
301             return getValue() == null ? "null" : getValue().toString();
302         }
303     }
304     
305     
306     public class TagsEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
307         PropertyEnv env;
308         
309         public TagsEditor() {
310         }
311         
312         public String JavaDoc[] getTags() {
313             return new String JavaDoc[] {"a","b","c","d","Value"};
314         }
315         
316         public void attachEnv(PropertyEnv env) {
317             this.env = env;
318             env.setState(env.STATE_INVALID);
319         }
320         
321         public boolean supportsCustomEditor() {
322             return false;
323         }
324         
325         public void setValue(Object JavaDoc newValue) {
326             super.setValue(newValue);
327         }
328     }
329     
330     // Property definition
331
public class TProperty2 extends PropertySupport {
332         private Object JavaDoc myValue = "Value";
333         // Create new Property
334
public TProperty2(String JavaDoc name, boolean isWriteable) {
335             super(name, Object JavaDoc.class, name, "", true, isWriteable);
336         }
337         // get property value
338
public Object JavaDoc getValue() {
339             return myValue;
340         }
341         // set property value
342
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
343             Object JavaDoc oldVal = myValue;
344             myValue = value;
345             tn.fireMethod(getName(), oldVal, myValue);
346         }
347         // get the property editor
348
public PropertyEditor JavaDoc getPropertyEditor() {
349             return new TagsEditor();
350         }
351     }
352     
353     // Property definition
354
public class TProperty3 extends PropertySupport {
355         private Boolean JavaDoc myValue = Boolean.FALSE;
356         // Create new Property
357
public TProperty3(String JavaDoc name, boolean isWriteable) {
358             super(name, Boolean JavaDoc.class, name, "", true, isWriteable);
359         }
360         // get property value
361
public Object JavaDoc getValue() {
362             return myValue;
363         }
364         
365         // set property value
366
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
367             Object JavaDoc oldVal = myValue;
368             myValue = (Boolean JavaDoc) value;
369             tn.fireMethod(getName(), oldVal, myValue);
370         }
371         
372         public Object JavaDoc getValue(String JavaDoc key) {
373             if ("nameIcon".equals(key)) {
374                 return new NameIcon();
375             } else if ("valueIcon".equals(key)) {
376                 return new ValueIcon();
377             } else if ("postSetAction".equals(key)) {
378                 return postSetAction;
379             }
380             return super.getValue(key);
381         }
382         
383         public PropertyEditor JavaDoc getPropertyEditor() {
384             return new WrapperEx(super.getPropertyEditor());
385         }
386         
387         public class WrapperEx implements ExPropertyEditor {
388             private PropertyEditor JavaDoc orig;
389             public WrapperEx(PropertyEditor JavaDoc orig) {
390                 this.orig = orig;
391             }
392             
393             
394             public void attachEnv(PropertyEnv env) {
395                 env.setState(myValue == Boolean.FALSE ? env.STATE_INVALID : env.STATE_VALID);
396             }
397             
398             public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
399                 orig.addPropertyChangeListener(listener);
400             }
401             
402             public String JavaDoc getAsText() {
403                 return orig.getAsText();
404             }
405             
406             public Component JavaDoc getCustomEditor() {
407                 return orig.getCustomEditor();
408             }
409             
410             public String JavaDoc getJavaInitializationString() {
411                 return orig.getJavaInitializationString();
412             }
413             
414             public String JavaDoc[] getTags() {
415                 return orig.getTags();
416             }
417             
418             public Object JavaDoc getValue() {
419                 return orig.getValue();
420             }
421             
422             public boolean isPaintable() {
423                 return orig.isPaintable();
424             }
425             
426             public void paintValue(Graphics JavaDoc gfx, Rectangle JavaDoc box) {
427                 orig.paintValue(gfx, box);
428             }
429             
430             public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
431                 orig.removePropertyChangeListener(listener);
432             }
433             
434             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
435                 orig.setAsText(text);
436             }
437             
438             public void setValue(Object JavaDoc value) {
439                 orig.setValue(value);
440             }
441             
442             public boolean supportsCustomEditor() {
443                 return true;
444             }
445         }
446     }
447     
448     public class BadEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
449         PropertyEnv env;
450         
451         public BadEditor() {
452         }
453         
454         public String JavaDoc[] getTags() {
455             //return new String[] {"a","b","c","d","Value"};
456
return null;
457         }
458         
459         public void attachEnv(PropertyEnv env) {
460             this.env = env;
461             env.setState(env.STATE_INVALID);
462         }
463         
464         public boolean supportsCustomEditor() {
465             return true;
466         }
467         
468         public void setValue(Object JavaDoc newValue) {
469             super.setValue(newValue);
470         }
471     }
472     
473     private class NameIcon implements Icon JavaDoc {
474         
475         public int getIconHeight() {
476             return 12;
477         }
478         
479         public int getIconWidth() {
480             return 12;
481         }
482         
483         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
484             Color JavaDoc col = g.getColor();
485             try {
486                 g.setColor(Color.BLUE);
487                 g.drawRect(x, y, getIconWidth(), getIconHeight());
488                 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5);
489             } finally {
490                 g.setColor(col);
491             }
492         }
493         
494     }
495     
496     private class ValueIcon implements Icon JavaDoc {
497         
498         public int getIconHeight() {
499             return 12;
500         }
501         
502         public int getIconWidth() {
503             return 12;
504         }
505         
506         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
507             Color JavaDoc col = g.getColor();
508             try {
509                 g.setColor(Color.GREEN);
510                 g.drawRect(x, y, getIconWidth(), getIconHeight());
511                 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5);
512             } finally {
513                 g.setColor(col);
514             }
515         }
516     }
517     
518     private class PostSetAction extends AbstractAction JavaDoc {
519         boolean performed = false;
520         
521         public void assertPerformed() {
522             assertTrue("Action was not performed", performed);
523             performed = false;
524         }
525         
526         public void assertNotPerformed() {
527             assertTrue("Action should not be performed before an appropriate event is triggered", !performed);
528         }
529         
530         public void actionPerformed(ActionEvent JavaDoc e) {
531             performed = true;
532         }
533         
534     }
535     
536     
537     private static TNode tn;
538     private static TProperty tp;
539     private static TProperty2 tp1;
540     private static TProperty2 tp2;
541     private static TProperty2 tp3;
542     private static TProperty3 tp4;
543     private static TEditor te;
544     private static PostSetAction postSetAction;
545     private static String JavaDoc initEditorValue;
546     private static String JavaDoc initPropertyValue;
547     private static String JavaDoc postChangePropertyValue;
548     private static String JavaDoc postChangeEditorValue;
549     
550     //Shamelessly stolen from util.IconManager
551
private static final BufferedImage JavaDoc toBufferedImage(Image JavaDoc img) {
552         // load the image
553
new ImageIcon JavaDoc(img);
554         BufferedImage JavaDoc rep = createBufferedImage(img.getWidth(null), img.getHeight(null));
555         Graphics JavaDoc g = rep.createGraphics();
556         g.drawImage(img, 0, 0, null);
557         g.dispose();
558         img.flush();
559         return rep;
560     }
561     
562     /** Creates BufferedImage 16x16 and Transparency.BITMASK */
563     private static final BufferedImage JavaDoc createBufferedImage(int width, int height) {
564         ColorModel JavaDoc model = GraphicsEnvironment.getLocalGraphicsEnvironment().
565                 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(Transparency.BITMASK);
566         BufferedImage JavaDoc buffImage = new BufferedImage JavaDoc(model,
567                 model.createCompatibleWritableRaster(width, height), model.isAlphaPremultiplied(), null);
568         return buffImage;
569     }
570     
571     
572     
573 }
574
Popular Tags