1. /*
  2. * @(#)BasicDesktopIconUI.java 1.35 03/12/19
  3. *
  4. * Copyright 2004 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.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. * Basic L&F for a minimized window on a desktop.
  19. *
  20. * @version 1.35 12/19/03
  21. * @author David Kloba
  22. * @author Steve Wilson
  23. * @author Rich Schiavi
  24. */
  25. public class BasicDesktopIconUI extends DesktopIconUI {
  26. protected JInternalFrame.JDesktopIcon desktopIcon;
  27. protected JInternalFrame frame;
  28. /**
  29. * The title pane component used in the desktop icon.
  30. *
  31. * @since 1.5
  32. */
  33. protected JComponent iconPane;
  34. MouseInputListener mouseInputListener;
  35. public static ComponentUI createUI(JComponent c) {
  36. return new BasicDesktopIconUI();
  37. }
  38. public BasicDesktopIconUI() {
  39. }
  40. public void installUI(JComponent c) {
  41. desktopIcon = (JInternalFrame.JDesktopIcon)c;
  42. frame = desktopIcon.getInternalFrame();
  43. installDefaults();
  44. installComponents();
  45. // Update icon layout if frame is already iconified
  46. JInternalFrame f = desktopIcon.getInternalFrame();
  47. if (f.isIcon() && f.getParent() == null) {
  48. JDesktopPane desktop = desktopIcon.getDesktopPane();
  49. if (desktop != null) {
  50. DesktopManager desktopManager = desktop.getDesktopManager();
  51. if (desktopManager instanceof DefaultDesktopManager) {
  52. desktopManager.iconifyFrame(f);
  53. }
  54. }
  55. }
  56. installListeners();
  57. JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
  58. }
  59. public void uninstallUI(JComponent c) {
  60. uninstallDefaults();
  61. uninstallComponents();
  62. // Force future UI to relayout icon
  63. JInternalFrame f = desktopIcon.getInternalFrame();
  64. if (f.isIcon()) {
  65. JDesktopPane desktop = desktopIcon.getDesktopPane();
  66. if (desktop != null) {
  67. DesktopManager desktopManager = desktop.getDesktopManager();
  68. if (desktopManager instanceof DefaultDesktopManager) {
  69. // This will cause DefaultDesktopManager to layout the icon
  70. f.putClientProperty("wasIconOnce", null);
  71. // Move aside to allow fresh layout of all icons
  72. desktopIcon.setLocation(Integer.MIN_VALUE, 0);
  73. }
  74. }
  75. }
  76. uninstallListeners();
  77. frame = null;
  78. desktopIcon = null;
  79. }
  80. protected void installComponents() {
  81. iconPane = new BasicInternalFrameTitlePane(frame);
  82. desktopIcon.setLayout(new BorderLayout());
  83. desktopIcon.add(iconPane, BorderLayout.CENTER);
  84. }
  85. protected void uninstallComponents() {
  86. desktopIcon.remove(iconPane);
  87. desktopIcon.setLayout(null);
  88. iconPane = null;
  89. }
  90. protected void installListeners() {
  91. mouseInputListener = createMouseInputListener();
  92. desktopIcon.addMouseMotionListener(mouseInputListener);
  93. desktopIcon.addMouseListener(mouseInputListener);
  94. }
  95. protected void uninstallListeners() {
  96. desktopIcon.removeMouseMotionListener(mouseInputListener);
  97. desktopIcon.removeMouseListener(mouseInputListener);
  98. mouseInputListener = null;
  99. }
  100. protected void installDefaults() {
  101. LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
  102. LookAndFeel.installProperty(desktopIcon, "opaque", Boolean.TRUE);
  103. }
  104. protected void uninstallDefaults() {
  105. LookAndFeel.uninstallBorder(desktopIcon);
  106. }
  107. protected MouseInputListener createMouseInputListener() {
  108. return new MouseInputHandler();
  109. }
  110. public Dimension getPreferredSize(JComponent c) {
  111. return desktopIcon.getLayout().preferredLayoutSize(desktopIcon);
  112. }
  113. public Dimension getMinimumSize(JComponent c) {
  114. Dimension dim = new Dimension(iconPane.getMinimumSize());
  115. Border border = frame.getBorder();
  116. if (border != null) {
  117. dim.height += border.getBorderInsets(frame).bottom +
  118. border.getBorderInsets(frame).top;
  119. }
  120. return dim;
  121. }
  122. /**
  123. * Desktop icons can not be resized. Therefore, we should always
  124. * return the minimum size of the desktop icon.
  125. *
  126. * @see #getMinimumSize
  127. */
  128. public Dimension getMaximumSize(JComponent c){
  129. return iconPane.getMaximumSize();
  130. }
  131. public Insets getInsets(JComponent c) {
  132. JInternalFrame iframe = desktopIcon.getInternalFrame();
  133. Border border = iframe.getBorder();
  134. if(border != null)
  135. return border.getBorderInsets(iframe);
  136. return new Insets(0,0,0,0);
  137. }
  138. public void deiconize() {
  139. try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  140. }
  141. /**
  142. * Listens for mouse movements and acts on them.
  143. *
  144. * This inner class is marked "public" due to a compiler bug.
  145. * This class should be treated as a "protected" inner class.
  146. * Instantiate it only within subclasses of <Foo>.
  147. */
  148. public class MouseInputHandler extends MouseInputAdapter
  149. {
  150. // _x & _y are the mousePressed location in absolute coordinate system
  151. int _x, _y;
  152. // __x & __y are the mousePressed location in source view's coordinate system
  153. int __x, __y;
  154. Rectangle startingBounds;
  155. public void mouseReleased(MouseEvent e) {
  156. _x = 0;
  157. _y = 0;
  158. __x = 0;
  159. __y = 0;
  160. startingBounds = null;
  161. JDesktopPane d;
  162. if((d = desktopIcon.getDesktopPane()) != null) {
  163. DesktopManager dm = d.getDesktopManager();
  164. dm.endDraggingFrame(desktopIcon);
  165. }
  166. }
  167. public void mousePressed(MouseEvent e) {
  168. Point p = SwingUtilities.convertPoint((Component)e.getSource(),
  169. e.getX(), e.getY(), null);
  170. __x = e.getX();
  171. __y = e.getY();
  172. _x = p.x;
  173. _y = p.y;
  174. startingBounds = desktopIcon.getBounds();
  175. JDesktopPane d;
  176. if((d = desktopIcon.getDesktopPane()) != null) {
  177. DesktopManager dm = d.getDesktopManager();
  178. dm.beginDraggingFrame(desktopIcon);
  179. }
  180. try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  181. if(desktopIcon.getParent() instanceof JLayeredPane) {
  182. ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
  183. }
  184. if(e.getClickCount() > 1) {
  185. if(frame.isIconifiable() && frame.isIcon()) {
  186. deiconize();
  187. }
  188. }
  189. }
  190. public void mouseMoved(MouseEvent e) {}
  191. public void mouseDragged(MouseEvent e) {
  192. Point p;
  193. int newX, newY, newW, newH;
  194. int deltaX;
  195. int deltaY;
  196. Dimension min;
  197. Dimension max;
  198. p = SwingUtilities.convertPoint((Component)e.getSource(),
  199. e.getX(), e.getY(), null);
  200. Insets i = desktopIcon.getInsets();
  201. int pWidth, pHeight;
  202. pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
  203. pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
  204. if (startingBounds == null) {
  205. // (STEVE) Yucky work around for bug ID 4106552
  206. return;
  207. }
  208. newX = startingBounds.x - (_x - p.x);
  209. newY = startingBounds.y - (_y - p.y);
  210. // Make sure we stay in-bounds
  211. if(newX + i.left <= -__x)
  212. newX = -__x - i.left;
  213. if(newY + i.top <= -__y)
  214. newY = -__y - i.top;
  215. if(newX + __x + i.right > pWidth)
  216. newX = pWidth - __x - i.right;
  217. if(newY + __y + i.bottom > pHeight)
  218. newY = pHeight - __y - i.bottom;
  219. JDesktopPane d;
  220. if((d = desktopIcon.getDesktopPane()) != null) {
  221. DesktopManager dm = d.getDesktopManager();
  222. dm.dragFrame(desktopIcon, newX, newY);
  223. } else {
  224. moveAndRepaint(desktopIcon, newX, newY,
  225. desktopIcon.getWidth(), desktopIcon.getHeight());
  226. }
  227. return;
  228. }
  229. public void moveAndRepaint(JComponent f, int newX, int newY,
  230. int newWidth, int newHeight) {
  231. Rectangle r = f.getBounds();
  232. f.setBounds(newX, newY, newWidth, newHeight);
  233. SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  234. f.getParent().repaint(r.x, r.y, r.width, r.height);
  235. }
  236. }; /// End MotionListener
  237. }