1
package org.pushingpixels.lafwidget.preview;
4
import java.awt.image.BufferedImage;
5
import java.util.WeakHashMap;
8
import javax.swing.border.Border;
9
import javax.swing.plaf.basic.BasicInternalFrameUI;
11
import org.pushingpixels.lafwidget.LafWidgetUtilities;
13
public class InternalFramePreviewPainter extends DefaultPreviewPainter {
17
private static WeakHashMap snapshots = new WeakHashMap();
19
public static void refreshSnaphost(JInternalFrame frame) {
20
if (!frame.isShowing())
22
// Draw the current state of the internal frame to a
23
// temp image (w/o border and decorations). It would be nice
24
// to use Robot, but this frame may be partially obscured,
25
// so we take our chances that the frame will be properly
26
// drawn by the user code.
27
int frameWidth = frame.getWidth();
28
int frameHeight = frame.getHeight();
32
// Now we need to remove the border and the title pane :)
33
Border internalFrameBorder = UIManager
34
.getBorder("InternalFrame.border");
35
Insets borderInsets = internalFrameBorder.getBorderInsets(frame);
36
dx += borderInsets.left;
37
dy += borderInsets.top;
38
frameWidth -= (borderInsets.left + borderInsets.right);
39
frameHeight -= (borderInsets.top + borderInsets.bottom);
41
BasicInternalFrameUI frameUI = (BasicInternalFrameUI) frame.getUI();
42
JComponent frameTitlePane = frameUI.getNorthPane();
44
if (frameTitlePane != null) {
45
dy += frameTitlePane.getHeight();
46
frameHeight -= frameTitlePane.getHeight();
49
// fix for defect 112 - checking frame height and width
50
if ((frameWidth > 0) && (frameHeight > 0)) {
51
// draw frame (note the canvas translation)
52
BufferedImage tempCanvas = new BufferedImage(frameWidth,
53
frameHeight, BufferedImage.TYPE_INT_ARGB);
54
Graphics tempCanvasGraphics = tempCanvas.getGraphics();
55
tempCanvasGraphics.translate(-dx, -dy);
56
frame.paint(tempCanvasGraphics);
58
int maxWidth = UIManager.getInt("DesktopIcon.width");
59
int maxHeight = maxWidth;
61
// check if need to scale down
62
double coef = Math.min((double) maxWidth / (double) frameWidth,
63
(double) maxHeight / (double) frameHeight);
65
int sdWidth = (int) (coef * frameWidth);
66
// int sdHeight = (int) (coef * frameHeight);
67
// BufferedImage scaledDown = new BufferedImage(sdWidth,
69
// BufferedImage.TYPE_INT_ARGB);
70
// Graphics g = scaledDown.getGraphics();
71
// g.drawImage(tempCanvas, 0, 0, sdWidth, sdHeight, 0, 0,
72
// frameWidth, frameHeight, null);
73
BufferedImage scaledDown = LafWidgetUtilities.createThumbnail(
75
// System.out.println("Putting " + frame.hashCode() + "
76
// -> " + scaledDown.hashCode());
77
snapshots.put(frame, scaledDown);
79
// System.out.println("Putting " + frame.hashCode() + "
80
// -> " + snapshot.hashCode());
81
snapshots.put(frame, tempCanvas);
87
public void previewComponent(Container parent, Component component,
88
int componentIndex, Graphics g, int x, int y, int w, int h) {
89
BufferedImage preview = (BufferedImage) snapshots.get(component);
90
if (preview != null) {
91
g.drawImage(preview, x, y, null);
96
public Dimension getPreviewWindowDimension(Container parent,
97
Component component, int componentIndex) {
98
return new Dimension(UIManager.getInt("DesktopIcon.width"), UIManager
99
.getInt("DesktopIcon.width"));