1 /* DefaultPreviewPanel.java --
2    Copyright (C) 2004 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package javax.swing.colorchooser;
40 
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Insets;
47 
48 import javax.swing.JColorChooser;
49 import javax.swing.JPanel;
50 import javax.swing.SwingUtilities;
51 import javax.swing.border.Border;
52 
53 /**
54  * This is the default preview panel for the JColorChooser. The default
55  * preview panel is responsible for displaying the currently selected color
56  * of the JColorChooser.
57  */
58 class DefaultPreviewPanel extends JPanel
59 {
60   /**
61    * This is the border around the preview panel.
62    */
63   class PreviewBorder implements Border
64   {
65     /** This is the value of the top, bottom, top, and right inset. */
66     private static final int edge = 20;
67 
68     /**
69      * This is the distance from the top left corner of the border to the
70      * text.
71      */
72     private static final int lead = 5;
73 
74     /** This is the horizontal gap between the text and the border. */
75     private static final int gap = 3;
76 
77     /**
78      * This method returns the border insets for the given Component.
79      *
80      * @param c The Component to retrieve insets for.
81      *
82      * @return The insets for the given Component.
83      */
getBorderInsets(Component c)84     public Insets getBorderInsets(Component c)
85     {
86       return new Insets(edge, edge, edge, edge);
87     }
88 
89     /**
90      * This method returns whether the border is responsible for painting its
91      * own background.
92      *
93      * @return Whether the border is responsible for painting its own
94      *         background.
95      */
isBorderOpaque()96     public boolean isBorderOpaque()
97     {
98       return true;
99     }
100 
101     /**
102      * This method paints the border for the given component with the graphics
103      * object using the given properties.
104      *
105      * @param c The Component to paint the border for.
106      * @param g The Graphics object to paint with.
107      * @param x The x location to paint at.
108      * @param y The y location to paint at.
109      * @param width The width of the component.
110      * @param height The height of the component.
111      */
paintBorder(Component c, Graphics g, int x, int y, int width, int height)112     public void paintBorder(Component c, Graphics g, int x, int y, int width,
113                             int height)
114     {
115       Color saved = g.getColor();
116       FontMetrics fm = g.getFontMetrics();
117 
118       g.setColor(Color.BLACK);
119       g.drawLine(x + edge / 2, y + edge / 2, x + edge / 2,
120                  y + height - edge / 2);
121       g.drawLine(x + edge / 2, y + height - edge / 2, x + width - edge / 2,
122                  y + height - edge / 2);
123       g.drawLine(x + width - edge / 2, y + edge / 2, x + width - edge / 2,
124                  y + height - edge / 2);
125       g.drawLine(x + edge / 2, y + edge / 2, x + edge / 2 + lead, y + edge / 2);
126 
127       int strwidth = fm.stringWidth("Preview");
128 
129       g.drawString("Preview", x + edge / 2 + lead + gap,
130                    y + edge / 2 + fm.getAscent() / 2);
131 
132       g.drawLine(x + lead + edge / 2 + strwidth + gap * 2, y + edge / 2,
133                  x + width - edge / 2, y + edge / 2);
134 
135       g.setColor(saved);
136     }
137   }
138 
139   /** A standard large gap size. */
140   private static int largeGap = 6;
141 
142   /** A standard small gap size. */
143   private static int smallGap = 2;
144 
145   /** The size of each side of the square. */
146   private static int squareSize = 36;
147 
148   /** This padding between the text and the edge of its box. */
149   private static int textPadding = 4;
150 
151   /** The width of the right most rectangles. */
152   private static int rightSideRectWidth = 60;
153 
154   /** The sample text. */
155   private static String sample = "Sample Text   Sample Text";
156 
157   /**
158    * Creates a new DefaultPreviewPanel object.
159    */
DefaultPreviewPanel()160   DefaultPreviewPanel()
161   {
162     super();
163     setBorder(new PreviewBorder());
164   }
165 
166   /**
167    * This method paints the default preview panel with the given Graphics
168    * object.
169    *
170    * @param g The Graphics object.
171    */
paint(Graphics g)172   public void paint(Graphics g)
173   {
174     super.paint(g);
175     Color currentColor = null;
176     JColorChooser chooser = (JColorChooser) SwingUtilities.getAncestorOfClass(JColorChooser.class,
177                                                                               this);
178     if (chooser != null)
179       currentColor = chooser.getColor();
180 
181     Color saved = g.getColor();
182     Insets insets = getInsets();
183 
184     int down = insets.top + squareSize + largeGap;
185     int currX = insets.left;
186 
187     paintSquare(g, currX, insets.top, Color.WHITE, currentColor, Color.WHITE,
188                 -1, -1, -1);
189     paintSquare(g, currX, down, currentColor, null, null, -1, -1, -1);
190 
191     currX += squareSize + largeGap;
192 
193     paintSquare(g, currX, insets.top, Color.BLACK, currentColor, Color.WHITE,
194                 -1, -1, -1);
195     paintSquare(g, currX, down, Color.WHITE, currentColor, null, -1, -1, -1);
196 
197     currX += squareSize + largeGap;
198 
199     paintSquare(g, currX, insets.top, Color.WHITE, currentColor, Color.BLACK,
200                 -1, -1, -1);
201     paintSquare(g, currX, down, Color.BLACK, currentColor, null, -1, -1, -1);
202 
203     FontMetrics fm = g.getFontMetrics();
204     int strWidth = fm.stringWidth(sample);
205     int strHeight = fm.getHeight();
206 
207     currX += squareSize + largeGap;
208 
209     int boxWidth = 2 * textPadding + strWidth;
210     int boxHeight = 2 * textPadding + strHeight;
211 
212     int first = insets.top + textPadding;
213     int second = insets.top + boxHeight + smallGap;
214     int third = insets.top + 2 * (boxHeight + smallGap);
215 
216     g.setColor(Color.WHITE);
217     g.fillRect(currX, third, boxWidth, boxHeight);
218 
219     g.setColor(currentColor);
220     g.drawString(sample, currX + textPadding,
221                  first + textPadding + fm.getAscent());
222 
223     g.fillRect(currX, second, boxWidth, boxHeight);
224 
225     g.drawString(sample, currX + textPadding,
226                  third + textPadding + fm.getAscent());
227 
228     g.setColor(Color.BLACK);
229     g.drawString(sample, currX + textPadding,
230                  second + textPadding + fm.getAscent());
231 
232     currX += boxWidth + largeGap;
233 
234     g.setColor(Color.WHITE);
235     g.fillRect(currX, insets.top, rightSideRectWidth, squareSize
236                + largeGap / 2);
237 
238     g.setColor(currentColor);
239     g.fillRect(currX, insets.top + squareSize + largeGap / 2,
240                rightSideRectWidth, squareSize + largeGap / 2);
241 
242     g.setColor(saved);
243   }
244 
245   /**
246    * This method creates and paints a square. The square has two smaller
247    * squares inside of it. Each of the three squares has their sizes
248    * determined by the size arguments. If the size is not given (by passing
249    * in -1), then the size is determined automatically.
250    *
251    * @param g The Graphics object to paint with.
252    * @param x The x location to paint at.
253    * @param y The y location to paint at.
254    * @param first The color of the first square.
255    * @param second The color of the second square.
256    * @param third The color of the third square.
257    * @param firstSize The size of the first square.
258    * @param secondSize The size of the second square.
259    * @param thirdSize The size of the third square.
260    */
paintSquare(Graphics g, int x, int y, Color first, Color second, Color third, int firstSize, int secondSize, int thirdSize)261   private void paintSquare(Graphics g, int x, int y, Color first,
262                            Color second, Color third, int firstSize,
263                            int secondSize, int thirdSize)
264   {
265     Color saved = g.getColor();
266     if (firstSize == -1)
267       firstSize = squareSize;
268     if (secondSize == -1)
269       secondSize = squareSize * 2 / 3;
270     if (thirdSize == -1)
271       thirdSize = squareSize / 3;
272     int secondOffset = (firstSize - secondSize) / 2;
273     int thirdOffset = (firstSize - thirdSize) / 2;
274 
275     if (first == null)
276       return;
277     g.setColor(first);
278     g.fillRect(x, y, firstSize, firstSize);
279     if (second == null)
280       return;
281     g.setColor(second);
282     g.fillRect(x + secondOffset, y + secondOffset, secondSize, secondSize);
283     if (third == null)
284       return;
285     g.setColor(third);
286     g.fillRect(x + thirdOffset, y + thirdOffset, thirdSize, thirdSize);
287 
288     g.setColor(saved);
289   }
290 
291   /**
292    * This method returns the preferred size of the default preview panel.
293    *
294    * @return The preferred size of the default preview panel.
295    */
getPreferredSize()296   public Dimension getPreferredSize()
297   {
298     Graphics g = getGraphics();
299     FontMetrics fm = g.getFontMetrics();
300     g.dispose();
301 
302     int strWidth = fm.stringWidth(sample);
303     int strHeight = fm.getHeight();
304 
305     int h1 = (strHeight + 2 * textPadding) * 3 + 2 * smallGap;
306     int h2 = 2 * squareSize + largeGap;
307 
308     int height = Math.max(h1, h2);
309 
310     int width = 3 * (squareSize + largeGap) + strWidth + 2 * textPadding
311                 + largeGap + rightSideRectWidth;
312 
313     Insets insets = getInsets();
314 
315     return new Dimension(width + insets.right + insets.left,
316                          height + insets.top + insets.bottom);
317   }
318 }
319