1 /*
2 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
6 * conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
25 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
26 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
27 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
28 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
29 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
30 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
31 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
32 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 *
34 * You acknowledge that this software is not designed, licensed or
35 * intended for use in the design, construction, operation or
36 * maintenance of any nuclear facility.
37 */
38
39 package javax.swing.beaninfo;
40
41 import javax.swing;
42 import javax.swing.colorchooser;
43 import javax.swing.border;
44 import javax.swing.event;
45 import java.awt;
46 import java.awt.image;
47 import java.awt.event;
48 import java.beans.PropertyChangeEvent;
49 import java.beans.PropertyChangeListener;
50 import java.io.Serializable;
51
52
53 /**
54 * Modified from the standard color swatch chooser.
55 * <p>
56 * <strong>Warning:</strong>
57 * Serialized objects of this class will not be compatible with
58 * future Swing releases. The current serialization support is appropriate
59 * for short term storage or RMI between applications running the same
60 * version of Swing. A future release of Swing will provide support for
61 * long term persistence.
62 *
63 * @version @(#)SwatchChooserPanel.java 1.3 1.3
64 * @author Steve Wilson
65 */
66 public class SwatchChooserPanel extends AbstractColorChooserPanel {
67
68 SwatchPanel swatchPanel;
69 RecentSwatchPanel recentSwatchPanel;
70 MouseListener mainSwatchListener;
71 MouseListener recentSwatchListener;
72 SwingColorEditor chooser;
73 ChooserComboPopup popup;
74
75 private static String recentStr = UIManager.getString("ColorChooser.swatchesRecentText");
76
77 public SwatchChooserPanel(SwingColorEditor c, ChooserComboPopup p) {
78 super();
79 this.chooser = c;
80 this.popup = p;
81 }
82
83 public String getDisplayName() {
84 return UIManager.getString("ColorChooser.swatchesNameText");
85 }
86
87 public Icon getSmallDisplayIcon() {
88 return null;
89 }
90
91 public Icon getLargeDisplayIcon() {
92 return null;
93 }
94
95 /**
96 * The background color, foreground color, and font are already set to the
97 * defaults from the defaults table before this method is called.
98 */
99 public void installChooserPanel(JColorChooser enclosingChooser) {
100 super.installChooserPanel(enclosingChooser);
101 }
102
103 protected void buildChooser() {
104
105 JPanel superHolder = new JPanel();
106 superHolder.setLayout(new BoxLayout(superHolder, BoxLayout.Y_AXIS)); // new BorderLayout());
107 swatchPanel = new MainSwatchPanel();
108 swatchPanel.getAccessibleContext().setAccessibleName(getDisplayName());
109
110 recentSwatchPanel = new RecentSwatchPanel();
111 recentSwatchPanel.getAccessibleContext().setAccessibleName(recentStr);
112
113 mainSwatchListener = new MainSwatchListener();
114 swatchPanel.addMouseListener(mainSwatchListener);
115 recentSwatchListener = new RecentSwatchListener();
116 recentSwatchPanel.addMouseListener(recentSwatchListener);
117
118
119 JPanel mainHolder = new JPanel(new BorderLayout());
120 Border border = new CompoundBorder( new LineBorder(Color.black),
121 new LineBorder(Color.white) );
122 mainHolder.setBorder(border);
123 mainHolder.add(swatchPanel, BorderLayout.CENTER);
124 // mainHolder.add(recentSwatchPanel, BorderLayout.NORTH);
125
126 JPanel recentHolder = new JPanel( new BorderLayout() );
127 recentHolder.setBorder(border);
128 recentHolder.add(recentSwatchPanel, BorderLayout.CENTER);
129
130 superHolder.add(recentHolder); // , BorderLayout.NORTH);
131 superHolder.add(Box.createRigidArea(new Dimension(0,3)));
132 superHolder.add( mainHolder); // , BorderLayout.CENTER );
133
134
135
136 // JPanel recentLabelHolder = new JPanel(new BorderLayout());
137 // recentLabelHolder.add(recentHolder, BorderLayout.CENTER);
138 // JLabel l = new JLabel(recentStr);
139 // l.setLabelFor(recentSwatchPanel);
140 // recentLabelHolder.add(l, BorderLayout.NORTH);
141 // JPanel recentHolderHolder = new JPanel(new BorderLayout()); // was centerlayout
142 // recentHolderHolder.setBorder(new EmptyBorder(2,10,2,2));
143 // recentHolderHolder.add(recentLabelHolder);
144
145 // superHolder.add( recentHolderHolder, BorderLayout.NORTH );
146
147 add(superHolder);
148
149 }
150
151 public void uninstallChooserPanel(JColorChooser enclosingChooser) {
152 super.uninstallChooserPanel(enclosingChooser);
153 swatchPanel.removeMouseListener(mainSwatchListener);
154 swatchPanel = null;
155 mainSwatchListener = null;
156 removeAll(); // strip out all the sub-components
157 }
158
159 public void updateChooser() {
160
161 }
162
163 class RecentSwatchListener extends MouseAdapter implements Serializable {
164 public void mousePressed(MouseEvent e) {
165 Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
166 // getColorSelectionModel().setSelectedColor(color);
167 chooser.setValue(color);
168 popup.setVisible(false);
169
170 }
171 }
172
173
174 class MainSwatchListener extends MouseAdapter implements Serializable {
175 public void mousePressed(MouseEvent e) {
176 Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
177 // getColorSelectionModel().setSelectedColor(color);
178 chooser.setValue(color);
179 recentSwatchPanel.setMostRecentColor(color);
180 popup.setVisible(false);
181 }
182 }
183
184 }
185
186
187
188 class SwatchPanel extends JPanel {
189 protected Color[] colors;
190 protected Dimension swatchSize = new Dimension(13,13);
191 protected Dimension numSwatches;
192 protected Dimension gap;
193
194 public SwatchPanel() {
195 initValues();
196 initColors();
197 setToolTipText(""); // register for events
198 setOpaque(true);
199 setBackground(Color.gray);
200 setFocusable(false);
201 }
202
203 public boolean isFocusAble() {
204 return false;
205 }
206
207 protected void initValues() {
208 }
209
210 public void paintComponent(Graphics g) {
211 g.setColor(getBackground());
212 g.fillRect(0,0,getWidth(), getHeight());
213 for (int row = 0; row < numSwatches.height; row++) {
214 for (int column = 0; column < numSwatches.width; column++) {
215 g.setColor( getColorForCell(column, row) );
216 int x = column * (swatchSize.width + gap.width);
217 int y = row * (swatchSize.height + gap.height);
218 g.fillRect( x, y, swatchSize.width, swatchSize.height);
219 g.setColor(Color.black);
220 g.drawLine( x+swatchSize.width-1, y, x+swatchSize.width-1, y+swatchSize.height-1);
221 g.drawLine( x, y+swatchSize.height-1, x+swatchSize.width-1, y+swatchSize.width-1);
222 }
223 }
224 }
225
226 public Dimension getPreferredSize() {
227 int x = numSwatches.width * (swatchSize.width + gap.width) -1;
228 int y = numSwatches.height * (swatchSize.height + gap.height) -1;
229 return new Dimension( x, y );
230 }
231
232 protected void initColors() {
233 }
234
235 public String getToolTipText(MouseEvent e) {
236 Color color = getColorForLocation(e.getX(), e.getY());
237 return color.getRed()+", "+ color.getGreen() + ", " + color.getBlue();
238 }
239
240 public Color getColorForLocation( int x, int y ) {
241 int column = x / (swatchSize.width + gap.width);
242 int row = y / (swatchSize.height + gap.height);
243 return getColorForCell(column, row);
244 }
245
246 private Color getColorForCell( int column, int row) {
247 return colors[ (row * numSwatches.width) + column ]; // (STEVE) - change data orientation here
248 }
249 }
250
251 class RecentSwatchPanel extends SwatchPanel {
252 protected void initValues() {
253 // swatchSize = UIManager.getDimension("ColorChooser.swatchesRecentSwatchSize");
254 swatchSize = new Dimension(13,13);
255 numSwatches = new Dimension( 6, 1 );
256 gap = new Dimension(1, 1);
257 }
258
259
260 protected void initColors() {
261 Color defaultRecentColor = UIManager.getColor("ColorChooser.swatchesDefaultRecentColor");
262 int numColors = numSwatches.width * numSwatches.height;
263
264 colors = new Color[numColors];
265 for (int i = 0; i < numColors ; i++) {
266 colors[i] = defaultRecentColor;
267 }
268 }
269
270 public void setMostRecentColor(Color c) {
271
272 System.arraycopy( colors, 0, colors, 1, colors.length-1);
273 colors[0] = c;
274 repaint();
275 }
276 }
277
278 class MainSwatchPanel extends SwatchPanel {
279 protected void initValues() {
280 // swatchSize = UIManager.getDimension("ColorChooser.swatchesSwatchSize");
281 swatchSize = new Dimension(13,13);
282 // numSwatches = new Dimension( 31, 10 );
283 numSwatches = new Dimension(6,6);
284 gap = new Dimension(1, 1);
285 }
286
287 protected void initColors() {
288 int[] rawValues = initRawValues();
289 int numColors = rawValues.length / 3;
290
291 colors = new Color[numColors];
292 for (int i = 0; i < numColors ; i++) {
293 colors[i] = new Color( rawValues[(i*3)], rawValues[(i*3)+1], rawValues[(i*3)+2] );
294 }
295 }
296
297 private int[] initRawValues() {
298
299 int[] rawValues = { 255, 255, 255,
300 192,192,192,
301 128,128,128,
302 64,64,64,
303 0,0,0,
304 255,0,0,
305 100,100,100,
306 255,175,175,
307 255,200,0,
308 255,255,0,
309 0,255,0,
310 255,0,255,
311 0,255,255,
312 0,0,255, // repeat here
313 255, 255, 255,
314 192,192,192,
315 128,128,128,
316 64,64,64,
317 0,0,0,
318 255,0,0,
319 100,100,100,
320 255,175,175,
321 255,200,0,
322 255,255,0,
323 0,255,0,
324 255,0,255,
325 0,255,255,
326 0,0,255, // repeat here
327 100,100,100,
328 255,175,175,
329 255,200,0,
330 255,255,0,
331 0,255,0,
332 255,0,255,
333 0,255,255,
334 0,0,255,
335 };
336 return rawValues;
337 }
338 }