1: 
  37: 
  38: 
  39: package ;
  40: 
  41: import ;
  42: import ;
  43: import ;
  44: import ;
  45: import ;
  46: import ;
  47: import ;
  48: import ;
  49: import ;
  50: import ;
  51: import ;
  52: import ;
  53: import ;
  54: import ;
  55: import ;
  56: 
  57: import ;
  58: import ;
  59: import ;
  60: import ;
  61: import ;
  62: import ;
  63: import ;
  64: import ;
  65: import ;
  66: import ;
  67: import ;
  68: import ;
  69: import ;
  70: 
  71: 
  74: public class BasicInternalFrameTitlePane extends JComponent
  75: {
  76:   
  83:   public class CloseAction extends AbstractAction
  84:   {
  85:     
  88:     public CloseAction()
  89:     {
  90:       super("Close");
  91:     }
  92:     
  93:     
  98:     public void actionPerformed(ActionEvent e)
  99:     {
 100:       if (frame.isClosable())
 101:         {
 102:           try
 103:             {
 104:               frame.setClosed(true);
 105:             }
 106:           catch (PropertyVetoException pve)
 107:             {
 108:               
 109:             }
 110:         }
 111:     }
 112:   }
 113: 
 114:   
 121:   public class IconifyAction extends AbstractAction
 122:   {
 123:     
 126:     public IconifyAction()
 127:     {
 128:       super("Minimize");
 129:     }
 130: 
 131:     
 137:     public void actionPerformed(ActionEvent e)
 138:     {
 139:       if (frame.isIconifiable() && ! frame.isIcon())
 140:         {
 141:           try
 142:             {
 143:               frame.setIcon(true);
 144:             }
 145:           catch (PropertyVetoException pve)
 146:             {
 147:               
 148:             }
 149:         }
 150:     }
 151:   }
 152: 
 153:   
 160:   public class MaximizeAction extends AbstractAction
 161:   {
 162:     
 165:     public MaximizeAction()
 166:     {
 167:       super("Maximize");
 168:     }
 169:     
 175:     public void actionPerformed(ActionEvent e)
 176:     {
 177:       try
 178:         {
 179:           if (frame.isMaximizable() && ! frame.isMaximum())
 180:             {
 181:               frame.setMaximum(true);
 182:               maxButton.setIcon(minIcon);
 183:             }
 184:           else if (frame.isMaximum())
 185:             {
 186:               frame.setMaximum(false);
 187:               maxButton.setIcon(maxIcon);
 188:             }
 189:         }
 190:       catch (PropertyVetoException pve)
 191:         {
 192:           
 193:         }
 194:     }
 195:   }
 196: 
 197:   
 204:   public class MoveAction extends AbstractAction
 205:   {
 206:     
 209:     public MoveAction()
 210:     {
 211:       super("Move");
 212:     }
 213:     
 218:     public void actionPerformed(ActionEvent e)
 219:     {
 220:       
 221:     }
 222:   }
 223: 
 224:   
 232:   public class RestoreAction extends AbstractAction
 233:   {
 234:     
 237:     public RestoreAction()
 238:     {
 239:       super("Restore");
 240:     }
 241:     
 247:     public void actionPerformed(ActionEvent e)
 248:     {
 249:       if (frame.isMaximum())
 250:         {
 251:           try
 252:             {
 253:               frame.setMaximum(false);
 254:             }
 255:           catch (PropertyVetoException pve)
 256:             {
 257:               
 258:             }
 259:         }
 260:     }
 261:   }
 262: 
 263:   
 270:   public class SizeAction extends AbstractAction
 271:   {
 272:     
 275:     public SizeAction()
 276:     {
 277:       super("Size");
 278:     }
 279:     
 284:     public void actionPerformed(ActionEvent e)
 285:     {
 286:       
 287:     }
 288:   }
 289: 
 290:   
 298:   public class PropertyChangeHandler implements PropertyChangeListener
 299:   {
 300:     
 306:     public void propertyChange(PropertyChangeEvent evt)
 307:     {
 308:       String propName = evt.getPropertyName();
 309:       if (propName.equals("closable"))
 310:         {
 311:           if (evt.getNewValue().equals(Boolean.TRUE))
 312:             closeButton.setVisible(true);
 313:           else
 314:             closeButton.setVisible(false);
 315:         }
 316:       else if (propName.equals("iconable"))
 317:         {
 318:           if (evt.getNewValue().equals(Boolean.TRUE))
 319:             iconButton.setVisible(true);
 320:           else
 321:             iconButton.setVisible(false);
 322:         }
 323:       else if (propName.equals("maximizable"))
 324:         {
 325:           if (evt.getNewValue().equals(Boolean.TRUE))
 326:             maxButton.setVisible(true);
 327:           else
 328:             maxButton.setVisible(false);
 329:         }
 330:       enableActions();
 331:     }
 332:   }
 333: 
 334:   
 342:   public class SystemMenuBar extends JMenuBar
 343:   {
 344:     
 349:     public boolean isFocusTraversable()
 350:     {
 351:       return true;
 352:     }
 353: 
 354:     
 360:     public boolean isOpaque()
 361:     {
 362:       return true;
 363:     }
 364: 
 365:     
 370:     public void paint(Graphics g)
 371:     {
 372:       Icon frameIcon = frame.getFrameIcon();
 373:       if (frameIcon == null)
 374:     frameIcon = BasicDesktopIconUI.defaultIcon;
 375:       frameIcon.paintIcon(this, g, 0, 0);
 376:     }
 377: 
 378:     
 381:     public void requestFocus()
 382:     {
 383:       super.requestFocus();
 384:     }
 385:   }
 386: 
 387:   
 394:   public class TitlePaneLayout implements LayoutManager
 395:   {
 396:     
 399:     public TitlePaneLayout()
 400:     {
 401:       
 402:     }
 403: 
 404:     
 410:     public void addLayoutComponent(String name, Component c)
 411:     {
 412:       
 413:     }
 414: 
 415:     
 420:     public void layoutContainer(Container c)
 421:     {
 422:       Dimension size = c.getSize();
 423:       Insets insets = c.getInsets();
 424:       int width = size.width - insets.left - insets.right;
 425:       int height = size.height - insets.top - insets.bottom;
 426: 
 427:       
 428:       Dimension menupref = menuBar.getPreferredSize();
 429:       menuBar.setBounds(insets.left, insets.top, menupref.width, height);
 430: 
 431:       int loc = width + insets.left - 1;
 432:       int top = insets.top + 1;
 433:       int buttonHeight = height - 4;
 434:       if (closeButton.isVisible())
 435:         {
 436:           int buttonWidth = closeIcon.getIconWidth();
 437:           loc -= buttonWidth + 2;
 438:           closeButton.setBounds(loc, top, buttonWidth, buttonHeight);
 439:         }
 440: 
 441:       if (maxButton.isVisible())
 442:         {
 443:           int buttonWidth = maxIcon.getIconWidth();
 444:           loc -= buttonWidth + 2;
 445:           maxButton.setBounds(loc, top, buttonWidth, buttonHeight);
 446:         }
 447: 
 448:       if (iconButton.isVisible())
 449:         {
 450:           int buttonWidth = iconIcon.getIconWidth();
 451:           loc -= buttonWidth + 2;
 452:           iconButton.setBounds(loc, top, buttonWidth, buttonHeight);
 453:         }
 454: 
 455:       if (title != null)
 456:     title.setBounds(insets.left + menupref.width, insets.top,
 457:                     loc - menupref.width - insets.left, height);
 458:     }
 459: 
 460:     
 468:     public Dimension minimumLayoutSize(Container c)
 469:     {
 470:       return preferredLayoutSize(c);
 471:     }
 472: 
 473:     
 481:     public Dimension preferredLayoutSize(Container c)
 482:     {
 483:       return new Dimension(22, 18);
 484:     }
 485: 
 486:     
 491:     public void removeLayoutComponent(Component c)
 492:     {
 493:       
 494:     }
 495:   }
 496: 
 497:   
 502:   private class PaneButton extends JButton
 503:   {
 504:     
 509:     public PaneButton(Action a)
 510:     {
 511:       super(a);
 512:       setMargin(new Insets(0, 0, 0, 0));
 513:     }
 514: 
 515:     
 520:     public boolean isFocusable()
 521:     {
 522:       
 523:       return false;
 524:     }
 525: 
 526:   }
 527: 
 528:   
 529:   protected static final String CLOSE_CMD;
 530: 
 531:   
 532:   protected static final String ICONIFY_CMD;
 533: 
 534:   
 535:   protected static final String MAXIMIZE_CMD;
 536: 
 537:   
 538:   protected static final String MOVE_CMD;
 539: 
 540:   
 541:   protected static final String RESTORE_CMD;
 542: 
 543:   
 544:   protected static final String SIZE_CMD;
 545: 
 546:   
 547:   protected Action closeAction;
 548: 
 549:   
 550:   protected Action iconifyAction;
 551: 
 552:   
 553:   protected Action maximizeAction;
 554: 
 555:   
 556:   protected Action moveAction;
 557: 
 558:   
 559:   protected Action restoreAction;
 560: 
 561:   
 562:   protected Action sizeAction;
 563: 
 564:   
 565:   protected JButton closeButton;
 566: 
 567:   
 568:   protected JButton iconButton;
 569: 
 570:   
 571:   protected JButton maxButton;
 572: 
 573:   
 574:   protected Icon minIcon = BasicIconFactory.createEmptyFrameIcon();
 575: 
 576:   
 577:   protected Icon maxIcon = BasicIconFactory.createEmptyFrameIcon();
 578: 
 579:   
 580:   protected Icon iconIcon = BasicIconFactory.createEmptyFrameIcon();
 581: 
 582:   
 583:   protected Icon closeIcon;
 584:   
 585:   
 586:   protected JInternalFrame frame;
 587: 
 588:   
 589:   protected JMenuBar menuBar;
 590: 
 591:   
 592:   protected JMenu windowMenu;
 593: 
 594:   
 597:   protected Color notSelectedTextColor;
 598: 
 599:   
 603:   protected Color notSelectedTitleColor;
 604: 
 605:   
 606:   protected Color selectedTextColor;
 607: 
 608:   
 612:   protected Color selectedTitleColor;
 613: 
 614:   
 615:   protected PropertyChangeListener propertyChangeListener;
 616: 
 617:   
 622:   transient JLabel title;
 623:   
 624:   static
 625:     {
 626:       
 627:       CLOSE_CMD = "Close";
 628:       ICONIFY_CMD = "Minimize";
 629:       MAXIMIZE_CMD = "Maximize";
 630:       MOVE_CMD = "Move";
 631:       RESTORE_CMD = "Restore";
 632:       SIZE_CMD = "Size";
 633:     }
 634: 
 635:   
 642:   public BasicInternalFrameTitlePane(JInternalFrame f)
 643:   {
 644:     frame = f;
 645:     setLayout(createLayout());
 646:     title = new JLabel();
 647:     title.setHorizontalAlignment(SwingConstants.LEFT);
 648:     title.setHorizontalTextPosition(SwingConstants.LEFT);
 649:     title.setOpaque(false);
 650:     setOpaque(true);
 651: 
 652:     setBackground(Color.LIGHT_GRAY);
 653:     setOpaque(true);
 654: 
 655:     installTitlePane();
 656:   }
 657: 
 658:   
 663:   protected void installTitlePane()
 664:   {
 665:     installDefaults();
 666:     installListeners();
 667:     createActions();
 668: 
 669:     assembleSystemMenu();
 670: 
 671:     createButtons();
 672:     setButtonIcons();
 673:     addSubComponents();
 674:     enableActions();
 675:   }
 676: 
 677:   
 680:   protected void addSubComponents()
 681:   {
 682:     add(menuBar);
 683: 
 684:     add(closeButton);
 685:     add(iconButton);
 686:     add(maxButton);
 687:   }
 688: 
 689:   
 693:   protected void createActions()
 694:   {
 695:     closeAction = new CloseAction();
 696:     closeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, CLOSE_CMD);
 697: 
 698:     iconifyAction = new IconifyAction();
 699:     iconifyAction.putValue(AbstractAction.ACTION_COMMAND_KEY, ICONIFY_CMD);
 700: 
 701:     maximizeAction = new MaximizeAction();
 702:     maximizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MAXIMIZE_CMD);
 703: 
 704:     sizeAction = new SizeAction();
 705:     sizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, SIZE_CMD);
 706: 
 707:     restoreAction = new RestoreAction();
 708:     restoreAction.putValue(AbstractAction.ACTION_COMMAND_KEY, RESTORE_CMD);
 709: 
 710:     moveAction = new MoveAction();
 711:     moveAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MOVE_CMD);
 712:   }
 713: 
 714:   
 717:   protected void installListeners()
 718:   {
 719:     propertyChangeListener = createPropertyChangeListener();
 720:     frame.addPropertyChangeListener(propertyChangeListener);
 721:   }
 722: 
 723:   
 726:   protected void uninstallListeners()
 727:   {
 728:     frame.removePropertyChangeListener(propertyChangeListener);
 729:     propertyChangeListener = null;
 730:   }
 731: 
 732:   
 735:   protected void installDefaults()
 736:   {
 737:     title.setFont(UIManager.getFont("InternalFrame.titleFont"));
 738:     selectedTextColor = UIManager.getColor("InternalFrame.activeTitleForeground");
 739:     selectedTitleColor = UIManager.getColor("InternalFrame.activeTitleBackground");
 740:     notSelectedTextColor = UIManager.getColor("InternalFrame.inactiveTitleForeground");
 741:     notSelectedTitleColor = UIManager.getColor("InternalFrame.inactiveTitleBackground");
 742:   
 743:     closeIcon = UIManager.getIcon("InternalFrame.closeIcon");
 744:     iconIcon = UIManager.getIcon("InternalFrame.iconifyIcon");
 745:     maxIcon = UIManager.getIcon("InternalFrame.maximizeIcon");
 746:   }
 747: 
 748:   
 751:   protected void uninstallDefaults()
 752:   {
 753:     setFont(null);
 754:     selectedTextColor = null;
 755:     selectedTitleColor = null;
 756:     notSelectedTextColor = null;
 757:     notSelectedTitleColor = null;
 758:     
 759:     closeIcon = null;
 760:     iconIcon = null;
 761:     maxIcon = null;
 762:   }
 763: 
 764:   
 767:   protected void createButtons()
 768:   {
 769:     closeButton = new PaneButton(closeAction);
 770:     closeButton.setText(null);
 771:     if (!frame.isClosable())
 772:       closeButton.setVisible(false);
 773:     iconButton = new PaneButton(iconifyAction);
 774:     iconButton.setText(null);
 775:     if (!frame.isIconifiable())
 776:       iconButton.setVisible(false);
 777:     maxButton = new PaneButton(maximizeAction);
 778:     maxButton.setText(null);
 779:     if (!frame.isMaximizable())
 780:       maxButton.setVisible(false);
 781:   }
 782: 
 783:   
 786:   protected void setButtonIcons()
 787:   {
 788:     if (closeIcon != null && closeButton != null)
 789:       closeButton.setIcon(closeIcon);
 790:     if (iconIcon != null && iconButton != null)
 791:       iconButton.setIcon(iconIcon);
 792:     if (maxIcon != null && maxButton != null)
 793:       maxButton.setIcon(maxIcon);
 794:   }
 795: 
 796:   
 799:   protected void assembleSystemMenu()
 800:   {
 801:     menuBar = createSystemMenuBar();
 802:     windowMenu = createSystemMenu();
 803: 
 804:     menuBar.add(windowMenu);
 805: 
 806:     addSystemMenuItems(windowMenu);
 807:     enableActions();
 808:   }
 809: 
 810:   
 815:   protected void addSystemMenuItems(JMenu systemMenu)
 816:   {
 817:     JMenuItem tmp;
 818: 
 819:     tmp = new JMenuItem(RESTORE_CMD);
 820:     tmp.addActionListener(restoreAction);
 821:     tmp.setMnemonic(KeyEvent.VK_R);
 822:     systemMenu.add(tmp);
 823: 
 824:     tmp = new JMenuItem(MOVE_CMD);
 825:     tmp.addActionListener(moveAction);
 826:     tmp.setMnemonic(KeyEvent.VK_M);
 827:     systemMenu.add(tmp);
 828: 
 829:     tmp = new JMenuItem(SIZE_CMD);
 830:     tmp.addActionListener(sizeAction);
 831:     tmp.setMnemonic(KeyEvent.VK_S);
 832:     systemMenu.add(tmp);
 833: 
 834:     tmp = new JMenuItem(ICONIFY_CMD);
 835:     tmp.addActionListener(iconifyAction);
 836:     tmp.setMnemonic(KeyEvent.VK_N);
 837:     systemMenu.add(tmp);
 838: 
 839:     tmp = new JMenuItem(MAXIMIZE_CMD);
 840:     tmp.addActionListener(maximizeAction);
 841:     tmp.setMnemonic(KeyEvent.VK_X);
 842:     systemMenu.add(tmp);
 843: 
 844:     systemMenu.addSeparator();
 845: 
 846:     tmp = new JMenuItem(CLOSE_CMD);
 847:     tmp.addActionListener(closeAction);
 848:     tmp.setMnemonic(KeyEvent.VK_C);
 849:     systemMenu.add(tmp);
 850:   }
 851: 
 852:   
 857:   protected JMenuBar createSystemMenuBar()
 858:   {
 859:     if (menuBar == null)
 860:       menuBar = new SystemMenuBar();
 861:     menuBar.removeAll();
 862:     return menuBar;
 863:   }
 864: 
 865:   
 870:   protected JMenu createSystemMenu()
 871:   {
 872:     if (windowMenu == null)
 873:       windowMenu = new JMenu();
 874:     windowMenu.removeAll();
 875:     return windowMenu;
 876:   }
 877: 
 878:   
 881:   protected void showSystemMenu()
 882:   {
 883:     
 884:     menuBar.getMenu(1).getPopupMenu().show();
 885:   }
 886: 
 887:   
 892:   public void paintComponent(Graphics g)
 893:   {
 894:     paintTitleBackground(g);
 895:     if (frame.getTitle() != null && title != null)
 896:       {
 897:     Color saved = g.getColor();
 898:         Font f = title.getFont();
 899:         g.setFont(f);
 900:         FontMetrics fm = g.getFontMetrics(f);
 901:     if (frame.isSelected())
 902:       g.setColor(selectedTextColor);
 903:     else
 904:       g.setColor(notSelectedTextColor);
 905:     title.setText(getTitle(frame.getTitle(), fm, title.getBounds().width));
 906:     SwingUtilities.paintComponent(g, title, null, title.getBounds());
 907:     g.setColor(saved);
 908:       }
 909:   }
 910: 
 911:   
 916:   protected void paintTitleBackground(Graphics g)
 917:   {
 918:     if (!isOpaque())
 919:       return;
 920: 
 921:     Color saved = g.getColor();
 922:     Dimension dims = getSize();
 923: 
 924:     Color bg = getBackground();
 925:     if (frame.isSelected())
 926:       bg = selectedTitleColor;
 927:     else
 928:       bg = notSelectedTitleColor;
 929:     g.setColor(bg);
 930:     g.fillRect(0, 0, dims.width, dims.height);
 931:     g.setColor(saved);
 932:   }
 933: 
 934:   
 944:   protected String getTitle(String text, FontMetrics fm, int availableWidth)
 945:   {
 946:     Rectangle vr = new Rectangle(0, 0, availableWidth, fm.getHeight());
 947:     Rectangle ir = new Rectangle();
 948:     Rectangle tr = new Rectangle();
 949:     String value = SwingUtilities.layoutCompoundLabel(this, fm, text, null,
 950:                                                       SwingConstants.CENTER,
 951:                                                       SwingConstants.LEFT,
 952:                                                       SwingConstants.CENTER,
 953:                                                       SwingConstants.LEFT, vr,
 954:                                                       ir, tr, 0);
 955:     return value;
 956:   }
 957: 
 958:   
 963:   protected void postClosingEvent(JInternalFrame frame)
 964:   {
 965:     
 966:     
 967:     
 968:     
 969:     
 970:     
 971:   }
 972: 
 973:   
 977:   protected void enableActions()
 978:   {
 979:     closeAction.setEnabled(frame.isClosable());
 980: 
 981:     iconifyAction.setEnabled(frame.isIconifiable());
 982:     
 983:     
 984:     maximizeAction.setEnabled(frame.isMaximizable());
 985: 
 986:     
 987:     
 988:     restoreAction.setEnabled(frame.isMaximum());
 989: 
 990:     sizeAction.setEnabled(frame.isResizable());
 991: 
 992:     
 993:     moveAction.setEnabled(false);
 994:   }
 995: 
 996:   
1001:   protected PropertyChangeListener createPropertyChangeListener()
1002:   {
1003:     return new PropertyChangeHandler();
1004:   }
1005: 
1006:   
1011:   protected LayoutManager createLayout()
1012:   {
1013:     return new TitlePaneLayout();
1014:   }
1015: }