KickJava   Java API By Example, From Geeks To Geeks.

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


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 InplaceEditorNoModifyOnTextChangeContractStringEditorTest extends NbTestCase {
41     public InplaceEditorNoModifyOnTextChangeContractStringEditorTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     static Component JavaDoc edComp = null;
46     static PropertyEditor JavaDoc ped = null;
47     static InplaceEditor ied = null;
48     static ActionEvent JavaDoc[] events = new ActionEvent JavaDoc[10];
49     static Object JavaDoc postSetValuePropertyEdValue=null;
50     static Object JavaDoc preSetValuePropertyEdValue=null;
51     static Object JavaDoc finalValuePropertyEdValue=null;
52     
53     private static int idx=0;
54     private static InplaceEditorFactory factory = new InplaceEditorFactory(true, new ReusablePropertyEnv());
55     
56     static boolean canRun = ExtTestCase.canSafelyRunFocusTests();
57     
58     protected void setUp() throws Exception JavaDoc {
59         if (!canRun) {
60             return;
61         }
62         PropUtils.forceRadioButtons=false;
63         if (idx != 0) return;
64         // Create new TestProperty
65
tp = new TProperty("TProperty", true);
66         // Create new TEditor
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             
83             postSetValuePropertyEdValue=ped.getValue();
84             
85             edComp = ied.getComponent();
86             JFrame JavaDoc jf = new JFrame JavaDoc();
87             jf.getContentPane().add(edComp);
88             jf.setSize(200,200);
89             new ExtTestCase.WaitWindow(jf);
90             
91             new ExtTestCase.WaitFocus(edComp);
92             
93             ied.addActionListener(al);
94             sleep();
95             
96             final KeyEvent JavaDoc ke = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER);
97             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
98                 public void run() {
99                     edComp.dispatchEvent(ke);
100                 }
101             });
102             sleep();
103             final KeyEvent JavaDoc ke2 = new KeyEvent JavaDoc(edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER);
104             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
105                 public void run() {
106                     edComp.dispatchEvent(ke2);
107                 }
108             });
109             sleep();
110             sleep();
111             sleep();
112             
113             idx++;
114             finalValuePropertyEdValue = ped.getValue();
115             jf.hide();
116             jf.dispose();
117             ied.removeActionListener(al);
118             ied.clear();
119         } catch (Exception JavaDoc e) {
120             e.printStackTrace();
121             fail("FAILED - Exception thrown "+e.getClass().toString());
122         }
123     }
124     
125     private void sleep() {
126         try {
127             ExtTestCase.sleep();
128         } catch (Exception JavaDoc e) {
129             //do nothing
130
}
131     }
132     
133     public void testInplaceEditorSetValueDidNotChangePropertyEditorValue() throws Exception JavaDoc {
134         if (!canRun) return;
135         assertTrue("PreSetValue value is " + preSetValuePropertyEdValue + " but post value is " + postSetValuePropertyEdValue, preSetValuePropertyEdValue == postSetValuePropertyEdValue);
136     }
137     
138     public void testEnterTriggeredActionSuccess() {
139         if (!canRun) return;
140         try {
141             Thread.currentThread().sleep(2000);
142         } catch (Exception JavaDoc e) {
143         }
144         assertTrue("Enter keystroke did not produce an action event", events[0] != null);
145         assertTrue("Action command for faked Enter keystroke should be " + InplaceEditor.COMMAND_SUCCESS + " but is " + events[0].getActionCommand(), InplaceEditor.COMMAND_SUCCESS.equals(events[0].getActionCommand()));
146     }
147     
148     public void testFinalPropertyValueIsUnchanged() {
149         if (!canRun) return;
150         assertTrue("Final value should be unchanged but is " + finalValuePropertyEdValue, "Value".equals(finalValuePropertyEdValue));
151     }
152     
153     // Property definition
154
public class TProperty extends PropertySupport {
155         private String JavaDoc myValue = "Value";
156         // Create new Property
157
public TProperty(String JavaDoc name, boolean isWriteable) {
158             super(name, String JavaDoc.class, name, "", true, isWriteable);
159         }
160         // get property value
161
public Object JavaDoc getValue() {
162             return myValue;
163         }
164         // set property value
165
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
166             Object JavaDoc oldVal = myValue;
167             myValue = value.toString();
168         }
169         // get the property editor
170
public PropertyEditor JavaDoc getPropertyEditor() {
171             return te;
172         }
173     }
174     
175     public class TagsEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
176         PropertyEnv env;
177         
178         public TagsEditor() {
179         }
180         
181         public void attachEnv(PropertyEnv env) {
182             this.env = env;
183         }
184         
185         public boolean supportsCustomEditor() {
186             return false;
187         }
188         
189         public void setValue(Object JavaDoc newValue) {
190             super.setValue(newValue);
191         }
192     }
193     
194     private TProperty tp;
195     private TagsEditor te;
196     private String JavaDoc initEditorValue;
197     private String JavaDoc initPropertyValue;
198     private String JavaDoc postChangePropertyValue;
199     private String JavaDoc postChangeEditorValue;
200 }
201
Popular Tags