001 /*
002 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.basic;
027
028 import java.awt.*;
029 import java.awt.event.*;
030 import javax.swing.*;
031 import javax.swing.event.*;
032 import javax.swing.border.*;
033 import javax.swing.plaf.*;
034 import java.beans.*;
035 import java.util.EventListener;
036 import java.io.Serializable;
037
038 /**
039 * Basic L&F for a minimized window on a desktop.
040 *
041 * @version 1.42 05/05/07
042 * @author David Kloba
043 * @author Steve Wilson
044 * @author Rich Schiavi
045 */
046 public class BasicDesktopIconUI extends DesktopIconUI {
047
048 protected JInternalFrame.JDesktopIcon desktopIcon;
049 protected JInternalFrame frame;
050
051 /**
052 * The title pane component used in the desktop icon.
053 *
054 * @since 1.5
055 */
056 protected JComponent iconPane;
057 MouseInputListener mouseInputListener;
058
059 public static ComponentUI createUI(JComponent c) {
060 return new BasicDesktopIconUI();
061 }
062
063 public BasicDesktopIconUI() {
064 }
065
066 public void installUI(JComponent c) {
067 desktopIcon = (JInternalFrame.JDesktopIcon) c;
068 frame = desktopIcon.getInternalFrame();
069 installDefaults();
070 installComponents();
071
072 // Update icon layout if frame is already iconified
073 JInternalFrame f = desktopIcon.getInternalFrame();
074 if (f.isIcon() && f.getParent() == null) {
075 JDesktopPane desktop = desktopIcon.getDesktopPane();
076 if (desktop != null) {
077 DesktopManager desktopManager = desktop
078 .getDesktopManager();
079 if (desktopManager instanceof DefaultDesktopManager) {
080 desktopManager.iconifyFrame(f);
081 }
082 }
083 }
084
085 installListeners();
086 JLayeredPane
087 .putLayer(desktopIcon, JLayeredPane.getLayer(frame));
088 }
089
090 public void uninstallUI(JComponent c) {
091 uninstallDefaults();
092 uninstallComponents();
093
094 // Force future UI to relayout icon
095 JInternalFrame f = desktopIcon.getInternalFrame();
096 if (f.isIcon()) {
097 JDesktopPane desktop = desktopIcon.getDesktopPane();
098 if (desktop != null) {
099 DesktopManager desktopManager = desktop
100 .getDesktopManager();
101 if (desktopManager instanceof DefaultDesktopManager) {
102 // This will cause DefaultDesktopManager to layout the icon
103 f.putClientProperty("wasIconOnce", null);
104 // Move aside to allow fresh layout of all icons
105 desktopIcon.setLocation(Integer.MIN_VALUE, 0);
106 }
107 }
108 }
109
110 uninstallListeners();
111 frame = null;
112 desktopIcon = null;
113 }
114
115 protected void installComponents() {
116 iconPane = new BasicInternalFrameTitlePane(frame);
117 desktopIcon.setLayout(new BorderLayout());
118 desktopIcon.add(iconPane, BorderLayout.CENTER);
119 }
120
121 protected void uninstallComponents() {
122 desktopIcon.remove(iconPane);
123 desktopIcon.setLayout(null);
124 iconPane = null;
125 }
126
127 protected void installListeners() {
128 mouseInputListener = createMouseInputListener();
129 desktopIcon.addMouseMotionListener(mouseInputListener);
130 desktopIcon.addMouseListener(mouseInputListener);
131 }
132
133 protected void uninstallListeners() {
134 desktopIcon.removeMouseMotionListener(mouseInputListener);
135 desktopIcon.removeMouseListener(mouseInputListener);
136 mouseInputListener = null;
137 }
138
139 protected void installDefaults() {
140 LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
141 LookAndFeel
142 .installProperty(desktopIcon, "opaque", Boolean.TRUE);
143 }
144
145 protected void uninstallDefaults() {
146 LookAndFeel.uninstallBorder(desktopIcon);
147 }
148
149 protected MouseInputListener createMouseInputListener() {
150 return new MouseInputHandler();
151 }
152
153 public Dimension getPreferredSize(JComponent c) {
154 return desktopIcon.getLayout().preferredLayoutSize(desktopIcon);
155 }
156
157 public Dimension getMinimumSize(JComponent c) {
158 Dimension dim = new Dimension(iconPane.getMinimumSize());
159 Border border = frame.getBorder();
160
161 if (border != null) {
162 dim.height += border.getBorderInsets(frame).bottom
163 + border.getBorderInsets(frame).top;
164 }
165 return dim;
166 }
167
168 /**
169 * Desktop icons can not be resized. Therefore, we should always
170 * return the minimum size of the desktop icon.
171 *
172 * @see #getMinimumSize
173 */
174 public Dimension getMaximumSize(JComponent c) {
175 return iconPane.getMaximumSize();
176 }
177
178 public Insets getInsets(JComponent c) {
179 JInternalFrame iframe = desktopIcon.getInternalFrame();
180 Border border = iframe.getBorder();
181 if (border != null)
182 return border.getBorderInsets(iframe);
183
184 return new Insets(0, 0, 0, 0);
185 }
186
187 public void deiconize() {
188 try {
189 frame.setIcon(false);
190 } catch (PropertyVetoException e2) {
191 }
192 }
193
194 /**
195 * Listens for mouse movements and acts on them.
196 *
197 * This inner class is marked "public" due to a compiler bug.
198 * This class should be treated as a "protected" inner class.
199 * Instantiate it only within subclasses of <Foo>.
200 */
201 public class MouseInputHandler extends MouseInputAdapter {
202 // _x & _y are the mousePressed location in absolute coordinate system
203 int _x, _y;
204 // __x & __y are the mousePressed location in source view's coordinate system
205 int __x, __y;
206 Rectangle startingBounds;
207
208 public void mouseReleased(MouseEvent e) {
209 _x = 0;
210 _y = 0;
211 __x = 0;
212 __y = 0;
213 startingBounds = null;
214
215 JDesktopPane d;
216 if ((d = desktopIcon.getDesktopPane()) != null) {
217 DesktopManager dm = d.getDesktopManager();
218 dm.endDraggingFrame(desktopIcon);
219 }
220
221 }
222
223 public void mousePressed(MouseEvent e) {
224 Point p = SwingUtilities.convertPoint((Component) e
225 .getSource(), e.getX(), e.getY(), null);
226 __x = e.getX();
227 __y = e.getY();
228 _x = p.x;
229 _y = p.y;
230 startingBounds = desktopIcon.getBounds();
231
232 JDesktopPane d;
233 if ((d = desktopIcon.getDesktopPane()) != null) {
234 DesktopManager dm = d.getDesktopManager();
235 dm.beginDraggingFrame(desktopIcon);
236 }
237
238 try {
239 frame.setSelected(true);
240 } catch (PropertyVetoException e1) {
241 }
242 if (desktopIcon.getParent() instanceof JLayeredPane) {
243 ((JLayeredPane) desktopIcon.getParent())
244 .moveToFront(desktopIcon);
245 }
246
247 if (e.getClickCount() > 1) {
248 if (frame.isIconifiable() && frame.isIcon()) {
249 deiconize();
250 }
251 }
252
253 }
254
255 public void mouseMoved(MouseEvent e) {
256 }
257
258 public void mouseDragged(MouseEvent e) {
259 Point p;
260 int newX, newY, newW, newH;
261 int deltaX;
262 int deltaY;
263 Dimension min;
264 Dimension max;
265 p = SwingUtilities.convertPoint((Component) e.getSource(),
266 e.getX(), e.getY(), null);
267
268 Insets i = desktopIcon.getInsets();
269 int pWidth, pHeight;
270 pWidth = ((JComponent) desktopIcon.getParent()).getWidth();
271 pHeight = ((JComponent) desktopIcon.getParent())
272 .getHeight();
273
274 if (startingBounds == null) {
275 // (STEVE) Yucky work around for bug ID 4106552
276 return;
277 }
278 newX = startingBounds.x - (_x - p.x);
279 newY = startingBounds.y - (_y - p.y);
280 // Make sure we stay in-bounds
281 if (newX + i.left <= -__x)
282 newX = -__x - i.left;
283 if (newY + i.top <= -__y)
284 newY = -__y - i.top;
285 if (newX + __x + i.right > pWidth)
286 newX = pWidth - __x - i.right;
287 if (newY + __y + i.bottom > pHeight)
288 newY = pHeight - __y - i.bottom;
289
290 JDesktopPane d;
291 if ((d = desktopIcon.getDesktopPane()) != null) {
292 DesktopManager dm = d.getDesktopManager();
293 dm.dragFrame(desktopIcon, newX, newY);
294 } else {
295 moveAndRepaint(desktopIcon, newX, newY, desktopIcon
296 .getWidth(), desktopIcon.getHeight());
297 }
298 return;
299 }
300
301 public void moveAndRepaint(JComponent f, int newX, int newY,
302 int newWidth, int newHeight) {
303 Rectangle r = f.getBounds();
304 f.setBounds(newX, newY, newWidth, newHeight);
305 SwingUtilities.computeUnion(newX, newY, newWidth,
306 newHeight, r);
307 f.getParent().repaint(r.x, r.y, r.width, r.height);
308 }
309 }; /// End MotionListener
310
311 }
|