1. /*
  2. * @(#)MotifDesktopIconUI.java 1.18 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package com.sun.java.swing.plaf.motif;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.*;
  14. import javax.swing.border.*;
  15. import javax.swing.plaf.*;
  16. import javax.swing.plaf.basic.*;
  17. import java.beans.*;
  18. import java.util.EventListener;
  19. import java.io.Serializable;
  20. /**
  21. * Motif rendition of the component.
  22. *
  23. * @version 1.18 02/02/00
  24. * @author Thomas Ball
  25. * @author Rich Schiavi
  26. */
  27. public class MotifDesktopIconUI extends BasicDesktopIconUI
  28. {
  29. protected DesktopIconActionListener desktopIconActionListener;
  30. protected DesktopIconMouseListener desktopIconMouseListener;
  31. protected Icon defaultIcon;
  32. protected IconButton iconButton;
  33. protected IconLabel iconLabel;
  34. JPopupMenu systemMenu;
  35. EventListener mml;
  36. final static int LABEL_HEIGHT = 18;
  37. final static int LABEL_DIVIDER = 4; // padding between icon and label
  38. final static Font defaultTitleFont = new Font("SansSerif", Font.PLAIN, 12);
  39. public static ComponentUI createUI(JComponent c) {
  40. return new MotifDesktopIconUI();
  41. }
  42. public MotifDesktopIconUI() {
  43. }
  44. protected void installDefaults(){
  45. super.installDefaults();
  46. setDefaultIcon(UIManager.getIcon("DesktopIcon.icon"));
  47. iconButton = createIconButton(defaultIcon);
  48. // An unhanded way of creating a system popup menu.
  49. MotifInternalFrameTitlePane titlePane =
  50. new MotifInternalFrameTitlePane(frame);
  51. systemMenu = titlePane.getSystemMenu();
  52. MotifBorders.FrameBorder border = new MotifBorders.FrameBorder(desktopIcon);
  53. desktopIcon.setLayout(new BorderLayout());
  54. iconButton.setBorder(border);
  55. desktopIcon.add(iconButton, BorderLayout.CENTER);
  56. iconLabel = createIconLabel(frame);
  57. iconLabel.setBorder(border);
  58. desktopIcon.add(iconLabel, BorderLayout.SOUTH);
  59. desktopIcon.setSize(desktopIcon.getPreferredSize());
  60. desktopIcon.validate();
  61. JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
  62. }
  63. protected void installComponents(){
  64. }
  65. protected void uninstallComponents(){
  66. }
  67. protected void installListeners(){
  68. super.installListeners();
  69. desktopIconActionListener = createDesktopIconActionListener();
  70. desktopIconMouseListener = createDesktopIconMouseListener();
  71. iconButton.addActionListener(desktopIconActionListener);
  72. iconButton.addMouseListener(desktopIconMouseListener);
  73. }
  74. JInternalFrame.JDesktopIcon getDesktopIcon(){
  75. return desktopIcon;
  76. }
  77. void setDesktopIcon(JInternalFrame.JDesktopIcon d){
  78. desktopIcon = d;
  79. }
  80. JInternalFrame getFrame(){
  81. return frame;
  82. }
  83. void setFrame(JInternalFrame f){
  84. frame = f ;
  85. }
  86. protected void showSystemMenu(){
  87. systemMenu.show(iconButton, 0, getDesktopIcon().getHeight());
  88. }
  89. protected void hideSystemMenu(){
  90. systemMenu.setVisible(false);
  91. }
  92. protected IconLabel createIconLabel(JInternalFrame frame){
  93. return new IconLabel(frame);
  94. }
  95. protected IconButton createIconButton(Icon i){
  96. return new IconButton(i);
  97. }
  98. protected DesktopIconActionListener createDesktopIconActionListener(){
  99. return new DesktopIconActionListener();
  100. }
  101. protected DesktopIconMouseListener createDesktopIconMouseListener(){
  102. return new DesktopIconMouseListener();
  103. }
  104. protected void uninstallDefaults(){
  105. super.uninstallDefaults();
  106. desktopIcon.setLayout(null);
  107. desktopIcon.remove(iconButton);
  108. desktopIcon.remove(iconLabel);
  109. }
  110. protected void uninstallListeners(){
  111. super.uninstallListeners();
  112. iconButton.removeActionListener(desktopIconActionListener);
  113. iconButton.removeMouseListener(desktopIconMouseListener);
  114. }
  115. public Dimension getMinimumSize(JComponent c) {
  116. JInternalFrame iframe = desktopIcon.getInternalFrame();
  117. int w = defaultIcon.getIconWidth();
  118. int h = defaultIcon.getIconHeight() + LABEL_HEIGHT + LABEL_DIVIDER;
  119. Border border = iframe.getBorder();
  120. if(border != null) {
  121. w += border.getBorderInsets(iframe).left +
  122. border.getBorderInsets(iframe).right;
  123. h += border.getBorderInsets(iframe).bottom +
  124. border.getBorderInsets(iframe).top;
  125. }
  126. return new Dimension(w, h);
  127. }
  128. public Dimension getPreferredSize(JComponent c) {
  129. return getMinimumSize(c);
  130. }
  131. public Dimension getMaximumSize(JComponent c){
  132. return getMinimumSize(c);
  133. }
  134. /**
  135. * Returns the default desktop icon.
  136. */
  137. public Icon getDefaultIcon() {
  138. return defaultIcon;
  139. }
  140. /**
  141. * Sets the icon used as the default desktop icon.
  142. */
  143. public void setDefaultIcon(Icon newIcon) {
  144. defaultIcon = newIcon;
  145. }
  146. protected class IconLabel extends JPanel {
  147. JInternalFrame frame;
  148. IconLabel(JInternalFrame f) {
  149. super();
  150. this.frame = f;
  151. setFont(defaultTitleFont);
  152. // Forward mouse events to titlebar for moves.
  153. addMouseMotionListener(new MouseMotionListener() {
  154. public void mouseDragged(MouseEvent e) {
  155. forwardEventToParent(e);
  156. }
  157. public void mouseMoved(MouseEvent e) {
  158. forwardEventToParent(e);
  159. }
  160. });
  161. addMouseListener(new MouseListener() {
  162. public void mouseClicked(MouseEvent e) {
  163. forwardEventToParent(e);
  164. }
  165. public void mousePressed(MouseEvent e) {
  166. forwardEventToParent(e);
  167. }
  168. public void mouseReleased(MouseEvent e) {
  169. forwardEventToParent(e);
  170. }
  171. public void mouseEntered(MouseEvent e) {
  172. forwardEventToParent(e);
  173. }
  174. public void mouseExited(MouseEvent e) {
  175. forwardEventToParent(e);
  176. }
  177. });
  178. }
  179. void forwardEventToParent(MouseEvent e) {
  180. getParent().dispatchEvent(new MouseEvent(
  181. getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  182. e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  183. }
  184. public boolean isFocusTraversable() {
  185. return false;
  186. }
  187. public Dimension getMinimumSize() {
  188. return new Dimension(defaultIcon.getIconWidth() + 1,
  189. LABEL_HEIGHT + LABEL_DIVIDER);
  190. }
  191. public Dimension getPreferredSize() {
  192. String title = frame.getTitle();
  193. FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(defaultTitleFont);
  194. int w = fm.stringWidth(title) + 4;
  195. return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER);
  196. }
  197. public void paint(Graphics g) {
  198. super.paint(g);
  199. // touch-up frame
  200. int maxX = getWidth() - 1;
  201. Color shadow =
  202. UIManager.getColor("inactiveCaptionBorder").darker().darker();
  203. g.setColor(shadow);
  204. g.setClip(0, 0, getWidth(), getHeight());
  205. g.drawLine(maxX - 1, 1, maxX - 1, 1);
  206. g.drawLine(maxX, 0, maxX, 0);
  207. // fill background
  208. g.setColor(UIManager.getColor("inactiveCaption"));
  209. g.fillRect(2, 1, maxX - 3, LABEL_HEIGHT + 1);
  210. // draw text -- clipping to truncate text like CDE/Motif
  211. g.setClip(2, 1, maxX - 4, LABEL_HEIGHT);
  212. int y = LABEL_HEIGHT - g.getFontMetrics().getDescent();
  213. g.setColor(UIManager.getColor("inactiveCaptionText"));
  214. g.drawString(frame.getTitle(), 4, y);
  215. }
  216. }
  217. protected class IconButton extends JButton {
  218. Icon icon;
  219. IconButton(Icon icon) {
  220. super(icon);
  221. this.icon = icon;
  222. // Forward mouse events to titlebar for moves.
  223. addMouseMotionListener(new MouseMotionListener() {
  224. public void mouseDragged(MouseEvent e) {
  225. forwardEventToParent(e);
  226. }
  227. public void mouseMoved(MouseEvent e) {
  228. forwardEventToParent(e);
  229. }
  230. });
  231. addMouseListener(new MouseListener() {
  232. public void mouseClicked(MouseEvent e) {
  233. forwardEventToParent(e);
  234. }
  235. public void mousePressed(MouseEvent e) {
  236. forwardEventToParent(e);
  237. }
  238. public void mouseReleased(MouseEvent e) {
  239. if (!systemMenu.isShowing()) {
  240. forwardEventToParent(e);
  241. }
  242. }
  243. public void mouseEntered(MouseEvent e) {
  244. forwardEventToParent(e);
  245. }
  246. public void mouseExited(MouseEvent e) {
  247. forwardEventToParent(e);
  248. }
  249. });
  250. }
  251. void forwardEventToParent(MouseEvent e) {
  252. getParent().dispatchEvent(new MouseEvent(
  253. getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  254. e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  255. }
  256. public boolean isFocusTraversable() {
  257. return false;
  258. }
  259. }
  260. protected class DesktopIconActionListener implements ActionListener {
  261. public void actionPerformed(ActionEvent e){
  262. systemMenu.show(iconButton, 0, getDesktopIcon().getHeight());
  263. }
  264. }
  265. protected class DesktopIconMouseListener extends MouseAdapter {
  266. // if we drag or move we should deengage the popup
  267. public void mousePressed(MouseEvent e){
  268. if (e.getClickCount() == 2){
  269. try {
  270. getFrame().setIcon(false);
  271. } catch (PropertyVetoException e2){ }
  272. systemMenu.setVisible(false);
  273. /* the mouse release will not get reported correctly,
  274. because the icon will no longer be in the hierarchy;
  275. maybe that should be fixed, but until it is, we need
  276. to do the required cleanup here. */
  277. getFrame().getDesktopPane().getDesktopManager().endDraggingFrame((JComponent)e.getSource());
  278. }
  279. }
  280. }
  281. }