KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.KeyboardFocusManager JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.awt.event.MouseEvent JavaDoc;
29 import java.awt.event.WindowAdapter JavaDoc;
30 import java.awt.event.WindowEvent JavaDoc;
31 import java.beans.PropertyEditor JavaDoc;
32 import java.beans.PropertyEditorManager JavaDoc;
33 import java.beans.PropertyEditorSupport JavaDoc;
34 import java.lang.reflect.InvocationTargetException JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.JFrame JavaDoc;
37 import javax.swing.JRootPane JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import org.netbeans.junit.NbTestCase;
40 import org.netbeans.modules.openide.explorer.UIException;
41 import org.openide.ErrorManager;
42 import org.openide.nodes.AbstractNode;
43 import org.openide.nodes.Children;
44 import org.openide.nodes.Node;
45 import org.openide.nodes.PropertySupport;
46 import org.openide.nodes.Sheet;
47
48 /** Tests property marking functionality and the ability of a Property to
49  * provide a "postSetAction" action hint, which will be run if the user
50  * successfully changes the property value.
51  */

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

79     
80     private boolean focusWorks = false;
81     protected void setUp() throws Exception JavaDoc {
82         if (setup) return;
83         
84         try {
85             focusWorks = ExtTestCase.canSafelyRunFocusTests();
86             if (!focusWorks) {
87                 return;
88             }
89             
90             tp = new TProperty("Dont set me!", true);
91             
92             tn = new TNode();
93             // PropUtils.forceRadioButtons=true;
94
final PropertySheet ps = new PropertySheet();
95             
96             //ensure no stored value in preferences:
97
ps.setCurrentNode(tn);
98             sleep();
99             ps.setSortingMode(PropertySheet.UNSORTED);
100             
101             jf = new JFrame JavaDoc();
102             jf.getContentPane().add(ps);
103             jf.setLocation(20,20);
104             jf.setSize(300, 400);
105             new WaitWindow(jf);
106             tb = ps.table;
107             
108             jf.repaint();
109             
110             
111         } catch (Exception JavaDoc e) {
112             e.printStackTrace();
113             fail("FAILED - Exception thrown "+e.getClass().toString());
114         } finally {
115             setup = true;
116         }
117     }
118     
119     public void tearDown() {
120         // jf.hide();
121
// jf.dispose();
122
}
123     private void requestFocus(final JComponent JavaDoc jc) throws Exception JavaDoc {
124         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
125             public void run() {
126                 jc.requestFocus();
127             }
128         });
129         sleep();
130     }
131     
132     public void testFocusReturn() throws Exception JavaDoc {
133         if (!focusWorks) {
134             return;
135         }
136         clickOn(tb, 1, 1);
137         requestFocus(tb);
138         typeKey(tb, KeyEvent.VK_B);
139         typeKey(tb, KeyEvent.VK_E);
140         typeKey(tb, KeyEvent.VK_SPACE);
141         typeKey(tb, KeyEvent.VK_N);
142         typeKey(tb, KeyEvent.VK_I);
143         typeKey(tb, KeyEvent.VK_C);
144         typeKey(tb, KeyEvent.VK_E);
145         sleep();
146         SLEEP_LENGTH=1000;
147         Component JavaDoc c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
148         sleep();
149         pressKey(c, KeyEvent.VK_ENTER);
150         typeKey(c, KeyEvent.VK_ENTER);
151         sleep();
152         sleep();
153         sleep();
154         sleep();
155         c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
156         assertNotNull(c);
157         Container JavaDoc top = ((JComponent JavaDoc) c).getTopLevelAncestor();
158         assertTrue("Focus should no longer be on the property sheet after an erroneous value was entered", top != jf);
159         assertTrue("An error dialog should be showing after an exception was thrown in setAsText() but focus owner is " + c, jf != top);
160         
161         JRootPane JavaDoc jrp = ((JComponent JavaDoc) c).getRootPane();
162         jrp.getDefaultButton().doClick();
163         sleep();
164         
165         c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
166         assertTrue("After the error dialog is closed following a bad property edit, the table should return to edit mode on the previously edited property",
167                 c instanceof InplaceEditor);
168         
169     }
170     
171     
172     private static class WaitWindow extends WindowAdapter JavaDoc {
173         boolean shown=false;
174         public WaitWindow(JFrame JavaDoc f) {
175             f.addWindowListener(this);
176             f.show();
177             if (!shown) {
178                 synchronized(this) {
179                     try {
180                         //System.err.println("Waiting for window");
181
wait(5000);
182                     } catch (Exception JavaDoc e) {}
183                 }
184             }
185         }
186         
187         public void windowOpened(WindowEvent JavaDoc e) {
188             shown = true;
189             synchronized(this) {
190                 //System.err.println("window opened");
191
notifyAll();
192                 ((JFrame JavaDoc) e.getSource()).removeWindowListener(this);
193             }
194         }
195     }
196     
197     private static int SLEEP_LENGTH=500;
198     private void sleep() {
199         //useful when running interactively
200

201         
202         try {
203             Thread.currentThread().sleep(SLEEP_LENGTH);
204         } catch (InterruptedException JavaDoc ie) {
205             //go away
206
}
207         
208         
209         try {
210             //jf.getTreeLock().wait();
211
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
212                 public void run() {
213                     System.currentTimeMillis();
214                 }
215             });
216             //jf.getTreeLock().wait();
217
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
218                 public void run() {
219                     System.currentTimeMillis();
220                 }
221             });
222         } catch (Exception JavaDoc e) {
223         }
224         
225         
226     }
227     
228     
229     private void clickOn(final SheetTable tb, final int row, final int col) throws Exception JavaDoc {
230         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
231             public void run() {
232                 Rectangle JavaDoc r = tb.getCellRect(row, col, false);
233                 Point JavaDoc toClick = r.getLocation();
234                 toClick.x += 15;
235                 toClick.y +=3;
236                 MouseEvent JavaDoc me = new MouseEvent JavaDoc(tb, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
237                 tb.dispatchEvent(me);
238             }
239         });
240         sleep();
241     }
242     
243     private void releaseKey(final Component JavaDoc target, final int key) throws Exception JavaDoc {
244         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
245             public void run() {
246                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key);
247                 target.dispatchEvent(ke);
248             }
249         });
250         sleep();
251     }
252     
253     private void pressKey(final Component JavaDoc target, final int key) throws Exception JavaDoc {
254         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
255             public void run() {
256                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key);
257                 target.dispatchEvent(ke);
258             }
259         });
260         sleep();
261     }
262     
263     private void typeKey(final Component JavaDoc target, final int key) throws Exception JavaDoc {
264         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
265             public void run() {
266                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key);
267                 target.dispatchEvent(ke);
268             }
269         });
270         sleep();
271     }
272     
273     //Node definition
274
public class TNode extends AbstractNode {
275         //create Node
276
public TNode() {
277             super(Children.LEAF);
278             setName("TNode"); // or, super.setName if needed
279
setDisplayName("TNode");
280         }
281         //clone existing Node
282
public Node cloneNode() {
283             return new TNode();
284         }
285         
286         public void addProp(Node.Property p) {
287             props.put(p);
288             this.firePropertyChange(PROP_PROPERTY_SETS, null, null);
289             this.firePropertySetsChange(null, null);
290         }
291         
292         Sheet sheet=null;
293         Sheet.Set props=null;
294         // Create a property sheet:
295
protected Sheet createSheet() {
296             sheet = super.createSheet();
297             // Make sure there is a "Properties" set:
298
props = sheet.get(Sheet.PROPERTIES);
299             if (props == null) {
300                 props = Sheet.createPropertiesSet();
301                 sheet.put(props);
302             }
303             props.put(tp);
304             return sheet;
305         }
306         // Method firing changes
307
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
308             firePropertyChange(s,o1,o2);
309         }
310     }
311     
312     // Property definition
313
public class TProperty extends PropertySupport {
314         private Object JavaDoc myValue = "Value";
315         // Create new Property
316
public TProperty(String JavaDoc name, boolean isWriteable) {
317             super(name, String JavaDoc.class, name, "", true, isWriteable);
318         }
319         // get property value
320
public Object JavaDoc getValue() {
321             return myValue;
322         }
323         // set property value
324
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
325             Object JavaDoc oldVal = myValue;
326             myValue = value;
327             tn.fireMethod(getName(), oldVal, myValue);
328         }
329         // get the property editor
330
public PropertyEditor JavaDoc getPropertyEditor() {
331             return new BadEditor();
332         }
333         
334     }
335     
336     
337     public class BadEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
338         PropertyEnv env;
339         
340         public BadEditor() {
341         }
342         
343         public String JavaDoc[] getTags() {
344             //return new String[] {"a","b","c","d","Value"};
345
return null;
346         }
347         
348         public void attachEnv(PropertyEnv env) {
349             this.env = env;
350             env.setState(env.STATE_INVALID);
351         }
352         
353         public boolean supportsCustomEditor() {
354             return false;
355         }
356         
357         public void setValue(Object JavaDoc newValue) {
358             super.setValue(newValue);
359         }
360         
361         public void setAsText(String JavaDoc txt) {
362             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc("Bad, bad, bad");
363             UIException.annotateUser(iae, "bad, bad, bad",
364                                      "I can\'t be nice, I\'m the evil property editor.",
365                                      null, null);
366             throw iae;
367         }
368     }
369     
370     
371     private static TNode tn;
372     private static TProperty tp;
373     
374 }
375
Popular Tags