Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/desktop/DesktopIconHoverPreviewWidget.java

raw
1 /*
2 * Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * o Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.pushingpixels.lafwidget.desktop;
31
32 import java.awt.*;
33 import java.awt.event.MouseEvent;
34 import java.awt.image.BufferedImage;
35 import java.beans.PropertyChangeEvent;
36 import java.beans.PropertyChangeListener;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 import javax.swing.*;
41 import javax.swing.JInternalFrame.JDesktopIcon;
42 import javax.swing.border.Border;
43 import javax.swing.event.MouseInputAdapter;
44 import javax.swing.plaf.basic.BasicInternalFrameUI;
45
46 import org.pushingpixels.lafwidget.*;
47
48 /**
49 * Adds internal frame thumbnail preview on desktop icon mouse hover.
50 *
51 * @author Kirill Grouchnikov
52 */
53 public class DesktopIconHoverPreviewWidget extends
54 LafWidgetAdapter<JDesktopIcon> {
55 /**
56 * The component that initiates the desktop icon preview (when the mouse
57 * hover above it).
58 */
59 protected JComponent compToHover;
60
61 /**
62 * Listens on the changes to the ancestor.
63 */
64 protected PropertyChangeListener internalFramePropertyListener;
65
66 /**
67 * Snapshot map.
68 */
69 // private static WeakHashMap snapshots = new WeakHashMap();
70 private BufferedImage snapshot;
71
72 /**
73 * Preview window (activated on hover).
74 */
75 private JWindow previewWindow;
76
77 /**
78 * Indicates whether the corresponding desktop icon is dragged.
79 */
80 private boolean isInDrag;
81
82 /**
83 * Mouse handler for the {@link #compToHover}.
84 */
85 protected TitleMouseHandler titleMouseHandler;
86
87 /**
88 * Mouse handler for showing / hiding the preview window.
89 *
90 * @author Kirill Grouchnikov.
91 */
92 protected class TitleMouseHandler extends MouseInputAdapter {
93 @Override
94 public void mouseEntered(MouseEvent e) {
95 if (DesktopIconHoverPreviewWidget.this.isInDrag)
96 return;
97 // final BufferedImage previewImage = DesktopIconHoverPreviewWidget
98 // .getSnapshot(DesktopIconHoverPreviewWidget.this.desktopIcon
99 // .getInternalFrame());
100 BufferedImage previewImage = snapshot;
101 if (previewImage != null) {
102 DesktopIconHoverPreviewWidget.this.previewWindow
103 .getContentPane().removeAll();
104 JLabel previewLabel = new JLabel(new ImageIcon(previewImage));
105 // previewLabel.setBorder(new SubstanceBorder());
106 DesktopIconHoverPreviewWidget.this.previewWindow
107 .getContentPane()
108 .add(previewLabel, BorderLayout.CENTER);
109 DesktopIconHoverPreviewWidget.this.previewWindow.setSize(
110 previewImage.getWidth(), previewImage.getHeight());
111 DesktopIconHoverPreviewWidget.this.syncPreviewWindow(true);
112 DesktopIconHoverPreviewWidget.this.previewWindow
113 .setVisible(true);
114 }
115 }
116
117 @Override
118 public void mouseExited(MouseEvent e) {
119 DesktopIconHoverPreviewWidget.this.isInDrag = false;
120 DesktopIconHoverPreviewWidget.this.previewWindow.dispose();// setVisible(false);
121 }
122
123 @Override
124 public void mousePressed(MouseEvent e) {
125 DesktopIconHoverPreviewWidget.this.previewWindow.dispose();// setVisible(false);
126 }
127
128 @Override
129 public void mouseReleased(MouseEvent e) {
130 DesktopIconHoverPreviewWidget.this.isInDrag = false;
131 DesktopIconHoverPreviewWidget.this.syncPreviewWindow(true);
132 DesktopIconHoverPreviewWidget.this.previewWindow.setVisible(true);
133 }
134
135 @Override
136 public void mouseDragged(MouseEvent e) {
137 DesktopIconHoverPreviewWidget.this.isInDrag = true;
138 if (DesktopIconHoverPreviewWidget.this.previewWindow.isVisible()) {
139 DesktopIconHoverPreviewWidget.this.syncPreviewWindow(false);
140 DesktopIconHoverPreviewWidget.this.previewWindow.dispose();// setVisible(false);
141 }
142 }
143 }
144
145 /*
146 * (non-Javadoc)
147 *
148 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installComponents()
149 */
150 @Override
151 public void installComponents() {
152 this.previewWindow = new JWindow();
153 this.previewWindow.getContentPane().setLayout(new BorderLayout());
154 }
155
156 /*
157 * (non-Javadoc)
158 *
159 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
160 */
161 @Override
162 public void installListeners() {
163 this.internalFramePropertyListener = new PropertyChangeListener() {
164 @Override
165 public void propertyChange(PropertyChangeEvent evt) {
166 if ("ancestor".equals(evt.getPropertyName())) {
167 updateSnapshot(jcomp.getInternalFrame());
168 }
169 }
170 };
171 jcomp.getInternalFrame().addPropertyChangeListener(
172 this.internalFramePropertyListener);
173
174 this.titleMouseHandler = new TitleMouseHandler();
175
176 LafWidgetSupport lafSupport = LafWidgetRepository.getRepository()
177 .getLafSupport();
178 this.compToHover = lafSupport.getComponentForHover(jcomp);
179
180 if (this.compToHover != null) {
181 this.compToHover.addMouseMotionListener(this.titleMouseHandler);
182 this.compToHover.addMouseListener(this.titleMouseHandler);
183 }
184 }
185
186 /*
187 * (non-Javadoc)
188 *
189 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
190 */
191 @Override
192 public void uninstallListeners() {
193 jcomp.getInternalFrame().removePropertyChangeListener(
194 this.internalFramePropertyListener);
195 this.internalFramePropertyListener = null;
196
197 if (this.compToHover != null) {
198 this.compToHover.removeMouseMotionListener(this.titleMouseHandler);
199 this.compToHover.removeMouseListener(this.titleMouseHandler);
200 }
201 this.titleMouseHandler = null;
202 }
203
204 /**
205 * Synchronizes the preview window.
206 *
207 * @param toShow
208 * Indication whether the preview window is shown.
209 */
210 private void syncPreviewWindow(boolean toShow) {
211 if (toShow) {
212 int x = jcomp.getLocationOnScreen().x;
213 int y = jcomp.getLocationOnScreen().y;
214
215 this.previewWindow.setLocation(x, y
216 - this.previewWindow.getHeight());
217
218 }
219 }
220
221 /**
222 * Updates the snapshot of the specified internal frame.
223 *
224 * @param frame
225 * Internal frame.
226 */
227 private void updateSnapshot(JInternalFrame frame) {
228 if (!frame.isShowing())
229 return;
230 // Draw the current state of the internal frame to a
231 // temp image (w/o border and decorations). It would be nice
232 // to use Robot, but this frame may be partially obscured,
233 // so we take our chances that the frame will be properly
234 // drawn by the user code.
235 int frameWidth = frame.getWidth();
236 int frameHeight = frame.getHeight();
237
238 int dx = 0;
239 int dy = 0;
240 // Now we need to remove the border and the title pane :)
241 Border internalFrameBorder = UIManager
242 .getBorder("InternalFrame.border");
243 Insets borderInsets = internalFrameBorder.getBorderInsets(frame);
244 dx += borderInsets.left;
245 dy += borderInsets.top;
246 frameWidth -= (borderInsets.left + borderInsets.right);
247 frameHeight -= (borderInsets.top + borderInsets.bottom);
248
249 BasicInternalFrameUI frameUI = (BasicInternalFrameUI) frame.getUI();
250 JComponent frameTitlePane = frameUI.getNorthPane();
251
252 if (frameTitlePane != null) {
253 dy += frameTitlePane.getHeight();
254 frameHeight -= frameTitlePane.getHeight();
255 }
256
257 // fix for defect 112 - checking frame height and width
258 if ((frameWidth > 0) && (frameHeight > 0)) {
259 // draw frame (note the canvas translation)
260 BufferedImage tempCanvas = new BufferedImage(frameWidth,
261 frameHeight, BufferedImage.TYPE_INT_ARGB);
262 Graphics tempCanvasGraphics = tempCanvas.getGraphics();
263 tempCanvasGraphics.translate(-dx, -dy);
264 Map<Component, Boolean> dbSnapshot = new HashMap<Component, Boolean>();
265 LafWidgetUtilities.makePreviewable(frame, dbSnapshot);
266 frame.paint(tempCanvasGraphics);
267 LafWidgetUtilities.restorePreviewable(frame, dbSnapshot);
268
269 int maxWidth = UIManager.getInt("DesktopIcon.width");
270 int maxHeight = maxWidth;
271
272 // check if need to scale down
273 double coef = Math.min((double) maxWidth / (double) frameWidth,
274 (double) maxHeight / (double) frameHeight);
275 if (coef < 1.0) {
276 int sdWidth = (int) (coef * frameWidth);
277 // int sdHeight = (int) (coef * frameHeight);
278 // BufferedImage scaledDown = new BufferedImage(sdWidth,
279 // sdHeight,
280 // BufferedImage.TYPE_INT_ARGB);
281 // Graphics g = scaledDown.getGraphics();
282 // g.drawImage(tempCanvas, 0, 0, sdWidth, sdHeight, 0, 0,
283 // frameWidth, frameHeight, null);
284 BufferedImage scaledDown = LafWidgetUtilities.createThumbnail(
285 tempCanvas, sdWidth);
286 // System.out.println("Putting " + frame.hashCode() + "
287 // -> " + scaledDown.hashCode());
288 snapshot = scaledDown;
289 // DesktopIconHoverPreviewWidget.snapshots.put(frame,
290 // scaledDown);
291 } else {
292 // System.out.println("Putting " + frame.hashCode() + "
293 // -> " + snapshot.hashCode());
294 snapshot = tempCanvas;
295 // DesktopIconHoverPreviewWidget.snapshots.put(frame,
296 // tempCanvas);
297 }
298 }
299 }
300
301 /**
302 * Returns the snapshot of the specified internal frame.
303 *
304 * @param frame
305 * Internal frame.
306 * @return The snapshot of the specified internal frame.
307 */
308 public synchronized BufferedImage getSnapshot(JInternalFrame frame) {
309 // BufferedImage result = (BufferedImage)
310 // DesktopIconHoverPreviewWidget.snapshots
311 // .get(frame);
312 // if (result != null)
313 // return result;
314 // // frame.setVisible(true);
315 // // updateSnapshot(frame);
316 // // frame.setVisible(false);
317 // // return snapshots.get(frame);
318 // return null;
319 return this.snapshot;
320 }
321
322 /*
323 * (non-Javadoc)
324 *
325 * @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
326 */
327 @Override
328 public boolean requiresCustomLafSupport() {
329 return false;
330 }
331 }