1 19 20 package org.openide.awt; 21 22 23 import java.awt.*; 24 import java.awt.event.*; 25 import java.util.*; 26 import javax.accessibility.*; 27 import javax.swing.*; 28 import javax.swing.JComponent.AccessibleJComponent; 29 import javax.swing.border.Border ; 30 import org.openide.cookies.InstanceCookie; 31 import org.openide.filesystems.*; 32 import org.openide.loaders.*; 33 import org.openide.util.*; 34 35 39 public final class ToolbarPool extends JComponent implements Accessible { 40 41 private static ToolbarPool defaultPool; 42 43 44 private Folder instance; 45 46 47 private DataFolder folder; 48 49 50 private Map<String , Toolbar> toolbars; 51 52 private Map<String , ToolbarPool.Configuration> toolbarConfigs; 53 54 55 private String name = ""; 57 58 private Component center; 59 60 61 private PopupListener listener; 62 63 64 private AccessibleContext toolbarAccessibleContext; 65 66 67 public static final String DEFAULT_CONFIGURATION = "Standard"; 69 private TPTaskListener taskListener; 70 71 72 private int preferredIconSize = 24; 73 74 78 public static synchronized ToolbarPool getDefault () { 79 if (defaultPool == null) { 80 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("Toolbars"); if (fo == null) throw new IllegalStateException ("No Toolbars/"); DataFolder folder = DataFolder.findFolder(fo); 83 defaultPool = new ToolbarPool(folder); 84 defaultPool.instance.recreate(); 87 } 88 return defaultPool; 89 } 90 91 static final long serialVersionUID =3420915387298484008L; 92 93 94 101 public ToolbarPool (DataFolder df) { 102 folder = df; 103 104 setLayout (new BorderLayout ()); 105 listener = new PopupListener(); 106 toolbars = new TreeMap<String , Toolbar>(); 107 toolbarConfigs = new TreeMap<String , ToolbarPool.Configuration>(); 108 109 instance = new Folder (df); 110 111 getAccessibleContext().setAccessibleName(instance.instanceName()); 112 getAccessibleContext().setAccessibleDescription(instance.instanceName()); 113 114 if ("Windows".equals(UIManager.getLookAndFeel().getID())) { 115 if( isXPTheme() ) { 116 setBorder(BorderFactory.createCompoundBorder( 118 upperBorder, 119 BorderFactory.createCompoundBorder( 120 BorderFactory.createMatteBorder(0, 0, 1, 0, 121 fetchColor("controlShadow", Color.DARK_GRAY)), 122 BorderFactory.createMatteBorder(0, 0, 1, 0, mid)) 123 )); } else { 125 setBorder( BorderFactory.createEtchedBorder() ); 126 } 127 } else if ("GTK".equals(UIManager.getLookAndFeel().getID())) { 128 setBorder (BorderFactory.createEmptyBorder(0, 0, 0, 0)); 130 } 131 } 132 133 139 public int getPreferredIconSize () { 140 return preferredIconSize; 141 } 142 143 149 public void setPreferredIconSize (int preferredIconSize) throws IllegalArgumentException { 150 if ((preferredIconSize != 16) && (preferredIconSize != 24)) { 151 throw new IllegalArgumentException ("Unsupported argument value:" + preferredIconSize); } 153 this.preferredIconSize = preferredIconSize; 154 } 155 156 public Border getBorder() { 157 if (center != null && center instanceof Container && 161 ((Container)center).getComponentCount() > 0) { 162 163 boolean show = false; 164 for (int i=0; i < ((Container)center).getComponentCount(); i++) { 165 Component c = ((Container)center).getComponent(i); 166 if (c.isVisible()) { 167 show = true; 168 break; 169 } 170 } 171 if (show) { 172 return super.getBorder(); 173 } 174 } 175 return lowerBorder; 176 } 177 178 private static Color fetchColor (String key, Color fallback) { 179 Color result = (Color) UIManager.get(key); 182 if (result == null) { 183 result = fallback; 184 } 185 return result; 186 } 187 188 private static Color mid; 189 static { 190 Color lo = fetchColor("controlShadow", Color.DARK_GRAY); Color hi = fetchColor("control", Color.GRAY); 193 int r = (lo.getRed() + hi.getRed()) / 2; 194 int g = (lo.getGreen() + hi.getGreen()) / 2; 195 int b = (lo.getBlue() + hi.getBlue()) / 2; 196 mid = new Color(r, g, b); 197 } 198 199 private static final Border lowerBorder = BorderFactory.createCompoundBorder( 200 BorderFactory.createMatteBorder(0, 0, 1, 0, 201 fetchColor("controlShadow", Color.DARK_GRAY)), 202 BorderFactory.createMatteBorder(0, 0, 1, 0, mid)); 204 private static final Border upperBorder = BorderFactory.createCompoundBorder( 205 BorderFactory.createMatteBorder(1, 0, 0, 0, 206 fetchColor("controlShadow", Color.DARK_GRAY)), 207 BorderFactory.createMatteBorder(1, 0, 0, 0, 208 fetchColor("controlLtHighlight", Color.WHITE))); 210 211 212 public final void waitFinished () { 213 instance.instanceFinished (); 214 } 215 216 220 void update (Map<String , Toolbar> toolbars, Map<String , ToolbarPool.Configuration> conf) { 221 this.toolbars = toolbars; 222 this.toolbarConfigs = conf; 223 224 if (!"".equals(name)) { 225 setConfiguration (name); 226 } 227 } 228 229 230 private synchronized void updateDefault () { 231 Toolbar[] toolbars = getToolbars (); 232 name = ""; 234 if (toolbars.length == 1) { 235 revalidate(toolbars[0]); 236 } else { 237 JPanel tp = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); 238 for (int i = 0; i < toolbars.length; i++) { 239 tp.add(toolbars[i]); 240 } 241 revalidate(tp); 242 } 243 } 244 245 248 private synchronized void activate (Configuration c) { 249 Component comp = c.activate (); 250 name = c.getName(); 251 revalidate (comp); 252 } 253 254 255 public void setToolbarsListener (Toolbar.DnDListener l) { 256 for (Toolbar t: toolbars.values()) { 257 t.setDnDListener (l); 258 } 259 } 260 261 262 private void revalidate (Component c) { 263 if (c != center) { 264 if (center != null) { 266 remove (center); 267 center.removeMouseListener (listener); 268 } 269 add (center = c, BorderLayout.CENTER); 270 center.addMouseListener (listener); 271 272 } 277 } 278 279 284 public final Toolbar findToolbar (String name) { 285 return toolbars.get (name); 286 } 287 288 292 public final String getConfiguration () { 293 return name; 294 } 295 296 300 public final void setConfiguration (String n) { 301 String old = name; 302 303 if (!instance.isFinished()) { 305 if (taskListener == null) { 306 taskListener = new TPTaskListener(); 307 instance.addTaskListener(taskListener); 308 } 309 taskListener.setConfiguration(n); 310 return; 311 } 312 if (taskListener != null) { 313 instance.removeTaskListener(taskListener); 314 taskListener = null; 315 } 316 317 Configuration config = null; 318 if (n != null) { 319 config = toolbarConfigs.get (n); 320 } 321 if (config != null) { activate (config); 323 } else if (toolbarConfigs.isEmpty()) { updateDefault (); 325 } else { 326 config = toolbarConfigs.get (DEFAULT_CONFIGURATION); 329 if (config == null) { 330 config = toolbarConfigs.values().iterator().next(); 331 } 332 activate (config); 333 } 334 335 firePropertyChange("configuration", old, name); 336 } 337 338 341 public final DataFolder getFolder() { 342 return folder; 343 } 344 345 349 public final synchronized Toolbar[] getToolbars() { 350 Toolbar[] arr = new Toolbar[toolbars.size ()]; 351 return toolbars.values ().toArray (arr); 352 } 353 354 357 public final synchronized String [] getConfigurations () { 358 ArrayList<String > list = new ArrayList<String >( toolbarConfigs.keySet() ); 359 Collections.sort( list ); 360 String [] arr = new String [ list.size() ]; 361 return list.toArray( arr ); 362 } 363 364 367 public AccessibleContext getAccessibleContext () { 368 if(toolbarAccessibleContext == null) { 369 toolbarAccessibleContext = new AccessibleJComponent() { 370 public AccessibleRole getAccessibleRole() { 371 return AccessibleRole.TOOL_BAR; 372 } 373 }; 374 } 375 return toolbarAccessibleContext; 376 } 377 378 382 private static Boolean isXP = null; 383 private static boolean isXPTheme () { 384 if (isXP == null) { 385 Boolean xp = (Boolean )Toolkit.getDefaultToolkit(). 386 getDesktopProperty("win.xpstyle.themeActive"); isXP = Boolean.TRUE.equals(xp)? Boolean.TRUE : Boolean.FALSE; 388 } 389 return isXP.booleanValue(); 390 } 391 392 395 boolean isInEditMode() { 396 return null != getClientProperty( "editMode" ); 397 } 398 399 404 private class TPTaskListener implements TaskListener { 405 private String conf; 406 407 TPTaskListener() {} 408 409 public void taskFinished(Task task) { 410 ToolbarPool.this.setConfiguration(conf); 411 conf = null; 412 } 413 414 void setConfiguration(String conf) { 415 if(this.conf == null) { 417 this.conf = conf; 418 } 419 } 420 } 421 422 426 private class Folder extends FolderInstance { 427 private WeakHashMap<DataFolder, InstanceCookie> foldersCache = 428 new WeakHashMap<DataFolder, InstanceCookie> (15); 429 430 public Folder (DataFolder f) { 431 super (f); 432 } 433 434 438 public String instanceName () { 439 return instanceClass().getName(); 440 } 441 442 446 public Class instanceClass () { 447 return ToolbarPool.class; 448 } 449 450 455 protected InstanceCookie acceptCookie (InstanceCookie cookie) 456 throws java.io.IOException , ClassNotFoundException { 457 Class cls = cookie.instanceClass(); 458 if (ToolbarPool.Configuration.class.isAssignableFrom (cls)) { 459 return cookie; 460 } 461 if (Component.class.isAssignableFrom (cls)) { 462 return cookie; 463 } 464 return null; 465 } 466 467 473 protected InstanceCookie acceptFolder (DataFolder df) { 474 InstanceCookie ic = foldersCache.get (df); 475 if (ic == null) { 476 ic = (FolderInstance)new Toolbar (df, true).waitFinished (); 477 foldersCache.put (df, ic); 478 } 479 return ic; 480 } 481 482 488 protected Object createInstance (InstanceCookie[] cookies) 489 throws java.io.IOException , ClassNotFoundException { 490 final int length = cookies.length; 491 492 final Map<String , Toolbar> toolbars = new TreeMap<String , Toolbar> (); 493 final Map<String , Configuration> conf = new TreeMap<String , Configuration> (); 494 495 for (int i = 0; i < length; i++) { 496 try { 497 java.lang.Object obj = cookies[i].instanceCreate(); 498 499 if (obj instanceof org.openide.awt.Toolbar) { 500 org.openide.awt.Toolbar toolbar = (org.openide.awt.Toolbar) obj; 501 502 toolbar.removeMouseListener(listener); 504 toolbar.addMouseListener(listener); 505 toolbars.put(toolbar.getName(), toolbar); 506 continue; 507 } 508 if (obj instanceof org.openide.awt.ToolbarPool.Configuration) { 509 org.openide.awt.ToolbarPool.Configuration config = (org.openide.awt.ToolbarPool.Configuration) obj; 510 java.lang.String name = config.getName(); 511 512 if (name == null) { 513 name = cookies[i].instanceName(); 514 } 515 conf.put(name, config); 516 continue; 517 } 518 if (obj instanceof java.awt.Component ) { 519 java.awt.Component comp = (java.awt.Component ) obj; 520 java.lang.String name = comp.getName(); 521 522 if (name == null) { 523 name = cookies[i].instanceName(); 524 } 525 conf.put(name, 526 new org.openide.awt.ToolbarPool.ComponentConfiguration(comp)); 527 continue; 528 } 529 } 530 catch (java.io.IOException ex) { 531 Exceptions.printStackTrace(ex); 532 } 533 catch (java.lang.ClassNotFoundException ex) { 534 Exceptions.printStackTrace(ex); 535 } 536 } 537 update (toolbars, conf); 538 539 return ToolbarPool.this; 540 } 541 542 544 protected Task postCreationTask (Runnable run) { 545 return new AWTTask (run); 546 } 547 548 } 550 551 554 private class PopupListener extends MouseUtils.PopupMouseAdapter { 555 PopupListener() {} 556 559 protected void showPopup (MouseEvent e) { 560 Configuration conf = toolbarConfigs.get (name); 561 if (conf != null) { 562 JPopupMenu pop = conf.getContextMenu(); 563 pop.show (e.getComponent (), e.getX (), e.getY ()); 564 } 565 } 566 } 568 569 572 public static interface Configuration { 573 577 public abstract Component activate (); 578 579 582 public abstract String getName (); 583 584 590 public abstract JPopupMenu getContextMenu (); 591 } 592 593 594 596 private static final class ComponentConfiguration extends JPopupMenu 597 implements Configuration, ActionListener { 598 private Component comp; 599 600 ComponentConfiguration() {} 601 602 static final long serialVersionUID =-409474484612485719L; 603 604 public ComponentConfiguration (Component comp) { 605 this.comp = comp; 606 } 607 608 609 public Component activate () { 610 return comp; 611 } 612 613 615 public String getName () { 616 return comp.getName (); 617 } 618 619 621 public JPopupMenu getContextMenu () { 622 removeAll (); 623 624 Iterator it = Arrays.asList (ToolbarPool.getDefault ().getConfigurations ()).iterator (); 626 ButtonGroup bg = new ButtonGroup (); 627 String current = ToolbarPool.getDefault ().getConfiguration (); 628 while (it.hasNext()) { 629 final String name = (String )it.next (); 630 JRadioButtonMenuItem mi = new JRadioButtonMenuItem (name, (name.compareTo (current) == 0)); 631 mi.addActionListener (this); 632 bg.add (mi); 633 this.add (mi); 634 } 635 636 return this; 637 } 638 639 641 public void actionPerformed (ActionEvent evt) { 642 ToolbarPool.getDefault().setConfiguration (evt.getActionCommand ()); 643 } 644 645 } 646 647 } 649 | Popular Tags |