1. /*
  2. * @(#)MotifDesktopPaneUI.java 1.23 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.motif;
  8. import javax.swing.*;
  9. import java.awt.Rectangle;
  10. import java.awt.Dimension;
  11. import java.awt.Insets;
  12. import java.awt.Color;
  13. import java.awt.Graphics;
  14. import java.awt.Component;
  15. import java.awt.Point;
  16. import javax.swing.plaf.*;
  17. import java.io.Serializable;
  18. /**
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.23 01/23/03
  28. * @author David Kloba
  29. */
  30. public class MotifDesktopPaneUI extends javax.swing.plaf.basic.BasicDesktopPaneUI
  31. {
  32. /// DesktopPaneUI methods
  33. public static ComponentUI createUI(JComponent d) {
  34. return new MotifDesktopPaneUI();
  35. }
  36. public MotifDesktopPaneUI() {
  37. }
  38. protected void installDesktopManager() {
  39. if(desktop.getDesktopManager() == null) {
  40. desktopManager = new MotifDesktopManager();
  41. desktop.setDesktopManager(desktopManager);
  42. ((MotifDesktopManager)desktopManager).adjustIcons(desktop);
  43. }
  44. }
  45. public Insets getInsets(JComponent c) {return new Insets(0,0,0,0);}
  46. ////////////////////////////////////////////////////////////////////////////////////
  47. /// DragPane class
  48. ////////////////////////////////////////////////////////////////////////////////////
  49. private class DragPane extends JComponent {
  50. public void paint(Graphics g) {
  51. g.setColor(Color.darkGray);
  52. g.drawRect(0, 0, getWidth()-1, getHeight()-1);
  53. }
  54. };
  55. ////////////////////////////////////////////////////////////////////////////////////
  56. /// MotifDesktopManager class
  57. ////////////////////////////////////////////////////////////////////////////////////
  58. private class MotifDesktopManager extends DefaultDesktopManager implements Serializable {
  59. JComponent dragPane;
  60. boolean usingDragPane = false;
  61. private transient JLayeredPane layeredPaneForDragPane;
  62. int iconWidth, iconHeight;
  63. // PENDING(klobad) this should be optimized
  64. public void setBoundsForFrame(JComponent f, int newX, int newY,
  65. int newWidth, int newHeight) {
  66. if(!usingDragPane) {
  67. boolean didResize;
  68. didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight);
  69. Rectangle r = f.getBounds();
  70. f.setBounds(newX, newY, newWidth, newHeight);
  71. SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  72. f.getParent().repaint(r.x, r.y, r.width, r.height);
  73. if(didResize) {
  74. f.validate();
  75. }
  76. } else {
  77. Rectangle r = dragPane.getBounds();
  78. dragPane.setBounds(newX, newY, newWidth, newHeight);
  79. SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  80. dragPane.getParent().repaint(r.x, r.y, r.width, r.height);
  81. }
  82. }
  83. public void beginDraggingFrame(JComponent f) {
  84. usingDragPane = false;
  85. if(f.getParent() instanceof JLayeredPane) {
  86. if(dragPane == null)
  87. dragPane = new DragPane();
  88. layeredPaneForDragPane = (JLayeredPane)f.getParent();
  89. layeredPaneForDragPane.setLayer(dragPane, Integer.MAX_VALUE);
  90. dragPane.setBounds(f.getX(), f.getY(), f.getWidth(), f.getHeight());
  91. layeredPaneForDragPane.add(dragPane);
  92. usingDragPane = true;
  93. }
  94. }
  95. public void dragFrame(JComponent f, int newX, int newY) {
  96. setBoundsForFrame(f, newX, newY, f.getWidth(), f.getHeight());
  97. }
  98. public void endDraggingFrame(JComponent f) {
  99. if(usingDragPane) {
  100. layeredPaneForDragPane.remove(dragPane);
  101. usingDragPane = false;
  102. if (f instanceof JInternalFrame) {
  103. setBoundsForFrame(f, dragPane.getX(), dragPane.getY(),
  104. dragPane.getWidth(), dragPane.getHeight());
  105. } else if (f instanceof JInternalFrame.JDesktopIcon) {
  106. adjustBoundsForIcon((JInternalFrame.JDesktopIcon)f,
  107. dragPane.getX(), dragPane.getY());
  108. }
  109. }
  110. }
  111. public void beginResizingFrame(JComponent f, int direction) {
  112. usingDragPane = false;
  113. if(f.getParent() instanceof JLayeredPane) {
  114. if(dragPane == null)
  115. dragPane = new DragPane();
  116. JLayeredPane p = (JLayeredPane)f.getParent();
  117. p.setLayer(dragPane, Integer.MAX_VALUE);
  118. dragPane.setBounds(f.getX(), f.getY(),
  119. f.getWidth(), f.getHeight());
  120. p.add(dragPane);
  121. usingDragPane = true;
  122. }
  123. }
  124. public void resizeFrame(JComponent f, int newX, int newY,
  125. int newWidth, int newHeight) {
  126. setBoundsForFrame(f, newX, newY, newWidth, newHeight);
  127. }
  128. public void endResizingFrame(JComponent f) {
  129. if(usingDragPane) {
  130. JLayeredPane p = (JLayeredPane)f.getParent();
  131. p.remove(dragPane);
  132. usingDragPane = false;
  133. setBoundsForFrame(f, dragPane.getX(), dragPane.getY(),
  134. dragPane.getWidth(), dragPane.getHeight());
  135. }
  136. }
  137. public void iconifyFrame(JInternalFrame f) {
  138. JInternalFrame.JDesktopIcon icon = f.getDesktopIcon();
  139. Point p = icon.getLocation();
  140. adjustBoundsForIcon(icon, p.x, p.y);
  141. super.iconifyFrame(f);
  142. }
  143. /**
  144. * Change positions of icons in the desktop pane so that
  145. * they do not overlap
  146. */
  147. protected void adjustIcons(JDesktopPane desktop) {
  148. // We need to know Motif icon size
  149. JInternalFrame.JDesktopIcon icon = new JInternalFrame.JDesktopIcon(
  150. new JInternalFrame());
  151. Dimension iconSize = icon.getPreferredSize();
  152. iconWidth = iconSize.width;
  153. iconHeight = iconSize.height;
  154. JInternalFrame[] frames = desktop.getAllFrames();
  155. for (int i=0; i<frames.length; i++) {
  156. icon = frames[i].getDesktopIcon();
  157. Point ip = icon.getLocation();
  158. adjustBoundsForIcon(icon, ip.x, ip.y);
  159. }
  160. }
  161. /**
  162. * Change positions of icon so that it doesn't overlap
  163. * other icons.
  164. */
  165. protected void adjustBoundsForIcon(JInternalFrame.JDesktopIcon icon,
  166. int x, int y) {
  167. JDesktopPane c = icon.getDesktopPane();
  168. int maxy = c.getHeight();
  169. int w = iconWidth;
  170. int h = iconHeight;
  171. c.repaint(x, y, w, h);
  172. x = x < 0 ? 0 : x;
  173. y = y < 0 ? 0 : y;
  174. /* Fix for disappearing icons. If the y value is maxy then this
  175. * algorithm would place the icon in a non-displayed cell. Never
  176. * to be ssen again.*/
  177. y = y >= maxy ? (maxy - 1) : y;
  178. /* Compute the offset for the cell we are trying to go in. */
  179. int lx = (x / w) * w;
  180. int ygap = maxy % h;
  181. int ly = ((y-ygap) / h) * h + ygap;
  182. /* How far are we into the cell we dropped the icon in. */
  183. int dx = x - lx;
  184. int dy = y - ly;
  185. /* Set coordinates for the icon. */
  186. x = dx < w2 ? lx: lx + w;
  187. y = dy < h2 ? ly: ((ly + h) < maxy ? ly + h: ly);
  188. while (getIconAt(c, icon, x, y) != null) {
  189. x += w;
  190. }
  191. /* Cancel the move if the x value was moved off screen. */
  192. if (x > c.getWidth()) {
  193. return;
  194. }
  195. if (icon.getParent() != null) {
  196. setBoundsForFrame(icon, x, y, w, h);
  197. } else {
  198. icon.setLocation(x, y);
  199. }
  200. }
  201. protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
  202. JInternalFrame.JDesktopIcon icon, int x, int y) {
  203. JInternalFrame.JDesktopIcon currentIcon = null;
  204. Component[] components = desktop.getComponents();
  205. for (int i=0; i<components.length; i++) {
  206. Component comp = components[i];
  207. if (comp instanceof JInternalFrame.JDesktopIcon &&
  208. comp != icon) {
  209. Point p = comp.getLocation();
  210. if (p.x == x && p.y == y) {
  211. return (JInternalFrame.JDesktopIcon)comp;
  212. }
  213. }
  214. }
  215. return null;
  216. }
  217. }; /// END of MotifDesktopManager
  218. }