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: import ;
71:
72:
75: public class BasicInternalFrameTitlePane extends JComponent
76: {
77:
84: public class CloseAction extends AbstractAction
85: {
86:
89: public CloseAction()
90: {
91: super("Close");
92: }
93:
94:
99: public void actionPerformed(ActionEvent e)
100: {
101: if (frame.isClosable())
102: {
103: try
104: {
105: frame.setClosed(true);
106: }
107: catch (PropertyVetoException pve)
108: {
109:
110: }
111: }
112: }
113: }
114:
115:
122: public class IconifyAction extends AbstractAction
123: {
124:
127: public IconifyAction()
128: {
129: super("Minimize");
130: }
131:
132:
138: public void actionPerformed(ActionEvent e)
139: {
140: if (frame.isIconifiable() && ! frame.isIcon())
141: {
142: try
143: {
144: frame.setIcon(true);
145: }
146: catch (PropertyVetoException pve)
147: {
148:
149: }
150: }
151: }
152: }
153:
154:
161: public class MaximizeAction extends AbstractAction
162: {
163:
166: public MaximizeAction()
167: {
168: super("Maximize");
169: }
170:
176: public void actionPerformed(ActionEvent e)
177: {
178: try
179: {
180: if (frame.isMaximizable() && ! frame.isMaximum())
181: frame.setMaximum(true);
182: else if (frame.isMaximum())
183: frame.setMaximum(false);
184: }
185: catch (PropertyVetoException pve)
186: {
187:
188: }
189: }
190: }
191:
192:
199: public class MoveAction extends AbstractAction
200: {
201:
204: public MoveAction()
205: {
206: super("Move");
207: }
208:
213: public void actionPerformed(ActionEvent e)
214: {
215:
216: }
217: }
218:
219:
227: public class RestoreAction extends AbstractAction
228: {
229:
232: public RestoreAction()
233: {
234: super("Restore");
235: }
236:
242: public void actionPerformed(ActionEvent e)
243: {
244: if (frame.isMaximum())
245: {
246: try
247: {
248: frame.setMaximum(false);
249: }
250: catch (PropertyVetoException pve)
251: {
252:
253: }
254: }
255: }
256: }
257:
258:
265: public class SizeAction extends AbstractAction
266: {
267:
270: public SizeAction()
271: {
272: super("Size");
273: }
274:
279: public void actionPerformed(ActionEvent e)
280: {
281:
282: }
283: }
284:
285:
293: public class PropertyChangeHandler implements PropertyChangeListener
294: {
295:
301: public void propertyChange(PropertyChangeEvent evt)
302: {
303: String propName = evt.getPropertyName();
304: if (propName.equals("closable"))
305: {
306: if (evt.getNewValue().equals(Boolean.TRUE))
307: closeButton.setVisible(true);
308: else
309: closeButton.setVisible(false);
310: }
311: else if (propName.equals("iconifiable"))
312: {
313: if (evt.getNewValue().equals(Boolean.TRUE))
314: iconButton.setVisible(true);
315: else
316: iconButton.setVisible(false);
317: }
318: else if (propName.equals("maximizable"))
319: {
320: if (evt.getNewValue().equals(Boolean.TRUE))
321: maxButton.setVisible(true);
322: else
323: maxButton.setVisible(false);
324: }
325:
326: }
327: }
328:
329:
337: public class SystemMenuBar extends JMenuBar
338: {
339:
344: public boolean isFocusTransversable()
345: {
346: return true;
347: }
348:
349:
355: public boolean isOpaque()
356: {
357: return true;
358: }
359:
360:
365: public void paint(Graphics g)
366: {
367: Icon frameIcon = frame.getFrameIcon();
368: if (frameIcon == null)
369: frameIcon = BasicDesktopIconUI.defaultIcon;
370: frameIcon.paintIcon(this, g, 0, 0);
371: }
372:
373:
376: public void requestFocus()
377: {
378: super.requestFocus();
379: }
380: }
381:
382:
389: public class TitlePaneLayout implements LayoutManager
390: {
391:
394: public TitlePaneLayout()
395: {
396:
397: }
398:
399:
405: public void addLayoutComponent(String name, Component c)
406: {
407:
408: }
409:
410:
415: public void layoutContainer(Container c)
416: {
417: Dimension size = c.getSize();
418: Insets insets = c.getInsets();
419: int width = size.width - insets.left - insets.right;
420: int height = size.height - insets.top - insets.bottom;
421:
422:
423: Dimension menupref = menuBar.getPreferredSize();
424: menuBar.setBounds(insets.left, insets.top, menupref.width, height);
425:
426: int loc = width + insets.left - 1;
427: int top = insets.top + 1;
428: int buttonHeight = height - 4;
429: if (closeButton.isVisible())
430: {
431: int buttonWidth = closeIcon.getIconWidth();
432: loc -= buttonWidth + 2;
433: closeButton.setBounds(loc, top, buttonWidth, buttonHeight);
434: }
435:
436: if (maxButton.isVisible())
437: {
438: int buttonWidth = maxIcon.getIconWidth();
439: loc -= buttonWidth + 2;
440: maxButton.setBounds(loc, top, buttonWidth, buttonHeight);
441: }
442:
443: if (iconButton.isVisible())
444: {
445: int buttonWidth = iconIcon.getIconWidth();
446: loc -= buttonWidth + 2;
447: iconButton.setBounds(loc, top, buttonWidth, buttonHeight);
448: }
449:
450: if (title != null)
451: title.setBounds(insets.left + menupref.width, insets.top,
452: loc - menupref.width - insets.left, height);
453: }
454:
455:
463: public Dimension minimumLayoutSize(Container c)
464: {
465: return preferredLayoutSize(c);
466: }
467:
468:
476: public Dimension preferredLayoutSize(Container c)
477: {
478: return new Dimension(22, 18);
479: }
480:
481:
486: public void removeLayoutComponent(Component c)
487: {
488:
489: }
490: }
491:
492:
497: private class PaneButton extends JButton
498: {
499:
504: public PaneButton(Action a)
505: {
506: super(a);
507: setMargin(new Insets(0, 0, 0, 0));
508: }
509:
510:
515: public boolean isFocusable()
516: {
517:
518: return false;
519: }
520:
521: }
522:
523:
524: protected static final String CLOSE_CMD = "Close";
525:
526:
527: protected static final String ICONIFY_CMD = "Minimize";
528:
529:
530: protected static final String MAXIMIZE_CMD = "Maximize";
531:
532:
533: protected static final String MOVE_CMD = "Move";
534:
535:
536: protected static final String RESTORE_CMD = "Restore";
537:
538:
539: protected static final String SIZE_CMD = "Size";
540:
541:
542: protected Action closeAction;
543:
544:
545: protected Action iconifyAction;
546:
547:
548: protected Action maximizeAction;
549:
550:
551: protected Action moveAction;
552:
553:
554: protected Action restoreAction;
555:
556:
557: protected Action sizeAction;
558:
559:
560: protected JButton closeButton;
561:
562:
563: protected JButton iconButton;
564:
565:
566: protected JButton maxButton;
567:
568:
569: protected Icon minIcon = BasicIconFactory.createEmptyFrameIcon();
570:
571:
572: protected Icon maxIcon = BasicIconFactory.createEmptyFrameIcon();
573:
574:
575: protected Icon iconIcon = BasicIconFactory.createEmptyFrameIcon();
576:
577:
578: protected Icon closeIcon;
579:
580:
581: protected JInternalFrame frame;
582:
583:
584: protected JMenuBar menuBar;
585:
586:
587: protected JMenu windowMenu;
588:
589:
592: protected Color notSelectedTextColor;
593:
594:
598: protected Color notSelectedTitleColor;
599:
600:
601: protected Color selectedTextColor;
602:
603:
607: protected Color selectedTitleColor;
608:
609:
610: protected PropertyChangeListener propertyChangeListener;
611:
612:
617: transient JLabel title;
618:
619:
626: public BasicInternalFrameTitlePane(JInternalFrame f)
627: {
628: frame = f;
629: setLayout(createLayout());
630: title = new JLabel();
631: title.setHorizontalAlignment(SwingConstants.LEFT);
632: title.setHorizontalTextPosition(SwingConstants.LEFT);
633: title.setOpaque(false);
634: setOpaque(true);
635:
636: setBackground(Color.LIGHT_GRAY);
637: setOpaque(true);
638:
639: installTitlePane();
640: }
641:
642:
647: protected void installTitlePane()
648: {
649: installDefaults();
650: installListeners();
651: createActions();
652:
653: assembleSystemMenu();
654:
655: createButtons();
656: setButtonIcons();
657: addSubComponents();
658: enableActions();
659: }
660:
661:
664: protected void addSubComponents()
665: {
666: add(menuBar);
667:
668: add(closeButton);
669: add(iconButton);
670: add(maxButton);
671: }
672:
673:
677: protected void createActions()
678: {
679: closeAction = new CloseAction();
680: closeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, CLOSE_CMD);
681:
682: iconifyAction = new IconifyAction();
683: iconifyAction.putValue(AbstractAction.ACTION_COMMAND_KEY, ICONIFY_CMD);
684:
685: maximizeAction = new MaximizeAction();
686: maximizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MAXIMIZE_CMD);
687:
688: sizeAction = new SizeAction();
689: sizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, SIZE_CMD);
690:
691: restoreAction = new RestoreAction();
692: restoreAction.putValue(AbstractAction.ACTION_COMMAND_KEY, RESTORE_CMD);
693:
694: moveAction = new MoveAction();
695: moveAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MOVE_CMD);
696: }
697:
698:
701: protected void installListeners()
702: {
703: propertyChangeListener = createPropertyChangeListener();
704: frame.addPropertyChangeListener(propertyChangeListener);
705: }
706:
707:
710: protected void uninstallListeners()
711: {
712: frame.removePropertyChangeListener(propertyChangeListener);
713: propertyChangeListener = null;
714: }
715:
716:
719: protected void installDefaults()
720: {
721: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
722:
723: title.setFont(defaults.getFont("InternalFrame.titleFont"));
724: selectedTextColor = defaults.getColor("InternalFrame.activeTitleForeground");
725: selectedTitleColor = defaults.getColor("InternalFrame.activeTitleBackground");
726: notSelectedTextColor = defaults.getColor("InternalFrame.inactiveTitleForeground");
727: notSelectedTitleColor = defaults.getColor("InternalFrame.inactiveTitleBackground");
728:
729: closeIcon = UIManager.getIcon("InternalFrame.closeIcon");
730: iconIcon = UIManager.getIcon("InternalFrame.iconifyIcon");
731: maxIcon = UIManager.getIcon("InternalFrame.maximizeIcon");
732: }
733:
734:
737: protected void uninstallDefaults()
738: {
739: setFont(null);
740: selectedTextColor = null;
741: selectedTitleColor = null;
742: notSelectedTextColor = null;
743: notSelectedTitleColor = null;
744:
745: closeIcon = null;
746: iconIcon = null;
747: maxIcon = null;
748: }
749:
750:
753: protected void createButtons()
754: {
755: closeButton = new PaneButton(closeAction);
756: closeButton.setText(null);
757: if (!frame.isClosable())
758: closeButton.setVisible(false);
759: iconButton = new PaneButton(iconifyAction);
760: iconButton.setText(null);
761: if (!frame.isIconifiable())
762: iconButton.setVisible(false);
763: maxButton = new PaneButton(maximizeAction);
764: maxButton.setText(null);
765: if (!frame.isMaximizable())
766: maxButton.setVisible(false);
767: }
768:
769:
772: protected void setButtonIcons()
773: {
774: if (closeIcon != null)
775: closeButton.setIcon(closeIcon);
776: if (iconIcon != null)
777: iconButton.setIcon(iconIcon);
778: if (maxIcon != null)
779: maxButton.setIcon(maxIcon);
780: }
781:
782:
785: protected void assembleSystemMenu()
786: {
787: menuBar = createSystemMenuBar();
788: windowMenu = createSystemMenu();
789:
790: menuBar.add(windowMenu);
791:
792: addSystemMenuItems(windowMenu);
793: enableActions();
794: }
795:
796:
801: protected void addSystemMenuItems(JMenu systemMenu)
802: {
803: JMenuItem tmp;
804:
805: tmp = new JMenuItem(RESTORE_CMD);
806: tmp.addActionListener(restoreAction);
807: tmp.setMnemonic(KeyEvent.VK_R);
808: systemMenu.add(tmp);
809:
810: tmp = new JMenuItem(MOVE_CMD);
811: tmp.addActionListener(moveAction);
812: tmp.setMnemonic(KeyEvent.VK_M);
813: systemMenu.add(tmp);
814:
815: tmp = new JMenuItem(SIZE_CMD);
816: tmp.addActionListener(sizeAction);
817: tmp.setMnemonic(KeyEvent.VK_S);
818: systemMenu.add(tmp);
819:
820: tmp = new JMenuItem(ICONIFY_CMD);
821: tmp.addActionListener(iconifyAction);
822: tmp.setMnemonic(KeyEvent.VK_N);
823: systemMenu.add(tmp);
824:
825: tmp = new JMenuItem(MAXIMIZE_CMD);
826: tmp.addActionListener(maximizeAction);
827: tmp.setMnemonic(KeyEvent.VK_X);
828: systemMenu.add(tmp);
829:
830: systemMenu.addSeparator();
831:
832: tmp = new JMenuItem(CLOSE_CMD);
833: tmp.addActionListener(closeAction);
834: tmp.setMnemonic(KeyEvent.VK_C);
835: systemMenu.add(tmp);
836: }
837:
838:
843: protected JMenuBar createSystemMenuBar()
844: {
845: if (menuBar == null)
846: menuBar = new SystemMenuBar();
847: menuBar.removeAll();
848: return menuBar;
849: }
850:
851:
856: protected JMenu createSystemMenu()
857: {
858: if (windowMenu == null)
859: windowMenu = new JMenu();
860: windowMenu.removeAll();
861: return windowMenu;
862: }
863:
864:
867: protected void showSystemMenu()
868: {
869:
870: menuBar.getMenu(1).getPopupMenu().show();
871: }
872:
873:
878: public void paintComponent(Graphics g)
879: {
880: paintTitleBackground(g);
881: if (frame.getTitle() != null && title != null)
882: {
883: Color saved = g.getColor();
884: Font f = title.getFont();
885: g.setFont(f);
886: FontMetrics fm = g.getFontMetrics(f);
887: if (frame.isSelected())
888: g.setColor(selectedTextColor);
889: else
890: g.setColor(notSelectedTextColor);
891: title.setText(getTitle(frame.getTitle(), fm, title.getBounds().width));
892: SwingUtilities.paintComponent(g, title, null, title.getBounds());
893: g.setColor(saved);
894: }
895: }
896:
897:
902: protected void paintTitleBackground(Graphics g)
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: }