Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/preview/DefaultPreviewPainter.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.preview;
31
32 import java.awt.*;
33 import java.awt.image.BufferedImage;
34
35 import javax.swing.JViewport;
36
37 import org.pushingpixels.lafwidget.LafWidgetUtilities;
38
39 /**
40 * Default implementation of the component preview painter. The component
41 * preview is a scaled-down (as necessary) thumbnail of the relevant component.
42 *
43 * @author Kirill Grouchnikov
44 */
45 public class DefaultPreviewPainter extends PreviewPainter {
46 /*
47 * (non-Javadoc)
48 *
49 * @see org.pushingpixels.lafwidget.PreviewPainter#hasPreview(java.awt.Container,
50 * java.awt.Component, int)
51 */
52 @Override
53 public boolean hasPreview(Container parent, Component component,
54 int componentIndex) {
55 return (component != null);
56 }
57
58 /*
59 * (non-Javadoc)
60 *
61 * @see org.pushingpixels.lafwidget.preview.PreviewPainter#previewComponent(java.awt.Container,
62 * java.awt.Component, int, java.awt.Graphics, int, int, int, int)
63 */
64 @Override
65 public void previewComponent(Container parent, Component component,
66 int componentIndex, Graphics g, int x, int y, int w, int h) {
67 if (component == null)
68 return;
69 int compWidth = component.getWidth();
70 int compHeight = component.getHeight();
71
72 if ((compWidth > 0) && (compHeight > 0)) {
73 // draw component
74 BufferedImage tempCanvas = new BufferedImage(compWidth, compHeight,
75 BufferedImage.TYPE_INT_ARGB);
76 Graphics tempCanvasGraphics = tempCanvas.getGraphics();
77 component.paint(tempCanvasGraphics);
78
79 // check if need to scale down
80 double coef = Math.min((double) w / (double) compWidth, (double) h
81 / (double) compHeight);
82 if (coef < 1.0) {
83 int sdWidth = (int) (coef * compWidth);
84 int sdHeight = (int) (coef * compHeight);
85 int dx = x + (w - sdWidth) / 2;
86 int dy = y + (h - sdHeight) / 2;
87
88 g.drawImage(LafWidgetUtilities.createThumbnail(tempCanvas,
89 sdWidth), dx, dy, null);
90
91 } else {
92 g.drawImage(tempCanvas, x, y, null);
93 }
94 }
95 }
96
97 /*
98 * (non-Javadoc)
99 *
100 * @see org.pushingpixels.lafwidget.PreviewPainter#hasPreviewWindow(java.awt.Container,
101 * java.awt.Component, int)
102 */
103 @Override
104 public boolean hasPreviewWindow(Container parent, Component component,
105 int componentIndex) {
106 return true;
107 }
108
109 /*
110 * (non-Javadoc)
111 *
112 * @see org.pushingpixels.lafwidget.preview.PreviewPainter#getPreviewWindowDimension(java.awt.Container,
113 * java.awt.Component, int)
114 */
115 @Override
116 public Dimension getPreviewWindowDimension(Container parent,
117 Component component, int componentIndex) {
118 Dimension superResult = super.getPreviewWindowDimension(parent,
119 component, componentIndex);
120 if (parent instanceof JViewport) {
121 Rectangle viewportRect = ((JViewport) parent).getViewRect();
122 int width = Math.min(viewportRect.width / 3, superResult.width);
123 int height = Math.min(viewportRect.height / 3, superResult.height);
124 return new Dimension(width, height);
125 }
126 return superResult;
127 }
128 }