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:
91: public void actionPerformed(ActionEvent e)
92: {
93: if (frame.isClosable())
94: {
95: try
96: {
97: frame.setClosed(true);
98: }
99: catch (PropertyVetoException pve)
100: {
101: }
102: }
103: }
104: }
105:
106:
113: public class IconifyAction extends AbstractAction
114: {
115:
121: public void actionPerformed(ActionEvent e)
122: {
123: if (frame.isIconifiable() && ! frame.isIcon())
124: {
125: try
126: {
127: frame.setIcon(true);
128: }
129: catch (PropertyVetoException pve)
130: {
131: }
132: }
133: }
134: }
135:
136:
143: public class MaximizeAction extends AbstractAction
144: {
145:
151: public void actionPerformed(ActionEvent e)
152: {
153: try
154: {
155: if (frame.isMaximizable() && ! frame.isMaximum())
156: frame.setMaximum(true);
157: else if (frame.isMaximum())
158: frame.setMaximum(false);
159: }
160: catch (PropertyVetoException pve)
161: {
162: }
163: }
164: }
165:
166:
173: public class MoveAction extends AbstractAction
174: {
175:
180: public void actionPerformed(ActionEvent e)
181: {
182:
183: }
184: }
185:
186:
194: public class RestoreAction extends AbstractAction
195: {
196:
202: public void actionPerformed(ActionEvent e)
203: {
204: if (frame.isMaximum())
205: {
206: try
207: {
208: frame.setMaximum(false);
209: }
210: catch (PropertyVetoException pve)
211: {
212: }
213: }
214: }
215: }
216:
217:
224: public class SizeAction extends AbstractAction
225: {
226:
231: public void actionPerformed(ActionEvent e)
232: {
233:
234: }
235: }
236:
237:
245: public class PropertyChangeHandler implements PropertyChangeListener
246: {
247:
253: public void propertyChange(PropertyChangeEvent evt)
254: {
255: String propName = evt.getPropertyName();
256: if (propName.equals("closable"))
257: {
258: if (evt.getNewValue().equals(Boolean.TRUE))
259: closeButton.setVisible(true);
260: else
261: closeButton.setVisible(false);
262: }
263: else if (propName.equals("iconifiable"))
264: {
265: if (evt.getNewValue().equals(Boolean.TRUE))
266: iconButton.setVisible(true);
267: else
268: iconButton.setVisible(false);
269: }
270: else if (propName.equals("maximizable"))
271: {
272: if (evt.getNewValue().equals(Boolean.TRUE))
273: maxButton.setVisible(true);
274: else
275: maxButton.setVisible(false);
276: }
277:
278: }
279: }
280:
281:
289: public class SystemMenuBar extends JMenuBar
290: {
291:
296: public boolean isFocusTransversable()
297: {
298: return true;
299: }
300:
301:
307: public boolean isOpaque()
308: {
309: return true;
310: }
311:
312:
317: public void paint(Graphics g)
318: {
319: Icon frameIcon = frame.getFrameIcon();
320: if (frameIcon == null)
321: frameIcon = BasicDesktopIconUI.defaultIcon;
322: frameIcon.paintIcon(this, g, 0, 0);
323: }
324:
325:
328: public void requestFocus()
329: {
330: super.requestFocus();
331: }
332: }
333:
334:
341: public class TitlePaneLayout implements LayoutManager
342: {
343:
346: public TitlePaneLayout()
347: {
348:
349: }
350:
351:
357: public void addLayoutComponent(String name, Component c)
358: {
359:
360: }
361:
362:
367: public void layoutContainer(Container c)
368: {
369: Dimension size = c.getSize();
370: Insets insets = c.getInsets();
371: int width = size.width - insets.left - insets.right;
372: int height = size.height - insets.top - insets.bottom;
373:
374:
375: Dimension menupref = menuBar.getPreferredSize();
376: menuBar.setBounds(insets.left, insets.top, menupref.width, height);
377:
378: int loc = width + insets.left - 1;
379: int top = insets.top + 1;
380: int buttonWidth = height - 2;
381: int buttonHeight = height - 4;
382: if (closeButton.isVisible())
383: {
384: loc -= buttonWidth + 2;
385: closeButton.setBounds(loc, top, buttonWidth, buttonHeight);
386: }
387:
388: if (maxButton.isVisible())
389: {
390: loc -= buttonWidth + 2;
391: maxButton.setBounds(loc, top, buttonWidth, buttonHeight);
392: }
393:
394: if (iconButton.isVisible())
395: {
396: loc -= buttonWidth + 2;
397: iconButton.setBounds(loc, top, buttonWidth, buttonHeight);
398: }
399:
400: if (title != null)
401: title.setBounds(insets.left + menupref.width, insets.top,
402: loc - menupref.width - insets.left, height);
403: }
404:
405:
413: public Dimension minimumLayoutSize(Container c)
414: {
415: return preferredLayoutSize(c);
416: }
417:
418:
426: public Dimension preferredLayoutSize(Container c)
427: {
428: return new Dimension(22, 18);
429: }
430:
431:
436: public void removeLayoutComponent(Component c)
437: {
438: }
439: }
440:
441:
446: private class PaneButton extends JButton
447: {
448:
453: public PaneButton(Action a)
454: {
455: super(a);
456: setMargin(new Insets(0, 0, 0, 0));
457: }
458:
459:
464: public boolean isFocusable()
465: {
466:
467: return false;
468: }
469: }
470:
471:
472: protected static final String CLOSE_CMD = "Close";
473:
474:
475: protected static final String ICONIFY_CMD = "Minimize";
476:
477:
478: protected static final String MAXIMIZE_CMD = "Maximize";
479:
480:
481: protected static final String MOVE_CMD = "Move";
482:
483:
484: protected static final String RESTORE_CMD = "Restore";
485:
486:
487: protected static final String SIZE_CMD = "Size";
488:
489:
490: protected Action closeAction;
491:
492:
493: protected Action iconifyAction;
494:
495:
496: protected Action maximizeAction;
497:
498:
499: protected Action moveAction;
500:
501:
502: protected Action restoreAction;
503:
504:
505: protected Action sizeAction;
506:
507:
508: protected JButton closeButton;
509:
510:
511: protected JButton iconButton;
512:
513:
514: protected JButton maxButton;
515:
516:
517: protected Icon minIcon = BasicIconFactory.createEmptyFrameIcon();
518:
519:
520: protected Icon maxIcon = BasicIconFactory.createEmptyFrameIcon();
521:
522:
523: protected Icon iconIcon = BasicIconFactory.createEmptyFrameIcon();
524:
525:
526: protected JInternalFrame frame;
527:
528:
529: protected JMenuBar menuBar;
530:
531:
532: protected JMenu windowMenu;
533:
534:
537: protected Color notSelectedTextColor;
538:
539:
543: protected Color notSelectedTitleColor;
544:
545:
546: protected Color selectedTextColor;
547:
548:
552: protected Color selectedTitleColor;
553:
554:
555: protected PropertyChangeListener propertyChangeListener;
556:
557:
562: transient JLabel title;
563:
564:
571: public BasicInternalFrameTitlePane(JInternalFrame f)
572: {
573: frame = f;
574: setLayout(createLayout());
575: title = new JLabel();
576: title.setHorizontalAlignment(SwingConstants.LEFT);
577: title.setHorizontalTextPosition(SwingConstants.LEFT);
578: title.setOpaque(false);
579: setOpaque(true);
580:
581: setBackground(Color.LIGHT_GRAY);
582: setOpaque(true);
583:
584: installTitlePane();
585: }
586:
587:
592: protected void installTitlePane()
593: {
594: installDefaults();
595: installListeners();
596: createActions();
597:
598: assembleSystemMenu();
599:
600: createButtons();
601: setButtonIcons();
602: addSubComponents();
603: enableActions();
604: }
605:
606:
609: protected void addSubComponents()
610: {
611: add(menuBar);
612:
613: add(closeButton);
614: add(iconButton);
615: add(maxButton);
616: }
617:
618:
622: protected void createActions()
623: {
624: closeAction = new CloseAction();
625: closeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, CLOSE_CMD);
626:
627: iconifyAction = new IconifyAction();
628: iconifyAction.putValue(AbstractAction.ACTION_COMMAND_KEY, ICONIFY_CMD);
629:
630: maximizeAction = new MaximizeAction();
631: maximizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MAXIMIZE_CMD);
632:
633: sizeAction = new SizeAction();
634: sizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, SIZE_CMD);
635:
636: restoreAction = new RestoreAction();
637: restoreAction.putValue(AbstractAction.ACTION_COMMAND_KEY, RESTORE_CMD);
638:
639: moveAction = new MoveAction();
640: moveAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MOVE_CMD);
641: }
642:
643:
646: protected void installListeners()
647: {
648: propertyChangeListener = new PropertyChangeHandler();
649: frame.addPropertyChangeListener(propertyChangeListener);
650: }
651:
652:
655: protected void uninstallListeners()
656: {
657: frame.removePropertyChangeListener(propertyChangeListener);
658: propertyChangeListener = null;
659: }
660:
661:
664: protected void installDefaults()
665: {
666:
667: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
668:
669: setFont(defaults.getFont("InternalFrame.titleFont"));
670: selectedTextColor = defaults.getColor("InternalFrame.activeTitleForeground");
671: selectedTitleColor = defaults.getColor("InternalFrame.activeTitleBackground");
672: notSelectedTextColor = defaults.getColor("InternalFrame.inactiveTitleForeground");
673: notSelectedTitleColor = defaults.getColor("InternalFrame.inactiveTitleBackground");
674: }
675:
676:
679: protected void uninstallDefaults()
680: {
681: setFont(null);
682: selectedTextColor = null;
683: selectedTitleColor = null;
684: notSelectedTextColor = null;
685: notSelectedTitleColor = null;
686: }
687:
688:
691: protected void createButtons()
692: {
693: closeButton = new PaneButton(closeAction);
694: if (!frame.isClosable())
695: closeButton.setVisible(false);
696: iconButton = new PaneButton(iconifyAction);
697: if (!frame.isIconifiable())
698: iconButton.setVisible(false);
699: maxButton = new PaneButton(maximizeAction);
700: if (!frame.isMaximizable())
701: maxButton.setVisible(false);
702: }
703:
704:
707: protected void setButtonIcons()
708: {
709: Icon icon = UIManager.getIcon("InternalFrame.closeIcon");
710: if (icon != null)
711: closeButton.setIcon(icon);
712: icon = UIManager.getIcon("InternalFrame.iconifyIcon");
713: if (icon != null)
714: iconButton.setIcon(icon);
715: icon = UIManager.getIcon("InternalFrame.maximizeIcon");
716: if (icon != null)
717: maxButton.setIcon(icon);
718: }
719:
720:
723: protected void assembleSystemMenu()
724: {
725: menuBar = createSystemMenuBar();
726: windowMenu = createSystemMenu();
727:
728: menuBar.add(windowMenu);
729:
730: addSystemMenuItems(windowMenu);
731: enableActions();
732: }
733:
734:
739: protected void addSystemMenuItems(JMenu systemMenu)
740: {
741: JMenuItem tmp;
742:
743: tmp = new JMenuItem(RESTORE_CMD);
744: tmp.addActionListener(restoreAction);
745: tmp.setMnemonic(KeyEvent.VK_R);
746: systemMenu.add(tmp);
747:
748: tmp = new JMenuItem(MOVE_CMD);
749: tmp.addActionListener(moveAction);
750: tmp.setMnemonic(KeyEvent.VK_M);
751: systemMenu.add(tmp);
752:
753: tmp = new JMenuItem(SIZE_CMD);
754: tmp.addActionListener(sizeAction);
755: tmp.setMnemonic(KeyEvent.VK_S);
756: systemMenu.add(tmp);
757:
758: tmp = new JMenuItem(ICONIFY_CMD);
759: tmp.addActionListener(iconifyAction);
760: tmp.setMnemonic(KeyEvent.VK_N);
761: systemMenu.add(tmp);
762:
763: tmp = new JMenuItem(MAXIMIZE_CMD);
764: tmp.addActionListener(maximizeAction);
765: tmp.setMnemonic(KeyEvent.VK_X);
766: systemMenu.add(tmp);
767:
768: systemMenu.addSeparator();
769:
770: tmp = new JMenuItem(CLOSE_CMD);
771: tmp.addActionListener(closeAction);
772: tmp.setMnemonic(KeyEvent.VK_C);
773: systemMenu.add(tmp);
774: }
775:
776:
781: protected JMenuBar createSystemMenuBar()
782: {
783: if (menuBar == null)
784: menuBar = new SystemMenuBar();
785: menuBar.removeAll();
786: return menuBar;
787: }
788:
789:
794: protected JMenu createSystemMenu()
795: {
796: if (windowMenu == null)
797: windowMenu = new JMenu();
798: windowMenu.removeAll();
799: return windowMenu;
800: }
801:
802:
805: protected void showSystemMenu()
806: {
807:
808: menuBar.getMenu(1).getPopupMenu().show();
809: }
810:
811:
816: public void paintComponent(Graphics g)
817: {
818: paintTitleBackground(g);
819: Font f = g.getFont();
820: FontMetrics fm = g.getFontMetrics(f);
821: if (frame.getTitle() != null && title != null)
822: {
823: Color saved = g.getColor();
824: if (frame.isSelected())
825: g.setColor(selectedTextColor);
826: else
827: g.setColor(notSelectedTextColor);
828: title.setText(getTitle(frame.getTitle(), fm, title.getBounds().width));
829: SwingUtilities.paintComponent(g, title, null, title.getBounds());
830: g.setColor(saved);
831: }
832: }
833:
834:
839: protected void paintTitleBackground(Graphics g)
840: {
841: Color saved = g.getColor();
842: Dimension dims = getSize();
843:
844: Color bg = getBackground();
845: if (frame.isSelected())
846: bg = selectedTitleColor;
847: else
848: bg = notSelectedTitleColor;
849: g.setColor(bg);
850: g.fillRect(0, 0, dims.width, dims.height);
851: g.setColor(saved);
852: }
853:
854:
864: protected String getTitle(String text, FontMetrics fm, int availableWidth)
865: {
866: Rectangle vr = new Rectangle(0, 0, availableWidth, fm.getHeight());
867: Rectangle ir = new Rectangle();
868: Rectangle tr = new Rectangle();
869: String value = SwingUtilities.layoutCompoundLabel(this, fm, text, null,
870: SwingConstants.CENTER,
871: SwingConstants.LEFT,
872: SwingConstants.CENTER,
873: SwingConstants.LEFT, vr,
874: ir, tr, 0);
875: return value;
876: }
877:
878:
883: protected void postClosingEvent(JInternalFrame frame)
884: {
885:
886:
887:
888:
889:
890:
891: }
892:
893:
897: protected void enableActions()
898: {
899: closeAction.setEnabled(frame.isClosable());
900:
901: iconifyAction.setEnabled(frame.isIconifiable());
902:
903:
904: maximizeAction.setEnabled(frame.isMaximizable());
905:
906:
907:
908: restoreAction.setEnabled(frame.isMaximum());
909:
910: sizeAction.setEnabled(frame.isResizable());
911:
912:
913: moveAction.setEnabled(false);
914: }
915:
916:
921: protected PropertyChangeListener createPropertyChangeListener()
922: {
923: return new PropertyChangeHandler();
924: }
925:
926:
931: protected LayoutManager createLayout()
932: {
933: return new TitlePaneLayout();
934: }
935: }