1. /*
  2. * @(#)BasicDesktopIconUI.java 1.26 01/11/29
  3. *
  4. * Copyright 2002 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.26 11/29/01
  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. JComponent iconPane;
  29. MouseInputListener mouseInputListener;
  30. public static ComponentUI createUI(JComponent c) {
  31. return new BasicDesktopIconUI();
  32. }
  33. public BasicDesktopIconUI() {
  34. }
  35. public void installUI(JComponent c) {
  36. desktopIcon = (JInternalFrame.JDesktopIcon)c;
  37. frame = desktopIcon.getInternalFrame();
  38. installDefaults();
  39. installComponents();
  40. installListeners();
  41. JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
  42. }
  43. public void uninstallUI(JComponent c) {
  44. // installDefaults( desktopIcon ); ?? install->uninstall??
  45. uninstallDefaults();
  46. uninstallComponents();
  47. uninstallListeners();
  48. desktopIcon = null;
  49. frame = null;
  50. }
  51. protected void installComponents() {
  52. frame = desktopIcon.getInternalFrame();
  53. iconPane = new BasicInternalFrameTitlePane(frame);
  54. desktopIcon.setLayout(new BorderLayout());
  55. desktopIcon.add(iconPane, BorderLayout.CENTER);
  56. }
  57. protected void uninstallComponents() {
  58. desktopIcon.setLayout(null);
  59. desktopIcon.remove(iconPane);
  60. }
  61. protected void installListeners() {
  62. mouseInputListener = createMouseInputListener();
  63. desktopIcon.addMouseMotionListener(mouseInputListener);
  64. desktopIcon.addMouseListener(mouseInputListener);
  65. }
  66. protected void uninstallListeners() {
  67. desktopIcon.removeMouseMotionListener(mouseInputListener);
  68. desktopIcon.removeMouseListener(mouseInputListener);
  69. }
  70. protected void installDefaults() {
  71. LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
  72. }
  73. protected void uninstallDefaults() {
  74. }
  75. protected MouseInputListener createMouseInputListener() {
  76. return new MouseInputHandler();
  77. }
  78. public Dimension getPreferredSize(JComponent c) {
  79. JInternalFrame iframe = desktopIcon.getInternalFrame();
  80. Border border = iframe.getBorder();
  81. int w2 = 157;
  82. int h2 = 18;
  83. if(border != null)
  84. h2 += border.getBorderInsets(iframe).bottom +
  85. border.getBorderInsets(iframe).top;
  86. return new Dimension(w2, h2);
  87. }
  88. public Dimension getMinimumSize(JComponent c) {
  89. return iconPane.getMinimumSize();
  90. }
  91. public Dimension getMaximumSize(JComponent c){
  92. return iconPane.getMaximumSize();
  93. }
  94. public Insets getInsets(JComponent c) {
  95. JInternalFrame iframe = desktopIcon.getInternalFrame();
  96. Border border = iframe.getBorder();
  97. if(border != null)
  98. return border.getBorderInsets(iframe);
  99. return new Insets(0,0,0,0);
  100. }
  101. public void deiconize() {
  102. try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  103. }
  104. /**
  105. * Listens for mouse movements and acts on them.
  106. *
  107. * This inner class is marked "public" due to a compiler bug.
  108. * This class should be treated as a "protected" inner class.
  109. * Instantiate it only within subclasses of <Foo>.
  110. */
  111. public class MouseInputHandler extends MouseInputAdapter
  112. {
  113. // _x & _y are the mousePressed location in absolute coordinate system
  114. int _x, _y;
  115. // __x & __y are the mousePressed location in source view's coordinate system
  116. int __x, __y;
  117. Rectangle startingBounds;
  118. public void mouseReleased(MouseEvent e) {
  119. _x = 0;
  120. _y = 0;
  121. __x = 0;
  122. __y = 0;
  123. startingBounds = null;
  124. JDesktopPane d;
  125. if((d = desktopIcon.getDesktopPane()) != null) {
  126. DesktopManager dm = d.getDesktopManager();
  127. dm.endDraggingFrame(desktopIcon);
  128. }
  129. }
  130. public void mousePressed(MouseEvent e) {
  131. Point p = SwingUtilities.convertPoint((Component)e.getSource(),
  132. e.getX(), e.getY(), null);
  133. __x = e.getX();
  134. __y = e.getY();
  135. _x = p.x;
  136. _y = p.y;
  137. startingBounds = desktopIcon.getBounds();
  138. JDesktopPane d;
  139. if((d = desktopIcon.getDesktopPane()) != null) {
  140. DesktopManager dm = d.getDesktopManager();
  141. dm.beginDraggingFrame(desktopIcon);
  142. }
  143. try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  144. if(desktopIcon.getParent() instanceof JLayeredPane) {
  145. ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
  146. }
  147. if(e.getClickCount() > 1) {
  148. if(frame.isIconifiable() && frame.isIcon()) {
  149. deiconize();
  150. }
  151. }
  152. }
  153. public void mouseMoved(MouseEvent e) {}
  154. public void mouseDragged(MouseEvent e) {
  155. Point p;
  156. int newX, newY, newW, newH;
  157. int deltaX;
  158. int deltaY;
  159. Dimension min;
  160. Dimension max;
  161. p = SwingUtilities.convertPoint((Component)e.getSource(),
  162. e.getX(), e.getY(), null);
  163. Insets i = desktopIcon.getInsets();
  164. int pWidth, pHeight;
  165. pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
  166. pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
  167. if (startingBounds == null) {
  168. // (STEVE) Yucky work around for bug ID 4106552
  169. return;
  170. }
  171. newX = startingBounds.x - (_x - p.x);
  172. newY = startingBounds.y - (_y - p.y);
  173. // Make sure we stay in-bounds
  174. if(newX + i.left <= -__x)
  175. newX = -__x - i.left;
  176. if(newY + i.top <= -__y)
  177. newY = -__y - i.top;
  178. if(newX + __x + i.right > pWidth)
  179. newX = pWidth - __x - i.right;
  180. if(newY + __y + i.bottom > pHeight)
  181. newY = pHeight - __y - i.bottom;
  182. JDesktopPane d;
  183. if((d = desktopIcon.getDesktopPane()) != null) {
  184. DesktopManager dm = d.getDesktopManager();
  185. dm.dragFrame(desktopIcon, newX, newY);
  186. } else {
  187. moveAndRepaint(desktopIcon, newX, newY,
  188. desktopIcon.getWidth(), desktopIcon.getHeight());
  189. }
  190. return;
  191. }
  192. public void moveAndRepaint(JComponent f, int newX, int newY,
  193. int newWidth, int newHeight) {
  194. Rectangle r = f.getBounds();
  195. f.setBounds(newX, newY, newWidth, newHeight);
  196. SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  197. f.getParent().repaint(r.x, r.y, r.width, r.height);
  198. }
  199. }; /// End MotionListener
  200. }