1 7 8 package javax.swing; 9 10 import java.awt.Color ; 11 import java.awt.Component ; 12 import java.awt.ComponentOrientation ; 13 import java.awt.Container ; 14 import java.awt.Dimension ; 15 import java.awt.Graphics ; 16 import java.awt.Insets ; 17 import java.awt.LayoutManager ; 18 import java.awt.LayoutManager2 ; 19 import java.awt.event.*; 20 import java.beans.*; 21 22 import javax.swing.border.Border ; 23 import javax.swing.plaf.*; 24 import javax.accessibility.*; 25 26 import java.io.Serializable ; 27 import java.io.ObjectOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.IOException ; 30 import java.util.Hashtable ; 31 32 33 67 public class JToolBar extends JComponent implements SwingConstants , Accessible 68 { 69 73 private static final String uiClassID = "ToolBarUI"; 74 75 private boolean paintBorder = true; 76 private Insets margin = null; 77 private boolean floatable = true; 78 private int orientation = HORIZONTAL; 79 80 83 public JToolBar() 84 { 85 this( HORIZONTAL ); 86 } 87 88 95 public JToolBar( int orientation ) 96 { 97 this(null, orientation); 98 } 99 100 108 public JToolBar( String name ) { 109 this(name, HORIZONTAL); 110 } 111 112 126 public JToolBar( String name , int orientation) { 127 setName(name); 128 checkOrientation( orientation ); 129 130 this.orientation = orientation; 131 DefaultToolBarLayout layout = new DefaultToolBarLayout( orientation ); 132 setLayout( layout ); 133 134 addPropertyChangeListener( layout ); 135 136 updateUI(); 137 } 138 139 143 public ToolBarUI getUI() { 144 return (ToolBarUI)ui; 145 } 146 147 158 public void setUI(ToolBarUI ui) { 159 super.setUI(ui); 160 } 161 162 169 public void updateUI() { 170 setUI((ToolBarUI)UIManager.getUI(this)); 171 if (getLayout() == null) { 175 setLayout(new DefaultToolBarLayout(getOrientation())); 176 } 177 invalidate(); 178 } 179 180 181 182 189 public String getUIClassID() { 190 return uiClassID; 191 } 192 193 194 202 public int getComponentIndex(Component c) { 203 int ncomponents = this.getComponentCount(); 204 Component [] component = this.getComponents(); 205 for (int i = 0 ; i < ncomponents ; i++) { 206 Component comp = component[i]; 207 if (comp == c) 208 return i; 209 } 210 return -1; 211 } 212 213 221 public Component getComponentAtIndex(int i) { 222 int ncomponents = this.getComponentCount(); 223 if ( i >= 0 && i < ncomponents) { 224 Component [] component = this.getComponents(); 225 return component[i]; 226 } 227 return null; 228 } 229 230 248 public void setMargin(Insets m) 249 { 250 Insets old = margin; 251 margin = m; 252 firePropertyChange("margin", old, m); 253 revalidate(); 254 repaint(); 255 } 256 257 264 public Insets getMargin() 265 { 266 if(margin == null) { 267 return new Insets (0,0,0,0); 268 } else { 269 return margin; 270 } 271 } 272 273 279 public boolean isBorderPainted() 280 { 281 return paintBorder; 282 } 283 284 285 299 public void setBorderPainted(boolean b) 300 { 301 if ( paintBorder != b ) 302 { 303 boolean old = paintBorder; 304 paintBorder = b; 305 firePropertyChange("borderPainted", old, b); 306 revalidate(); 307 repaint(); 308 } 309 } 310 311 320 protected void paintBorder(Graphics g) 321 { 322 if (isBorderPainted()) 323 { 324 super.paintBorder(g); 325 } 326 } 327 328 335 public boolean isFloatable() 336 { 337 return floatable; 338 } 339 340 358 public void setFloatable( boolean b ) 359 { 360 if ( floatable != b ) 361 { 362 boolean old = floatable; 363 floatable = b; 364 365 firePropertyChange("floatable", old, b); 366 revalidate(); 367 repaint(); 368 } 369 } 370 371 379 public int getOrientation() 380 { 381 return this.orientation; 382 } 383 384 400 public void setOrientation( int o ) 401 { 402 checkOrientation( o ); 403 404 if ( orientation != o ) 405 { 406 int old = orientation; 407 orientation = o; 408 409 firePropertyChange("orientation", old, o); 410 revalidate(); 411 repaint(); 412 } 413 } 414 415 432 public void setRollover(boolean rollover) { 433 putClientProperty("JToolBar.isRollover", 434 rollover ? Boolean.TRUE : Boolean.FALSE); 435 } 436 437 444 public boolean isRollover() { 445 Boolean rollover = (Boolean )getClientProperty("JToolBar.isRollover"); 446 if (rollover != null) { 447 return rollover.booleanValue(); 448 } 449 return false; 450 } 451 452 private void checkOrientation( int orientation ) 453 { 454 switch ( orientation ) 455 { 456 case VERTICAL: 457 case HORIZONTAL: 458 break; 459 default: 460 throw new IllegalArgumentException ( "orientation must be one of: VERTICAL, HORIZONTAL" ); 461 } 462 } 463 464 468 public void addSeparator() 469 { 470 addSeparator(null); 471 } 472 473 479 public void addSeparator( Dimension size ) 480 { 481 JToolBar.Separator s = new JToolBar.Separator ( size ); 482 add(s); 483 } 484 485 498 public JButton add(Action a) { 499 JButton b = createActionComponent(a); 500 b.setAction(a); 501 add(b); 502 return b; 503 } 504 505 521 protected JButton createActionComponent(Action a) { 522 String text = a!=null? (String )a.getValue(Action.NAME) : null; 523 Icon icon = a!=null? (Icon )a.getValue(Action.SMALL_ICON) : null; 524 boolean enabled = a!=null? a.isEnabled() : true; 525 String tooltip = a!=null? 526 (String )a.getValue(Action.SHORT_DESCRIPTION) : null; 527 JButton b = new JButton (text, icon) { 528 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 529 PropertyChangeListener pcl = createActionChangeListener(this); 530 if (pcl==null) { 531 pcl = super.createActionPropertyChangeListener(a); 532 } 533 return pcl; 534 } 535 }; 536 if (icon !=null) { 537 b.putClientProperty("hideActionText", Boolean.TRUE); 538 } 539 b.setHorizontalTextPosition(JButton.CENTER); 540 b.setVerticalTextPosition(JButton.BOTTOM); 541 b.setEnabled(enabled); 542 b.setToolTipText(tooltip); 543 return b; 544 } 545 546 560 protected PropertyChangeListener createActionChangeListener(JButton b) { 561 return null; 562 } 563 564 573 protected void addImpl(Component comp, Object constraints, int index) { 574 if (comp instanceof Separator) { 575 if (getOrientation() == VERTICAL) { 576 ( (Separator)comp ).setOrientation(JSeparator.HORIZONTAL); 577 } else { 578 ( (Separator)comp ).setOrientation(JSeparator.VERTICAL); 579 } 580 } 581 super.addImpl(comp, constraints, index); 582 if (comp instanceof JButton ) { 583 ((JButton )comp).setDefaultCapable(false); 584 } 585 } 586 587 588 592 static public class Separator extends JSeparator 593 { 594 private Dimension separatorSize; 595 596 600 public Separator() 601 { 602 this( null ); } 604 605 610 public Separator( Dimension size ) 611 { 612 super( JSeparator.HORIZONTAL ); 613 setSeparatorSize(size); 614 } 615 616 623 public String getUIClassID() 624 { 625 return "ToolBarSeparatorUI"; 626 } 627 628 633 public void setSeparatorSize( Dimension size ) 634 { 635 if (size != null) { 636 separatorSize = size; 637 } else { 638 super.updateUI(); 639 } 640 this.invalidate(); 641 } 642 643 649 public Dimension getSeparatorSize() 650 { 651 return separatorSize; 652 } 653 654 660 public Dimension getMinimumSize() 661 { 662 if (separatorSize != null) { 663 return separatorSize.getSize(); 664 } else { 665 return super.getMinimumSize(); 666 } 667 } 668 669 675 public Dimension getMaximumSize() 676 { 677 if (separatorSize != null) { 678 return separatorSize.getSize(); 679 } else { 680 return super.getMaximumSize(); 681 } 682 } 683 684 690 public Dimension getPreferredSize() 691 { 692 if (separatorSize != null) { 693 return separatorSize.getSize(); 694 } else { 695 return super.getPreferredSize(); 696 } 697 } 698 } 699 700 701 706 private void writeObject(ObjectOutputStream s) throws IOException { 707 s.defaultWriteObject(); 708 if (getUIClassID().equals(uiClassID)) { 709 byte count = JComponent.getWriteObjCounter(this); 710 JComponent.setWriteObjCounter(this, --count); 711 if (count == 0 && ui != null) { 712 ui.installUI(this); 713 } 714 } 715 } 716 717 718 728 protected String paramString() { 729 String paintBorderString = (paintBorder ? 730 "true" : "false"); 731 String marginString = (margin != null ? 732 margin.toString() : ""); 733 String floatableString = (floatable ? 734 "true" : "false"); 735 String orientationString = (orientation == HORIZONTAL ? 736 "HORIZONTAL" : "VERTICAL"); 737 738 return super.paramString() + 739 ",floatable=" + floatableString + 740 ",margin=" + marginString + 741 ",orientation=" + orientationString + 742 ",paintBorder=" + paintBorderString; 743 } 744 745 746 private class DefaultToolBarLayout 747 implements LayoutManager2 , Serializable , PropertyChangeListener, UIResource { 748 749 LayoutManager lm; 750 751 DefaultToolBarLayout(int orientation) { 752 if (orientation == JToolBar.VERTICAL) { 753 lm = new BoxLayout (JToolBar.this, BoxLayout.PAGE_AXIS); 754 } else { 755 lm = new BoxLayout (JToolBar.this, BoxLayout.LINE_AXIS); 756 } 757 } 758 759 public void addLayoutComponent(String name, Component comp) { 760 } 761 762 public void addLayoutComponent(Component comp, Object constraints) { 763 } 764 765 public void removeLayoutComponent(Component comp) { 766 } 767 768 public Dimension preferredLayoutSize(Container target) { 769 return lm.preferredLayoutSize(target); 770 } 771 772 public Dimension minimumLayoutSize(Container target) { 773 return lm.minimumLayoutSize(target); 774 } 775 776 public Dimension maximumLayoutSize(Container target) { 777 if (lm instanceof LayoutManager2 ) { 778 return ((LayoutManager2 )lm).maximumLayoutSize(target); 779 } else { 780 return new Dimension (Short.MAX_VALUE, Short.MAX_VALUE); 784 } 785 } 786 787 public void layoutContainer(Container target) { 788 lm.layoutContainer(target); 789 } 790 791 public float getLayoutAlignmentX(Container target) { 792 if (lm instanceof LayoutManager2 ) { 793 return ((LayoutManager2 )lm).getLayoutAlignmentX(target); 794 } else { 795 return CENTER_ALIGNMENT; 799 } 800 } 801 802 public float getLayoutAlignmentY(Container target) { 803 if (lm instanceof LayoutManager2 ) { 804 return ((LayoutManager2 )lm).getLayoutAlignmentY(target); 805 } else { 806 return CENTER_ALIGNMENT; 810 } 811 } 812 813 public void invalidateLayout(Container target) { 814 if (lm instanceof LayoutManager2 ) { 815 ((LayoutManager2 )lm).invalidateLayout(target); 816 } 817 } 818 819 public void propertyChange(PropertyChangeEvent e) { 820 String name = e.getPropertyName(); 821 if( name.equals("orientation") ) { 822 int o = ((Integer )e.getNewValue()).intValue(); 823 824 if (o == JToolBar.VERTICAL) 825 lm = new BoxLayout (JToolBar.this, BoxLayout.PAGE_AXIS); 826 else { 827 lm = new BoxLayout (JToolBar.this, BoxLayout.LINE_AXIS); 828 } 829 } 830 } 831 } 832 833 834 public void setLayout(LayoutManager mgr) { 835 LayoutManager oldMgr = getLayout(); 836 if (oldMgr instanceof PropertyChangeListener) { 837 removePropertyChangeListener((PropertyChangeListener)oldMgr); 838 } 839 super.setLayout(mgr); 840 } 841 842 846 855 public AccessibleContext getAccessibleContext() { 856 if (accessibleContext == null) { 857 accessibleContext = new AccessibleJToolBar(); 858 } 859 return accessibleContext; 860 } 861 862 867 protected class AccessibleJToolBar extends AccessibleJComponent { 868 869 876 public AccessibleStateSet getAccessibleStateSet() { 877 AccessibleStateSet states = super.getAccessibleStateSet(); 878 return states; 881 } 882 883 888 public AccessibleRole getAccessibleRole() { 889 return AccessibleRole.TOOL_BAR; 890 } 891 } } 893 | Popular Tags |