KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.beans.PropertyEditor JavaDoc;
26 import java.beans.PropertyEditorSupport JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.Children;
32 import org.openide.nodes.Node;
33 import org.openide.nodes.Sheet;
34
35 /**
36  * Ensures that the proper property editor is used for indexed properties
37  */

38 public class IndexedPropertyTest extends ExtTestCase {
39     private PropertySheet ps = null;
40     public IndexedPropertyTest(String JavaDoc name) {
41         super(name);
42         super.installCorePropertyEditors();
43     }
44     
45     protected boolean runInEQ() {
46         return false;
47     }
48     
49     private static boolean setup = false;
50 /*
51  * This test creates a Property, Editor and Node. First test checks if initialized
52  * editor contains the same value as property. The second checks if the property
53  * value is changed if the same change will be done in the editor.
54  */

55     protected void setUp() throws Exception JavaDoc {
56         // Create new TEditor
57
te = new TEditor();
58         // Create new TNode
59
tn = new TNode();
60         
61         //Replacing NodeOp w/ JFrame to eliminate depending on full IDE init
62
//and long delay while waiting for property sheet thus requested to
63
//initialize
64
final JFrame JavaDoc jf = new JFrame JavaDoc();
65         ps = new PropertySheet();
66         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
67         jf.getContentPane().add(ps, BorderLayout.CENTER);
68         jf.setLocation(30,30);
69         jf.setSize(500,500);
70         
71         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
72             public void run() {
73                 ps.setNodes(new Node[] {tn});
74                 //ps.setCurrentNode(tn);
75
jf.show();
76             }
77         });
78         
79         jf.show();
80         new ExtTestCase.WaitWindow(jf);
81         
82         try {
83             // Wait for the initialization
84
for (int i = 0; i < 10; i++) {
85                 if (te.getAsText().equals("null")) {
86                     //System.out.println("null");
87
Thread.sleep(1000);
88                 } else break;
89             }
90             ensurePainted(ps);
91             
92         } catch (Exception JavaDoc e) {
93             fail("FAILED - Exception thrown "+e.getClass().toString());
94         }
95     }
96     
97     private void ensurePainted(final PropertySheet ps) throws Exception JavaDoc {
98         //issues 39205 & 39206 - ensure the property sheet really repaints
99
//before we get the value, or the value in the editor will not
100
//have changed
101
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
102             public void run() {
103                 Graphics JavaDoc g = ps.getGraphics();
104                 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight());
105             }
106         });
107     }
108     
109     public void testIndexedProperty() throws Exception JavaDoc {
110         System.err.println("Plain Editor: " + PropUtils.getPropertyEditor(plain));
111         System.err.println("Fancy Editor: " + PropUtils.getPropertyEditor(fancy));
112         
113         assertTrue("Plain editor should be an IndexedPropertyEditor ", PropUtils.getPropertyEditor(plain) instanceof IndexedPropertyEditor);
114         assertTrue("Overridden editor should be used if present", PropUtils.getPropertyEditor(fancy) == te);
115         
116     }
117     
118     
119     //Node definition
120
public class TNode extends AbstractNode {
121         //create Node
122
public TNode() throws Exception JavaDoc {
123             super(Children.LEAF);
124             setName("TNode"); // or, super.setName if needed
125
setDisplayName("TNode");
126         }
127         //clone existing Node
128
public Node cloneNode() {
129             try {
130                 return new TNode();
131             } catch (Exception JavaDoc e) {
132                 throw new RuntimeException JavaDoc("Failed to clone node");
133             }
134         }
135         
136         public void destroy() {
137             fireNodeDestroyed();
138         }
139         
140         // Create a property sheet:
141
protected Sheet createSheet() {
142             Sheet sheet = super.createSheet();
143             // Make sure there is a "Properties" set:
144
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
145             if (props == null) {
146                 props = Sheet.createPropertiesSet();
147                 sheet.put(props);
148             }
149             props.put(plain);
150             props.put(fancy);
151             return sheet;
152         }
153         // Method firing changes
154
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
155             firePropertyChange(s,o1,o2);
156         }
157     }
158     
159     PlainIndexedProperty plain = new PlainIndexedProperty();
160     FancyIndexedProperty fancy = new FancyIndexedProperty();
161     
162     public class PlainIndexedProperty extends Node.IndexedProperty {
163         private StringBuffer JavaDoc value = new StringBuffer JavaDoc(getClass().getName());
164         public PlainIndexedProperty() {
165             super(char[].class, Character.TYPE);
166             setDisplayName("Plain");
167             setName(getDisplayName());
168         }
169         
170         public boolean canIndexedRead() {
171             return true;
172         }
173         
174         public boolean canIndexedWrite() {
175             return true;
176         }
177         
178         public boolean canRead() {
179             return true;
180         }
181         
182         public boolean canWrite() {
183             return true;
184         }
185         
186         public PropertyEditor JavaDoc getPropertyEditor() {
187             return null;
188         }
189         
190         public Object JavaDoc getIndexedValue(int index) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
191             return new Character JavaDoc(value.charAt(index));
192         }
193         
194         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
195             return value.toString().toCharArray();
196         }
197         
198         public void setIndexedValue(int indx, Object JavaDoc val) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
199             String JavaDoc old = value.toString();
200             value.setCharAt(indx, ((Character JavaDoc) val).charValue());
201             tn.fireMethod(getName(), old, val);
202         }
203         
204         public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
205             String JavaDoc old = value.toString();
206             value = new StringBuffer JavaDoc(val == null ? "" : val.toString());
207             tn.fireMethod(getName(), old, val);
208         }
209         
210         public int hashCode() {
211             return 23;
212         }
213         
214         public boolean equals(Object JavaDoc o) {
215             return o == this;
216         }
217     }
218     
219     public class FancyIndexedProperty extends PlainIndexedProperty {
220         public FancyIndexedProperty() {
221             setDisplayName("Fancy");
222             setName(getDisplayName());
223         }
224         
225         public PropertyEditor JavaDoc getPropertyEditor() {
226             return te;
227         }
228         
229         public int hashCode() {
230             return 24;
231         }
232     }
233     
234     // Editor definition
235
public class TEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
236         PropertyEnv env;
237         
238         // Create new TEditor
239
public TEditor() {
240         }
241         
242         /*
243          * This method is called by the IDE to pass
244          * the environment to the property editor.
245          */

246         public void attachEnv(PropertyEnv env) {
247             this.env = env;
248         }
249         
250         // Set that this Editor doesn't support custom Editor
251
public boolean supportsCustomEditor() {
252             return false;
253         }
254         
255         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
256             super.addPropertyChangeListener(l);
257         }
258         
259         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
260             super.removePropertyChangeListener(l);
261         }
262         
263         
264         
265         // Set the Property value threw the Editor
266
public void setValue(Object JavaDoc newValue) {
267             super.setValue(newValue);
268         }
269         
270         public void firePropertyChange() {
271             super.firePropertyChange();
272         }
273     }
274     
275     private TNode tn;
276     private TEditor te;
277 }
278
Popular Tags