1 19 20 package org.netbeans.swing.tabcontrol; 21 22 import javax.accessibility.Accessible ; 23 import javax.accessibility.AccessibleRole ; 24 import javax.swing.event.ChangeEvent ; 25 import org.netbeans.swing.tabcontrol.event.TabActionEvent; 26 import org.netbeans.swing.tabcontrol.plaf.AquaEditorTabDisplayerUI; 27 import org.netbeans.swing.tabcontrol.plaf.AquaViewTabDisplayerUI; 28 import org.netbeans.swing.tabcontrol.plaf.BasicSlidingTabDisplayerUI; 29 import org.netbeans.swing.tabcontrol.plaf.WinClassicEditorTabDisplayerUI; 30 import org.netbeans.swing.tabcontrol.plaf.WinClassicViewTabDisplayerUI; 31 32 import javax.swing.*; 33 import javax.swing.plaf.ComponentUI ; 34 import java.awt.*; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.MouseEvent ; 37 import java.util.ArrayList ; 38 import java.util.Collections ; 39 import java.util.List ; 40 import javax.accessibility.AccessibleContext ; 41 import javax.accessibility.AccessibleSelection ; 42 import javax.swing.event.ChangeListener ; 43 import org.netbeans.swing.tabcontrol.plaf.ToolbarTabDisplayerUI; 44 import org.netbeans.swing.tabcontrol.plaf.WinXPEditorTabDisplayerUI; 45 import org.netbeans.swing.tabcontrol.plaf.WinXPViewTabDisplayerUI; 46 47 48 69 public final class TabDisplayer extends JComponent implements Accessible { 70 71 private boolean initialized = false; 72 private TabDataModel model; 73 private SingleSelectionModel sel = null; 74 private boolean active; 75 private final int type; 76 77 82 public static final int TYPE_VIEW = TabbedContainer.TYPE_VIEW; 83 88 public static final int TYPE_EDITOR = TabbedContainer.TYPE_EDITOR; 89 90 public static final int TYPE_SLIDING = TabbedContainer.TYPE_SLIDING; 91 92 public static final int TYPE_TOOLBAR = TabbedContainer.TYPE_TOOLBAR; 93 94 99 public static final String PROP_ACTIVE = TabbedContainer.PROP_ACTIVE; 100 101 102 106 public static final String COMMAND_CLOSE = TabbedContainer.COMMAND_CLOSE; 107 108 112 public static final String COMMAND_SELECT = TabbedContainer.COMMAND_SELECT; 113 114 118 public static final String COMMAND_POPUP_REQUEST = TabbedContainer.COMMAND_POPUP_REQUEST; 119 120 124 public static final String COMMAND_MAXIMIZE = TabbedContainer.COMMAND_MAXIMIZE; 125 126 130 public static final String COMMAND_CLOSE_ALL = TabbedContainer.COMMAND_CLOSE_ALL; 132 136 public static final String COMMAND_CLOSE_ALL_BUT_THIS = TabbedContainer.COMMAND_CLOSE_ALL_BUT_THIS; 138 142 public static final String COMMAND_ENABLE_AUTO_HIDE = TabbedContainer.COMMAND_ENABLE_AUTO_HIDE; 144 148 public static final String COMMAND_DISABLE_AUTO_HIDE = TabbedContainer.COMMAND_DISABLE_AUTO_HIDE; 150 153 public static final String EDITOR_TAB_DISPLAYER_UI_CLASS_ID = "EditorTabDisplayerUI"; 155 158 public static final String VIEW_TAB_DISPLAYER_UI_CLASS_ID = "ViewTabDisplayerUI"; 160 163 public static final String SLIDING_TAB_DISPLAYER_UI_CLASS_ID = "SlidingTabDisplayerUI"; 165 168 public static final String TOOLBAR_TAB_DISPLAYER_UI_CLASS_ID = "ToolbarTabDisplayerUI"; 170 173 public static final String PROP_ORIENTATION = "orientation"; 175 177 public static final Object ORIENTATION_EAST = "east"; 180 public static final Object ORIENTATION_WEST = "west"; 183 public static final Object ORIENTATION_NORTH = "north"; 186 public static final Object ORIENTATION_SOUTH = "south"; 189 public static final Object ORIENTATION_CENTER = "center"; 191 193 public static final Object ORIENTATION_INVISIBLE = "invisible"; 195 196 199 private transient List <ActionListener > actionListenerList; 200 201 private WinsysInfoForTabbed winsysInfo = null; 202 203 @Deprecated 204 private LocationInformer locationInformer = null; 205 206 private boolean showClose = !Boolean.getBoolean( 207 "nb.tabs.suppressCloseButton"); 209 public TabDisplayer () { 210 this (new DefaultTabDataModel(), TYPE_VIEW); 211 } 212 213 216 public TabDisplayer(TabDataModel model, int type) { 217 this (model, type, (WinsysInfoForTabbed)null); 218 } 219 220 223 @Deprecated 224 public TabDisplayer(TabDataModel model, int type, LocationInformer locationInformer) { 225 this (model, type, (WinsysInfoForTabbed)null); 226 this.locationInformer = locationInformer; 227 } 228 229 232 public TabDisplayer(TabDataModel model, int type, WinsysInfoForTabbed winsysInfo) { 233 switch (type) { 234 case TYPE_VIEW: 235 case TYPE_EDITOR: 236 case TYPE_SLIDING: 237 case TYPE_TOOLBAR: 238 break; 239 default : 240 throw new IllegalArgumentException ("Unknown UI type: " + type); } 242 this.model = model; 243 this.type = type; 244 this.winsysInfo = winsysInfo; 245 putClientProperty (PROP_ORIENTATION, ORIENTATION_NORTH); 246 initialized = true; 247 updateUI(); 248 setFocusable(false); 249 } 252 253 public final TabDisplayerUI getUI() { 254 return (TabDisplayerUI) ui; 255 } 256 257 261 public final void updateUI() { 262 if (!initialized) { 263 return; 264 } 265 266 if (type == TYPE_TOOLBAR) { 267 setUI (new ToolbarTabDisplayerUI(this)); 268 return; 269 } else if (type == TYPE_SLIDING) { 270 setUI (new BasicSlidingTabDisplayerUI(this)); 271 return; 272 } 273 274 ComponentUI ui = null; 275 if (UIManager.get(getUIClassID()) != null) { try { 277 ui = UIManager.getUI(this); 278 } catch (Error error) { 279 System.err.println("Could not load a UI for " + getUIClassID() + 280 " - missing class?"); 281 } 282 } else { 283 ui = findUIStandalone(); 284 } 285 286 if (ui == null) { 287 ui = getType() == TYPE_VIEW ? 288 WinClassicViewTabDisplayerUI.createUI(this) : 289 WinClassicEditorTabDisplayerUI.createUI(this); 290 } 291 setUI((TabDisplayerUI) ui); 292 293 } 294 295 299 private ComponentUI findUIStandalone() { 300 ComponentUI result = null; 301 String lf = UIManager.getLookAndFeel().getID(); 302 switch (type) { 303 case TYPE_VIEW : 304 if ("Aqua".equals(lf)) { result = AquaViewTabDisplayerUI.createUI(this); 306 } else if ("Windows".equals(lf)) { result = isXPLF() ? 308 WinXPViewTabDisplayerUI.createUI(this) : 309 WinClassicViewTabDisplayerUI.createUI(this); 310 } 311 break; 312 case TYPE_EDITOR : 313 if ("Aqua".equals(lf)) { result = AquaEditorTabDisplayerUI.createUI(this); 315 } else if ("Windows".equals(lf)) { result = isXPLF() ? 317 WinXPEditorTabDisplayerUI.createUI(this) : 318 WinClassicEditorTabDisplayerUI.createUI(this); 319 } 320 break; 321 } 322 return result; 323 } 324 325 327 private static boolean isXPLF () { 328 Boolean isXP = (Boolean )Toolkit.getDefaultToolkit(). 329 getDesktopProperty("win.xpstyle.themeActive"); return isXP == null ? false : isXP.booleanValue(); 331 } 332 333 334 336 public String getUIClassID() { 337 switch (getType()) { 338 case TYPE_VIEW : return VIEW_TAB_DISPLAYER_UI_CLASS_ID; 339 case TYPE_EDITOR : return EDITOR_TAB_DISPLAYER_UI_CLASS_ID; 340 case TYPE_SLIDING : return SLIDING_TAB_DISPLAYER_UI_CLASS_ID; 341 case TYPE_TOOLBAR : return TOOLBAR_TAB_DISPLAYER_UI_CLASS_ID; 342 default : 343 throw new IllegalArgumentException ("Unknown UI type: " + 344 getType()); 345 } 346 } 347 348 352 public final int getType() { 353 return type; 354 } 355 356 public final Dimension getPreferredSize() { 357 return getUI().getPreferredSize(this); 358 } 359 360 public final Dimension getMinimumSize() { 361 return getUI().getMinimumSize(this); 362 } 363 364 369 public final void requestAttention (int tab) { 370 getUI().requestAttention(tab); 371 } 372 373 376 public final void cancelRequestAttention (int tab) { 377 getUI().cancelRequestAttention (tab); 378 } 379 380 public final boolean requestAttention (TabData data) { 381 int idx = getModel().indexOf(data); 382 boolean result = idx >= 0; 383 if (result) { 384 requestAttention (idx); 385 } 386 return result; 387 } 388 389 392 void setSelectionModel(SingleSelectionModel sel) { 393 this.sel = sel; 394 } 395 396 399 public SingleSelectionModel getSelectionModel() { 400 return sel; 401 } 402 403 404 public final TabDataModel getModel() { 405 return model; 406 } 407 408 409 public final void setActive(boolean active) { 410 if (active != this.active) { 411 this.active = active; 412 firePropertyChange(PROP_ACTIVE, !active, active); } 414 } 415 416 419 public final boolean isActive() { 420 return active; 421 } 422 423 427 public final String getToolTipText(MouseEvent event) { 428 if (ui != null) { 429 Point p = event.getPoint(); 430 if (event.getSource() != this) { 431 Component c = (Component) event.getSource(); 432 p = SwingUtilities.convertPoint(c, p, this); 433 } 434 int index = getUI().tabForCoordinate(p); 435 if (index != -1) { 436 return getModel().getTab(index).tip; 437 } 438 } 439 return super.getToolTipText(event); 440 } 441 442 444 public final void makeTabVisible(int index) { 445 getUI().makeTabVisible(index); 446 } 447 448 449 public final Rectangle getTabRect(int tab, Rectangle dest) { 450 if (dest == null) { 451 dest = new Rectangle(); 452 } 453 getUI().getTabRect(tab, dest); 454 return dest; 455 } 456 457 @Deprecated 458 public final Image getDragImage(int index) { 459 return null; 460 } 461 462 481 public final synchronized void addActionListener(ActionListener listener) { 482 if (actionListenerList == null) { 483 actionListenerList = new ArrayList <ActionListener >(); 484 } 485 actionListenerList.add(listener); 486 } 487 488 493 public final synchronized void removeActionListener(ActionListener listener) { 494 if (actionListenerList != null) { 495 actionListenerList.remove(listener); 496 } 497 } 498 499 public void registerShortcuts(JComponent comp) { 500 getUI().registerShortcuts(comp); 501 } 502 503 public void unregisterShortcuts(JComponent comp) { 504 getUI().unregisterShortcuts(comp); 505 } 506 507 512 protected final void postActionEvent(TabActionEvent event) { 513 List <ActionListener > list; 514 synchronized (this) { 515 if (actionListenerList == null) { 516 return; 517 } 518 list = Collections.unmodifiableList(actionListenerList); 519 } 520 for (int i = 0; i < list.size(); i++) { 521 list.get(i).actionPerformed(event); 522 } 523 } 524 525 public int tabForCoordinate(Point p) { 526 return getUI().tabForCoordinate(p); 527 } 528 529 public WinsysInfoForTabbed getWinsysInfo() { 530 return winsysInfo; 531 } 532 533 @Deprecated 534 public LocationInformer getLocationInformer() { 535 return locationInformer; 536 } 537 538 public AccessibleContext getAccessibleContext() { 539 if (accessibleContext == null) { 540 accessibleContext = new AccessibleTabDisplayer(); 541 } 542 return accessibleContext; 543 } 544 545 551 public final void setShowCloseButton (boolean val) { 552 boolean wasShow = isShowCloseButton(); 553 if (wasShow != val) { 554 showClose = val; 555 if (isShowing()) { 556 repaint(); 557 } 558 firePropertyChange ("showCloseButton", !val, val); 559 } 560 } 561 562 563 public final boolean isShowCloseButton () { 564 return showClose; 565 } 566 567 568 protected class AccessibleTabDisplayer extends AccessibleJComponent 569 implements AccessibleSelection , ChangeListener { 570 571 574 public AccessibleTabDisplayer() { 575 super(); 576 getModel().addChangeListener(this); 577 } 578 579 public void stateChanged(ChangeEvent e) { 580 Object o = e.getSource(); 581 firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY, 582 null, o); 583 } 584 585 586 592 public AccessibleRole getAccessibleRole() { 593 return AccessibleRole.PAGE_TAB_LIST; 594 } 595 596 601 public int getAccessibleChildrenCount() { 602 return getModel().size(); 603 } 604 605 612 public Accessible getAccessibleChild(int i) { 613 if (i < 0 || i >= getModel().size()) { 614 return null; 615 } 616 TabData data = getModel().getTab(i); 617 if (data.getComponent() instanceof Accessible ) { 618 return (Accessible )data.getComponent(); 619 } 620 return null; 621 } 622 623 624 625 634 public AccessibleSelection getAccessibleSelection() { 635 return this; 636 } 637 638 646 public Accessible getAccessibleAt(Point p) { 647 int tab = tabForCoordinate(p); 648 if (tab == -1) { 649 tab = getSelectionModel().getSelectedIndex(); 650 } 651 return getAccessibleChild(tab); 652 } 653 654 660 public int getAccessibleSelectionCount() { 661 return 1; 662 } 663 664 676 public Accessible getAccessibleSelection(int i) { 677 int index = getSelectionModel().getSelectedIndex(); 679 return getAccessibleChild(index); 680 } 681 682 689 public boolean isAccessibleChildSelected(int i) { 690 return i == getSelectionModel().getSelectedIndex(); 691 } 692 693 703 public void addAccessibleSelection(int i) { 704 } 706 707 715 public void removeAccessibleSelection(int i) { 716 } 718 719 723 public void clearAccessibleSelection() { 724 } 726 727 731 public void selectAllAccessibleSelection() { 732 } 734 } 735 736 } 737 | Popular Tags |