KickJava   Java API By Example, From Geeks To Geeks.

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


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

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