1 19 20 package org.openide.util.actions; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Graphics ; 26 import java.awt.Image ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.image.BufferedImage ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Set ; 35 import java.util.logging.Logger ; 36 import javax.swing.Action ; 37 import javax.swing.Icon ; 38 import javax.swing.ImageIcon ; 39 import javax.swing.JComponent ; 40 import javax.swing.JLabel ; 41 import javax.swing.JPopupMenu ; 42 import javax.swing.JToolBar ; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.SharedClassObject; 45 import org.openide.util.Utilities; 46 47 61 public abstract class SystemAction extends SharedClassObject implements Action , HelpCtx.Provider { 62 63 private static final Logger LOG = Logger.getLogger(SystemAction.class.getName()); 64 65 66 public static final String PROP_ENABLED = "enabled"; 68 69 public static final String PROP_ICON = "icon"; 71 72 private static final String PROP_ICON_TEXTUAL = "iconTextual"; private static Icon BLANK_ICON = new Icon () { 74 public void paintIcon(Component c, Graphics g, int x, int y) {} 75 public int getIconWidth() { 76 return 16; 77 } 78 public int getIconHeight() { 79 return 16; 80 } 81 }; 82 private static final Set <String > relativeIconResourceClasses = new HashSet <String >(200); 83 84 private static final long serialVersionUID = -8361232596876856810L; 86 87 96 public static <T extends SystemAction> T get(Class <T> actionClass) { 97 return findObject(actionClass, true); 98 } 99 100 107 public abstract String getName(); 108 109 112 public abstract HelpCtx getHelpCtx(); 113 114 117 public boolean isEnabled() { 118 return getProperty(PROP_ENABLED).equals(Boolean.TRUE); 119 } 120 121 124 public void setEnabled(boolean value) { 125 putProperty(PROP_ENABLED, value ? Boolean.TRUE : Boolean.FALSE, true); 126 } 127 128 134 public final void putValue(String name, Object value) { 135 putProperty(name, value, true); 136 137 } 140 141 147 public final Object getValue(String name) { 148 if ("iconBase".equals(name)) { 150 return iconResource(); 151 } 152 153 Object val = getProperty(name); 154 155 if (val == null) { 156 if (NAME.equals(name)) { 157 val = getName(); 158 } else if (SMALL_ICON.equals(name)) { 159 val = getIcon(); 160 } 161 } 162 163 return val; 164 } 165 166 174 public abstract void actionPerformed(ActionEvent ev); 175 176 179 @Override 180 protected void initialize() { 181 putProperty(PROP_ENABLED, Boolean.TRUE); 182 183 super.initialize(); 184 } 185 186 189 @Override 190 protected boolean clearSharedData() { 191 return false; 192 } 193 194 197 public final void setIcon(Icon icon) { 198 putProperty(PROP_ICON, icon, true); 199 putProperty(PROP_ICON_TEXTUAL, icon); 200 } 201 202 206 public final Icon getIcon() { 207 return getIcon(false); 208 } 209 210 216 public final Icon getIcon(boolean createLabel) { 217 synchronized (getLock()) { 218 Icon img = (Icon ) getProperty(createLabel ? PROP_ICON_TEXTUAL : PROP_ICON); 219 220 if (img == null) { 221 String resName = iconResource(); 223 224 if (resName != null) { 225 if (resName.indexOf('/') == -1) { 226 String clazz = getClass().getName(); 231 URL u = getClass().getResource(resName); 232 233 if (u != null) { 234 img = new ImageIcon (u); 235 236 if (relativeIconResourceClasses.add(clazz)) { 237 LOG.warning("Deprecated relative path in " + clazz + ".iconResource (cf. #20072)"); 238 } 239 } else { 240 LOG.warning("No such icon from " + clazz + ": " + resName); 241 } 242 } else { 243 Image i = Utilities.loadImage(resName, true); 245 246 if (i != null) { 247 img = new ImageIcon (i); 249 } else { 250 URL u = getClass().getResource(resName); 252 String clazz = getClass().getName(); 253 254 if (u != null) { 255 img = new ImageIcon (u); 257 258 if (relativeIconResourceClasses.add(clazz)) { 259 LOG.warning("Deprecated relative path in " + clazz + ".iconResource (cf. #26887)"); 260 } 261 } else { 262 LOG.warning("No such icon from " + clazz + ": " + resName); 264 } 265 } 266 } 267 268 putProperty(PROP_ICON, img); 269 putProperty(PROP_ICON_TEXTUAL, img); 270 } 271 } 272 273 if (img == null) { 274 if (createLabel) { 275 String text = getName(); 276 if (text.endsWith("...")) { text = text.substring(0, text.length() - 3); 278 } 279 text = text.trim(); 280 int ampr = text.indexOf('&'); 281 if (ampr != -1) { 282 text = new StringBuffer (text).deleteCharAt(ampr).toString(); 283 } 284 img = new ComponentIcon(new JLabel (text)); 285 putProperty(PROP_ICON_TEXTUAL, img); 286 } else { 287 img = BLANK_ICON; 288 putProperty(PROP_ICON, img); 289 } 290 } 291 292 return img; 293 } 294 } 295 296 310 protected String iconResource() { 311 return null; 312 } 313 314 320 public static JToolBar createToolbarPresenter(SystemAction[] actions) { 321 JToolBar p = new JToolBar (); 322 for (SystemAction action : actions) { 323 if (action == null) { 324 p.addSeparator(); 325 } else if (action instanceof Presenter.Toolbar) { 326 p.add(((Presenter.Toolbar) action).getToolbarPresenter()); 327 } 328 } 329 return p; 330 } 331 332 337 public static SystemAction[] linkActions(SystemAction[] actions1, SystemAction[] actions2) { 338 List <SystemAction> l = new ArrayList <SystemAction>(Arrays.asList(actions1)); 339 l.addAll(Arrays.asList(actions2)); 340 341 return l.toArray(actions1); 342 } 343 344 350 @Deprecated 351 public static JPopupMenu createPopupMenu(SystemAction[] actions) { 352 return Utilities.actionsToPopup(actions, Utilities.actionsGlobalContext()); 353 } 354 355 358 private static class ComponentIcon extends ImageIcon { 359 private JComponent comp; 360 private BufferedImage image; 361 362 366 public ComponentIcon(JComponent comp) { 367 if (comp.getParent() != null) { 368 throw new IllegalArgumentException (); 369 } 370 371 this.comp = comp; 372 373 Dimension size = comp.getPreferredSize(); 374 375 comp.setSize(Math.max(size.width, 16), Math.max(size.height, 16)); 378 } 379 380 protected void loadImage(Image i) { 381 } 382 383 public void paintIcon(Component c, Graphics g, int x, int y) { 384 comp.setBackground(c.getBackground()); 386 comp.setForeground(c.getForeground()); 387 388 Graphics clip = g.create(x, y, getIconWidth(), getIconHeight()); 389 comp.paint(clip); 390 } 391 392 public int getIconWidth() { 393 return comp.getWidth(); 394 } 395 396 public int getIconHeight() { 397 return comp.getHeight(); 398 } 399 400 public Image getImage() { 404 if (image == null) { 405 image = new BufferedImage (getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB); 406 407 comp.setForeground(Color.black); 411 comp.paint(image.getGraphics()); 412 } 413 414 return image; 415 } 416 } 417 } 418 | Popular Tags |