1 19 20 package org.netbeans.spi.looks; 21 22 import javax.swing.Action ; 23 import java.beans.BeanInfo ; 24 import java.text.MessageFormat ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import org.netbeans.modules.looks.RegistryBridge; 28 import org.openide.util.Utilities; 29 import org.openide.util.Lookup; 30 31 38 public abstract class DefaultLook extends Look { 39 42 private static final MessageFormat [] icons = { 43 new MessageFormat ("{0}.gif"), new MessageFormat ("{0}32.gif"), new MessageFormat ("{0}.gif"), new MessageFormat ("{0}32.gif"), new MessageFormat ("{0}Open.gif"), new MessageFormat ("{0}Open32.gif"), new MessageFormat ("{0}Open.gif"), new MessageFormat ("{0}Open32.gif"), }; 60 63 private static final int ICON_BASE = -1; 64 65 private static final int OPENED_ICON_BASE = 3; 66 67 private RegistryBridge registryBridge; 68 69 72 public DefaultLook(String name) { 73 this( RegistryBridge.getDefault( null ), name ); 74 } 75 76 DefaultLook(RegistryBridge registryBridge, String name) { 77 super( name ); 78 this.registryBridge = registryBridge; 79 } 80 81 83 89 public java.awt.Image getIcon(Object representedObject, int type, Lookup env ) { 90 return findIcon ( representedObject, type, ICON_BASE, env); 91 } 92 93 99 public java.awt.Image getOpenedIcon(Object representedObject, int type, Lookup env ) { 100 return findIcon ( representedObject, type, OPENED_ICON_BASE, env); 101 } 102 103 105 111 public Action [] getActions(Object representedObject, Lookup env ) { 112 return actionsForContext (registryBridge, actionBase (representedObject, false, env )); 113 } 114 115 121 public Action [] getContextActions(Object representedObject, Lookup env ) { 122 return actionsForContext (registryBridge, actionBase (representedObject, true, env )); 123 } 124 125 130 public Action getDefaultAction(Object representedObject, Lookup env ) { 131 Action [] arr = getActions (representedObject, env ); 132 return arr != null && arr.length > 0 ? arr[0] : null; 133 } 134 135 137 144 public boolean hasCustomizer(Object representedObject, Lookup env ) { 145 return getCustomizer (representedObject, env ) != null; 146 } 147 148 149 151 167 protected String iconBase ( Object representedObject, Lookup env ) { 168 return null; 169 } 170 171 185 protected String actionBase ( Object representedObject, boolean context, Lookup env ) { 186 return "Looks/Actions/" + getClass ().getName ().replace ('.', '/'); } 188 189 191 195 private static Action [] actionsForContext (RegistryBridge registryBridge, String name) { 196 Enumeration en = registryBridge.getObjects(name, null); 197 if (!en.hasMoreElements ()) { 198 return null; 199 } 200 201 ArrayList arr = new ArrayList (); 202 while (en.hasMoreElements()) { 203 arr.add (en.nextElement ()); 204 } 205 206 return (Action [])arr.toArray (new Action [arr.size ()]); 207 } 208 209 214 private java.awt.Image findIcon ( Object representedObject, int type, int ib, Lookup env) { 215 String [] base = { iconBase (representedObject, env ) }; 216 if (base[0] == null) { 217 return null; 218 } 219 220 String res = icons[type + ib].format (base); 221 java.awt.Image im = Utilities.loadImage (res); 222 223 if (im != null) return im; 224 225 res = icons[BeanInfo.ICON_COLOR_16x16 + ib].format (base); 227 228 im = Utilities.loadImage (res); 229 230 if (im != null) return im; 231 232 if (ib == OPENED_ICON_BASE) { 233 return findIcon (representedObject, type, ICON_BASE, env); 235 } 236 237 return null; 239 } 240 241 } 242 | Popular Tags |