1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.KeyboardFocusManager ; 25 import java.awt.Point ; 26 import java.awt.Rectangle ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.MouseEvent ; 29 import java.awt.event.WindowAdapter ; 30 import java.awt.event.WindowEvent ; 31 import java.beans.PropertyEditor ; 32 import java.beans.PropertyEditorManager ; 33 import java.beans.PropertyEditorSupport ; 34 import java.lang.reflect.InvocationTargetException ; 35 import javax.swing.JComponent ; 36 import javax.swing.JFrame ; 37 import javax.swing.JRootPane ; 38 import javax.swing.SwingUtilities ; 39 import org.netbeans.junit.NbTestCase; 40 import org.netbeans.modules.openide.explorer.UIException; 41 import org.openide.ErrorManager; 42 import org.openide.nodes.AbstractNode; 43 import org.openide.nodes.Children; 44 import org.openide.nodes.Node; 45 import org.openide.nodes.PropertySupport; 46 import org.openide.nodes.Sheet; 47 48 52 public class FocusAfterBadEditTest extends NbTestCase { 53 private static boolean setup=false; 54 55 static { 56 String [] syspesp = PropertyEditorManager.getEditorSearchPath(); 58 String [] nbpesp = new String [] { 59 "org.netbeans.beaninfo.editors", "org.openide.explorer.propertysheet.editors", }; 62 String [] allpesp = new String [syspesp.length + nbpesp.length]; 63 System.arraycopy(nbpesp, 0, allpesp, 0, nbpesp.length); 64 System.arraycopy(syspesp, 0, allpesp, nbpesp.length, syspesp.length); 65 PropertyEditorManager.setEditorSearchPath(allpesp); 66 } 67 68 public FocusAfterBadEditTest(String name) { 69 super(name); 70 } 71 72 static SheetTable tb=null; 73 static JFrame jf=null; 74 79 80 private boolean focusWorks = false; 81 protected void setUp() throws Exception { 82 if (setup) return; 83 84 try { 85 focusWorks = ExtTestCase.canSafelyRunFocusTests(); 86 if (!focusWorks) { 87 return; 88 } 89 90 tp = new TProperty("Dont set me!", true); 91 92 tn = new TNode(); 93 final PropertySheet ps = new PropertySheet(); 95 96 ps.setCurrentNode(tn); 98 sleep(); 99 ps.setSortingMode(PropertySheet.UNSORTED); 100 101 jf = new JFrame (); 102 jf.getContentPane().add(ps); 103 jf.setLocation(20,20); 104 jf.setSize(300, 400); 105 new WaitWindow(jf); 106 tb = ps.table; 107 108 jf.repaint(); 109 110 111 } catch (Exception e) { 112 e.printStackTrace(); 113 fail("FAILED - Exception thrown "+e.getClass().toString()); 114 } finally { 115 setup = true; 116 } 117 } 118 119 public void tearDown() { 120 } 123 private void requestFocus(final JComponent jc) throws Exception { 124 SwingUtilities.invokeAndWait(new Runnable () { 125 public void run() { 126 jc.requestFocus(); 127 } 128 }); 129 sleep(); 130 } 131 132 public void testFocusReturn() throws Exception { 133 if (!focusWorks) { 134 return; 135 } 136 clickOn(tb, 1, 1); 137 requestFocus(tb); 138 typeKey(tb, KeyEvent.VK_B); 139 typeKey(tb, KeyEvent.VK_E); 140 typeKey(tb, KeyEvent.VK_SPACE); 141 typeKey(tb, KeyEvent.VK_N); 142 typeKey(tb, KeyEvent.VK_I); 143 typeKey(tb, KeyEvent.VK_C); 144 typeKey(tb, KeyEvent.VK_E); 145 sleep(); 146 SLEEP_LENGTH=1000; 147 Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 148 sleep(); 149 pressKey(c, KeyEvent.VK_ENTER); 150 typeKey(c, KeyEvent.VK_ENTER); 151 sleep(); 152 sleep(); 153 sleep(); 154 sleep(); 155 c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 156 assertNotNull(c); 157 Container top = ((JComponent ) c).getTopLevelAncestor(); 158 assertTrue("Focus should no longer be on the property sheet after an erroneous value was entered", top != jf); 159 assertTrue("An error dialog should be showing after an exception was thrown in setAsText() but focus owner is " + c, jf != top); 160 161 JRootPane jrp = ((JComponent ) c).getRootPane(); 162 jrp.getDefaultButton().doClick(); 163 sleep(); 164 165 c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 166 assertTrue("After the error dialog is closed following a bad property edit, the table should return to edit mode on the previously edited property", 167 c instanceof InplaceEditor); 168 169 } 170 171 172 private static class WaitWindow extends WindowAdapter { 173 boolean shown=false; 174 public WaitWindow(JFrame f) { 175 f.addWindowListener(this); 176 f.show(); 177 if (!shown) { 178 synchronized(this) { 179 try { 180 wait(5000); 182 } catch (Exception e) {} 183 } 184 } 185 } 186 187 public void windowOpened(WindowEvent e) { 188 shown = true; 189 synchronized(this) { 190 notifyAll(); 192 ((JFrame ) e.getSource()).removeWindowListener(this); 193 } 194 } 195 } 196 197 private static int SLEEP_LENGTH=500; 198 private void sleep() { 199 201 202 try { 203 Thread.currentThread().sleep(SLEEP_LENGTH); 204 } catch (InterruptedException ie) { 205 } 207 208 209 try { 210 SwingUtilities.invokeAndWait(new Runnable () { 212 public void run() { 213 System.currentTimeMillis(); 214 } 215 }); 216 SwingUtilities.invokeAndWait(new Runnable () { 218 public void run() { 219 System.currentTimeMillis(); 220 } 221 }); 222 } catch (Exception e) { 223 } 224 225 226 } 227 228 229 private void clickOn(final SheetTable tb, final int row, final int col) throws Exception { 230 SwingUtilities.invokeAndWait(new Runnable () { 231 public void run() { 232 Rectangle r = tb.getCellRect(row, col, false); 233 Point toClick = r.getLocation(); 234 toClick.x += 15; 235 toClick.y +=3; 236 MouseEvent me = new MouseEvent (tb, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 237 tb.dispatchEvent(me); 238 } 239 }); 240 sleep(); 241 } 242 243 private void releaseKey(final Component target, final int key) throws Exception { 244 SwingUtilities.invokeAndWait(new Runnable () { 245 public void run() { 246 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key); 247 target.dispatchEvent(ke); 248 } 249 }); 250 sleep(); 251 } 252 253 private void pressKey(final Component target, final int key) throws Exception { 254 SwingUtilities.invokeAndWait(new Runnable () { 255 public void run() { 256 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key); 257 target.dispatchEvent(ke); 258 } 259 }); 260 sleep(); 261 } 262 263 private void typeKey(final Component target, final int key) throws Exception { 264 SwingUtilities.invokeAndWait(new Runnable () { 265 public void run() { 266 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key); 267 target.dispatchEvent(ke); 268 } 269 }); 270 sleep(); 271 } 272 273 public class TNode extends AbstractNode { 275 public TNode() { 277 super(Children.LEAF); 278 setName("TNode"); setDisplayName("TNode"); 280 } 281 public Node cloneNode() { 283 return new TNode(); 284 } 285 286 public void addProp(Node.Property p) { 287 props.put(p); 288 this.firePropertyChange(PROP_PROPERTY_SETS, null, null); 289 this.firePropertySetsChange(null, null); 290 } 291 292 Sheet sheet=null; 293 Sheet.Set props=null; 294 protected Sheet createSheet() { 296 sheet = super.createSheet(); 297 props = sheet.get(Sheet.PROPERTIES); 299 if (props == null) { 300 props = Sheet.createPropertiesSet(); 301 sheet.put(props); 302 } 303 props.put(tp); 304 return sheet; 305 } 306 public void fireMethod(String s, Object o1, Object o2) { 308 firePropertyChange(s,o1,o2); 309 } 310 } 311 312 public class TProperty extends PropertySupport { 314 private Object myValue = "Value"; 315 public TProperty(String name, boolean isWriteable) { 317 super(name, String .class, name, "", true, isWriteable); 318 } 319 public Object getValue() { 321 return myValue; 322 } 323 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 325 Object oldVal = myValue; 326 myValue = value; 327 tn.fireMethod(getName(), oldVal, myValue); 328 } 329 public PropertyEditor getPropertyEditor() { 331 return new BadEditor(); 332 } 333 334 } 335 336 337 public class BadEditor extends PropertyEditorSupport implements ExPropertyEditor { 338 PropertyEnv env; 339 340 public BadEditor() { 341 } 342 343 public String [] getTags() { 344 return null; 346 } 347 348 public void attachEnv(PropertyEnv env) { 349 this.env = env; 350 env.setState(env.STATE_INVALID); 351 } 352 353 public boolean supportsCustomEditor() { 354 return false; 355 } 356 357 public void setValue(Object newValue) { 358 super.setValue(newValue); 359 } 360 361 public void setAsText(String txt) { 362 IllegalArgumentException iae = new IllegalArgumentException ("Bad, bad, bad"); 363 UIException.annotateUser(iae, "bad, bad, bad", 364 "I can\'t be nice, I\'m the evil property editor.", 365 null, null); 366 throw iae; 367 } 368 } 369 370 371 private static TNode tn; 372 private static TProperty tp; 373 374 } 375 | Popular Tags |