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: frame.setMaximum(true);
181: else if (frame.isMaximum())
182: frame.setMaximum(false);
183: }
184: catch (PropertyVetoException pve)
185: {
186:
187: }
188: }
189: }
190:
191:
198: public class MoveAction extends AbstractAction
199: {
200:
203: public MoveAction()
204: {
205: super("Move");
206: }
207:
212: public void actionPerformed(ActionEvent e)
213: {
214:
215: }
216: }
217:
218:
226: public class RestoreAction extends AbstractAction
227: {
228:
231: public RestoreAction()
232: {
233: super("Restore");
234: }
235:
241: public void actionPerformed(ActionEvent e)
242: {
243: if (frame.isMaximum())
244: {
245: try
246: {
247: frame.setMaximum(false);
248: }
249: catch (PropertyVetoException pve)
250: {
251:
252: }
253: }
254: }
255: }
256:
257:
264: public class SizeAction extends AbstractAction
265: {
266:
269: public SizeAction()
270: {
271: super("Size");
272: }
273:
278: public void actionPerformed(ActionEvent e)
279: {
280:
281: }
282: }
283:
284:
292: public class PropertyChangeHandler implements PropertyChangeListener
293: {
294:
300: public void propertyChange(PropertyChangeEvent evt)
301: {
302: String propName = evt.getPropertyName();
303: if (propName.equals("closable"))
304: {
305: if (evt.getNewValue().equals(Boolean.TRUE))
306: closeButton.setVisible(true);
307: else
308: closeButton.setVisible(false);
309: }
310: else if (propName.equals("iconifiable"))
311: {
312: if (evt.getNewValue().equals(Boolean.TRUE))
313: iconButton.setVisible(true);
314: else
315: iconButton.setVisible(false);
316: }
317: else if (propName.equals("maximizable"))
318: {
319: if (evt.getNewValue().equals(Boolean.TRUE))
320: maxButton.setVisible(true);
321: else
322: maxButton.setVisible(false);
323: }
324:
325: }
326: }
327:
328:
336: public class SystemMenuBar extends JMenuBar
337: {
338:
343: public boolean isFocusTransversable()
344: {
345: return true;
346: }
347:
348:
354: public boolean isOpaque()
355: {
356: return true;
357: }
358:
359:
364: public void paint(Graphics g)
365: {
366: Icon frameIcon = frame.getFrameIcon();
367: if (frameIcon == null)
368: frameIcon = BasicDesktopIconUI.defaultIcon;
369: frameIcon.paintIcon(this, g, 0, 0);
370: }
371:
372:
375: public void requestFocus()
376: {
377: super.requestFocus();
378: }
379: }
380:
381:
388: public class TitlePaneLayout implements LayoutManager
389: {
390:
393: public TitlePaneLayout()
394: {
395:
396: }
397:
398:
404: public void addLayoutComponent(String name, Component c)
405: {
406:
407: }
408:
409:
414: public void layoutContainer(Container c)
415: {
416: Dimension size = c.getSize();
417: Insets insets = c.getInsets();
418: int width = size.width - insets.left - insets.right;
419: int height = size.height - insets.top - insets.bottom;
420:
421:
422: Dimension menupref = menuBar.getPreferredSize();
423: menuBar.setBounds(insets.left, insets.top, menupref.width, height);
424:
425: int loc = width + insets.left - 1;
426: int top = insets.top + 1;
427: int buttonHeight = height - 4;
428: if (closeButton.isVisible())
429: {
430: int buttonWidth = closeIcon.getIconWidth();
431: loc -= buttonWidth + 2;
432: closeButton.setBounds(loc, top, buttonWidth, buttonHeight);
433: }
434:
435: if (maxButton.isVisible())
436: {
437: int buttonWidth = maxIcon.getIconWidth();
438: loc -= buttonWidth + 2;
439: maxButton.setBounds(loc, top, buttonWidth, buttonHeight);
440: }
441:
442: if (iconButton.isVisible())
443: {
444: int buttonWidth = iconIcon.getIconWidth();
445: loc -= buttonWidth + 2;
446: iconButton.setBounds(loc, top, buttonWidth, buttonHeight);
447: }
448:
449: if (title != null)
450: title.setBounds(insets.left + menupref.width, insets.top,
451: loc - menupref.width - insets.left, height);
452: }
453:
454:
462: public Dimension minimumLayoutSize(Container c)
463: {
464: return preferredLayoutSize(c);
465: }
466:
467:
475: public Dimension preferredLayoutSize(Container c)
476: {
477: return new Dimension(22, 18);
478: }
479:
480:
485: public void removeLayoutComponent(Component c)
486: {
487:
488: }
489: }
490:
491:
496: private class PaneButton extends JButton
497: {
498:
503: public PaneButton(Action a)
504: {
505: super(a);
506: setMargin(new Insets(0, 0, 0, 0));
507: }
508:
509:
514: public boolean isFocusable()
515: {
516:
517: return false;
518: }
519:
520: }
521:
522:
523: protected static final String CLOSE_CMD = "Close";
524:
525:
526: protected static final String ICONIFY_CMD = "Minimize";
527:
528:
529: protected static final String MAXIMIZE_CMD = "Maximize";
530:
531:
532: protected static final String MOVE_CMD = "Move";
533:
534:
535: protected static final String RESTORE_CMD = "Restore";
536:
537:
538: protected static final String SIZE_CMD = "Size";
539:
540:
541: protected Action closeAction;
542:
543:
544: protected Action iconifyAction;
545:
546:
547: protected Action maximizeAction;
548:
549:
550: protected Action moveAction;
551:
552:
553: protected Action restoreAction;
554:
555:
556: protected Action sizeAction;
557:
558:
559: protected JButton closeButton;
560:
561:
562: protected JButton iconButton;
563:
564:
565: protected JButton maxButton;
566:
567:
568: protected Icon minIcon = BasicIconFactory.createEmptyFrameIcon();
569:
570:
571: protected Icon maxIcon = BasicIconFactory.createEmptyFrameIcon();
572:
573:
574: protected Icon iconIcon = BasicIconFactory.createEmptyFrameIcon();
575:
576:
577: protected Icon closeIcon;
578:
579:
580: protected JInternalFrame frame;
581:
582:
583: protected JMenuBar menuBar;
584:
585:
586: protected JMenu windowMenu;
587:
588:
591: protected Color notSelectedTextColor;
592:
593:
597: protected Color notSelectedTitleColor;
598:
599:
600: protected Color selectedTextColor;
601:
602:
606: protected Color selectedTitleColor;
607:
608:
609: protected PropertyChangeListener propertyChangeListener;
610:
611:
616: transient JLabel title;
617:
618:
625: public BasicInternalFrameTitlePane(JInternalFrame f)
626: {
627: frame = f;
628: setLayout(createLayout());
629: title = new JLabel();
630: title.setHorizontalAlignment(SwingConstants.LEFT);
631: title.setHorizontalTextPosition(SwingConstants.LEFT);
632: title.setOpaque(false);
633: setOpaque(true);
634:
635: setBackground(Color.LIGHT_GRAY);
636: setOpaque(true);
637:
638: installTitlePane();
639: }
640:
641:
646: protected void installTitlePane()
647: {
648: installDefaults();
649: installListeners();
650: createActions();
651:
652: assembleSystemMenu();
653:
654: createButtons();
655: setButtonIcons();
656: addSubComponents();
657: enableActions();
658: }
659:
660:
663: protected void addSubComponents()
664: {
665: add(menuBar);
666:
667: add(closeButton);
668: add(iconButton);
669: add(maxButton);
670: }
671:
672:
676: protected void createActions()
677: {
678: closeAction = new CloseAction();
679: closeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, CLOSE_CMD);
680:
681: iconifyAction = new IconifyAction();
682: iconifyAction.putValue(AbstractAction.ACTION_COMMAND_KEY, ICONIFY_CMD);
683:
684: maximizeAction = new MaximizeAction();
685: maximizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MAXIMIZE_CMD);
686:
687: sizeAction = new SizeAction();
688: sizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, SIZE_CMD);
689:
690: restoreAction = new RestoreAction();
691: restoreAction.putValue(AbstractAction.ACTION_COMMAND_KEY, RESTORE_CMD);
692:
693: moveAction = new MoveAction();
694: moveAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MOVE_CMD);
695: }
696:
697:
700: protected void installListeners()
701: {
702: propertyChangeListener = createPropertyChangeListener();
703: frame.addPropertyChangeListener(propertyChangeListener);
704: }
705:
706:
709: protected void uninstallListeners()
710: {
711: frame.removePropertyChangeListener(propertyChangeListener);
712: propertyChangeListener = null;
713: }
714:
715:
718: protected void installDefaults()
719: {
720: title.setFont(UIManager.getFont("InternalFrame.titleFont"));
721: selectedTextColor = UIManager.getColor("InternalFrame.activeTitleForeground");
722: selectedTitleColor = UIManager.getColor("InternalFrame.activeTitleBackground");
723: notSelectedTextColor = UIManager.getColor("InternalFrame.inactiveTitleForeground");
724: notSelectedTitleColor = UIManager.getColor("InternalFrame.inactiveTitleBackground");
725:
726: closeIcon = UIManager.getIcon("InternalFrame.closeIcon");
727: iconIcon = UIManager.getIcon("InternalFrame.iconifyIcon");
728: maxIcon = UIManager.getIcon("InternalFrame.maximizeIcon");
729: }
730:
731:
734: protected void uninstallDefaults()
735: {
736: setFont(null);
737: selectedTextColor = null;
738: selectedTitleColor = null;
739: notSelectedTextColor = null;
740: notSelectedTitleColor = null;
741:
742: closeIcon = null;
743: iconIcon = null;
744: maxIcon = null;
745: }
746:
747:
750: protected void createButtons()
751: {
752: closeButton = new PaneButton(closeAction);
753: closeButton.setText(null);
754: if (!frame.isClosable())
755: closeButton.setVisible(false);
756: iconButton = new PaneButton(iconifyAction);
757: iconButton.setText(null);
758: if (!frame.isIconifiable())
759: iconButton.setVisible(false);
760: maxButton = new PaneButton(maximizeAction);
761: maxButton.setText(null);
762: if (!frame.isMaximizable())
763: maxButton.setVisible(false);
764: }
765:
766:
769: protected void setButtonIcons()
770: {
771: if (closeIcon != null && closeButton != null)
772: closeButton.setIcon(closeIcon);
773: if (iconIcon != null && iconButton != null)
774: iconButton.setIcon(iconIcon);
775: if (maxIcon != null && maxButton != null)
776: maxButton.setIcon(maxIcon);
777: }
778:
779:
782: protected void assembleSystemMenu()
783: {
784: menuBar = createSystemMenuBar();
785: windowMenu = createSystemMenu();
786:
787: menuBar.add(windowMenu);
788:
789: addSystemMenuItems(windowMenu);
790: enableActions();
791: }
792:
793:
798: protected void addSystemMenuItems(JMenu systemMenu)
799: {
800: JMenuItem tmp;
801:
802: tmp = new JMenuItem(RESTORE_CMD);
803: tmp.addActionListener(restoreAction);
804: tmp.setMnemonic(KeyEvent.VK_R);
805: systemMenu.add(tmp);
806:
807: tmp = new JMenuItem(MOVE_CMD);
808: tmp.addActionListener(moveAction);
809: tmp.setMnemonic(KeyEvent.VK_M);
810: systemMenu.add(tmp);
811:
812: tmp = new JMenuItem(SIZE_CMD);
813: tmp.addActionListener(sizeAction);
814: tmp.setMnemonic(KeyEvent.VK_S);
815: systemMenu.add(tmp);
816:
817: tmp = new JMenuItem(ICONIFY_CMD);
818: tmp.addActionListener(iconifyAction);
819: tmp.setMnemonic(KeyEvent.VK_N);
820: systemMenu.add(tmp);
821:
822: tmp = new JMenuItem(MAXIMIZE_CMD);
823: tmp.addActionListener(maximizeAction);
824: tmp.setMnemonic(KeyEvent.VK_X);
825: systemMenu.add(tmp);
826:
827: systemMenu.addSeparator();
828:
829: tmp = new JMenuItem(CLOSE_CMD);
830: tmp.addActionListener(closeAction);
831: tmp.setMnemonic(KeyEvent.VK_C);
832: systemMenu.add(tmp);
833: }
834:
835:
840: protected JMenuBar createSystemMenuBar()
841: {
842: if (menuBar == null)
843: menuBar = new SystemMenuBar();
844: menuBar.removeAll();
845: return menuBar;
846: }
847:
848:
853: protected JMenu createSystemMenu()
854: {
855: if (windowMenu == null)
856: windowMenu = new JMenu();
857: windowMenu.removeAll();
858: return windowMenu;
859: }
860:
861:
864: protected void showSystemMenu()
865: {
866:
867: menuBar.getMenu(1).getPopupMenu().show();
868: }
869:
870:
875: public void paintComponent(Graphics g)
876: {
877: paintTitleBackground(g);
878: if (frame.getTitle() != null && title != null)
879: {
880: Color saved = g.getColor();
881: Font f = title.getFont();
882: g.setFont(f);
883: FontMetrics fm = g.getFontMetrics(f);
884: if (frame.isSelected())
885: g.setColor(selectedTextColor);
886: else
887: g.setColor(notSelectedTextColor);
888: title.setText(getTitle(frame.getTitle(), fm, title.getBounds().width));
889: SwingUtilities.paintComponent(g, title, null, title.getBounds());
890: g.setColor(saved);
891: }
892: }
893:
894:
899: protected void paintTitleBackground(Graphics g)
900: {
901: if (!isOpaque())
902: return;
903:
904: Color saved = g.getColor();
905: Dimension dims = getSize();
906:
907: Color bg = getBackground();
908: if (frame.isSelected())
909: bg = selectedTitleColor;
910: else
911: bg = notSelectedTitleColor;
912: g.setColor(bg);
913: g.fillRect(0, 0, dims.width, dims.height);
914: g.setColor(saved);
915: }
916:
917:
927: protected String getTitle(String text, FontMetrics fm, int availableWidth)
928: {
929: Rectangle vr = new Rectangle(0, 0, availableWidth, fm.getHeight());
930: Rectangle ir = new Rectangle();
931: Rectangle tr = new Rectangle();
932: String value = SwingUtilities.layoutCompoundLabel(this, fm, text, null,
933: SwingConstants.CENTER,
934: SwingConstants.LEFT,
935: SwingConstants.CENTER,
936: SwingConstants.LEFT, vr,
937: ir, tr, 0);
938: return value;
939: }
940:
941:
946: protected void postClosingEvent(JInternalFrame frame)
947: {
948:
949:
950:
951:
952:
953:
954: }
955:
956:
960: protected void enableActions()
961: {
962: closeAction.setEnabled(frame.isClosable());
963:
964: iconifyAction.setEnabled(frame.isIconifiable());
965:
966:
967: maximizeAction.setEnabled(frame.isMaximizable());
968:
969:
970:
971: restoreAction.setEnabled(frame.isMaximum());
972:
973: sizeAction.setEnabled(frame.isResizable());
974:
975:
976: moveAction.setEnabled(false);
977: }
978:
979:
984: protected PropertyChangeListener createPropertyChangeListener()
985: {
986: return new PropertyChangeHandler();
987: }
988:
989:
994: protected LayoutManager createLayout()
995: {
996: return new TitlePaneLayout();
997: }
998: }