1 19 20 package org.netbeans.swing.tabcontrol; 21 import javax.accessibility.Accessible ; 22 import org.netbeans.swing.tabcontrol.event.TabActionEvent; 23 import org.netbeans.swing.tabcontrol.plaf.DefaultTabbedContainerUI; 24 25 import javax.swing.*; 26 import java.awt.*; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.HierarchyEvent ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import javax.accessibility.AccessibleRole ; 33 import javax.swing.JComponent.AccessibleJComponent; 34 import org.openide.util.NbBundle; 35 36 37 116 public class TabbedContainer extends JComponent implements Accessible { 117 120 public static final String TABBED_CONTAINER_UI_CLASS_ID = "TabbedContainerUI"; 122 127 public static final int TYPE_VIEW = 0; 128 134 public static final int TYPE_EDITOR = 1; 135 136 147 public static final int TYPE_SLIDING = 2; 148 149 153 public static final int TYPE_TOOLBAR = 3; 154 155 158 public static final String PROP_ACTIVE = "active"; 160 164 public static final String PROP_MANAGE_TAB_POSITION = "manageTabPosition"; 165 166 170 public static final String COMMAND_CLOSE = "close"; 172 176 public static final String COMMAND_SELECT = "select"; 178 179 public static final String COMMAND_POPUP_REQUEST = "popup"; 181 182 public static final String COMMAND_MAXIMIZE = "maximize"; 184 public static final String COMMAND_CLOSE_ALL = "closeAll"; 186 public static final String COMMAND_CLOSE_ALL_BUT_THIS = "closeAllButThis"; 188 public static final String COMMAND_ENABLE_AUTO_HIDE = "enableAutoHide"; 190 public static final String COMMAND_DISABLE_AUTO_HIDE = "disableAutoHide"; 192 194 203 private TabDataModel model; 204 205 209 private final int type; 210 211 215 private boolean active = false; 216 217 222 private boolean initialized = false; 223 224 227 private transient List <ActionListener > actionListenerList; 228 229 235 public static final int CONTENT_POLICY_ADD_ALL = 1; 236 243 public static final int CONTENT_POLICY_ADD_ON_FIRST_USE = 2; 244 248 public static final int CONTENT_POLICY_ADD_ONLY_SELECTED = 3; 249 250 private int contentPolicy = DEFAULT_CONTENT_POLICY ; 251 252 260 protected static int DEFAULT_CONTENT_POLICY = CONTENT_POLICY_ADD_ALL; 261 262 264 private ComponentConverter converter = null; 265 266 267 private WinsysInfoForTabbed winsysInfo = null; 268 269 @Deprecated 270 private LocationInformer locationInformer = null; 271 272 275 public TabbedContainer() { 276 this(null, TYPE_VIEW); 277 } 278 279 282 public TabbedContainer(TabDataModel model) { 283 this(model, TYPE_VIEW); 284 } 285 286 public TabbedContainer(int type) { 287 this (null, type); 288 } 289 290 295 public TabbedContainer(TabDataModel model, int type) { 296 this (model, type, (WinsysInfoForTabbed)null); 297 } 298 299 302 @Deprecated 303 public TabbedContainer(TabDataModel model, int type, LocationInformer locationInformer) { 304 this (model, type, (WinsysInfoForTabbed)null); 305 this.locationInformer = locationInformer; 306 } 307 308 312 public TabbedContainer(TabDataModel model, int type, WinsysInfoForTabbed winsysInfo) { 313 switch (type) { 314 case TYPE_VIEW: 315 case TYPE_EDITOR: 316 case TYPE_SLIDING: 317 case TYPE_TOOLBAR: 318 break; 319 default : 320 throw new IllegalArgumentException ("Unknown UI type: " + type); } 322 if (model == null) { 323 model = new DefaultTabDataModel(); 324 } 325 this.model = model; 326 this.type = Boolean.getBoolean("nb.tabcontrol.alltoolbar") ? TYPE_TOOLBAR : type; 327 this.winsysInfo = winsysInfo; 328 initialized = true; 329 updateUI(); 330 putClientProperty ("viewType", new Integer (type)); } 334 335 336 337 354 public void updateUI() { 355 if (!initialized) { 356 return; 361 } 362 TabbedContainerUI ui = null; 363 String UIClass = (String ) UIManager.get(getUIClassID()); 364 if (getUI() != null && (getUI().getClass().getName().equals(UIClass) | UIClass == null)) { 365 if (!getUI().shouldReplaceUI()) { 366 return; 367 } 368 } 369 370 if (UIClass != null) { try { 372 ui = (TabbedContainerUI) UIManager.getUI(this); 373 } catch (Error e) { 374 } 376 } 377 if (ui != null) { 378 setUI(ui); 379 } else { 380 setUI(DefaultTabbedContainerUI.createUI(this)); 381 } 382 } 383 384 388 public final int getType() { 389 return type; 390 } 391 392 395 public String getUIClassID() { 396 return TABBED_CONTAINER_UI_CLASS_ID; 397 } 398 399 400 public TabbedContainerUI getUI() { 401 return (TabbedContainerUI) ui; 402 } 403 404 415 public final void setComponentConverter (ComponentConverter cc) { 416 ComponentConverter old = converter; 417 converter = cc; 418 if (old instanceof ComponentConverter.Fixed && cc instanceof ComponentConverter.Fixed) { 419 List <TabData> l = getModel().getTabs(); 420 if (!l.isEmpty()) { 421 TabData[] td = l.toArray (new TabData[0]); 422 getModel().setTabs (new TabData[0]); 423 getModel().setTabs(td); 424 } 425 } 426 } 427 428 433 public final ComponentConverter getComponentConverter() { 434 if (converter != null) { 435 return converter; 436 } 437 return ComponentConverter.DEFAULT; 438 } 439 440 444 public final void setContentPolicy(int i) { 445 switch (i) { 446 case CONTENT_POLICY_ADD_ALL : 447 case CONTENT_POLICY_ADD_ON_FIRST_USE : 448 case CONTENT_POLICY_ADD_ONLY_SELECTED : 449 break; 450 default : 451 throw new IllegalArgumentException ("Unknown content policy: " 452 + i); 453 } 454 455 if (i != contentPolicy) { 456 int old = contentPolicy; 457 contentPolicy = i; 458 firePropertyChange ("contentPolicy", old, i); } 460 } 461 462 479 public int getContentPolicy() { 480 return contentPolicy; 481 } 482 483 public boolean isValidateRoot() { 484 return true; 485 } 486 487 public boolean isPaintingOrigin() { 488 return true; 489 } 490 491 492 public void setToolTipTextAt(int index, String toolTip) { 493 TabData tabData = getModel().getTab(index); 495 if (tabData != null) { 496 tabData.tip = toolTip; 497 } 498 } 499 500 506 public final TabDataModel getModel() { 507 return model; 508 } 509 510 517 public final SingleSelectionModel getSelectionModel() { 518 return getUI().getSelectionModel(); 519 } 520 521 525 public final Rectangle getTabRect(int index, final Rectangle r) { 526 return getUI().getTabRect(index, r); 527 } 528 529 530 public int tabForCoordinate (Point p) { 531 return getUI().tabForCoordinate(p); 532 } 533 534 547 public final void setActive(boolean active) { 548 if (active != this.active) { 549 this.active = active; 550 firePropertyChange(PROP_ACTIVE, !active, active); 551 } 552 } 553 554 558 public final void requestAttention (int tab) { 559 getUI().requestAttention(tab); 560 } 561 562 public final void cancelRequestAttention (int tab) { 563 getUI().cancelRequestAttention(tab); 564 } 565 566 570 public final boolean requestAttention (TabData data) { 571 int idx = getModel().indexOf(data); 572 boolean result = idx >= 0; 573 if (result) { 574 requestAttention (idx); 575 } 576 return result; 577 } 578 579 public final void cancelRequestAttention (TabData data) { 580 int idx = getModel().indexOf(data); 581 if (idx != -1) { 582 cancelRequestAttention(idx); 583 } 584 } 585 586 591 public final boolean isActive() { 592 return active; 593 } 594 595 614 public final synchronized void addActionListener(ActionListener listener) { 615 if (actionListenerList == null) { 616 actionListenerList = new ArrayList <ActionListener >(); 617 } 618 actionListenerList.add(listener); 619 } 620 621 626 public final synchronized void removeActionListener( 627 ActionListener listener) { 628 if (actionListenerList != null) { 629 actionListenerList.remove(listener); 630 if (actionListenerList.isEmpty()) { 631 actionListenerList = null; 632 } 633 } 634 } 635 636 644 protected final void postActionEvent(TabActionEvent event) { 645 List <ActionListener > list; 646 synchronized (this) { 647 if (actionListenerList == null) 648 return; 649 list = Collections.unmodifiableList(actionListenerList); 650 } 651 for( ActionListener l : list ) { 652 l.actionPerformed(event); 653 } 654 } 655 656 public void setIconAt(int index, Icon icon) { 657 getModel().setIcon(index, icon); 658 } 659 660 public void setTitleAt(int index, String title) { 661 getModel().setText(index, title); 662 } 663 664 665 public Image createImageOfTab(int idx) { 666 return getUI().createImageOfTab (idx); 667 } 668 669 670 public int getTabCount() { 671 return getModel().size(); 672 } 673 674 680 public final void setShowCloseButton (boolean val) { 681 boolean wasShow = isShowCloseButton(); 682 if (val != wasShow) { 683 getUI().setShowCloseButton(val); 684 firePropertyChange ("showCloseButton", wasShow, val); 685 } 686 } 687 688 691 public final boolean isShowCloseButton () { 692 return getUI().isShowCloseButton(); 693 } 694 695 696 public int indexOf (Component comp) { 697 int max = getModel().size(); 698 TabDataModel mdl = getModel(); 699 for (int i=0; i < max; i++) { 700 if (getComponentConverter().getComponent(mdl.getTab(i)) == comp) { 701 return i; 702 } 703 } 704 return -1; 705 } 706 707 713 public int dropIndexOfPoint (Point location) { 714 return getUI().dropIndexOfPoint(location); 715 } 716 717 730 public Shape getDropIndication(Object dragged, Point location) { 731 int ix; 732 if (dragged instanceof Component) { 733 ix = indexOf((Component)dragged); 734 } else if (dragged instanceof TabData) { 735 ix = getModel().indexOf((TabData) dragged); 736 } else { 737 ix = -1; 738 } 739 740 int over = dropIndexOfPoint(location); 741 742 if(over < 0) { Rectangle r = getBounds(); 744 r.setLocation(0, 0); 745 return r; 746 } 747 754 if (over == ix && ix != -1) { 755 return getUI().getExactTabIndication(over); 756 } else { 757 return getUI().getInsertTabIndication(over); 758 } 759 } 760 761 @Deprecated 762 public LocationInformer getLocationInformer() { 763 return locationInformer; 764 } 765 766 public WinsysInfoForTabbed getWinsysInfo() { 767 return winsysInfo; 768 } 769 770 static { 771 String s = System.getProperty("nb.tabcontrol.contentpolicy"); if (s != null) { 774 try { 775 DEFAULT_CONTENT_POLICY = Integer.parseInt (s); 776 switch (DEFAULT_CONTENT_POLICY) { 777 case CONTENT_POLICY_ADD_ALL : 778 case CONTENT_POLICY_ADD_ON_FIRST_USE : 779 case CONTENT_POLICY_ADD_ONLY_SELECTED : 780 System.err.println("Using custom content policy: " + DEFAULT_CONTENT_POLICY); 781 break; 782 default : 783 throw new Error ("Bad value for default content " + 784 "policy: " + s + " only values 1, 2 or 3" + 785 "are meaningful"); } 787 System.err.println ("Default content policy is " + DEFAULT_CONTENT_POLICY); 788 } catch (Exception e) { 789 System.err.println ("Error parsing default content " + 790 "policy: \"" + s + "\""); } 792 } 793 } 794 795 public javax.accessibility.AccessibleContext getAccessibleContext() { 796 if( null == accessibleContext ) { 797 accessibleContext = new AccessibleJComponent() { 798 public AccessibleRole getAccessibleRole() { 799 return AccessibleRole.PAGE_TAB_LIST; 800 } 801 }; 802 803 accessibleContext.setAccessibleName( NbBundle.getMessage(TabbedContainer.class, "ACS_TabbedContainer") ); 804 accessibleContext.setAccessibleDescription( NbBundle.getMessage(TabbedContainer.class, "ACSD_TabbedContainer") ); 805 } 806 807 return accessibleContext; 808 } 809 810 } 811 | Popular Tags |