KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dialog JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.KeyboardFocusManager JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import org.openide.nodes.AbstractNode;
32 import org.openide.util.HelpCtx;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Sheet;
35 import org.openide.nodes.PropertySupport;
36 import java.lang.reflect.InvocationTargetException JavaDoc;
37 import java.beans.PropertyEditor JavaDoc;
38 import java.beans.PropertyChangeListener JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JFrame JavaDoc;
41 import javax.swing.JTextArea JavaDoc;
42 import javax.swing.SwingUtilities JavaDoc;
43 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow;
44 import org.openide.nodes.Node;
45
46 /** Test finding help IDs in the property sheet.
47  * @author Jesse Glick
48  * @see "#14701"
49  */

50 public class NonEditabilityTest extends ExtTestCase {
51
52     public NonEditabilityTest(String JavaDoc name) {
53         super(name);
54     }
55     
56     protected boolean runInEQ() {
57         return false;
58     }
59
60     private PropertySheet sheet = null;
61     private JFrame JavaDoc frame = null;
62     protected void setUp() throws Exception JavaDoc {
63         //Ensure we don't have a bogus stored value
64
PropUtils.putSortOrder(PropertySheet.UNSORTED);
65
66         JFrame JavaDoc jf = new JFrame JavaDoc();
67         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
68         sheet = new PropertySheet();
69         jf.getContentPane().add (sheet);
70
71         jf.setBounds (20, 20, 200, 400);
72         frame = jf;
73         new WaitWindow(jf);
74     }
75
76     public void testClickInvokesCustomEditor() throws Exception JavaDoc {
77         if( !ExtTestCase.canSafelyRunFocusTests() )
78             return;
79         
80         Node n = new ANode();
81         setCurrentNode (n, sheet);
82
83         sleep();
84
85         requestFocus (sheet.table);
86         sleep();
87
88         Component JavaDoc owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
89         if (owner == sheet.table) { //sanity check to avoid random failures on some window managers
90

91             System.out.println ("About to click cell");
92
93             Rectangle JavaDoc r = sheet.table.getCellRect(1, 1, false);
94             final MouseEvent JavaDoc me = new MouseEvent JavaDoc (sheet.table, MouseEvent.MOUSE_PRESSED,
95                 System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, r.x + 3,
96                 r.y + 3, 2, false);
97
98             SwingUtilities.invokeLater(new Runnable JavaDoc() {
99                 public void run() {
100                     sheet.table.dispatchEvent(me);
101                 }
102             });
103
104             sleep();
105             sleep();
106
107             System.out.println ("Now checking focus");
108
109             owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
110             assertTrue ("Focus owner should be custom editor, not " + owner, owner instanceof JTextArea JavaDoc);
111
112             JComponent JavaDoc jc = (JComponent JavaDoc) owner;
113             assertTrue ("Custom editor should have been invoked, but focus owner's top level ancestor is not a dialog", jc.getTopLevelAncestor() instanceof Dialog JavaDoc);
114
115             Dialog JavaDoc d = (Dialog JavaDoc) jc.getTopLevelAncestor();
116
117             d.setVisible(false);
118         }
119
120         requestFocus (sheet.table);
121         sleep();
122
123         owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
124         if (owner == sheet.table) { //sanity check to avoid random failures on some window managers
125
pressKey(sheet.table, KeyEvent.VK_SPACE);
126             sleep();
127
128             owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
129             assertTrue ("After pressing a key, focus owner should still be the table, not " + owner, sheet.table == owner);
130         }
131
132     }
133     
134     private static final class ANode extends AbstractNode {
135         public ANode() {
136             super(Children.LEAF);
137         }
138         public HelpCtx getHelpCtx() {
139             return new HelpCtx("node-help");
140         }
141         protected Sheet createSheet() {
142             Sheet s = super.createSheet();
143             Sheet.Set ss = Sheet.createPropertiesSet();
144             ss.put (new AProperty());
145             ss.put (new AProperty());
146             s.put(ss);
147             return s;
148         }
149     }
150
151     private static final String JavaDoc name = "foo";
152     private static final class AProperty extends PropertySupport.ReadOnly {
153         public AProperty() {
154             super(name, String JavaDoc.class, name, name);
155         }
156         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
157             return "value-" + getName();
158         }
159
160         public PropertyEditor JavaDoc getPropertyEditor() {
161             return new APropertyEditor();
162         }
163     }
164
165     private static final class APropertyEditor implements PropertyEditor JavaDoc {
166         public void setValue(Object JavaDoc value) {
167
168         }
169
170         public Object JavaDoc getValue() {
171             return null;
172         }
173
174         public boolean isPaintable() {
175             return true;
176         }
177
178         public void paintValue(Graphics JavaDoc gfx, Rectangle JavaDoc box) {
179             gfx.setColor (Color.ORANGE);
180             gfx.fillRect (box.x, box.y, box.width, box.height);
181         }
182
183         public String JavaDoc getJavaInitializationString() {
184             return null;
185         }
186
187         public String JavaDoc getAsText() {
188             return null;
189         }
190
191         public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
192
193         }
194
195         public String JavaDoc[] getTags() {
196             return null;
197         }
198
199         public Component JavaDoc getCustomEditor() {
200             return new JTextArea JavaDoc();
201         }
202
203         public boolean supportsCustomEditor() {
204             return true;
205         }
206
207         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
208
209         }
210
211         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
212
213         }
214     }
215 }
216
Popular Tags