1. /*
  2. * @(#)BasicInternalFrameTitlePane.java 1.53 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.plaf.*;
  12. import javax.swing.border.*;
  13. import javax.swing.event.InternalFrameEvent;
  14. import java.util.EventListener;
  15. import java.beans.PropertyChangeListener;
  16. import java.beans.PropertyChangeEvent;
  17. import java.beans.VetoableChangeListener;
  18. import java.beans.PropertyVetoException;
  19. /**
  20. * The class that manages a basic title bar
  21. * <p>
  22. * <strong>Warning:</strong>
  23. * Serialized objects of this class will not be compatible with
  24. * future Swing releases. The current serialization support is
  25. * appropriate for short term storage or RMI between applications running
  26. * the same version of Swing. As of 1.4, support for long term storage
  27. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  28. * has been added to the <code>java.beans</code> package.
  29. * Please see {@link java.beans.XMLEncoder}.
  30. *
  31. * @version 1.41 01/18/01
  32. * @author David Kloba
  33. * @author Steve Wilson
  34. */
  35. public class BasicInternalFrameTitlePane extends JComponent
  36. {
  37. protected JMenuBar menuBar;
  38. protected JButton iconButton;
  39. protected JButton maxButton;
  40. protected JButton closeButton;
  41. protected JMenu windowMenu;
  42. protected JInternalFrame frame;
  43. protected Color selectedTitleColor;
  44. protected Color selectedTextColor;
  45. protected Color notSelectedTitleColor;
  46. protected Color notSelectedTextColor;
  47. protected Icon maxIcon;
  48. protected Icon minIcon;
  49. protected Icon iconIcon;
  50. protected Icon closeIcon;
  51. protected PropertyChangeListener propertyChangeListener;
  52. protected Action closeAction;
  53. protected Action maximizeAction;
  54. protected Action iconifyAction;
  55. protected Action restoreAction;
  56. protected Action moveAction;
  57. protected Action sizeAction;
  58. protected static final String CLOSE_CMD =
  59. UIManager.getString("InternalFrameTitlePane.closeButtonText");
  60. protected static final String ICONIFY_CMD =
  61. UIManager.getString("InternalFrameTitlePane.minimizeButtonText");
  62. protected static final String RESTORE_CMD =
  63. UIManager.getString("InternalFrameTitlePane.restoreButtonText");
  64. protected static final String MAXIMIZE_CMD =
  65. UIManager.getString("InternalFrameTitlePane.maximizeButtonText");
  66. protected static final String MOVE_CMD =
  67. UIManager.getString("InternalFrameTitlePane.moveButtonText");
  68. protected static final String SIZE_CMD =
  69. UIManager.getString("InternalFrameTitlePane.sizeButtonText");
  70. private String closeButtonToolTip;
  71. private String iconButtonToolTip;
  72. private String restoreButtonToolTip;
  73. private String maxButtonToolTip;
  74. public BasicInternalFrameTitlePane(JInternalFrame f) {
  75. frame = f;
  76. installTitlePane();
  77. }
  78. protected void installTitlePane() {
  79. installDefaults();
  80. installListeners();
  81. createActions();
  82. enableActions();
  83. createActionMap();
  84. setLayout(createLayout());
  85. assembleSystemMenu();
  86. createButtons();
  87. addSubComponents();
  88. }
  89. protected void addSubComponents() {
  90. add(menuBar);
  91. add(iconButton);
  92. add(maxButton);
  93. add(closeButton);
  94. }
  95. protected void createActions() {
  96. maximizeAction = new MaximizeAction();
  97. iconifyAction = new IconifyAction();
  98. closeAction = new CloseAction();
  99. restoreAction = new RestoreAction();
  100. moveAction = new MoveAction();
  101. sizeAction = new SizeAction();
  102. }
  103. ActionMap createActionMap() {
  104. ActionMap map = new ActionMapUIResource();
  105. map.put("showSystemMenu", new ShowSystemMenuAction(true));
  106. map.put("hideSystemMenu", new ShowSystemMenuAction(false));
  107. return map;
  108. }
  109. protected void installListeners() {
  110. if( propertyChangeListener == null ) {
  111. propertyChangeListener = createPropertyChangeListener();
  112. }
  113. frame.addPropertyChangeListener(propertyChangeListener);
  114. }
  115. protected void uninstallListeners() {
  116. frame.removePropertyChangeListener(propertyChangeListener);
  117. }
  118. protected void installDefaults() {
  119. maxIcon = UIManager.getIcon("InternalFrame.maximizeIcon");
  120. minIcon = UIManager.getIcon("InternalFrame.minimizeIcon");
  121. iconIcon = UIManager.getIcon("InternalFrame.iconifyIcon");
  122. closeIcon = UIManager.getIcon("InternalFrame.closeIcon");
  123. selectedTitleColor = UIManager.getColor("InternalFrame.activeTitleBackground");
  124. selectedTextColor = UIManager.getColor("InternalFrame.activeTitleForeground");
  125. notSelectedTitleColor = UIManager.getColor("InternalFrame.inactiveTitleBackground");
  126. notSelectedTextColor = UIManager.getColor("InternalFrame.inactiveTitleForeground");
  127. setFont(UIManager.getFont("InternalFrame.titleFont"));
  128. closeButtonToolTip =
  129. UIManager.getString("InternalFrame.closeButtonToolTip");
  130. iconButtonToolTip =
  131. UIManager.getString("InternalFrame.iconButtonToolTip");
  132. restoreButtonToolTip =
  133. UIManager.getString("InternalFrame.restoreButtonToolTip");
  134. maxButtonToolTip =
  135. UIManager.getString("InternalFrame.maxButtonToolTip");
  136. }
  137. protected void uninstallDefaults() {
  138. }
  139. protected void createButtons() {
  140. iconButton = new NoFocusButton();
  141. iconButton.addActionListener(iconifyAction);
  142. if (iconButtonToolTip != null && iconButtonToolTip.length() != 0) {
  143. iconButton.setToolTipText(iconButtonToolTip);
  144. }
  145. maxButton = new NoFocusButton();
  146. maxButton.addActionListener(maximizeAction);
  147. closeButton = new NoFocusButton();
  148. closeButton.addActionListener(closeAction);
  149. if (closeButtonToolTip != null && closeButtonToolTip.length() != 0) {
  150. closeButton.setToolTipText(closeButtonToolTip);
  151. }
  152. setButtonIcons();
  153. }
  154. protected void setButtonIcons() {
  155. if(frame.isIcon()) {
  156. iconButton.setIcon(minIcon);
  157. if (restoreButtonToolTip != null &&
  158. restoreButtonToolTip.length() != 0) {
  159. iconButton.setToolTipText(restoreButtonToolTip);
  160. }
  161. maxButton.setIcon(maxIcon);
  162. if (maxButtonToolTip != null && maxButtonToolTip.length() != 0) {
  163. maxButton.setToolTipText(maxButtonToolTip);
  164. }
  165. } else if (frame.isMaximum()) {
  166. iconButton.setIcon(iconIcon);
  167. maxButton.setIcon(minIcon);
  168. if (restoreButtonToolTip != null &&
  169. restoreButtonToolTip.length() != 0) {
  170. maxButton.setToolTipText(restoreButtonToolTip);
  171. }
  172. } else {
  173. iconButton.setIcon(iconIcon);
  174. if (iconButtonToolTip != null && iconButtonToolTip.length() != 0) {
  175. iconButton.setToolTipText(iconButtonToolTip);
  176. }
  177. maxButton.setIcon(maxIcon);
  178. if (maxButtonToolTip != null && maxButtonToolTip.length() != 0) {
  179. maxButton.setToolTipText(maxButtonToolTip);
  180. }
  181. }
  182. closeButton.setIcon(closeIcon);
  183. }
  184. protected void assembleSystemMenu() {
  185. menuBar = createSystemMenuBar();
  186. windowMenu = createSystemMenu();
  187. menuBar.add(windowMenu);
  188. addSystemMenuItems(windowMenu);
  189. enableActions();
  190. }
  191. protected void addSystemMenuItems(JMenu systemMenu) {
  192. JMenuItem mi = (JMenuItem)systemMenu.add(restoreAction);
  193. mi.setMnemonic('R');
  194. mi = (JMenuItem)systemMenu.add(moveAction);
  195. mi.setMnemonic('M');
  196. mi = (JMenuItem)systemMenu.add(sizeAction);
  197. mi.setMnemonic('S');
  198. mi = (JMenuItem)systemMenu.add(iconifyAction);
  199. mi.setMnemonic('n');
  200. mi = (JMenuItem)systemMenu.add(maximizeAction);
  201. mi.setMnemonic('x');
  202. systemMenu.add(new JSeparator());
  203. mi = (JMenuItem)systemMenu.add(closeAction);
  204. mi.setMnemonic('C');
  205. }
  206. protected JMenu createSystemMenu() {
  207. return new JMenu(" ");
  208. }
  209. protected JMenuBar createSystemMenuBar() {
  210. menuBar = new SystemMenuBar();
  211. menuBar.setBorderPainted(false);
  212. return menuBar;
  213. }
  214. protected void showSystemMenu(){
  215. // windowMenu.setPopupMenuVisible(true);
  216. // windowMenu.setVisible(true);
  217. windowMenu.doClick();
  218. }
  219. public void paintComponent(Graphics g) {
  220. paintTitleBackground(g);
  221. if(frame.getTitle() != null) {
  222. boolean isSelected = frame.isSelected();
  223. Font f = g.getFont();
  224. g.setFont(getFont());
  225. if(isSelected)
  226. g.setColor(selectedTextColor);
  227. else
  228. g.setColor(notSelectedTextColor);
  229. // Center text vertically.
  230. FontMetrics fm = g.getFontMetrics();
  231. int baseline = (getHeight() + fm.getAscent() - fm.getLeading() -
  232. fm.getDescent()) / 2;
  233. int titleX;
  234. Rectangle r = new Rectangle(0, 0, 0, 0);
  235. if (frame.isIconifiable()) r = iconButton.getBounds();
  236. else if (frame.isMaximizable()) r = maxButton.getBounds();
  237. else if (frame.isClosable()) r = closeButton.getBounds();
  238. int titleW;
  239. String title = frame.getTitle();
  240. if( BasicGraphicsUtils.isLeftToRight(frame) ) {
  241. if (r.x == 0) r.x = frame.getWidth()-frame.getInsets().right;
  242. titleX = menuBar.getX() + menuBar.getWidth() + 2;
  243. titleW = r.x - titleX - 3;
  244. title = getTitle(frame.getTitle(), fm, titleW);
  245. } else {
  246. titleX = menuBar.getX() - 2
  247. - SwingUtilities.computeStringWidth(fm,title);
  248. }
  249. g.drawString(title, titleX, baseline);
  250. g.setFont(f);
  251. }
  252. }
  253. /**
  254. * Invoked from paintComponent.
  255. * Paints the background of the titlepane. All text and icons will
  256. * then be rendered on top of this background.
  257. * @param g the graphics to use to render the background
  258. * @since 1.4
  259. */
  260. protected void paintTitleBackground(Graphics g) {
  261. boolean isSelected = frame.isSelected();
  262. if(isSelected)
  263. g.setColor(selectedTitleColor);
  264. else
  265. g.setColor(notSelectedTitleColor);
  266. g.fillRect(0, 0, getWidth(), getHeight());
  267. }
  268. protected String getTitle(String text, FontMetrics fm, int availTextWidth) {
  269. if ( (text == null) || (text.equals("")) ) return "";
  270. int textWidth = SwingUtilities.computeStringWidth(fm, text);
  271. String clipString = "...";
  272. if (textWidth > availTextWidth) {
  273. int totalWidth = SwingUtilities.computeStringWidth(fm, clipString);
  274. int nChars;
  275. for(nChars = 0; nChars < text.length(); nChars++) {
  276. totalWidth += fm.charWidth(text.charAt(nChars));
  277. if (totalWidth > availTextWidth) {
  278. break;
  279. }
  280. }
  281. text = text.substring(0, nChars) + clipString;
  282. }
  283. return text;
  284. }
  285. /**
  286. * Post a WINDOW_CLOSING-like event to the frame, so that it can
  287. * be treated like a regular Frame.
  288. */
  289. protected void postClosingEvent(JInternalFrame frame) {
  290. InternalFrameEvent e = new InternalFrameEvent(
  291. frame, InternalFrameEvent.INTERNAL_FRAME_CLOSING);
  292. // Try posting event, unless there's a SecurityManager.
  293. if (JInternalFrame.class.getClassLoader() == null) {
  294. try {
  295. Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
  296. return;
  297. } catch (SecurityException se) {
  298. // Use dispatchEvent instead.
  299. }
  300. }
  301. frame.dispatchEvent(e);
  302. }
  303. protected void enableActions() {
  304. restoreAction.setEnabled(frame.isMaximum() || frame.isIcon());
  305. maximizeAction.setEnabled(frame.isMaximizable() && !frame.isMaximum() );
  306. iconifyAction.setEnabled(frame.isIconifiable() && !frame.isIcon());
  307. closeAction.setEnabled(frame.isClosable());
  308. sizeAction.setEnabled(false);
  309. moveAction.setEnabled(false);
  310. }
  311. protected PropertyChangeListener createPropertyChangeListener() {
  312. return new PropertyChangeHandler();
  313. }
  314. protected LayoutManager createLayout() {
  315. return new TitlePaneLayout();
  316. }
  317. /**
  318. * This inner class is marked "public" due to a compiler bug.
  319. * This class should be treated as a "protected" inner class.
  320. * Instantiate it only within subclasses of <Foo>.
  321. */
  322. public class PropertyChangeHandler implements PropertyChangeListener {
  323. public void propertyChange(PropertyChangeEvent evt) {
  324. String prop = (String)evt.getPropertyName();
  325. if(JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
  326. repaint();
  327. return;
  328. }
  329. if(JInternalFrame.IS_ICON_PROPERTY.equals(prop) ||
  330. JInternalFrame.IS_MAXIMUM_PROPERTY.equals(prop)) {
  331. setButtonIcons();
  332. enableActions();
  333. return;
  334. }
  335. if( prop.equals("closable") ) {
  336. if( (Boolean)evt.getNewValue() == Boolean.TRUE )
  337. add(closeButton);
  338. else
  339. remove(closeButton);
  340. } else if( prop.equals("maximizable") ) {
  341. if( (Boolean)evt.getNewValue() == Boolean.TRUE )
  342. add(maxButton);
  343. else
  344. remove(maxButton);
  345. } else if( prop.equals("iconable") ) {
  346. if( (Boolean)evt.getNewValue() == Boolean.TRUE )
  347. add(iconButton);
  348. else
  349. remove(iconButton);
  350. }
  351. enableActions();
  352. revalidate();
  353. repaint();
  354. }
  355. } // end PropertyHandler class
  356. /**
  357. * This inner class is marked "public" due to a compiler bug.
  358. * This class should be treated as a "protected" inner class.
  359. * Instantiate it only within subclasses of <Foo>.
  360. */
  361. public class TitlePaneLayout implements LayoutManager {
  362. public void addLayoutComponent(String name, Component c) {}
  363. public void removeLayoutComponent(Component c) {}
  364. public Dimension preferredLayoutSize(Container c) {
  365. return minimumLayoutSize(c);
  366. }
  367. public Dimension minimumLayoutSize(Container c) {
  368. // Calculate width.
  369. int width = 22;
  370. if (frame.isClosable()) {
  371. width += 19;
  372. }
  373. if (frame.isMaximizable()) {
  374. width += 19;
  375. }
  376. if (frame.isIconifiable()) {
  377. width += 19;
  378. }
  379. FontMetrics fm = getFontMetrics(getFont());
  380. String frameTitle = frame.getTitle();
  381. int title_w = frameTitle != null ? fm.stringWidth(frameTitle) : 0;
  382. int title_length = frameTitle != null ? frameTitle.length() : 0;
  383. // Leave room for three characters in the title.
  384. if (title_length > 3) {
  385. int subtitle_w =
  386. fm.stringWidth(frameTitle.substring(0, 3) + "...");
  387. width += (title_w < subtitle_w) ? title_w : subtitle_w;
  388. } else {
  389. width += title_w;
  390. }
  391. // Calculate height.
  392. Icon icon = frame.getFrameIcon();
  393. int fontHeight = fm.getHeight();
  394. fontHeight += 2;
  395. int iconHeight = 0;
  396. if (icon != null) {
  397. // SystemMenuBar forces the icon to be 16x16 or less.
  398. iconHeight = Math.min(icon.getIconHeight(), 16);
  399. }
  400. iconHeight += 2;
  401. int height = Math.max( fontHeight, iconHeight );
  402. Dimension dim = new Dimension(width, height);
  403. // Take into account the border insets if any.
  404. if (getBorder() != null) {
  405. Insets insets = getBorder().getBorderInsets(c);
  406. dim.height += insets.top + insets.bottom;
  407. dim.width += insets.left + insets.right;
  408. }
  409. return dim;
  410. }
  411. public void layoutContainer(Container c) {
  412. boolean leftToRight = BasicGraphicsUtils.isLeftToRight(frame);
  413. int w = getWidth();
  414. int h = getHeight();
  415. int x;
  416. int buttonHeight = closeButton.getIcon().getIconHeight();
  417. //int buttonWidth = closeButton.getIcon().getIconWidth();
  418. Icon icon = frame.getFrameIcon();
  419. int iconHeight = 0;
  420. if (icon != null) {
  421. iconHeight = icon.getIconHeight();
  422. }
  423. x = (leftToRight) ? 2 : w - 16 - 2;
  424. menuBar.setBounds(x, (h - iconHeight) / 2, 16, 16);
  425. x = (leftToRight) ? w - 16 - 2 : 2;
  426. if (frame.isClosable()) {
  427. closeButton.setBounds(x, (h - buttonHeight) / 2, 16, 14);
  428. x += (leftToRight) ? -(16 + 2) : 16 + 2;
  429. }
  430. if(frame.isMaximizable()) {
  431. maxButton.setBounds(x, (h - buttonHeight) / 2, 16, 14);
  432. x += (leftToRight) ? -(16 + 2) : 16 + 2;
  433. }
  434. if(frame.isIconifiable()) {
  435. iconButton.setBounds(x, (h - buttonHeight) / 2, 16, 14);
  436. }
  437. }
  438. } // end TitlePaneLayout
  439. /**
  440. * This inner class is marked "public" due to a compiler bug.
  441. * This class should be treated as a "protected" inner class.
  442. * Instantiate it only within subclasses of <Foo>.
  443. */
  444. public class CloseAction extends AbstractAction {
  445. public CloseAction() {
  446. super(CLOSE_CMD);
  447. }
  448. public void actionPerformed(ActionEvent e) {
  449. if(frame.isClosable()) {
  450. frame.doDefaultCloseAction();
  451. }
  452. }
  453. } // end CloseAction
  454. /**
  455. * This inner class is marked "public" due to a compiler bug.
  456. * This class should be treated as a "protected" inner class.
  457. * Instantiate it only within subclasses of <Foo>.
  458. */
  459. public class MaximizeAction extends AbstractAction {
  460. public MaximizeAction() {
  461. super(MAXIMIZE_CMD);
  462. }
  463. public void actionPerformed(ActionEvent e) {
  464. if(frame.isMaximizable()) {
  465. if(!frame.isMaximum()) {
  466. try { frame.setMaximum(true); } catch (PropertyVetoException e5) { }
  467. } else {
  468. try {
  469. frame.setMaximum(false);
  470. } catch (PropertyVetoException e6) { }
  471. }
  472. }
  473. }
  474. } // MaximizeAction
  475. /**
  476. * This inner class is marked "public" due to a compiler bug.
  477. * This class should be treated as a "protected" inner class.
  478. * Instantiate it only within subclasses of <Foo>.
  479. */
  480. public class IconifyAction extends AbstractAction {
  481. public IconifyAction() {
  482. super(ICONIFY_CMD);
  483. }
  484. public void actionPerformed(ActionEvent e) {
  485. if(frame.isIconifiable()) {
  486. if(!frame.isIcon()) {
  487. try { frame.setIcon(true); } catch (PropertyVetoException e1) { }
  488. } else{
  489. try { frame.setIcon(false); } catch (PropertyVetoException e1) { }
  490. }
  491. }
  492. }
  493. } // end IconifyAction
  494. /**
  495. * This inner class is marked "public" due to a compiler bug.
  496. * This class should be treated as a "protected" inner class.
  497. * Instantiate it only within subclasses of <Foo>.
  498. */
  499. public class RestoreAction extends AbstractAction {
  500. public RestoreAction() {
  501. super(RESTORE_CMD);
  502. }
  503. public void actionPerformed(ActionEvent e) {
  504. if(frame.isMaximizable() && frame.isMaximum()) {
  505. try { frame.setMaximum(false); } catch (PropertyVetoException e4) { }
  506. }
  507. else if ( frame.isIconifiable() && frame.isIcon() ) {
  508. try { frame.setIcon(false); } catch (PropertyVetoException e4) { }
  509. }
  510. }
  511. } // end RestoreAction
  512. /**
  513. * This inner class is marked "public" due to a compiler bug.
  514. * This class should be treated as a "protected" inner class.
  515. * Instantiate it only within subclasses of <Foo>.
  516. */
  517. public class MoveAction extends AbstractAction {
  518. public MoveAction() {
  519. super(MOVE_CMD);
  520. }
  521. public void actionPerformed(ActionEvent e) {
  522. // This action is currently undefined
  523. }
  524. } // end MoveAction
  525. /*
  526. * Handles showing and hiding the system menu.
  527. */
  528. private class ShowSystemMenuAction extends AbstractAction {
  529. private boolean show; // whether to show the menu
  530. public ShowSystemMenuAction(boolean show) {
  531. this.show = show;
  532. }
  533. public void actionPerformed(ActionEvent e) {
  534. if (show) {
  535. windowMenu.doClick();
  536. } else {
  537. windowMenu.setVisible(false);
  538. }
  539. }
  540. }
  541. /**
  542. * This inner class is marked "public" due to a compiler bug.
  543. * This class should be treated as a "protected" inner class.
  544. * Instantiate it only within subclasses of <Foo>.
  545. */
  546. public class SizeAction extends AbstractAction {
  547. public SizeAction() {
  548. super(SIZE_CMD);
  549. }
  550. public void actionPerformed(ActionEvent e) {
  551. // This action is currently undefined
  552. }
  553. } // end SizeAction
  554. /**
  555. * This inner class is marked "public" due to a compiler bug.
  556. * This class should be treated as a "protected" inner class.
  557. * Instantiate it only within subclasses of <Foo>.
  558. */
  559. public class SystemMenuBar extends JMenuBar {
  560. public boolean isFocusTraversable() { return false; }
  561. public void requestFocus() {}
  562. public void paint(Graphics g) {
  563. Icon icon = frame.getFrameIcon();
  564. if (icon == null) {
  565. icon = UIManager.getIcon("InternalFrame.icon");
  566. }
  567. if (icon != null) {
  568. // Resize to 16x16 if necessary.
  569. if (icon instanceof ImageIcon && (icon.getIconWidth() > 16 || icon.getIconHeight() > 16)) {
  570. Image img = ((ImageIcon)icon).getImage();
  571. ((ImageIcon)icon).setImage(img.getScaledInstance(16, 16, Image.SCALE_SMOOTH));
  572. }
  573. icon.paintIcon(this, g, 0, 0);
  574. }
  575. }
  576. public boolean isOpaque() {
  577. return true;
  578. }
  579. } // end SystemMenuBar
  580. private class NoFocusButton extends JButton {
  581. public NoFocusButton() {
  582. setFocusPainted(false);
  583. setMargin(new Insets(0,0,0,0));
  584. setOpaque(true);
  585. }
  586. public boolean isFocusTraversable() { return false; }
  587. public void requestFocus() {};
  588. }; // end NoFocusButton
  589. } // End Title Pane Class