2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.swing.plaf.basic;
|
|
27 |
|
|
28 |
import java.awt.*;
|
|
29 |
import java.awt.event.*;
|
|
30 |
import javax.swing.*;
|
|
31 |
import javax.swing.event.*;
|
|
32 |
import javax.swing.border.*;
|
|
33 |
import javax.swing.plaf.*;
|
|
34 |
import java.beans.*;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Basic L&F for a minimized window on a desktop.
|
|
38 |
*
|
|
39 |
* @author David Kloba
|
|
40 |
* @author Steve Wilson
|
|
41 |
* @author Rich Schiavi
|
|
42 |
*/
|
|
43 |
public class BasicDesktopIconUI extends DesktopIconUI {
|
|
44 |
|
|
45 |
protected JInternalFrame.JDesktopIcon desktopIcon;
|
|
46 |
protected JInternalFrame frame;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* The title pane component used in the desktop icon.
|
|
50 |
*
|
|
51 |
* @since 1.5
|
|
52 |
*/
|
|
53 |
protected JComponent iconPane;
|
|
54 |
MouseInputListener mouseInputListener;
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
public static ComponentUI createUI(JComponent c) {
|
|
59 |
return new BasicDesktopIconUI();
|
|
60 |
}
|
|
61 |
|
|
62 |
public BasicDesktopIconUI() {
|
|
63 |
}
|
|
64 |
|
|
65 |
public void installUI(JComponent c) {
|
|
66 |
desktopIcon = (JInternalFrame.JDesktopIcon)c;
|
|
67 |
frame = desktopIcon.getInternalFrame();
|
|
68 |
installDefaults();
|
|
69 |
installComponents();
|
|
70 |
|
|
71 |
// Update icon layout if frame is already iconified
|
|
72 |
JInternalFrame f = desktopIcon.getInternalFrame();
|
|
73 |
if (f.isIcon() && f.getParent() == null) {
|
|
74 |
JDesktopPane desktop = desktopIcon.getDesktopPane();
|
|
75 |
if (desktop != null) {
|
|
76 |
DesktopManager desktopManager = desktop.getDesktopManager();
|
|
77 |
if (desktopManager instanceof DefaultDesktopManager) {
|
|
78 |
desktopManager.iconifyFrame(f);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
installListeners();
|
|
84 |
JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
|
|
85 |
}
|
|
86 |
|
|
87 |
public void uninstallUI(JComponent c) {
|
|
88 |
uninstallDefaults();
|
|
89 |
uninstallComponents();
|
|
90 |
|
|
91 |
// Force future UI to relayout icon
|
|
92 |
JInternalFrame f = desktopIcon.getInternalFrame();
|
|
93 |
if (f.isIcon()) {
|
|
94 |
JDesktopPane desktop = desktopIcon.getDesktopPane();
|
|
95 |
if (desktop != null) {
|
|
96 |
DesktopManager desktopManager = desktop.getDesktopManager();
|
|
97 |
if (desktopManager instanceof DefaultDesktopManager) {
|
|
98 |
// This will cause DefaultDesktopManager to layout the icon
|
|
99 |
f.putClientProperty("wasIconOnce", null);
|
|
100 |
// Move aside to allow fresh layout of all icons
|
|
101 |
desktopIcon.setLocation(Integer.MIN_VALUE, 0);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
uninstallListeners();
|
|
107 |
frame = null;
|
|
108 |
desktopIcon = null;
|
|
109 |
}
|
|
110 |
|
|
111 |
protected void installComponents() {
|
|
112 |
iconPane = new BasicInternalFrameTitlePane(frame);
|
|
113 |
desktopIcon.setLayout(new BorderLayout());
|
|
114 |
desktopIcon.add(iconPane, BorderLayout.CENTER);
|
|
115 |
}
|
|
116 |
|
|
117 |
protected void uninstallComponents() {
|
|
118 |
desktopIcon.remove(iconPane);
|
|
119 |
desktopIcon.setLayout(null);
|
|
120 |
iconPane = null;
|
|
121 |
}
|
|
122 |
|
|
123 |
protected void installListeners() {
|
|
124 |
mouseInputListener = createMouseInputListener();
|
|
125 |
desktopIcon.addMouseMotionListener(mouseInputListener);
|
|
126 |
desktopIcon.addMouseListener(mouseInputListener);
|
|
127 |
}
|
|
128 |
|
|
129 |
protected void uninstallListeners() {
|
|
130 |
desktopIcon.removeMouseMotionListener(mouseInputListener);
|
|
131 |
desktopIcon.removeMouseListener(mouseInputListener);
|
|
132 |
mouseInputListener = null;
|
|
133 |
}
|
|
134 |
|
|
135 |
protected void installDefaults() {
|
|
136 |
LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
|
|
137 |
LookAndFeel.installProperty(desktopIcon, "opaque", Boolean.TRUE);
|
|
138 |
}
|
|
139 |
|
|
140 |
protected void uninstallDefaults() {
|
|
141 |
LookAndFeel.uninstallBorder(desktopIcon);
|
|
142 |
}
|
|
143 |
|
|
144 |
protected MouseInputListener createMouseInputListener() {
|
|
145 |
return new MouseInputHandler();
|
|
146 |
}
|
|
147 |
|
|
148 |
public Dimension getPreferredSize(JComponent c) {
|
|
149 |
return desktopIcon.getLayout().preferredLayoutSize(desktopIcon);
|
|
150 |
}
|
|
151 |
|
|
152 |
public Dimension getMinimumSize(JComponent c) {
|
|
153 |
Dimension dim = new Dimension(iconPane.getMinimumSize());
|
|
154 |
Border border = frame.getBorder();
|
|
155 |
|
|
156 |
if (border != null) {
|
|
157 |
dim.height += border.getBorderInsets(frame).bottom +
|
|
158 |
border.getBorderInsets(frame).top;
|
|
159 |
}
|
|
160 |
return dim;
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Desktop icons can not be resized. Therefore, we should always
|
|
165 |
* return the minimum size of the desktop icon.
|
|
166 |
*
|
|
167 |
* @see #getMinimumSize
|
|
168 |
*/
|
|
169 |
public Dimension getMaximumSize(JComponent c){
|
|
170 |
return iconPane.getMaximumSize();
|
|
171 |
}
|
|
172 |
|
|
173 |
public Insets getInsets(JComponent c) {
|
|
174 |
JInternalFrame iframe = desktopIcon.getInternalFrame();
|
|
175 |
Border border = iframe.getBorder();
|
|
176 |
if(border != null)
|
|
177 |
return border.getBorderInsets(iframe);
|
|
178 |
|
|
179 |
return new Insets(0,0,0,0);
|
|
180 |
}
|
|
181 |
|
|
182 |
public void deiconize() {
|
|
183 |
try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Listens for mouse movements and acts on them.
|
|
188 |
*
|
|
189 |
* This inner class is marked "public" due to a compiler bug.
|
|
190 |
* This class should be treated as a "protected" inner class.
|
|
191 |
* Instantiate it only within subclasses of <Foo>.
|
|
192 |
*/
|
|
193 |
public class MouseInputHandler extends MouseInputAdapter
|
|
194 |
{
|
|
195 |
// _x & _y are the mousePressed location in absolute coordinate system
|
|
196 |
int _x, _y;
|
|
197 |
// __x & __y are the mousePressed location in source view's coordinate system
|
|
198 |
int __x, __y;
|
|
199 |
Rectangle startingBounds;
|
|
200 |
|
|
201 |
public void mouseReleased(MouseEvent e) {
|
|
202 |
_x = 0;
|
|
203 |
_y = 0;
|
|
204 |
__x = 0;
|
|
205 |
__y = 0;
|
|
206 |
startingBounds = null;
|
|
207 |
|
|
208 |
JDesktopPane d;
|
|
209 |
if((d = desktopIcon.getDesktopPane()) != null) {
|
|
210 |
DesktopManager dm = d.getDesktopManager();
|
|
211 |
dm.endDraggingFrame(desktopIcon);
|
|
212 |
}
|
|
213 |
|
|
214 |
}
|
|
215 |
|
|
216 |
public void mousePressed(MouseEvent e) {
|
|
217 |
Point p = SwingUtilities.convertPoint((Component)e.getSource(),
|
|
218 |
e.getX(), e.getY(), null);
|
|
219 |
__x = e.getX();
|
|
220 |
__y = e.getY();
|
|
221 |
_x = p.x;
|
|
222 |
_y = p.y;
|
|
223 |
startingBounds = desktopIcon.getBounds();
|
|
224 |
|
|
225 |
JDesktopPane d;
|
|
226 |
if((d = desktopIcon.getDesktopPane()) != null) {
|
|
227 |
DesktopManager dm = d.getDesktopManager();
|
|
228 |
dm.beginDraggingFrame(desktopIcon);
|
|
229 |
}
|
|
230 |
|
|
231 |
try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
|
|
232 |
if(desktopIcon.getParent() instanceof JLayeredPane) {
|
|
233 |
((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
|
|
234 |
}
|
|
235 |
|
|
236 |
if(e.getClickCount() > 1) {
|
|
237 |
if(frame.isIconifiable() && frame.isIcon()) {
|
|
238 |
deiconize();
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
}
|
|
243 |
|
|
244 |
public void mouseMoved(MouseEvent e) {}
|
|
245 |
|
|
246 |
public void mouseDragged(MouseEvent e) {
|
|
247 |
Point p;
|
|
248 |
int newX, newY, newW, newH;
|
|
249 |
int deltaX;
|
|
250 |
int deltaY;
|
|
251 |
Dimension min;
|
|
252 |
Dimension max;
|
|
253 |
p = SwingUtilities.convertPoint((Component)e.getSource(),
|
|
254 |
e.getX(), e.getY(), null);
|
|
255 |
|
|
256 |
Insets i = desktopIcon.getInsets();
|
|
257 |
int pWidth, pHeight;
|
|
258 |
pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
|
|
259 |
pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
|
|
260 |
|
|
261 |
if (startingBounds == null) {
|
|
262 |
// (STEVE) Yucky work around for bug ID 4106552
|
|
263 |
return;
|
|
264 |
}
|
|
265 |
newX = startingBounds.x - (_x - p.x);
|
|
266 |
newY = startingBounds.y - (_y - p.y);
|
|
267 |
// Make sure we stay in-bounds
|
|
268 |
if(newX + i.left <= -__x)
|
|
269 |
newX = -__x - i.left;
|
|
270 |
if(newY + i.top <= -__y)
|
|
271 |
newY = -__y - i.top;
|
|
272 |
if(newX + __x + i.right > pWidth)
|
|
273 |
newX = pWidth - __x - i.right;
|
|
274 |
if(newY + __y + i.bottom > pHeight)
|
|
275 |
newY = pHeight - __y - i.bottom;
|
|
276 |
|
|
277 |
JDesktopPane d;
|
|
278 |
if((d = desktopIcon.getDesktopPane()) != null) {
|
|
279 |
DesktopManager dm = d.getDesktopManager();
|
|
280 |
dm.dragFrame(desktopIcon, newX, newY);
|
|
281 |
} else {
|
|
282 |
moveAndRepaint(desktopIcon, newX, newY,
|
|
283 |
desktopIcon.getWidth(), desktopIcon.getHeight());
|
|
284 |
}
|
|
285 |
return;
|
|
286 |
}
|
|
287 |
|
|
288 |
public void moveAndRepaint(JComponent f, int newX, int newY,
|
|
289 |
int newWidth, int newHeight) {
|
|
290 |
Rectangle r = f.getBounds();
|
|
291 |
f.setBounds(newX, newY, newWidth, newHeight);
|
|
292 |
SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
|
|
293 |
f.getParent().repaint(r.x, r.y, r.width, r.height);
|
|
294 |
}
|
|
295 |
}; /// End MotionListener
|
|
296 |
|
|
297 |
}
|