1 19 20 package org.netbeans.spi.looks; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import org.netbeans.modules.looks.RegistryBridge; 26 import org.openide.ErrorManager; 27 import org.openide.filesystems.FileObject; 28 29 36 public abstract class Looks { 37 38 static final String CONTEXT="context"; 40 static final String LOOK_SELECTOR = "lookSelector"; 42 private static final String LOOK = "look"; private static final String DELEGATE = "delegateLook"; private static final String ALL_METHODS = "ALL_METHODS"; private static final String NO_METHODS = "NO_METHODS"; 48 49 private static final Look BEAN_LOOK = new org.netbeans.modules.looks.BeanLook( "JavaBeans" ); 51 52 54 55 private static final HashMap mapFilterMethods = new HashMap (16); 56 57 58 private Looks() { 59 } 60 61 63 65 static final Look composite(FileObject fo) throws IOException { 66 67 org.netbeans.modules.looks.RegistryBridge registryBridge = RegistryBridge.getDefault( fo ); 68 String contextName = readStringAttribute (fo, CONTEXT); 69 70 71 return new org.netbeans.modules.looks.CompositeLook ( 72 fo.getPath(), 73 new LookSelector( org.netbeans.modules.looks.SelectorImplFactory.context( registryBridge, contextName ) ) ); 74 } 75 76 78 83 84 86 static final Look filter(FileObject fo) throws IOException { 87 if (mapFilterMethods.isEmpty ()) 88 initFilterMethods (); 89 90 Look delegate = readLookAttribute (fo, DELEGATE); 91 92 long mask = Look.ALL_METHODS; 97 Boolean helpBool = null; 98 long helpLong; 99 helpBool = readBooleanAttribute (fo, ALL_METHODS); 100 mask = helpBool == null || helpBool.booleanValue () ? Look.ALL_METHODS : Look.NO_METHODS; 101 102 helpBool = readBooleanAttribute (fo, NO_METHODS); 103 mask = helpBool != null && helpBool.booleanValue () ? Look.NO_METHODS : mask; 104 105 String attr; 106 for (Enumeration attrs = fo.getAttributes (); attrs.hasMoreElements (); ) { 107 attr = (String )attrs.nextElement (); 108 if (mapFilterMethods.containsKey (attr)) { 109 helpBool = readBooleanAttribute (fo, attr); 110 helpLong = ((Long )mapFilterMethods.get (attr)).longValue (); 111 mask = helpBool == null ? mask : (helpBool.booleanValue () ? 112 mask | helpLong : 114 mask & ~helpLong 116 ); 117 } 118 } 119 120 return new org.netbeans.modules.looks.FilterLook ( fo.getName(), delegate, mask); 121 } 122 123 125 static final Look childrenSelectorProvider(FileObject fo) throws IOException { 126 127 Look look = null; 128 String lookName = readStringAttribute (fo, LOOK); 129 130 Object delegateObject = RegistryBridge.getDefault( fo ).resolve(lookName); 132 133 if ((delegateObject != null) && (delegateObject instanceof Look)) { 134 look = (Look)delegateObject; 135 } 136 if (look == null) { 137 ErrorManager.getDefault ().notify (new Exception ("Look not found at " + look)); return null; 139 } 140 141 LookSelector lookSelector = null; 142 String selectorName = readStringAttribute (fo, LOOK_SELECTOR); 143 144 delegateObject = RegistryBridge.getDefault( fo ).resolve (selectorName); 146 if ((delegateObject != null) && (delegateObject instanceof LookSelector)) { 147 lookSelector = (LookSelector)delegateObject; 148 } 149 150 if (lookSelector == null) { 151 ErrorManager.getDefault ().notify (new Exception ("LookSelector not found at " + lookSelector)); return null; 153 } 154 155 return new org.netbeans.modules.looks.ChildrenSelectorProvider( fo.getName(), look, lookSelector ); 156 } 157 158 159 161 175 public static final Look bean() { 176 return BEAN_LOOK; 177 } 178 179 228 public static final Look filter( String name, Look delegate, long mask ) { 229 return new org.netbeans.modules.looks.FilterLook( name, delegate, mask ); 230 } 231 232 260 public static final Look composite( String name, Look[] delegates) { 261 return new org.netbeans.modules.looks.CompositeLook( name, delegates ); 262 } 263 264 295 296 302 303 public static final Look childrenSelectorProvider( String name, Look look, LookSelector selector ) { 304 return new org.netbeans.modules.looks.ChildrenSelectorProvider( name, look, selector ); 305 } 306 307 308 310 static Boolean readBooleanAttribute(FileObject fo, String attribute) throws IOException { 311 Object value = fo.getAttribute (attribute); 312 if (value == null) 313 return null; 314 if (value instanceof Boolean ) 315 return (Boolean )value; 316 else 317 throw new IOException ("Attribute " + attribute + " is not Boolean but: " + value); } 319 320 static String readStringAttribute(FileObject fo, String attribute) throws IOException { 321 Object value = fo.getAttribute (attribute); 322 if (value == null) 323 return null; 324 if (value instanceof String ) 325 return (String )value; 326 else 327 throw new IOException ("Attribute " + attribute + " is not String but: " + value); } 329 330 331 static Object readLookOrSelectorAttribute (FileObject fo, String attribute, boolean look) throws IOException { 332 Object value = fo.getAttribute (attribute); 333 if (look) { 334 if (value instanceof Look) { 335 return value; 336 } 337 } else { 338 if (value instanceof LookSelector) { 339 return value; 340 } 341 } 342 343 if (!(value instanceof String )) { 344 throw new IOException ("Attribute " + attribute + " is not String but: " + value); } 346 347 Object delegate = RegistryBridge.getDefault( fo ).resolve ((String )value); 349 if (look) { 350 if (delegate instanceof Look) { 351 return delegate; 352 } 353 } 354 else { 355 if (delegate instanceof LookSelector) { 356 return delegate; 357 } 358 } 359 360 362 IOException newEx = new IOException ( 363 "Look/LookSelector " + value + " not found. Attribute " + attribute + " on " + fo); 365 throw newEx; 366 } 367 368 static Look readLookAttribute (FileObject fo, String attribute) throws IOException { 369 return (Look)readLookOrSelectorAttribute (fo, attribute, true); 370 } 371 372 373 static private final void initFilterMethods () { 374 mapFilterMethods.put ("GET_LOOKUP_ITEMS", new Long (Look.GET_LOOKUP_ITEMS)); mapFilterMethods.put ("GET_NAME", new Long (Look.GET_NAME)); mapFilterMethods.put ("RENAME", new Long (Look.RENAME)); mapFilterMethods.put ("GET_DISPLAY_NAME", new Long (Look.GET_DISPLAY_NAME)); mapFilterMethods.put ("GET_SHORT_DESCRIPTION", new Long (Look.GET_SHORT_DESCRIPTION)); mapFilterMethods.put ("GET_ICON", new Long (Look.GET_ICON)); mapFilterMethods.put ("GET_OPENED_ICON", new Long (Look.GET_OPENED_ICON)); mapFilterMethods.put ("GET_HELP_CTX", new Long (Look.GET_HELP_CTX)); mapFilterMethods.put ("GET_CHILD_OBJECTS", new Long (Look.GET_CHILD_OBJECTS)); mapFilterMethods.put ("GET_NEW_TYPES", new Long (Look.GET_NEW_TYPES)); mapFilterMethods.put ("GET_ACTIONS", new Long (Look.GET_ACTIONS)); mapFilterMethods.put ("GET_CONTEXT_ACTIONS", new Long (Look.GET_CONTEXT_ACTIONS)); mapFilterMethods.put ("GET_DEFAULT_ACTION", new Long (Look.GET_DEFAULT_ACTION)); mapFilterMethods.put ("GET_PROPERTY_SETS", new Long (Look.GET_PROPERTY_SETS)); mapFilterMethods.put ("GET_CUSTOMIZER", new Long (Look.GET_CUSTOMIZER)); mapFilterMethods.put ("CAN_RENAME", new Long (Look.CAN_RENAME)); mapFilterMethods.put ("CAN_DESTROY", new Long (Look.CAN_DESTROY)); mapFilterMethods.put ("CAN_COPY", new Long (Look.CAN_COPY)); mapFilterMethods.put ("CAN_CUT", new Long (Look.CAN_CUT)); mapFilterMethods.put ("GET_PASTE_TYPES", new Long (Look.GET_PASTE_TYPES)); mapFilterMethods.put ("GET_DROP_TYPE", new Long (Look.GET_DROP_TYPE)); mapFilterMethods.put ("CLIPBOARD_COPY", new Long (Look.CLIPBOARD_COPY)); mapFilterMethods.put ("CLIPBOARD_CUT", new Long (Look.CLIPBOARD_CUT)); mapFilterMethods.put ("DRAG", new Long (Look.DRAG)); mapFilterMethods.put ("DESTROY", new Long (Look.DESTROY)); mapFilterMethods.put ("HAS_CUSTOMIZER", new Long (Look.HAS_CUSTOMIZER)); } 403 404 } 405 | Popular Tags |