1. /*
  2. * @(#)MotifInternalFrameTitlePane.java 1.32 04/04/15
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.motif;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.border.*;
  12. import javax.swing.event.InternalFrameEvent;
  13. import javax.swing.plaf.basic.*;
  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. * Class that manages a Motif title bar
  21. * @version 1.32 04/15/04
  22. *
  23. * @since 1.3
  24. */
  25. public class MotifInternalFrameTitlePane
  26. extends BasicInternalFrameTitlePane implements LayoutManager, ActionListener, PropertyChangeListener
  27. {
  28. SystemButton systemButton;
  29. MinimizeButton minimizeButton;
  30. MaximizeButton maximizeButton;
  31. JPopupMenu systemMenu;
  32. Title title;
  33. Color color;
  34. Color highlight;
  35. Color shadow;
  36. // The width and height of a title pane button
  37. public final static int BUTTON_SIZE = 19; // 17 + 1 pixel border
  38. public MotifInternalFrameTitlePane(JInternalFrame frame) {
  39. super(frame);
  40. }
  41. protected void installDefaults() {
  42. setFont(UIManager.getFont("InternalFrame.titleFont"));
  43. setPreferredSize(new Dimension(100, BUTTON_SIZE));
  44. }
  45. protected void uninstallListeners() {
  46. // Get around protected method in superclass
  47. super.uninstallListeners();
  48. }
  49. protected PropertyChangeListener createPropertyChangeListener() {
  50. return this;
  51. }
  52. protected LayoutManager createLayout() {
  53. return this;
  54. }
  55. JPopupMenu getSystemMenu() {
  56. return systemMenu;
  57. }
  58. protected void assembleSystemMenu() {
  59. systemMenu = new JPopupMenu();
  60. JMenuItem mi = (JMenuItem)systemMenu.add(new JMenuItem(restoreAction));
  61. mi.setMnemonic('R');
  62. mi = (JMenuItem) systemMenu.add(new JMenuItem(moveAction));
  63. mi.setMnemonic('M');
  64. mi = (JMenuItem) systemMenu.add(new JMenuItem(sizeAction));
  65. mi.setMnemonic('S');
  66. mi = (JMenuItem) systemMenu.add(new JMenuItem(iconifyAction));
  67. mi.setMnemonic('n');
  68. mi = (JMenuItem) systemMenu.add(new JMenuItem(maximizeAction));
  69. mi.setMnemonic('x');
  70. systemMenu.add(new JSeparator());
  71. mi = (JMenuItem) systemMenu.add(new JMenuItem(closeAction));
  72. mi.setMnemonic('C');
  73. systemButton = new SystemButton();
  74. systemButton.addActionListener(new ActionListener() {
  75. public void actionPerformed(ActionEvent e) {
  76. systemMenu.show(systemButton, 0, BUTTON_SIZE);
  77. }
  78. });
  79. systemButton.addMouseListener(new MouseAdapter() {
  80. public void mousePressed(MouseEvent evt) {
  81. if ((evt.getClickCount() == 2)) {
  82. closeAction.actionPerformed(new
  83. ActionEvent(evt.getSource(),
  84. ActionEvent.ACTION_PERFORMED,
  85. null, evt.getWhen(), 0));
  86. systemMenu.setVisible(false);
  87. }
  88. }
  89. });
  90. }
  91. protected void createButtons() {
  92. minimizeButton = new MinimizeButton();
  93. minimizeButton.addActionListener(iconifyAction);
  94. maximizeButton = new MaximizeButton();
  95. maximizeButton.addActionListener(maximizeAction);
  96. }
  97. protected void addSubComponents() {
  98. title = new Title(frame.getTitle());
  99. title.setFont(getFont());
  100. add(systemButton);
  101. add(title);
  102. add(minimizeButton);
  103. add(maximizeButton);
  104. }
  105. public void paintComponent(Graphics g) {
  106. }
  107. void setColors(Color c, Color h, Color s) {
  108. color = c;
  109. highlight = h;
  110. shadow = s;
  111. }
  112. public void actionPerformed(ActionEvent e) {
  113. }
  114. public void propertyChange(PropertyChangeEvent evt) {
  115. String prop = (String)evt.getPropertyName();
  116. JInternalFrame f = (JInternalFrame)evt.getSource();
  117. boolean value = false;
  118. if (JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
  119. repaint();
  120. } else if (prop.equals("maximizable")) {
  121. if ((Boolean)evt.getNewValue() == Boolean.TRUE)
  122. add(maximizeButton);
  123. else
  124. remove(maximizeButton);
  125. revalidate();
  126. repaint();
  127. } else if (prop.equals("iconable")) {
  128. if ((Boolean)evt.getNewValue() == Boolean.TRUE)
  129. add(minimizeButton);
  130. else
  131. remove(minimizeButton);
  132. revalidate();
  133. repaint();
  134. } else if (prop.equals(JInternalFrame.TITLE_PROPERTY)) {
  135. repaint();
  136. }
  137. enableActions();
  138. }
  139. public void addLayoutComponent(String name, Component c) {}
  140. public void removeLayoutComponent(Component c) {}
  141. public Dimension preferredLayoutSize(Container c) {
  142. return minimumLayoutSize(c);
  143. }
  144. public Dimension minimumLayoutSize(Container c) {
  145. return new Dimension(100, BUTTON_SIZE);
  146. }
  147. public void layoutContainer(Container c) {
  148. int w = getWidth();
  149. systemButton.setBounds(0, 0, BUTTON_SIZE, BUTTON_SIZE);
  150. int x = w - BUTTON_SIZE;
  151. if(frame.isMaximizable()) {
  152. maximizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
  153. x -= BUTTON_SIZE;
  154. } else if(maximizeButton.getParent() != null) {
  155. maximizeButton.getParent().remove(maximizeButton);
  156. }
  157. if(frame.isIconifiable()) {
  158. minimizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
  159. x -= BUTTON_SIZE;
  160. } else if(minimizeButton.getParent() != null) {
  161. minimizeButton.getParent().remove(minimizeButton);
  162. }
  163. title.setBounds(BUTTON_SIZE, 0, x, BUTTON_SIZE);
  164. }
  165. protected void showSystemMenu(){
  166. systemMenu.show(systemButton, 0, BUTTON_SIZE);
  167. }
  168. protected void hideSystemMenu(){
  169. systemMenu.setVisible(false);
  170. }
  171. static Dimension buttonDimension = new Dimension(BUTTON_SIZE, BUTTON_SIZE);
  172. private abstract class FrameButton extends JButton {
  173. FrameButton() {
  174. super();
  175. setFocusPainted(false);
  176. setBorderPainted(false);
  177. }
  178. public boolean isFocusTraversable() {
  179. return false;
  180. }
  181. public void requestFocus() {
  182. // ignore request.
  183. }
  184. public Dimension getMinimumSize() {
  185. return buttonDimension;
  186. }
  187. public Dimension getPreferredSize() {
  188. return buttonDimension;
  189. }
  190. public void paintComponent(Graphics g) {
  191. Dimension d = getSize();
  192. int maxX = d.width - 1;
  193. int maxY = d.height - 1;
  194. // draw background
  195. g.setColor(color);
  196. g.fillRect(1, 1, d.width, d.height);
  197. // draw border
  198. boolean pressed = getModel().isPressed();
  199. g.setColor(pressed ? shadow : highlight);
  200. g.drawLine(0, 0, maxX, 0);
  201. g.drawLine(0, 0, 0, maxY);
  202. g.setColor(pressed ? highlight : shadow);
  203. g.drawLine(1, maxY, maxX, maxY);
  204. g.drawLine(maxX, 1, maxX, maxY);
  205. }
  206. }
  207. private class MinimizeButton extends FrameButton {
  208. public void paintComponent(Graphics g) {
  209. super.paintComponent(g);
  210. g.setColor(highlight);
  211. g.drawLine(7, 8, 7, 11);
  212. g.drawLine(7, 8, 10, 8);
  213. g.setColor(shadow);
  214. g.drawLine(8, 11, 10, 11);
  215. g.drawLine(11, 9, 11, 11);
  216. }
  217. }
  218. private class MaximizeButton extends FrameButton {
  219. public void paintComponent(Graphics g) {
  220. super.paintComponent(g);
  221. int max = BUTTON_SIZE - 5;
  222. boolean isMaxed = frame.isMaximum();
  223. g.setColor(isMaxed ? shadow : highlight);
  224. g.drawLine(4, 4, 4, max);
  225. g.drawLine(4, 4, max, 4);
  226. g.setColor(isMaxed ? highlight : shadow);
  227. g.drawLine(5, max, max, max);
  228. g.drawLine(max, 5, max, max);
  229. }
  230. }
  231. private class SystemButton extends FrameButton {
  232. public boolean isFocusTraversable() { return false; }
  233. public void requestFocus() {}
  234. public void paintComponent(Graphics g) {
  235. super.paintComponent(g);
  236. g.setColor(highlight);
  237. g.drawLine(4, 8, 4, 11);
  238. g.drawLine(4, 8, BUTTON_SIZE - 5, 8);
  239. g.setColor(shadow);
  240. g.drawLine(5, 11, BUTTON_SIZE - 5, 11);
  241. g.drawLine(BUTTON_SIZE - 5, 9, BUTTON_SIZE - 5, 11);
  242. }
  243. }
  244. private class Title extends FrameButton {
  245. Title(String title) {
  246. super();
  247. setText(title);
  248. setHorizontalAlignment(SwingConstants.CENTER);
  249. setBorder(BorderFactory.createBevelBorder(
  250. BevelBorder.RAISED,
  251. UIManager.getColor("activeCaptionBorder"),
  252. UIManager.getColor("inactiveCaptionBorder")));
  253. // Forward mouse events to titlebar for moves.
  254. addMouseMotionListener(new MouseMotionListener() {
  255. public void mouseDragged(MouseEvent e) {
  256. forwardEventToParent(e);
  257. }
  258. public void mouseMoved(MouseEvent e) {
  259. forwardEventToParent(e);
  260. }
  261. });
  262. addMouseListener(new MouseListener() {
  263. public void mouseClicked(MouseEvent e) {
  264. forwardEventToParent(e);
  265. }
  266. public void mousePressed(MouseEvent e) {
  267. forwardEventToParent(e);
  268. }
  269. public void mouseReleased(MouseEvent e) {
  270. forwardEventToParent(e);
  271. }
  272. public void mouseEntered(MouseEvent e) {
  273. forwardEventToParent(e);
  274. }
  275. public void mouseExited(MouseEvent e) {
  276. forwardEventToParent(e);
  277. }
  278. });
  279. }
  280. void forwardEventToParent(MouseEvent e) {
  281. getParent().dispatchEvent(new MouseEvent(
  282. getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  283. e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  284. }
  285. public void paintComponent(Graphics g) {
  286. super.paintComponent(g);
  287. if (frame.isSelected()) {
  288. g.setColor(UIManager.getColor("activeCaptionText"));
  289. } else {
  290. g.setColor(UIManager.getColor("inactiveCaptionText"));
  291. }
  292. Dimension d = getSize();
  293. String frameTitle = frame.getTitle();
  294. if (frameTitle != null) {
  295. MotifGraphicsUtils.drawStringInRect(frame, g, frameTitle,
  296. 0, 0, d.width, d.height,
  297. SwingConstants.CENTER);
  298. }
  299. }
  300. }
  301. }