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