KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionListener JavaDoc;
24 import java.awt.event.InputEvent JavaDoc;
25 import java.beans.PropertyEditor JavaDoc;
26 import java.beans.PropertyEditorSupport JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.KeyStroke JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.nodes.PropertySupport;
32
33 /** Tests basic functionality of InplaceEditorFactory and its code to
34  * correctly configure a property editor and associated InplaceEditor
35  * with the data encapsulated by a Node.Property.
36  *
37  * @author Tim Boudreau
38  */

39 public class CustomInplaceEditorTest extends NbTestCase {
40     public CustomInplaceEditorTest(String JavaDoc name) {
41         super(name);
42     }
43     
44     Component JavaDoc edComp = null;
45     PropertyEditor JavaDoc ped = null;
46     InplaceEditor ied = null;
47     InplaceEditor ied2 = null;
48     private static InplaceEditorFactory factory = new InplaceEditorFactory(true, new ReusablePropertyEnv());
49     
50     protected void setUp() throws Exception JavaDoc {
51         // Create new TestProperty
52
tp = new TProperty("TProperty", true);
53         // Create new TEditor
54
te = new TEditor();
55         
56         TProperty2 tp2 = new TProperty2("TProperty2", true);
57         
58         try {
59             ied = factory.getInplaceEditor(tp, false);
60             ied2 = factory.getInplaceEditor(tp2, false);
61             edComp = ied.getComponent();
62             ped = ied.getPropertyEditor();
63         } catch (Exception JavaDoc e) {
64             e.printStackTrace();
65             fail("FAILED - Exception thrown "+e.getClass().toString());
66         }
67     }
68     
69     public void testRegisterInplaceEditorViaPropertyEnv() throws Exception JavaDoc {
70         assertTrue("Inplace editor should be instance of test class registered by PropertyEnv.registerInplaceEditor, but is instance of " + ied.getClass(), ied instanceof TInplaceEditor);
71     }
72     
73     public void testRegisterInplaceEditorViaHint() throws Exception JavaDoc {
74         assertTrue("Inplace editor should be instance of test class as returned by TProperty2.getValue(\"inplaceEditor\"), but is instance of " + ied2.getClass(), ied2 instanceof TInplaceEditor);
75     }
76     
77     // Property definition
78
public class TProperty2 extends PropertySupport {
79         private Boolean JavaDoc myValue = Boolean.TRUE;
80         // Create new Property
81
public TProperty2(String JavaDoc name, boolean isWriteable) {
82             super(name, Boolean JavaDoc.class, name, "", true, isWriteable);
83         }
84         // get property value
85
public Object JavaDoc getValue() {
86             return myValue;
87         }
88         
89         public Object JavaDoc getValue(String JavaDoc key) {
90             if ("inplaceEditor".equals(key)) {
91                 return new TInplaceEditor();
92             } else {
93                 return super.getValue(key);
94             }
95         }
96         
97         // set property value
98
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
99             myValue = (Boolean JavaDoc) value;
100         }
101     }
102     
103     // Property definition
104
public class TProperty extends PropertySupport {
105         private String JavaDoc myValue = "foo";
106         // Create new Property
107
public TProperty(String JavaDoc name, boolean isWriteable) {
108             super(name, String JavaDoc.class, name, "", true, isWriteable);
109         }
110         // get property value
111
public Object JavaDoc getValue() {
112             return myValue;
113         }
114         // set property value
115
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
116             Object JavaDoc oldVal = myValue;
117             myValue = value.toString();
118         }
119         // get the property editor
120
public PropertyEditor JavaDoc getPropertyEditor() {
121             return te;
122         }
123     }
124     
125     public class TEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor, InplaceEditor.Factory {
126         PropertyEnv env;
127         
128         public TEditor() {
129         }
130         
131         public void attachEnv(PropertyEnv env) {
132             this.env = env;
133             env.registerInplaceEditorFactory(this);
134         }
135         
136         public boolean supportsCustomEditor() {
137             return false;
138         }
139         
140         public void setValue(Object JavaDoc newValue) {
141             super.setValue(newValue);
142         }
143         
144         public InplaceEditor getInplaceEditor() {
145             return new TInplaceEditor();
146         }
147         
148     }
149     
150     public class TInplaceEditor extends JComponent JavaDoc implements InplaceEditor {
151         PropertyEditor JavaDoc pe=null;
152         public void clear() {
153         }
154         
155         public void connect(PropertyEditor JavaDoc pe, PropertyEnv env) {
156             this.pe = pe;
157         }
158         
159         public JComponent JavaDoc getComponent() {
160             return this;
161         }
162         
163         public KeyStroke JavaDoc[] getKeyStrokes() {
164             return null;
165         }
166         
167         public PropertyEditor JavaDoc getPropertyEditor() {
168             return pe;
169         }
170         
171         public PropertyModel getPropertyModel() {
172             return null;
173         }
174         
175         public Object JavaDoc getValue() {
176             return null;
177         }
178         
179         public void handleInitialInputEvent(InputEvent JavaDoc e) {
180         }
181         
182         public boolean isKnownComponent(Component JavaDoc c) {
183             return false;
184         }
185         
186         public void reset() {
187         }
188         
189         public void setPropertyModel(PropertyModel pm) {
190         }
191         
192         public void setValue(Object JavaDoc o) {
193         }
194         
195         public boolean supportsTextEntry() {
196             return false;
197         }
198         
199         public void addActionListener(ActionListener JavaDoc al) {
200         }
201         
202         public void removeActionListener(ActionListener JavaDoc al) {
203         }
204         
205     }
206     
207     private TProperty tp;
208     private TEditor te;
209     private String JavaDoc initEditorValue;
210     private String JavaDoc initPropertyValue;
211     private String JavaDoc postChangePropertyValue;
212     private String JavaDoc postChangeEditorValue;
213 }
214
Popular Tags