1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Graphics ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyEditor ; 26 import java.beans.PropertyEditorSupport ; 27 import java.lang.reflect.InvocationTargetException ; 28 import javax.swing.JFrame ; 29 import javax.swing.SwingUtilities ; 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 38 public class IndexedPropertyTest extends ExtTestCase { 39 private PropertySheet ps = null; 40 public IndexedPropertyTest(String 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 55 protected void setUp() throws Exception { 56 te = new TEditor(); 58 tn = new TNode(); 60 61 final JFrame jf = new JFrame (); 65 ps = new PropertySheet(); 66 jf.getContentPane().setLayout(new BorderLayout ()); 67 jf.getContentPane().add(ps, BorderLayout.CENTER); 68 jf.setLocation(30,30); 69 jf.setSize(500,500); 70 71 SwingUtilities.invokeAndWait(new Runnable () { 72 public void run() { 73 ps.setNodes(new Node[] {tn}); 74 jf.show(); 76 } 77 }); 78 79 jf.show(); 80 new ExtTestCase.WaitWindow(jf); 81 82 try { 83 for (int i = 0; i < 10; i++) { 85 if (te.getAsText().equals("null")) { 86 Thread.sleep(1000); 88 } else break; 89 } 90 ensurePainted(ps); 91 92 } catch (Exception e) { 93 fail("FAILED - Exception thrown "+e.getClass().toString()); 94 } 95 } 96 97 private void ensurePainted(final PropertySheet ps) throws Exception { 98 SwingUtilities.invokeAndWait(new Runnable () { 102 public void run() { 103 Graphics g = ps.getGraphics(); 104 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight()); 105 } 106 }); 107 } 108 109 public void testIndexedProperty() throws Exception { 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 public class TNode extends AbstractNode { 121 public TNode() throws Exception { 123 super(Children.LEAF); 124 setName("TNode"); setDisplayName("TNode"); 126 } 127 public Node cloneNode() { 129 try { 130 return new TNode(); 131 } catch (Exception e) { 132 throw new RuntimeException ("Failed to clone node"); 133 } 134 } 135 136 public void destroy() { 137 fireNodeDestroyed(); 138 } 139 140 protected Sheet createSheet() { 142 Sheet sheet = super.createSheet(); 143 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 public void fireMethod(String s, Object o1, Object 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 value = new StringBuffer (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 getPropertyEditor() { 187 return null; 188 } 189 190 public Object getIndexedValue(int index) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 191 return new Character (value.charAt(index)); 192 } 193 194 public Object getValue() throws IllegalAccessException , InvocationTargetException { 195 return value.toString().toCharArray(); 196 } 197 198 public void setIndexedValue(int indx, Object val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 199 String old = value.toString(); 200 value.setCharAt(indx, ((Character ) val).charValue()); 201 tn.fireMethod(getName(), old, val); 202 } 203 204 public void setValue(Object val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 205 String old = value.toString(); 206 value = new StringBuffer (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 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 getPropertyEditor() { 226 return te; 227 } 228 229 public int hashCode() { 230 return 24; 231 } 232 } 233 234 public class TEditor extends PropertyEditorSupport implements ExPropertyEditor { 236 PropertyEnv env; 237 238 public TEditor() { 240 } 241 242 246 public void attachEnv(PropertyEnv env) { 247 this.env = env; 248 } 249 250 public boolean supportsCustomEditor() { 252 return false; 253 } 254 255 public void addPropertyChangeListener(PropertyChangeListener l) { 256 super.addPropertyChangeListener(l); 257 } 258 259 public void removePropertyChangeListener(PropertyChangeListener l) { 260 super.removePropertyChangeListener(l); 261 } 262 263 264 265 public void setValue(Object 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 |