1. /*
  2. * @(#)SynthDesktopIconUI.java 1.8 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 com.sun.java.swing.plaf.gtk;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.event.*;
  12. import javax.swing.border.*;
  13. import javax.swing.plaf.*;
  14. import java.beans.*;
  15. import java.util.EventListener;
  16. import java.io.Serializable;
  17. /**
  18. * Synth L&F for a minimized window on a desktop.
  19. *
  20. * @version 1.8, 01/23/03 (originally from version 1.30 of BasicDesktopIconUI)
  21. * @author David Kloba
  22. * @author Joshua Outwater
  23. * @author Rich Schiavi
  24. * @author Steve Wilson
  25. */
  26. class SynthDesktopIconUI extends DesktopIconUI implements SynthUI {
  27. private SynthStyle style;
  28. protected JInternalFrame.JDesktopIcon desktopIcon;
  29. protected JInternalFrame frame;
  30. // This component is only used for windows look and feel, but it's package
  31. // private. This should be protected. (joutwate 2/22/2001)
  32. protected JComponent iconPane;
  33. public static ComponentUI createUI(JComponent c) {
  34. return new SynthDesktopIconUI();
  35. }
  36. public SynthDesktopIconUI() {
  37. }
  38. public void installUI(JComponent c) {
  39. desktopIcon = (JInternalFrame.JDesktopIcon)c;
  40. frame = desktopIcon.getInternalFrame();
  41. installDefaults();
  42. installComponents();
  43. installListeners();
  44. JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
  45. }
  46. public void uninstallUI(JComponent c) {
  47. uninstallDefaults();
  48. uninstallComponents();
  49. uninstallListeners();
  50. frame = null;
  51. desktopIcon = null;
  52. }
  53. protected void installComponents() {
  54. iconPane = new SynthInternalFrameTitlePane(frame);
  55. desktopIcon.setLayout(new BorderLayout());
  56. desktopIcon.add(iconPane, BorderLayout.CENTER);
  57. }
  58. protected void uninstallComponents() {
  59. desktopIcon.setLayout(null);
  60. desktopIcon.remove(iconPane);
  61. iconPane = null;
  62. }
  63. protected void installListeners() {
  64. EventHandler handler = createEventHandler();
  65. desktopIcon.addMouseMotionListener(handler);
  66. desktopIcon.addMouseListener(handler);
  67. desktopIcon.addPropertyChangeListener(handler);
  68. }
  69. protected void uninstallListeners() {
  70. EventHandler handler = getEventHandler(desktopIcon);
  71. desktopIcon.removeMouseMotionListener(handler);
  72. desktopIcon.removeMouseListener(handler);
  73. desktopIcon.removePropertyChangeListener(handler);
  74. }
  75. protected void installDefaults() {
  76. fetchStyle(desktopIcon);
  77. }
  78. private void fetchStyle(JComponent c) {
  79. SynthContext context = getContext(c, ENABLED);
  80. style = SynthLookAndFeel.updateStyle(context, this);
  81. context.dispose();
  82. }
  83. protected void uninstallDefaults() {
  84. SynthContext context = getContext(desktopIcon, ENABLED);
  85. style.uninstallDefaults(context);
  86. context.dispose();
  87. style = null;
  88. }
  89. EventHandler createEventHandler() {
  90. return new EventHandler();
  91. }
  92. public Dimension getPreferredSize(JComponent c) {
  93. return desktopIcon.getLayout().preferredLayoutSize(desktopIcon);
  94. }
  95. public Dimension getMinimumSize(JComponent c) {
  96. Dimension dim = new Dimension(iconPane.getMinimumSize());
  97. Border border = frame.getBorder();
  98. if (border != null) {
  99. dim.height += border.getBorderInsets(frame).bottom +
  100. border.getBorderInsets(frame).top;
  101. }
  102. return dim;
  103. }
  104. public Dimension getMaximumSize(JComponent c){
  105. return iconPane.getMaximumSize();
  106. }
  107. public Insets getInsets(JComponent c) {
  108. JInternalFrame iframe = desktopIcon.getInternalFrame();
  109. Border border = iframe.getBorder();
  110. if(border != null)
  111. return border.getBorderInsets(iframe);
  112. return new Insets(0,0,0,0);
  113. }
  114. public void deiconize() {
  115. try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  116. }
  117. public SynthContext getContext(JComponent c) {
  118. return getContext(c, getComponentState(c));
  119. }
  120. private SynthContext getContext(JComponent c, int state) {
  121. Region region = getRegion(c);
  122. return SynthContext.getContext(SynthContext.class, c, region,
  123. style, state);
  124. }
  125. private int getComponentState(JComponent c) {
  126. return SynthLookAndFeel.getComponentState(c);
  127. }
  128. Region getRegion(JComponent c) {
  129. return SynthLookAndFeel.getRegion(c);
  130. }
  131. public void update(Graphics g, JComponent c) {
  132. SynthContext context = getContext(c);
  133. SynthLookAndFeel.update(context, g);
  134. paint(context, g);
  135. context.dispose();
  136. }
  137. public void paint(Graphics g, JComponent c) {
  138. SynthContext context = getContext(c);
  139. paint(context, g);
  140. context.dispose();
  141. }
  142. protected void paint(SynthContext context, Graphics g) {
  143. // PENDING: Josh, should this paint an icon?
  144. }
  145. private EventHandler getEventHandler(JComponent c) {
  146. return (EventHandler)SynthLookAndFeel.getSynthEventListener(c);
  147. }
  148. /**
  149. * Listens for mouse movements and acts on them.
  150. *
  151. * This inner class is marked "public" due to a compiler bug.
  152. * This class should be treated as a "protected" inner class.
  153. * Instantiate it only within subclasses of <Foo>.
  154. */
  155. private class EventHandler extends MouseInputAdapter implements PropertyChangeListener, SynthEventListener {
  156. // _x & _y are the mousePressed location in absolute coordinate system
  157. int _x, _y;
  158. // __x & __y are the mousePressed location in source view's coordinate system
  159. int __x, __y;
  160. Rectangle startingBounds;
  161. public void mouseReleased(MouseEvent e) {
  162. _x = 0;
  163. _y = 0;
  164. __x = 0;
  165. __y = 0;
  166. startingBounds = null;
  167. JDesktopPane d;
  168. if((d = desktopIcon.getDesktopPane()) != null) {
  169. DesktopManager dm = d.getDesktopManager();
  170. dm.endDraggingFrame(desktopIcon);
  171. }
  172. }
  173. public void mousePressed(MouseEvent e) {
  174. Point p = SwingUtilities.convertPoint((Component)e.getSource(),
  175. e.getX(), e.getY(), null);
  176. __x = e.getX();
  177. __y = e.getY();
  178. _x = p.x;
  179. _y = p.y;
  180. startingBounds = desktopIcon.getBounds();
  181. JDesktopPane d;
  182. if((d = desktopIcon.getDesktopPane()) != null) {
  183. DesktopManager dm = d.getDesktopManager();
  184. dm.beginDraggingFrame(desktopIcon);
  185. }
  186. try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  187. if(desktopIcon.getParent() instanceof JLayeredPane) {
  188. ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
  189. }
  190. if(e.getClickCount() > 1) {
  191. if(frame.isIconifiable() && frame.isIcon()) {
  192. deiconize();
  193. }
  194. }
  195. }
  196. public void mouseMoved(MouseEvent e) {}
  197. public void mouseDragged(MouseEvent e) {
  198. Point p;
  199. int newX, newY, newW, newH;
  200. int deltaX;
  201. int deltaY;
  202. Dimension min;
  203. Dimension max;
  204. p = SwingUtilities.convertPoint((Component)e.getSource(),
  205. e.getX(), e.getY(), null);
  206. Insets i = desktopIcon.getInsets();
  207. int pWidth, pHeight;
  208. pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
  209. pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
  210. if (startingBounds == null) {
  211. // (STEVE) Yucky work around for bug ID 4106552
  212. return;
  213. }
  214. newX = startingBounds.x - (_x - p.x);
  215. newY = startingBounds.y - (_y - p.y);
  216. // Make sure we stay in-bounds
  217. if(newX + i.left <= -__x)
  218. newX = -__x - i.left;
  219. if(newY + i.top <= -__y)
  220. newY = -__y - i.top;
  221. if(newX + __x + i.right > pWidth)
  222. newX = pWidth - __x - i.right;
  223. if(newY + __y + i.bottom > pHeight)
  224. newY = pHeight - __y - i.bottom;
  225. JDesktopPane d;
  226. if((d = desktopIcon.getDesktopPane()) != null) {
  227. DesktopManager dm = d.getDesktopManager();
  228. dm.dragFrame(desktopIcon, newX, newY);
  229. } else {
  230. moveAndRepaint(desktopIcon, newX, newY,
  231. desktopIcon.getWidth(), desktopIcon.getHeight());
  232. }
  233. return;
  234. }
  235. public void moveAndRepaint(JComponent f, int newX, int newY,
  236. int newWidth, int newHeight) {
  237. Rectangle r = f.getBounds();
  238. f.setBounds(newX, newY, newWidth, newHeight);
  239. SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  240. f.getParent().repaint(r.x, r.y, r.width, r.height);
  241. }
  242. public void propertyChange(PropertyChangeEvent evt) {
  243. if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
  244. fetchStyle((JInternalFrame.JDesktopIcon)evt.getSource());
  245. }
  246. }
  247. }; /// End MotionListener
  248. }