KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dimension JavaDoc;
24 import java.awt.KeyboardFocusManager JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.beans.PropertyEditor JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32 import javax.swing.SwingUtilities JavaDoc;
33 import org.netbeans.junit.NbTestCase;
34 import org.openide.nodes.PropertySupport;
35
36 /** Tests the contract that an inplace editor will not modify the property
37  * editor if its value changes (the infrastructure should do this by
38  * accepting the COMMAND_SUCCESS action event).
39  *
40  * @author Tim Boudreau
41  */

42
43 public class InplaceEditorNoModifyOnTextChangeContractBooleanEditorTest extends NbTestCase {
44     public InplaceEditorNoModifyOnTextChangeContractBooleanEditorTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     static Component JavaDoc edComp = null;
49     static PropertyEditor JavaDoc ped = null;
50     static InplaceEditor ied = null;
51     static ActionEvent JavaDoc[] events = new ActionEvent JavaDoc[10];
52     static Object JavaDoc postSetValuePropertyEdValue=null;
53     static Object JavaDoc preSetValuePropertyEdValue=null;
54     static Object JavaDoc finalValuePropertyEdValue=null;
55     static Object JavaDoc finalInplaceEditorValue=null;
56     
57     int i=0;
58     
59     private int idx=0;
60     
61     private static InplaceEditorFactory factory = new InplaceEditorFactory(true, new ReusablePropertyEnv());
62     
63     private static boolean canRun = ExtTestCase.canSafelyRunFocusTests();
64     
65     private static boolean setup = false;
66     protected void setUp() throws Exception JavaDoc {
67         if (!canRun) {
68             return;
69         }
70         
71         PropUtils.forceRadioButtons=false;
72         factory.setUseRadioBoolean(false);
73         
74         tp = new TProperty("TProperty", true);
75         
76         ActionListener JavaDoc al = new ActionListener JavaDoc() {
77             public void actionPerformed(ActionEvent JavaDoc ae) {
78                 System.err.println("Got an action event - " + ae.getActionCommand());
79                 events[idx] = ae;
80             }
81         };
82         
83         try {
84             ied = factory.getInplaceEditor(tp, false);
85             edComp = ied.getComponent();
86             System.err.println("EdComp is " + edComp);
87             
88             
89             
90             ped = ied.getPropertyEditor();
91             
92             preSetValuePropertyEdValue=ped.getValue();
93             ied.setValue("newValue");
94             
95             sleep();
96             postSetValuePropertyEdValue=ped.getValue();
97             
98             edComp = ied.getComponent();
99             JFrame JavaDoc jf = new JFrame JavaDoc();
100             jf.getContentPane().add(edComp);
101             jf.setLocation(new Point JavaDoc(20,20));
102             jf.setSize(new Dimension JavaDoc(30, 200));
103             new ExtTestCase.WaitWindow(jf);
104             
105             sleep();
106             sleep();
107             sleep();
108             
109             while (!edComp.isShowing()) {
110                 
111             }
112             
113             new ExtTestCase.WaitFocus(edComp);
114             
115             ied.addActionListener(al);
116             
117             Component JavaDoc comp = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
118             canRun = edComp == comp;
119             if (!canRun) {
120                 System.err.println("Platform focus behavior not sane - aborting tests");
121             }
122             
123             sleep();
124             System.err.println("Sending key pressed - space");
125             KeyEvent JavaDoc ke = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_SPACE, (char) KeyEvent.VK_SPACE);
126             dispatchEvent(ke, edComp);
127             
128             sleep();
129             System.err.println("Sending key released - space");
130             ke = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_SPACE, (char) KeyEvent.VK_SPACE);
131             dispatchEvent(ke, edComp);
132             
133             sleep();
134             
135             System.err.println("Sending key pressed - enter");
136             ke = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER);
137             dispatchEvent(ke, edComp);
138             sleep();
139             
140             System.err.println("Sending key released - enter");
141             ke = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER);
142             dispatchEvent(ke, edComp);
143             
144             sleep();
145             
146             idx++;
147             
148             sleep();
149             
150             ke = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, (char) KeyEvent.VK_ESCAPE);
151             dispatchEvent(ke, edComp);
152             ke = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, (char) KeyEvent.VK_ESCAPE);
153             dispatchEvent(ke, edComp);
154             
155             sleep();
156             sleep();
157             
158             finalInplaceEditorValue = ied.getValue();
159             jf.hide();
160             jf.dispose();
161             sleep();
162             
163             finalValuePropertyEdValue = ped.getValue();
164             ied.removeActionListener(al);
165             ied.clear();
166         } catch (Exception JavaDoc e) {
167             e.printStackTrace();
168             fail("FAILED - Exception thrown "+e.getClass().toString());
169         }
170         setup = true;
171     }
172     
173     public void testInplaceEditorSetValueDidNotChangePropertyEditorValue() throws Exception JavaDoc {
174         if (!canRun) return;
175         assertTrue("PreSetValue value is " + preSetValuePropertyEdValue + " but post value is " + postSetValuePropertyEdValue, preSetValuePropertyEdValue == postSetValuePropertyEdValue);
176     }
177     
178     public void testEnterTriggeredActionSuccess() {
179         if (!canRun) return;
180         assertTrue("Enter keystroke did not produce an action event", events[0] != null);
181         assertTrue("Action command for faked Enter keystroke should be " + InplaceEditor.COMMAND_SUCCESS + " but is " + events[0].getActionCommand(), InplaceEditor.COMMAND_SUCCESS.equals(events[0].getActionCommand()));
182     }
183     
184     public void testFinalInplaceEditorValue() throws Exception JavaDoc {
185         if (!canRun) return;
186         assertTrue("Final inplace editor value should be Boolean.FALSE but is " + finalInplaceEditorValue, Boolean.FALSE.equals(finalInplaceEditorValue));
187     }
188     
189     public void testFinalPropertyValueIsUnchanged() {
190         if (!canRun) return;
191         assertTrue("Final value should be unchanged but is " + finalValuePropertyEdValue, Boolean.TRUE.equals(finalValuePropertyEdValue));
192     }
193     
194     // Property definition
195
public class TProperty extends PropertySupport {
196         private Boolean JavaDoc myValue = Boolean.TRUE;
197         // Create new Property
198
public TProperty(String JavaDoc name, boolean isWriteable) {
199             super(name, Boolean JavaDoc.class, name, "", true, isWriteable);
200         }
201         // get property value
202
public Object JavaDoc getValue() {
203             return myValue;
204         }
205         // set property value
206
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
207             myValue = (Boolean JavaDoc) value;
208         }
209     }
210     
211     private void sleep() throws Exception JavaDoc {
212         Thread.currentThread().sleep(100);
213         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
214             public void run() {
215                 System.currentTimeMillis();
216             }
217         });
218         Thread.currentThread().sleep(100);
219     }
220     
221     private void dispatchEvent(final KeyEvent JavaDoc ke, final Component JavaDoc comp) throws Exception JavaDoc {
222         if (!SwingUtilities.isEventDispatchThread()) {
223             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
224                 public void run() {
225                     comp.dispatchEvent(ke);
226                 }
227             });
228             
229         } else {
230             comp.dispatchEvent(ke);
231         }
232     }
233     
234     static {
235         ExtTestCase.installCorePropertyEditors();
236     }
237     
238     private TProperty tp;
239     private String JavaDoc initEditorValue;
240     private String JavaDoc initPropertyValue;
241     private String JavaDoc postChangePropertyValue;
242     private String JavaDoc postChangeEditorValue;
243 }
244
245
Popular Tags