2
|
1 |
/*
|
|
2 |
* Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.swing.colorchooser;
|
|
27 |
|
|
28 |
import javax.swing.*;
|
|
29 |
import javax.swing.border.*;
|
|
30 |
import javax.swing.event.*;
|
|
31 |
import javax.swing.text.*;
|
|
32 |
import java.awt.*;
|
|
33 |
import java.awt.image.*;
|
|
34 |
import java.awt.event.*;
|
|
35 |
import java.beans.PropertyChangeEvent;
|
|
36 |
import java.beans.PropertyChangeListener;
|
|
37 |
import java.io.Serializable;
|
|
38 |
import sun.swing.SwingUtilities2;
|
|
39 |
|
|
40 |
|
|
41 |
/**
|
|
42 |
* The standard preview panel for the color chooser.
|
|
43 |
* <p>
|
|
44 |
* <strong>Warning:</strong>
|
|
45 |
* Serialized objects of this class will not be compatible with
|
|
46 |
* future Swing releases. The current serialization support is
|
|
47 |
* appropriate for short term storage or RMI between applications running
|
|
48 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
49 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
50 |
* has been added to the <code>java.beans</code> package.
|
|
51 |
* Please see {@link java.beans.XMLEncoder}.
|
|
52 |
*
|
|
53 |
* @author Steve Wilson
|
|
54 |
* @see JColorChooser
|
|
55 |
*/
|
|
56 |
class DefaultPreviewPanel extends JPanel {
|
|
57 |
|
|
58 |
private int squareSize = 25;
|
|
59 |
private int squareGap = 5;
|
|
60 |
private int innerGap = 5;
|
|
61 |
|
|
62 |
|
|
63 |
private int textGap = 5;
|
|
64 |
private Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
|
|
65 |
private String sampleText;
|
|
66 |
|
|
67 |
private int swatchWidth = 50;
|
|
68 |
|
|
69 |
private Color oldColor = null;
|
|
70 |
|
|
71 |
private JColorChooser getColorChooser() {
|
|
72 |
return (JColorChooser)SwingUtilities.getAncestorOfClass(
|
|
73 |
JColorChooser.class, this);
|
|
74 |
}
|
|
75 |
|
|
76 |
public Dimension getPreferredSize() {
|
|
77 |
JComponent host = getColorChooser();
|
|
78 |
if (host == null) {
|
|
79 |
host = this;
|
|
80 |
}
|
|
81 |
FontMetrics fm = host.getFontMetrics(getFont());
|
|
82 |
|
|
83 |
int ascent = fm.getAscent();
|
|
84 |
int height = fm.getHeight();
|
|
85 |
int width = SwingUtilities2.stringWidth(host, fm, getSampleText());
|
|
86 |
|
|
87 |
int y = height*3 + textGap*3;
|
|
88 |
int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3;
|
|
89 |
return new Dimension( x,y );
|
|
90 |
}
|
|
91 |
|
|
92 |
public void paintComponent(Graphics g) {
|
|
93 |
if (oldColor == null)
|
|
94 |
oldColor = getForeground();
|
|
95 |
|
|
96 |
g.setColor(getBackground());
|
|
97 |
g.fillRect(0,0,getWidth(),getHeight());
|
|
98 |
|
|
99 |
if (this.getComponentOrientation().isLeftToRight()) {
|
|
100 |
int squareWidth = paintSquares(g, 0);
|
|
101 |
int textWidth = paintText(g, squareWidth);
|
|
102 |
paintSwatch(g, squareWidth + textWidth);
|
|
103 |
} else {
|
|
104 |
int swatchWidth = paintSwatch(g, 0);
|
|
105 |
int textWidth = paintText(g, swatchWidth);
|
|
106 |
paintSquares(g , swatchWidth + textWidth);
|
|
107 |
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
private int paintSwatch(Graphics g, int offsetX) {
|
|
112 |
int swatchX = offsetX;
|
|
113 |
g.setColor(oldColor);
|
|
114 |
g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap/2));
|
|
115 |
g.setColor(getForeground());
|
|
116 |
g.fillRect(swatchX, (squareSize) + (squareGap/2), swatchWidth, (squareSize) + (squareGap/2) );
|
|
117 |
return (swatchX+swatchWidth);
|
|
118 |
}
|
|
119 |
|
|
120 |
private int paintText(Graphics g, int offsetX) {
|
|
121 |
g.setFont(getFont());
|
|
122 |
JComponent host = getColorChooser();
|
|
123 |
if (host == null) {
|
|
124 |
host = this;
|
|
125 |
}
|
|
126 |
FontMetrics fm = SwingUtilities2.getFontMetrics(host, g);
|
|
127 |
|
|
128 |
int ascent = fm.getAscent();
|
|
129 |
int height = fm.getHeight();
|
|
130 |
int width = SwingUtilities2.stringWidth(host, fm, getSampleText());
|
|
131 |
|
|
132 |
int textXOffset = offsetX + textGap;
|
|
133 |
|
|
134 |
Color color = getForeground();
|
|
135 |
|
|
136 |
g.setColor(color);
|
|
137 |
|
|
138 |
SwingUtilities2.drawString(host, g, getSampleText(),textXOffset+(textGap/2),
|
|
139 |
ascent+2);
|
|
140 |
|
|
141 |
g.fillRect(textXOffset,
|
|
142 |
( height) + textGap,
|
|
143 |
width + (textGap),
|
|
144 |
height +2);
|
|
145 |
|
|
146 |
g.setColor(Color.black);
|
|
147 |
SwingUtilities2.drawString(host, g, getSampleText(),
|
|
148 |
textXOffset+(textGap/2),
|
|
149 |
height+ascent+textGap+2);
|
|
150 |
|
|
151 |
|
|
152 |
g.setColor(Color.white);
|
|
153 |
|
|
154 |
g.fillRect(textXOffset,
|
|
155 |
( height + textGap) * 2,
|
|
156 |
width + (textGap),
|
|
157 |
height +2);
|
|
158 |
|
|
159 |
g.setColor(color);
|
|
160 |
SwingUtilities2.drawString(host, g, getSampleText(),
|
|
161 |
textXOffset+(textGap/2),
|
|
162 |
((height+textGap) * 2)+ascent+2);
|
|
163 |
|
|
164 |
return width + textGap*3;
|
|
165 |
|
|
166 |
}
|
|
167 |
|
|
168 |
private int paintSquares(Graphics g, int offsetX) {
|
|
169 |
|
|
170 |
int squareXOffset = offsetX;
|
|
171 |
Color color = getForeground();
|
|
172 |
|
|
173 |
g.setColor(Color.white);
|
|
174 |
g.fillRect(squareXOffset,0,squareSize,squareSize);
|
|
175 |
g.setColor(color);
|
|
176 |
g.fillRect(squareXOffset+innerGap,
|
|
177 |
innerGap,
|
|
178 |
squareSize - (innerGap*2),
|
|
179 |
squareSize - (innerGap*2));
|
|
180 |
g.setColor(Color.white);
|
|
181 |
g.fillRect(squareXOffset+innerGap*2,
|
|
182 |
innerGap*2,
|
|
183 |
squareSize - (innerGap*4),
|
|
184 |
squareSize - (innerGap*4));
|
|
185 |
|
|
186 |
g.setColor(color);
|
|
187 |
g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize);
|
|
188 |
|
|
189 |
g.translate(squareSize+squareGap, 0);
|
|
190 |
g.setColor(Color.black);
|
|
191 |
g.fillRect(squareXOffset,0,squareSize,squareSize);
|
|
192 |
g.setColor(color);
|
|
193 |
g.fillRect(squareXOffset+innerGap,
|
|
194 |
innerGap,
|
|
195 |
squareSize - (innerGap*2),
|
|
196 |
squareSize - (innerGap*2));
|
|
197 |
g.setColor(Color.white);
|
|
198 |
g.fillRect(squareXOffset+innerGap*2,
|
|
199 |
innerGap*2,
|
|
200 |
squareSize - (innerGap*4),
|
|
201 |
squareSize - (innerGap*4));
|
|
202 |
g.translate(-(squareSize+squareGap), 0);
|
|
203 |
|
|
204 |
g.translate(squareSize+squareGap, squareSize+squareGap);
|
|
205 |
g.setColor(Color.white);
|
|
206 |
g.fillRect(squareXOffset,0,squareSize,squareSize);
|
|
207 |
g.setColor(color);
|
|
208 |
g.fillRect(squareXOffset+innerGap,
|
|
209 |
innerGap,
|
|
210 |
squareSize - (innerGap*2),
|
|
211 |
squareSize - (innerGap*2));
|
|
212 |
g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
g.translate((squareSize+squareGap)*2, 0);
|
|
217 |
g.setColor(Color.white);
|
|
218 |
g.fillRect(squareXOffset,0,squareSize,squareSize);
|
|
219 |
g.setColor(color);
|
|
220 |
g.fillRect(squareXOffset+innerGap,
|
|
221 |
innerGap,
|
|
222 |
squareSize - (innerGap*2),
|
|
223 |
squareSize - (innerGap*2));
|
|
224 |
g.setColor(Color.black);
|
|
225 |
g.fillRect(squareXOffset+innerGap*2,
|
|
226 |
innerGap*2,
|
|
227 |
squareSize - (innerGap*4),
|
|
228 |
squareSize - (innerGap*4));
|
|
229 |
g.translate(-((squareSize+squareGap)*2), 0);
|
|
230 |
|
|
231 |
g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
|
|
232 |
g.setColor(Color.black);
|
|
233 |
g.fillRect(squareXOffset,0,squareSize,squareSize);
|
|
234 |
g.setColor(color);
|
|
235 |
g.fillRect(squareXOffset+innerGap,
|
|
236 |
innerGap,
|
|
237 |
squareSize - (innerGap*2),
|
|
238 |
squareSize - (innerGap*2));
|
|
239 |
g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
|
|
240 |
|
|
241 |
return (squareSize*3+squareGap*2);
|
|
242 |
|
|
243 |
}
|
|
244 |
|
|
245 |
private String getSampleText() {
|
|
246 |
if (this.sampleText == null) {
|
|
247 |
this.sampleText = UIManager.getString("ColorChooser.sampleText", getLocale());
|
|
248 |
}
|
|
249 |
return this.sampleText;
|
|
250 |
}
|
|
251 |
}
|