1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.awt.Container ; 25 import java.awt.Graphics ; 26 import java.awt.KeyboardFocusManager ; 27 import java.awt.Window ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.MissingResourceException ; 30 import javax.swing.AbstractButton ; 31 import javax.swing.Action ; 32 import javax.swing.JDialog ; 33 import javax.swing.JFrame ; 34 import javax.swing.SwingUtilities ; 35 import org.openide.nodes.AbstractNode; 36 import org.openide.nodes.Children; 37 import org.openide.nodes.Node; 38 import org.openide.nodes.PropertySupport; 39 import org.openide.nodes.Sheet; 40 import org.openide.util.NbBundle; 41 42 public class RestoreDefaultValueTest extends ExtTestCase { 44 private PropertySheet ps = null; 45 public RestoreDefaultValueTest(String name) { 46 super(name); 47 } 48 49 protected boolean runInEQ() { 50 return false; 51 } 52 53 static { 54 ExtTestCase.installCorePropertyEditors(); 55 } 56 57 private static boolean setup = false; 58 63 protected void setUp() throws Exception { 64 tn = new TNode(); 66 67 68 final JFrame jf = new JFrame (); 72 ps = new PropertySheet(); 73 jf.getContentPane().setLayout(new BorderLayout ()); 74 jf.getContentPane().add(ps, BorderLayout.CENTER); 75 jf.setLocation(30,30); 76 jf.setSize(500,500); 77 78 SwingUtilities.invokeAndWait(new Runnable () { 79 public void run() { 80 ps.setNodes(new Node[] {tn}); 81 jf.show(); 83 jf.toFront(); 84 } 85 }); 86 87 new ExtTestCase.WaitWindow(jf); 88 89 try { 90 ensurePainted(ps); 91 } catch (Exception e) { 92 fail("FAILED - Exception thrown "+e.getClass().toString()); 93 } 94 } 95 96 private void ensurePainted(final PropertySheet ps) throws Exception { 97 SwingUtilities.invokeAndWait(new Runnable () { 101 public void run() { 102 Graphics g = ps.getGraphics(); 103 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight()); 104 } 105 }); 106 } 107 108 109 public void testRestoreDefaultValueWorks() throws Exception { 110 System.err.println("testRestoreDefaultValueWorks"); 111 if (!super.canSafelyRunFocusTests()) { 112 return; 113 } 114 115 sleep(); 116 sleep(); 117 sleep(); 118 119 assertNotNull("Property not created", tp); 120 121 final Action invoke = ps.table.getActionMap().get("invokeCustomEditor"); 122 assertNotNull("Incompatible change - no action from table for the key" + 123 "\"invokeCustomEditor\".", invoke); 124 125 pressCell(ps.table, 1, 1); 126 127 Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); 128 129 130 sleep(); 131 sleep(); 132 SwingUtilities.invokeLater(new Runnable () { 133 public void run() { 134 invoke.actionPerformed(null); 135 } 136 }); 137 138 int ct = 0; 139 while (!(w instanceof JDialog )) { 140 if (ct++ > 10000) { 141 fail("No dialog ever shown"); 142 } 143 sleep(); 144 w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); 145 } 146 sleep(); 147 System.out.println(" Dlg now showing"); 148 final AbstractButton jb = findResetToDefaultButton((JDialog )w); 149 150 if (jb == null) { 151 fail("Could not find Reset To Default Button in dialog"); 152 } 153 154 SwingUtilities.invokeAndWait(new Runnable () { 155 public void run() { 156 jb.doClick(); 157 } 158 }); 159 160 161 162 Thread.currentThread().sleep(10000); 163 164 } 165 166 private AbstractButton findResetToDefaultButton(JDialog jd) { 167 String txt = null; 168 try { 169 txt = NbBundle.getMessage(PropertyDialogManager.class, "CTL_Default"); 170 } catch (MissingResourceException mre) { 171 mre.printStackTrace(); 172 fail("Bundle key CTL_DEFAULT missing from org.openide.explorer.propertysheet.Bundle.properties"); 173 } 174 return findButton(jd.getContentPane(), txt); 175 } 176 177 private AbstractButton findButton(Container c, String s) { 178 if (c instanceof AbstractButton && s.equals(((AbstractButton ) c).getText())) { 179 return ((AbstractButton ) c); 180 } else { 181 Component [] cs = c.getComponents(); 182 for (int i=0; i < cs.length; i++) { 183 if (cs[i] instanceof Container ) { 184 AbstractButton result = findButton((Container ) cs[i], s); 185 if (result != null) { 186 return result; 187 } 188 } 189 } 190 } 191 return null; 192 } 193 194 public class TNode extends AbstractNode { 196 public TNode() { 198 super(Children.LEAF); 199 setName("TNode"); setDisplayName("TNode"); 201 } 202 public Node cloneNode() { 204 return new TNode(); 205 } 206 207 public void destroy() { 208 fireNodeDestroyed(); 209 } 210 211 protected Sheet createSheet() { 213 Sheet sheet = super.createSheet(); 214 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 216 if (props == null) { 217 props = Sheet.createPropertiesSet(); 218 sheet.put(props); 219 } 220 tp = new TProperty("property", true); 221 props.put(tp); 222 return sheet; 223 } 224 public void fireMethod(String s, Object o1, Object o2) { 226 firePropertyChange(s,o1,o2); 227 } 228 } 229 230 public class TProperty extends PropertySupport { 232 private String myValue = "Value"; 233 public TProperty(String name, boolean isWriteable) { 235 super(name, String .class, name, "", true, isWriteable); 236 } 237 public Object getValue() { 239 return myValue; 240 } 241 242 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 244 Object oldVal = myValue; 245 myValue = (String ) value; 246 tn.fireMethod(getName(), oldVal, myValue); 247 } 248 249 public boolean supportsDefaultValue() { 250 return true; 251 } 252 253 private boolean defValue = false; 254 public boolean isDefaultValue() { 255 return defValue; 256 } 257 258 private boolean rdv = false; 259 public void restoreDefaultValue() { 260 defValue = true; 261 rdv = true; 262 System.err.println("RestoreDefaultValue"); 263 try { 264 setValue("default"); 265 } catch (Exception e) { 266 e.printStackTrace(); 267 throw new RuntimeException (e.getMessage()); 268 } 269 } 270 271 public void assertRestoreDefaultValueCalled() { 272 assertTrue("Restore default value not called", rdv); 273 rdv = false; 274 } 275 276 public void assertRestoreDefaultValueNotCalled() { 277 assertFalse("Restore default value not called", rdv); 278 } 279 } 280 281 private TNode tn; 282 private TProperty tp; 283 } 284 | Popular Tags |