KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicDesktopIconUI


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
8 package javax.swing.plaf.basic;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.event.*;
14 import javax.swing.border.*;
15 import javax.swing.plaf.*;
16 import java.beans.*;
17 import java.util.EventListener JavaDoc;
18 import java.io.Serializable JavaDoc;
19
20
21 /**
22  * Basic L&F for a minimized window on a desktop.
23  *
24  * @version 1.35 12/19/03
25  * @author David Kloba
26  * @author Steve Wilson
27  * @author Rich Schiavi
28  */

29 public class BasicDesktopIconUI extends DesktopIconUI {
30
31     protected JInternalFrame.JDesktopIcon desktopIcon;
32     protected JInternalFrame frame;
33
34     /**
35      * The title pane component used in the desktop icon.
36      *
37      * @since 1.5
38      */

39     protected JComponent iconPane;
40     MouseInputListener mouseInputListener;
41
42
43
44     public static ComponentUI createUI(JComponent c) {
45         return new BasicDesktopIconUI JavaDoc();
46     }
47
48     public BasicDesktopIconUI() {
49     }
50
51     public void installUI(JComponent c) {
52     desktopIcon = (JInternalFrame.JDesktopIcon)c;
53     frame = desktopIcon.getInternalFrame();
54     installDefaults();
55     installComponents();
56
57     // Update icon layout if frame is already iconified
58
JInternalFrame f = desktopIcon.getInternalFrame();
59     if (f.isIcon() && f.getParent() == null) {
60         JDesktopPane desktop = desktopIcon.getDesktopPane();
61         if (desktop != null) {
62         DesktopManager desktopManager = desktop.getDesktopManager();
63         if (desktopManager instanceof DefaultDesktopManager) {
64             desktopManager.iconifyFrame(f);
65         }
66         }
67     }
68
69     installListeners();
70     JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
71     }
72
73     public void uninstallUI(JComponent c) {
74     uninstallDefaults();
75     uninstallComponents();
76
77     // Force future UI to relayout icon
78
JInternalFrame f = desktopIcon.getInternalFrame();
79     if (f.isIcon()) {
80         JDesktopPane desktop = desktopIcon.getDesktopPane();
81         if (desktop != null) {
82         DesktopManager desktopManager = desktop.getDesktopManager();
83         if (desktopManager instanceof DefaultDesktopManager) {
84             // This will cause DefaultDesktopManager to layout the icon
85
f.putClientProperty("wasIconOnce", null);
86             // Move aside to allow fresh layout of all icons
87
desktopIcon.setLocation(Integer.MIN_VALUE, 0);
88         }
89         }
90     }
91
92     uninstallListeners();
93     frame = null;
94     desktopIcon = null;
95     }
96
97     protected void installComponents() {
98     iconPane = new BasicInternalFrameTitlePane JavaDoc(frame);
99     desktopIcon.setLayout(new BorderLayout());
100     desktopIcon.add(iconPane, BorderLayout.CENTER);
101     }
102
103     protected void uninstallComponents() {
104     desktopIcon.remove(iconPane);
105     desktopIcon.setLayout(null);
106         iconPane = null;
107     }
108
109     protected void installListeners() {
110     mouseInputListener = createMouseInputListener();
111     desktopIcon.addMouseMotionListener(mouseInputListener);
112     desktopIcon.addMouseListener(mouseInputListener);
113     }
114
115     protected void uninstallListeners() {
116     desktopIcon.removeMouseMotionListener(mouseInputListener);
117     desktopIcon.removeMouseListener(mouseInputListener);
118         mouseInputListener = null;
119     }
120
121     protected void installDefaults() {
122         LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
123         LookAndFeel.installProperty(desktopIcon, "opaque", Boolean.TRUE);
124     }
125
126     protected void uninstallDefaults() {
127         LookAndFeel.uninstallBorder(desktopIcon);
128     }
129
130     protected MouseInputListener createMouseInputListener() {
131         return new MouseInputHandler();
132     }
133     
134     public Dimension getPreferredSize(JComponent c) {
135         return desktopIcon.getLayout().preferredLayoutSize(desktopIcon);
136     }
137
138     public Dimension getMinimumSize(JComponent c) {
139         Dimension dim = new Dimension(iconPane.getMinimumSize());
140         Border border = frame.getBorder();
141
142         if (border != null) {
143             dim.height += border.getBorderInsets(frame).bottom +
144                           border.getBorderInsets(frame).top;
145         }
146         return dim;
147     }
148
149     /**
150      * Desktop icons can not be resized. Therefore, we should always
151      * return the minimum size of the desktop icon.
152      *
153      * @see #getMinimumSize
154      */

155     public Dimension getMaximumSize(JComponent c){
156         return iconPane.getMaximumSize();
157     }
158
159     public Insets getInsets(JComponent c) {
160         JInternalFrame iframe = desktopIcon.getInternalFrame();
161     Border border = iframe.getBorder();
162     if(border != null)
163         return border.getBorderInsets(iframe);
164     
165     return new Insets(0,0,0,0);
166     }
167
168     public void deiconize() {
169         try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
170     }
171
172     /**
173      * Listens for mouse movements and acts on them.
174      *
175      * This inner class is marked "public" due to a compiler bug.
176      * This class should be treated as a "protected" inner class.
177      * Instantiate it only within subclasses of <Foo>.
178      */

179     public class MouseInputHandler extends MouseInputAdapter
180     {
181     // _x & _y are the mousePressed location in absolute coordinate system
182
int _x, _y;
183     // __x & __y are the mousePressed location in source view's coordinate system
184
int __x, __y;
185         Rectangle startingBounds;
186
187         public void mouseReleased(MouseEvent e) {
188             _x = 0;
189             _y = 0;
190             __x = 0;
191             __y = 0;
192             startingBounds = null;
193
194         JDesktopPane d;
195         if((d = desktopIcon.getDesktopPane()) != null) {
196             DesktopManager dm = d.getDesktopManager();
197         dm.endDraggingFrame(desktopIcon);
198         }
199
200         }
201                 
202         public void mousePressed(MouseEvent e) {
203             Point p = SwingUtilities.convertPoint((Component)e.getSource(),
204                         e.getX(), e.getY(), null);
205             __x = e.getX();
206             __y = e.getY();
207             _x = p.x;
208             _y = p.y;
209             startingBounds = desktopIcon.getBounds();
210
211         JDesktopPane d;
212         if((d = desktopIcon.getDesktopPane()) != null) {
213             DesktopManager dm = d.getDesktopManager();
214         dm.beginDraggingFrame(desktopIcon);
215         }
216
217             try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
218         if(desktopIcon.getParent() instanceof JLayeredPane) {
219         ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
220         }
221
222             if(e.getClickCount() > 1) {
223         if(frame.isIconifiable() && frame.isIcon()) {
224                     deiconize();
225         }
226             }
227
228     }
229
230          public void mouseMoved(MouseEvent e) {}
231
232          public void mouseDragged(MouseEvent e) {
233             Point p;
234         int newX, newY, newW, newH;
235             int deltaX;
236             int deltaY;
237         Dimension min;
238         Dimension max;
239             p = SwingUtilities.convertPoint((Component)e.getSource(),
240                                         e.getX(), e.getY(), null);
241         
242         Insets i = desktopIcon.getInsets();
243         int pWidth, pHeight;
244         pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
245         pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
246         
247         if (startingBounds == null) {
248           // (STEVE) Yucky work around for bug ID 4106552
249
return;
250         }
251         newX = startingBounds.x - (_x - p.x);
252         newY = startingBounds.y - (_y - p.y);
253         // Make sure we stay in-bounds
254
if(newX + i.left <= -__x)
255             newX = -__x - i.left;
256         if(newY + i.top <= -__y)
257             newY = -__y - i.top;
258         if(newX + __x + i.right > pWidth)
259             newX = pWidth - __x - i.right;
260         if(newY + __y + i.bottom > pHeight)
261             newY = pHeight - __y - i.bottom;
262         
263         JDesktopPane d;
264         if((d = desktopIcon.getDesktopPane()) != null) {
265             DesktopManager dm = d.getDesktopManager();
266             dm.dragFrame(desktopIcon, newX, newY);
267         } else {
268             moveAndRepaint(desktopIcon, newX, newY,
269                 desktopIcon.getWidth(), desktopIcon.getHeight());
270         }
271         return;
272     }
273
274         public void moveAndRepaint(JComponent f, int newX, int newY,
275                     int newWidth, int newHeight) {
276         Rectangle r = f.getBounds();
277         f.setBounds(newX, newY, newWidth, newHeight);
278         SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
279         f.getParent().repaint(r.x, r.y, r.width, r.height);
280         }
281     }; /// End MotionListener
282

283 }
284
285
286
Popular Tags