| 1 |
package org.pushingpixels.lafwidget.preview; |
| 2 |
|
| 3 |
import java.awt.*; |
| 4 |
import java.awt.image.BufferedImage; |
| 5 |
import java.util.WeakHashMap; |
| 6 |
|
| 7 |
import javax.swing.*; |
| 8 |
import javax.swing.border.Border; |
| 9 |
import javax.swing.plaf.basic.BasicInternalFrameUI; |
| 10 |
|
| 11 |
import org.pushingpixels.lafwidget.LafWidgetUtilities; |
| 12 |
|
| 13 |
public class InternalFramePreviewPainter extends DefaultPreviewPainter { |
| 14 |
/** |
| 15 |
* Snapshot map. |
| 16 |
*/ |
| 17 |
private static WeakHashMap snapshots = new WeakHashMap(); |
| 18 |
|
| 19 |
public static void refreshSnaphost(JInternalFrame frame) { |
| 20 |
if (!frame.isShowing()) |
| 21 |
return; |
| 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(); |
| 29 |
|
| 30 |
int dx = 0; |
| 31 |
int dy = 0; |
| 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); |
| 40 |
|
| 41 |
BasicInternalFrameUI frameUI = (BasicInternalFrameUI) frame.getUI(); |
| 42 |
JComponent frameTitlePane = frameUI.getNorthPane(); |
| 43 |
|
| 44 |
if (frameTitlePane != null) { |
| 45 |
dy += frameTitlePane.getHeight(); |
| 46 |
frameHeight -= frameTitlePane.getHeight(); |
| 47 |
} |
| 48 |
|
| 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); |
| 57 |
|
| 58 |
int maxWidth = UIManager.getInt("DesktopIcon.width"); |
| 59 |
int maxHeight = maxWidth; |
| 60 |
|
| 61 |
// check if need to scale down |
| 62 |
double coef = Math.min((double) maxWidth / (double) frameWidth, |
| 63 |
(double) maxHeight / (double) frameHeight); |
| 64 |
if (coef < 1.0) { |
| 65 |
int sdWidth = (int) (coef * frameWidth); |
| 66 |
// int sdHeight = (int) (coef * frameHeight); |
| 67 |
// BufferedImage scaledDown = new BufferedImage(sdWidth, |
| 68 |
// sdHeight, |
| 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( |
| 74 |
tempCanvas, sdWidth); |
| 75 |
// System.out.println("Putting " + frame.hashCode() + " |
| 76 |
// -> " + scaledDown.hashCode()); |
| 77 |
snapshots.put(frame, scaledDown); |
| 78 |
} else { |
| 79 |
// System.out.println("Putting " + frame.hashCode() + " |
| 80 |
// -> " + snapshot.hashCode()); |
| 81 |
snapshots.put(frame, tempCanvas); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 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); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Dimension getPreviewWindowDimension(Container parent, |
| 97 |
Component component, int componentIndex) { |
| 98 |
return new Dimension(UIManager.getInt("DesktopIcon.width"), UIManager |
| 99 |
.getInt("DesktopIcon.width")); |
| 100 |
} |
| 101 |
} |