1 14 package org.wings; 15 16 import org.wings.plaf.TabbedPaneCG; 17 import org.wings.style.CSSAttributeSet; 18 import org.wings.style.CSSProperty; 19 import org.wings.style.CSSSelector; 20 import org.wings.style.CSSStyleSheet; 21 22 import javax.swing.*; 23 import javax.swing.event.ChangeEvent ; 24 import javax.swing.event.ChangeListener ; 25 import java.awt.*; 26 import java.io.Serializable ; 27 import java.util.ArrayList ; 28 29 31 39 public class STabbedPane extends SContainer implements LowLevelEventListener, ChangeListener { 40 44 public static final CSSSelector.Pseudo SELECTOR_TAB_AREA = new CSSSelector.Pseudo("area containing the tab buttons"); 45 46 50 public static final CSSSelector.Pseudo SELECTOR_SELECTED_TAB = new CSSSelector.Pseudo("the elements of the selected tab"); 51 52 56 public static final CSSSelector.Pseudo SELECTOR_UNSELECTED_TAB = new CSSSelector.Pseudo("the elements of the unselected tab"); 57 58 59 64 protected int tabPlacement = SConstants.TOP; 65 66 69 protected SingleSelectionModel model; 70 71 ArrayList pages = new ArrayList (2); 72 73 76 final private SCardLayout card = new SCardLayout(); 77 78 82 final private SContainer contents = new SContainer(card); 83 84 87 protected int maxTabsPerLine = -1; 88 89 92 protected int selectedIndex = 0; 93 94 98 private int lleChangedIndex = -1; 99 100 103 private boolean epochCheckEnabled = true; 104 105 106 111 public STabbedPane() { 112 this(SConstants.TOP); 113 } 114 115 122 public STabbedPane(int tabPlacement) { 123 setTabPlacement(tabPlacement); 124 super.addComponent(contents, null, 0); 125 setModel(new DefaultSingleSelectionModel()); 126 } 127 128 133 public Color getSelectionBackground() { 134 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTED_TAB) == null ? null : CSSStyleSheet.getBackground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTED_TAB)); 135 } 136 137 142 public void setSelectionBackground(Color color) { 143 setAttribute(SELECTOR_SELECTED_TAB, CSSProperty.BACKGROUND_COLOR, CSSStyleSheet.getAttribute(color)); 144 } 145 146 151 public Color getSelectionForeground() { 152 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTED_TAB) == null ? null : CSSStyleSheet.getForeground((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTED_TAB)); 153 } 154 155 160 public void setSelectionForeground(Color color) { 161 setAttribute(SELECTOR_SELECTED_TAB, CSSProperty.COLOR, CSSStyleSheet.getAttribute(color)); 162 } 163 164 169 public void setSelectionFont(SFont font) { 170 setAttributes(SELECTOR_SELECTED_TAB, CSSStyleSheet.getAttributes(font)); 171 } 172 173 178 public SFont getSelectionFont() { 179 return dynamicStyles == null || dynamicStyles.get(SELECTOR_SELECTED_TAB) == null ? null : CSSStyleSheet.getFont((CSSAttributeSet) dynamicStyles.get(SELECTOR_SELECTED_TAB)); 180 } 181 182 188 public void addChangeListener(ChangeListener cl) { 189 addEventListener(ChangeListener .class, cl); 190 } 191 192 198 public void removeChangeListener(ChangeListener cl) { 199 removeEventListener(ChangeListener .class, cl); 200 } 201 202 205 protected void fireStateChanged() { 206 ChangeEvent event = null; 207 208 Object [] listeners = getListenerList(); 212 for (int i = listeners.length - 2; i >= 0; i -= 2) { 213 if (listeners[i] == ChangeListener .class) { 214 if (event == null) 216 event = new ChangeEvent (this); 217 ((ChangeListener ) listeners[i + 1]).stateChanged(event); 218 } 219 } 220 } 221 222 227 public int getTabPlacement() { 228 return tabPlacement; 229 } 230 231 243 public void setTabPlacement(int tabPlacement) { 244 if (tabPlacement != SConstants.TOP && tabPlacement != SConstants.LEFT && 245 tabPlacement != SConstants.BOTTOM && tabPlacement != SConstants.RIGHT) { 246 throw new IllegalArgumentException ("illegal tab placement: must be TOP, BOTTOM, LEFT, or RIGHT"); 247 } 248 249 this.tabPlacement = tabPlacement; 250 } 251 252 257 public SingleSelectionModel getModel() { 258 return model; 259 } 260 261 267 public void setModel(SingleSelectionModel model) { 268 if (this.model != null) 269 this.model.removeChangeListener(this); 270 this.model = model; 271 if (this.model != null) 272 this.model.addChangeListener(this); 273 } 274 275 282 public int getSelectedIndex() { 283 return model.getSelectedIndex(); 284 } 285 286 292 public void setSelectedIndex(int index) { 293 model.setSelectedIndex(index); 294 } 295 296 303 public SComponent getSelectedComponent() { 304 int index = getSelectedIndex(); 305 if (index == -1) { 306 return null; 307 } 308 return ((Page) pages.get(index)).component; 309 } 310 311 318 public void setSelectedComponent(SComponent c) { 319 int index = indexOfComponent(c); 320 if (index != -1) { 321 setSelectedIndex(index); 322 } else { 323 throw new IllegalArgumentException ("component not found in tabbed pane"); 324 } 325 } 326 327 333 public int indexOfComponent(SComponent component) { 334 for (int i = 0; i < getTabCount(); ++i) { 335 if (((Page) pages.get(i)).component.equals(component)) { 336 return i; 337 } 338 } 339 return -1; 340 } 341 342 347 public int getTabCount() { 348 return pages.size(); 349 } 350 351 365 public void insertTab(String title, SIcon icon, 366 SComponent component, String tip, 367 int index) { 368 369 SIcon disabledIcon = null; 370 371 if (icon != null && icon instanceof SImageIcon) { 372 disabledIcon = new SImageIcon(new ImageIcon(GrayFilter.createDisabledImage(((SImageIcon) icon).getImage()))); 373 } 374 375 Page p = new Page(title, icon, disabledIcon, component, tip); 376 pages.add(index, p); 377 378 contents.addComponent(p.component, p.component.getName()); 379 380 if (pages.size() == 1) { 381 setSelectedIndex(0); 382 } 383 } 384 385 397 public void addTab(String title, SIcon icon, SComponent component, String tip) { 398 insertTab(title, icon, component, tip, pages.size()); 399 } 400 401 412 public void addTab(String title, SIcon icon, SComponent component) { 413 insertTab(title, icon, component, null, pages.size()); 414 } 415 416 425 public void addTab(String title, SComponent component) { 426 insertTab(title, null, component, null, pages.size()); 427 } 428 429 430 439 public SComponent add(String title, SComponent component) { 440 addTab(title, component); 441 return component; 442 } 443 444 455 public SComponent addComponent(SComponent component, 456 Object constraints) { 457 return addComponent(component, constraints, pages.size()); 458 } 459 460 472 public SComponent addComponent(SComponent component, 473 Object constraints, int index) { 474 SIcon icon = constraints instanceof SIcon ? (SIcon) constraints : null; 475 String title = constraints instanceof String ? (String ) constraints : null; 476 insertTab(title, icon, component, null, Math.min(index, pages.size())); 477 478 return component; 479 } 480 481 491 public void removeTabAt(int index) { 492 int newTabCount = getTabCount() - 1; 497 int selected = getSelectedIndex(); 498 removePageAt(index); 499 if (newTabCount > 0) { 500 if (selected >= (newTabCount)) { 501 504 int decrement = 1; 505 506 while (newTabCount > decrement && !isEnabledAt(newTabCount - decrement)) { 507 decrement++; 508 } 509 if (isEnabledAt(newTabCount - decrement)) { 510 setSelectedIndex(newTabCount - decrement); 511 } else { 512 setSelectedIndex(-1); 514 } 515 } else { 516 int newTab = selected; 517 520 while ((newTabCount - 1 > newTab) && !isEnabledAt(newTab)) { 521 newTab++; 522 } 523 if (isEnabledAt(newTab)) { 524 setSelectedIndex(newTab); 525 getSelectedComponent().setVisible(true); 526 } else { 527 newTab = selected - 1; 529 if (newTab == -1) { 530 setSelectedIndex(-1); 531 return; 532 } 533 while (newTab > 0 && !isEnabledAt(newTab)) { 534 newTab--; 535 } 536 if (isEnabledAt(newTab)) { 537 setSelectedIndex(newTab); 538 getSelectedComponent().setVisible(true); 539 } else { 540 setSelectedIndex(-1); 542 } 543 } 544 545 } 546 } else { 547 setSelectedIndex(-1); 549 } 550 } 551 552 559 public void remove(SComponent component) { 560 int index = indexOfComponent(component); 561 if (index != -1) { 562 removeTabAt(index); 563 } 564 } 565 566 569 public void setMaxTabsPerLine(int tabs) { 570 maxTabsPerLine = tabs; 571 } 572 573 576 public int getMaxTabsPerLine() { 577 return maxTabsPerLine; 578 } 579 580 585 public String getTitleAt(int index) { 586 return ((Page) pages.get(index)).title; 587 } 588 589 594 public SIcon getIconAt(int index) { 595 return ((Page) pages.get(index)).icon; 596 } 597 598 603 public SIcon getDisabledIconAt(int index) { 604 return ((Page) pages.get(index)).disabledIcon; 605 } 606 607 612 public Color getBackgroundAt(int index) { 613 return ((Page) pages.get(index)).background; 614 } 615 616 621 public Color getForegroundAt(int index) { 622 return ((Page) pages.get(index)).foreground; 623 } 624 625 630 public String getStyleAt(int index) { 631 return ((Page) pages.get(index)).style; 632 } 633 634 640 public boolean isEnabledAt(int index) { 641 return ((Page) pages.get(index)).enabled; 642 } 643 644 652 public void setTitleAt(int index, String title) { 653 ((Page) pages.get(index)).title = title; 654 } 655 656 664 public void setIconAt(int index, SIcon icon) { 665 ((Page) pages.get(index)).icon = icon; 666 } 667 668 676 public void setDisabledIconAt(int index, SIcon disabledIcon) { 677 ((Page) pages.get(index)).disabledIcon = disabledIcon; 678 } 679 680 690 public void setBackgroundAt(int index, Color background) { 691 ((Page) pages.get(index)).background = background; 692 } 693 694 704 public void setForegroundAt(int index, Color foreground) { 705 ((Page) pages.get(index)).foreground = foreground; 706 } 707 708 718 public void setStyleAt(int index, String style) { 719 ((Page) pages.get(index)).style = style; 720 } 721 722 730 public void setEnabledAt(int index, boolean enabled) { 731 ((Page) pages.get(index)).enabled = enabled; 732 } 733 734 739 public void setToolTipTextAt(int index, String toolTip) { 740 ((Page) pages.get(index)).toolTip = toolTip; 741 } 742 743 748 public String getToolTipTextAt(int index) { 749 return ((Page) pages.get(index)).toolTip; 750 } 751 752 760 public void setComponent(int index, SComponent component) { 761 Page page = (Page) pages.get(index); 762 if (component != page.component) { 763 if (page.component != null) { 764 contents.remove(page.component); 765 } 766 page.component = component; 767 contents.addComponent(page.component, page.component.getName()); 768 if (getSelectedIndex() == index) 769 card.show(component); 770 } 771 } 772 773 779 public int indexOfTab(String title) { 780 for (int i = 0; i < getTabCount(); i++) { 781 String titleAt = getTitleAt(i); 782 if (title == null && titleAt == null || title != null && title.equals(titleAt)) 783 return i; 784 } 785 return -1; 786 } 787 788 794 public int indexOfTab(SIcon icon) { 795 for (int i = 0; i < getTabCount(); i++) { 796 SIcon iconAt = getIconAt(i); 797 if (icon == null && iconAt == null || icon != null && icon.equals(iconAt)) 798 return i; 799 } 800 return -1; 801 } 802 803 private void removePageAt(int i) { 804 contents.remove(((Page) pages.get(i)).component); 805 pages.remove(i); 806 } 807 808 811 private static class Page implements Serializable { 812 public String title; 813 public String toolTip; 814 public Color foreground; 815 public Color background; 816 public SIcon icon; 817 public SIcon disabledIcon; 818 public boolean enabled = true; 819 public String style; 820 public SComponent component; 821 822 public Page(String title, SIcon icon, 823 SIcon disabledIcon, SComponent component, String tip) { 824 this.title = title; 825 this.toolTip = tip; 826 this.icon = icon; 827 this.disabledIcon = disabledIcon; 828 this.component = component; 829 } 830 } 831 832 837 public void setParentFrame(SFrame f) { 838 super.setParentFrame(f); 839 contents.setParentFrame(f); 840 } 841 842 public void setCG(TabbedPaneCG cg) { 843 super.setCG(cg); 844 } 845 846 851 public void processLowLevelEvent(String action, String [] values) { 852 processKeyEvents(values); 853 854 for (int i = 0; i < values.length; ++i) { 855 try { 856 int index = new Integer (values[i]).intValue(); 857 if (index < 0 || index >= pages.size()) 858 continue; 859 860 863 if (((Page) pages.get(index)).enabled) { 864 lleChangedIndex = index; 865 SForm.addArmedComponent(this); 866 return; 867 } 868 } catch (NumberFormatException nfe) { 869 continue; 870 } 871 } 872 } 873 874 877 public void fireIntermediateEvents() { 878 } 879 880 884 public void fireFinalEvents() { 885 requestFocus(); 886 super.fireFinalEvents(); 887 if (lleChangedIndex > -1) 888 setSelectedIndex(lleChangedIndex); 889 lleChangedIndex = -1; 890 } 891 892 895 public boolean isEpochCheckEnabled() { 896 return epochCheckEnabled; 897 } 898 899 902 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 903 this.epochCheckEnabled = epochCheckEnabled; 904 } 905 906 911 public void stateChanged(ChangeEvent ce) { 912 final int index = model.getSelectedIndex(); 913 if (index >= pages.size() || index == -1) return; 914 card.show(((Page) pages.get(index)).component); 915 916 reload(); 917 fireStateChanged(); 918 } 919 920 public void removeAllTabs() { 921 while (getTabCount() != 0) { 922 removeTabAt(0); 923 } 924 } 925 926 } 927 928 929 | Popular Tags |