KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifDesktopPaneUI


1 /*
2  * @(#)MotifDesktopPaneUI.java 1.25 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
8 package com.sun.java.swing.plaf.motif;
9
10 import javax.swing.*;
11 import java.awt.Rectangle JavaDoc;
12 import java.awt.Dimension JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.Color JavaDoc;
15 import java.awt.Graphics JavaDoc;
16 import java.awt.Component JavaDoc;
17 import java.awt.Point JavaDoc;
18 import javax.swing.plaf.*;
19 import java.io.Serializable JavaDoc;
20
21 /**
22  * <p>
23  * <strong>Warning:</strong>
24  * Serialized objects of this class will not be compatible with
25  * future Swing releases. The current serialization support is appropriate
26  * for short term storage or RMI between applications running the same
27  * version of Swing. A future release of Swing will provide support for
28  * long term persistence.
29  *
30  * @version 1.25 12/19/03
31  * @author David Kloba
32  */

33 public class MotifDesktopPaneUI extends javax.swing.plaf.basic.BasicDesktopPaneUI JavaDoc
34 {
35
36 /// DesktopPaneUI methods
37
public static ComponentUI createUI(JComponent d) {
38         return new MotifDesktopPaneUI();
39     }
40
41     public MotifDesktopPaneUI() {
42     }
43
44     protected void installDesktopManager() {
45     desktopManager = desktop.getDesktopManager();
46     if(desktopManager == null) {
47         desktopManager = new MotifDesktopManager();
48         desktop.setDesktopManager(desktopManager);
49             ((MotifDesktopManager)desktopManager).adjustIcons(desktop);
50     }
51     }
52
53     public Insets JavaDoc getInsets(JComponent c) {return new Insets JavaDoc(0,0,0,0);}
54
55 ////////////////////////////////////////////////////////////////////////////////////
56
/// DragPane class
57
////////////////////////////////////////////////////////////////////////////////////
58
private class DragPane extends JComponent {
59     public void paint(Graphics JavaDoc g) {
60         g.setColor(Color.darkGray);
61         g.drawRect(0, 0, getWidth()-1, getHeight()-1);
62     }
63     };
64
65 ////////////////////////////////////////////////////////////////////////////////////
66
/// MotifDesktopManager class
67
////////////////////////////////////////////////////////////////////////////////////
68
private class MotifDesktopManager extends DefaultDesktopManager implements Serializable JavaDoc, UIResource {
69         JComponent dragPane;
70         boolean usingDragPane = false;
71         private transient JLayeredPane layeredPaneForDragPane;
72         int iconWidth, iconHeight;
73
74     // PENDING(klobad) this should be optimized
75
public void setBoundsForFrame(JComponent f, int newX, int newY,
76             int newWidth, int newHeight) {
77     if(!usingDragPane) {
78             boolean didResize;
79             didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight);
80         Rectangle JavaDoc r = f.getBounds();
81         f.setBounds(newX, newY, newWidth, newHeight);
82         SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
83         f.getParent().repaint(r.x, r.y, r.width, r.height);
84         if(didResize) {
85             f.validate();
86         }
87     } else {
88         Rectangle JavaDoc r = dragPane.getBounds();
89         dragPane.setBounds(newX, newY, newWidth, newHeight);
90         SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
91         dragPane.getParent().repaint(r.x, r.y, r.width, r.height);
92     }
93     }
94
95     public void beginDraggingFrame(JComponent f) {
96     usingDragPane = false;
97     if(f.getParent() instanceof JLayeredPane) {
98         if(dragPane == null)
99         dragPane = new DragPane();
100         layeredPaneForDragPane = (JLayeredPane)f.getParent();
101         layeredPaneForDragPane.setLayer(dragPane, Integer.MAX_VALUE);
102         dragPane.setBounds(f.getX(), f.getY(), f.getWidth(), f.getHeight());
103         layeredPaneForDragPane.add(dragPane);
104         usingDragPane = true;
105     }
106     }
107
108     public void dragFrame(JComponent f, int newX, int newY) {
109     setBoundsForFrame(f, newX, newY, f.getWidth(), f.getHeight());
110     }
111
112     public void endDraggingFrame(JComponent f) {
113     if(usingDragPane) {
114         layeredPaneForDragPane.remove(dragPane);
115         usingDragPane = false;
116             if (f instanceof JInternalFrame) {
117                 setBoundsForFrame(f, dragPane.getX(), dragPane.getY(),
118                         dragPane.getWidth(), dragPane.getHeight());
119             } else if (f instanceof JInternalFrame.JDesktopIcon) {
120                 adjustBoundsForIcon((JInternalFrame.JDesktopIcon)f,
121                         dragPane.getX(), dragPane.getY());
122             }
123     }
124     }
125
126     public void beginResizingFrame(JComponent f, int direction) {
127     usingDragPane = false;
128     if(f.getParent() instanceof JLayeredPane) {
129         if(dragPane == null)
130         dragPane = new DragPane();
131         JLayeredPane p = (JLayeredPane)f.getParent();
132         p.setLayer(dragPane, Integer.MAX_VALUE);
133         dragPane.setBounds(f.getX(), f.getY(),
134                 f.getWidth(), f.getHeight());
135         p.add(dragPane);
136         usingDragPane = true;
137     }
138     }
139
140     public void resizeFrame(JComponent f, int newX, int newY,
141                 int newWidth, int newHeight) {
142     setBoundsForFrame(f, newX, newY, newWidth, newHeight);
143     }
144
145     public void endResizingFrame(JComponent f) {
146     if(usingDragPane) {
147         JLayeredPane p = (JLayeredPane)f.getParent();
148         p.remove(dragPane);
149         usingDragPane = false;
150             setBoundsForFrame(f, dragPane.getX(), dragPane.getY(),
151                 dragPane.getWidth(), dragPane.getHeight());
152     }
153     }
154
155         public void iconifyFrame(JInternalFrame f) {
156             JInternalFrame.JDesktopIcon icon = f.getDesktopIcon();
157             Point JavaDoc p = icon.getLocation();
158             adjustBoundsForIcon(icon, p.x, p.y);
159             super.iconifyFrame(f);
160         }
161
162         /**
163          * Change positions of icons in the desktop pane so that
164          * they do not overlap
165          */

166         protected void adjustIcons(JDesktopPane desktop) {
167             // We need to know Motif icon size
168
JInternalFrame.JDesktopIcon icon = new JInternalFrame.JDesktopIcon(
169                     new JInternalFrame());
170             Dimension JavaDoc iconSize = icon.getPreferredSize();
171             iconWidth = iconSize.width;
172             iconHeight = iconSize.height;
173
174             JInternalFrame[] frames = desktop.getAllFrames();
175             for (int i=0; i<frames.length; i++) {
176                 icon = frames[i].getDesktopIcon();
177                 Point JavaDoc ip = icon.getLocation();
178                 adjustBoundsForIcon(icon, ip.x, ip.y);
179             }
180         }
181
182         /**
183          * Change positions of icon so that it doesn't overlap
184          * other icons.
185          */

186         protected void adjustBoundsForIcon(JInternalFrame.JDesktopIcon icon,
187                 int x, int y) {
188             JDesktopPane c = icon.getDesktopPane();
189
190             int maxy = c.getHeight();
191             int w = iconWidth;
192             int h = iconHeight;
193             c.repaint(x, y, w, h);
194             x = x < 0 ? 0 : x;
195             y = y < 0 ? 0 : y;
196
197             /* Fix for disappearing icons. If the y value is maxy then this
198              * algorithm would place the icon in a non-displayed cell. Never
199              * to be ssen again.*/

200             y = y >= maxy ? (maxy - 1) : y;
201
202             /* Compute the offset for the cell we are trying to go in. */
203             int lx = (x / w) * w;
204             int ygap = maxy % h;
205             int ly = ((y-ygap) / h) * h + ygap;
206
207             /* How far are we into the cell we dropped the icon in. */
208             int dx = x - lx;
209             int dy = y - ly;
210
211             /* Set coordinates for the icon. */
212             x = dx < w/2 ? lx: lx + w;
213             y = dy < h/2 ? ly: ((ly + h) < maxy ? ly + h: ly);
214
215             while (getIconAt(c, icon, x, y) != null) {
216                 x += w;
217             }
218
219             /* Cancel the move if the x value was moved off screen. */
220             if (x > c.getWidth()) {
221                 return;
222             }
223             if (icon.getParent() != null) {
224                 setBoundsForFrame(icon, x, y, w, h);
225             } else {
226                 icon.setLocation(x, y);
227             }
228         }
229
230         protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop,
231             JInternalFrame.JDesktopIcon icon, int x, int y) {
232
233             JInternalFrame.JDesktopIcon currentIcon = null;
234             Component JavaDoc[] components = desktop.getComponents();
235
236             for (int i=0; i<components.length; i++) {
237                 Component JavaDoc comp = components[i];
238                 if (comp instanceof JInternalFrame.JDesktopIcon &&
239                     comp != icon) {
240
241                     Point JavaDoc p = comp.getLocation();
242                     if (p.x == x && p.y == y) {
243                         return (JInternalFrame.JDesktopIcon)comp;
244                     }
245                 }
246             }
247             return null;
248         }
249     }; /// END of MotifDesktopManager
250
}
251
Popular Tags