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: import ;
57:
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: import ;
72:
73:
76: public class BasicInternalFrameTitlePane extends JComponent
77: {
78:
85: public class CloseAction extends AbstractAction
86: {
87:
92: public void actionPerformed(ActionEvent e)
93: {
94: if (frame.isClosable())
95: {
96: try
97: {
98: frame.setClosed(true);
99: }
100: catch (PropertyVetoException pve)
101: {
102: }
103: }
104: }
105: }
106:
107:
114: public class IconifyAction extends AbstractAction
115: {
116:
122: public void actionPerformed(ActionEvent e)
123: {
124: if (frame.isIconifiable() && ! frame.isIcon())
125: {
126: try
127: {
128: frame.setIcon(true);
129: }
130: catch (PropertyVetoException pve)
131: {
132: }
133: }
134: }
135: }
136:
137:
144: public class MaximizeAction extends AbstractAction
145: {
146:
152: public void actionPerformed(ActionEvent e)
153: {
154: try
155: {
156: if (frame.isMaximizable() && ! frame.isMaximum())
157: frame.setMaximum(true);
158: else if (frame.isMaximum())
159: frame.setMaximum(false);
160: }
161: catch (PropertyVetoException pve)
162: {
163: }
164: }
165: }
166:
167:
174: public class MoveAction extends AbstractAction
175: {
176:
181: public void actionPerformed(ActionEvent e)
182: {
183:
184: }
185: }
186:
187:
195: public class RestoreAction extends AbstractAction
196: {
197:
203: public void actionPerformed(ActionEvent e)
204: {
205: if (frame.isMaximum())
206: {
207: try
208: {
209: frame.setMaximum(false);
210: }
211: catch (PropertyVetoException pve)
212: {
213: }
214: }
215: }
216: }
217:
218:
225: public class SizeAction extends AbstractAction
226: {
227:
232: public void actionPerformed(ActionEvent e)
233: {
234:
235: }
236: }
237:
238:
246: public class PropertyChangeHandler implements PropertyChangeListener
247: {
248:
254: public void propertyChange(PropertyChangeEvent evt)
255: {
256: String propName = evt.getPropertyName();
257: if (propName.equals("closable"))
258: {
259: if (evt.getNewValue().equals(Boolean.TRUE))
260: closeButton.setVisible(true);
261: else
262: closeButton.setVisible(false);
263: }
264: else if (propName.equals("iconifiable"))
265: {
266: if (evt.getNewValue().equals(Boolean.TRUE))
267: iconButton.setVisible(true);
268: else
269: iconButton.setVisible(false);
270: }
271: else if (propName.equals("maximizable"))
272: {
273: if (evt.getNewValue().equals(Boolean.TRUE))
274: maxButton.setVisible(true);
275: else
276: maxButton.setVisible(false);
277: }
278:
279: }
280: }
281:
282:
290: public class SystemMenuBar extends JMenuBar
291: {
292:
297: public boolean isFocusTransversable()
298: {
299: return true;
300: }
301:
302:
308: public boolean isOpaque()
309: {
310: return true;
311: }
312:
313:
318: public void paint(Graphics g)
319: {
320: Icon frameIcon = frame.getFrameIcon();
321: if (frameIcon == null)
322: frameIcon = BasicDesktopIconUI.defaultIcon;
323: frameIcon.paintIcon(this, g, 0, 0);
324: }
325:
326:
329: public void requestFocus()
330: {
331: super.requestFocus();
332: }
333: }
334:
335:
342: public class TitlePaneLayout implements LayoutManager
343: {
344:
347: public TitlePaneLayout()
348: {
349:
350: }
351:
352:
358: public void addLayoutComponent(String name, Component c)
359: {
360:
361: }
362:
363:
368: public void layoutContainer(Container c)
369: {
370: Dimension size = c.getSize();
371: Insets insets = c.getInsets();
372: int width = size.width - insets.left - insets.right;
373: int height = size.height - insets.top - insets.bottom;
374:
375:
376: Dimension menupref = menuBar.getPreferredSize();
377: menuBar.setBounds(insets.left, insets.top, menupref.width, height);
378:
379: int loc = width + insets.left - 1;
380: int top = insets.top + 1;
381: int buttonWidth = height - 2;
382: int buttonHeight = height - 4;
383: if (closeButton.isVisible())
384: {
385: loc -= buttonWidth + 2;
386: closeButton.setBounds(loc, top, buttonWidth, buttonHeight);
387: }
388:
389: if (maxButton.isVisible())
390: {
391: loc -= buttonWidth + 2;
392: maxButton.setBounds(loc, top, buttonWidth, buttonHeight);
393: }
394:
395: if (iconButton.isVisible())
396: {
397: loc -= buttonWidth + 2;
398: iconButton.setBounds(loc, top, buttonWidth, buttonHeight);
399: }
400:
401: if (title != null)
402: title.setBounds(insets.left + menupref.width, insets.top,
403: loc - menupref.width - insets.left, height);
404: }
405:
406:
414: public Dimension minimumLayoutSize(Container c)
415: {
416: return preferredLayoutSize(c);
417: }
418:
419:
427: public Dimension preferredLayoutSize(Container c)
428: {
429: return new Dimension(22, 18);
430: }
431:
432:
437: public void removeLayoutComponent(Component c)
438: {
439: }
440: }
441:
442:
447: private class PaneButton extends JButton
448: {
449:
454: public PaneButton(Action a)
455: {
456: super(a);
457: setMargin(new Insets(0, 0, 0, 0));
458: }
459:
460:
465: public boolean isFocusable()
466: {
467:
468: return false;
469: }
470: }
471:
472:
473: protected static final String CLOSE_CMD = "Close";
474:
475:
476: protected static final String ICONIFY_CMD = "Minimize";
477:
478:
479: protected static final String MAXIMIZE_CMD = "Maximize";
480:
481:
482: protected static final String MOVE_CMD = "Move";
483:
484:
485: protected static final String RESTORE_CMD = "Restore";
486:
487:
488: protected static final String SIZE_CMD = "Size";
489:
490:
491: protected Action closeAction;
492:
493:
494: protected Action iconifyAction;
495:
496:
497: protected Action maximizeAction;
498:
499:
500: protected Action moveAction;
501:
502:
503: protected Action restoreAction;
504:
505:
506: protected Action sizeAction;
507:
508:
509: protected JButton closeButton;
510:
511:
512: protected JButton iconButton;
513:
514:
515: protected JButton maxButton;
516:
517:
518: protected Color activeBGColor;
519:
520:
521: protected Color activeFGColor;
522:
523:
524: protected Color inactiveBGColor;
525:
526:
527: protected Color inactiveFGColor;
528:
529:
530: protected Icon minIcon = BasicIconFactory.createEmptyFrameIcon();
531:
532:
533: protected Icon maxIcon = BasicIconFactory.createEmptyFrameIcon();
534:
535:
536: protected Icon iconIcon = BasicIconFactory.createEmptyFrameIcon();
537:
538:
539: protected JInternalFrame frame;
540:
541:
542: protected JMenuBar menuBar;
543:
544:
545: protected JMenu windowMenu;
546:
547:
550: protected Color notSelectedTextColor;
551:
552:
556: protected Color notSelectedTitleColor;
557:
558:
559: protected Color selectedTextColor;
560:
561:
565: protected Color selectedTitleColor;
566:
567:
568: protected PropertyChangeListener propertyChangeListener;
569:
570:
575: transient JLabel title;
576:
577:
584: public BasicInternalFrameTitlePane(JInternalFrame f)
585: {
586: frame = f;
587: setLayout(createLayout());
588: title = new JLabel();
589: title.setHorizontalAlignment(SwingConstants.LEFT);
590: title.setHorizontalTextPosition(SwingConstants.LEFT);
591: title.setOpaque(false);
592: setOpaque(true);
593:
594: setBackground(Color.LIGHT_GRAY);
595:
596: installTitlePane();
597: }
598:
599:
604: protected void installTitlePane()
605: {
606: installDefaults();
607: installListeners();
608: createActions();
609:
610: assembleSystemMenu();
611:
612: createButtons();
613: setButtonIcons();
614: addSubComponents();
615: enableActions();
616: }
617:
618:
621: protected void addSubComponents()
622: {
623: add(menuBar);
624:
625: add(closeButton);
626: add(iconButton);
627: add(maxButton);
628: }
629:
630:
634: protected void createActions()
635: {
636: closeAction = new CloseAction();
637: closeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, CLOSE_CMD);
638:
639: iconifyAction = new IconifyAction();
640: iconifyAction.putValue(AbstractAction.ACTION_COMMAND_KEY, ICONIFY_CMD);
641:
642: maximizeAction = new MaximizeAction();
643: maximizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MAXIMIZE_CMD);
644:
645: sizeAction = new SizeAction();
646: sizeAction.putValue(AbstractAction.ACTION_COMMAND_KEY, SIZE_CMD);
647:
648: restoreAction = new RestoreAction();
649: restoreAction.putValue(AbstractAction.ACTION_COMMAND_KEY, RESTORE_CMD);
650:
651: moveAction = new MoveAction();
652: moveAction.putValue(AbstractAction.ACTION_COMMAND_KEY, MOVE_CMD);
653: }
654:
655:
658: protected void installListeners()
659: {
660: propertyChangeListener = new PropertyChangeHandler();
661: frame.addPropertyChangeListener(propertyChangeListener);
662: }
663:
664:
667: protected void uninstallListeners()
668: {
669: frame.removePropertyChangeListener(propertyChangeListener);
670: propertyChangeListener = null;
671: }
672:
673:
676: protected void installDefaults()
677: {
678:
679: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
680:
681: setFont(defaults.getFont("InternalFrame.titleFont"));
682: activeFGColor = defaults.getColor("InternalFrame.activeTitleForeground");
683: activeBGColor = defaults.getColor("InternalFrame.activeTitleBackground");
684: inactiveFGColor = defaults.getColor("InternalFrame.inactiveTitleForeground");
685: inactiveBGColor = defaults.getColor("InternalFrame.inactiveTitleBackground");
686: }
687:
688:
691: protected void uninstallDefaults()
692: {
693: setFont(null);
694: activeFGColor = null;
695: activeBGColor = null;
696: inactiveFGColor = null;
697: inactiveBGColor = null;
698: }
699:
700:
703: protected void createButtons()
704: {
705: closeButton = new PaneButton(closeAction);
706: if (!frame.isClosable())
707: closeButton.setVisible(false);
708: iconButton = new PaneButton(iconifyAction);
709: if (!frame.isIconifiable())
710: iconButton.setVisible(false);
711: maxButton = new PaneButton(maximizeAction);
712: if (!frame.isMaximizable())
713: maxButton.setVisible(false);
714: }
715:
716:
721: protected void setButtonIcons()
722: {
723: }
724:
725:
728: protected void assembleSystemMenu()
729: {
730: menuBar = createSystemMenuBar();
731: windowMenu = createSystemMenu();
732:
733: menuBar.add(windowMenu);
734:
735: addSystemMenuItems(windowMenu);
736: enableActions();
737: }
738:
739:
744: protected void addSystemMenuItems(JMenu systemMenu)
745: {
746: JMenuItem tmp;
747:
748: tmp = new JMenuItem(RESTORE_CMD);
749: tmp.addActionListener(restoreAction);
750: tmp.setMnemonic(KeyEvent.VK_R);
751: systemMenu.add(tmp);
752:
753: tmp = new JMenuItem(MOVE_CMD);
754: tmp.addActionListener(moveAction);
755: tmp.setMnemonic(KeyEvent.VK_M);
756: systemMenu.add(tmp);
757:
758: tmp = new JMenuItem(SIZE_CMD);
759: tmp.addActionListener(sizeAction);
760: tmp.setMnemonic(KeyEvent.VK_S);
761: systemMenu.add(tmp);
762:
763: tmp = new JMenuItem(ICONIFY_CMD);
764: tmp.addActionListener(iconifyAction);
765: tmp.setMnemonic(KeyEvent.VK_N);
766: systemMenu.add(tmp);
767:
768: tmp = new JMenuItem(MAXIMIZE_CMD);
769: tmp.addActionListener(maximizeAction);
770: tmp.setMnemonic(KeyEvent.VK_X);
771: systemMenu.add(tmp);
772:
773: systemMenu.addSeparator();
774:
775: tmp = new JMenuItem(CLOSE_CMD);
776: tmp.addActionListener(closeAction);
777: tmp.setMnemonic(KeyEvent.VK_C);
778: systemMenu.add(tmp);
779: }
780:
781:
786: protected JMenuBar createSystemMenuBar()
787: {
788: if (menuBar == null)
789: menuBar = new SystemMenuBar();
790: menuBar.removeAll();
791: return menuBar;
792: }
793:
794:
799: protected JMenu createSystemMenu()
800: {
801: if (windowMenu == null)
802: windowMenu = new JMenu();
803: windowMenu.removeAll();
804: return windowMenu;
805: }
806:
807:
810: protected void showSystemMenu()
811: {
812:
813: menuBar.getMenu(1).getPopupMenu().show();
814: }
815:
816:
821: public void paintComponent(Graphics g)
822: {
823: paintTitleBackground(g);
824: Font f = g.getFont();
825: FontMetrics fm = g.getFontMetrics(f);
826: if (frame.getTitle() != null && title != null)
827: {
828: Color saved = g.getColor();
829: if (frame.isSelected())
830: g.setColor(activeFGColor);
831: else
832: g.setColor(inactiveFGColor);
833: title.setText(getTitle(frame.getTitle(), fm, title.getBounds().width));
834: SwingUtilities.paintComponent(g, title, null, title.getBounds());
835: g.setColor(saved);
836: }
837: }
838:
839:
844: protected void paintTitleBackground(Graphics g)
845: {
846: Color saved = g.getColor();
847: Dimension dims = getSize();
848:
849: Color bg = getBackground();
850: if (frame.isSelected())
851: bg = activeBGColor;
852: else
853: bg = inactiveBGColor;
854: g.setColor(bg);
855: g.fillRect(0, 0, dims.width, dims.height);
856: g.setColor(saved);
857: }
858:
859:
869: protected String getTitle(String text, FontMetrics fm, int availableWidth)
870: {
871: Rectangle vr = new Rectangle(0, 0, availableWidth, fm.getHeight());
872: Rectangle ir = new Rectangle();
873: Rectangle tr = new Rectangle();
874: String value = SwingUtilities.layoutCompoundLabel(this, fm, text, null,
875: SwingConstants.CENTER,
876: SwingConstants.LEFT,
877: SwingConstants.CENTER,
878: SwingConstants.LEFT, vr,
879: ir, tr, 0);
880: return value;
881: }
882:
883:
888: protected void postClosingEvent(JInternalFrame frame)
889: {
890:
891:
892:
893:
894:
895:
896: }
897:
898:
902: protected void enableActions()
903: {
904: closeAction.setEnabled(frame.isClosable());
905:
906: iconifyAction.setEnabled(frame.isIconifiable());
907:
908:
909: maximizeAction.setEnabled(frame.isMaximizable());
910:
911:
912:
913: restoreAction.setEnabled(frame.isMaximum());
914:
915: sizeAction.setEnabled(frame.isResizable());
916:
917:
918: moveAction.setEnabled(false);
919: }
920:
921:
926: protected PropertyChangeListener createPropertyChangeListener()
927: {
928: return new PropertyChangeHandler();
929: }
930:
931:
936: protected LayoutManager createLayout()
937: {
938: return new TitlePaneLayout();
939: }
940: }