1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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&trade;
  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 @SuppressWarnings("serial") // Same-version serialization only
  57 class DefaultPreviewPanel extends JPanel {
  58 
  59     private int squareSize = 25;
  60     private int squareGap = 5;
  61     private int innerGap = 5;
  62 
  63 
  64     private int textGap = 5;
  65     private Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
  66     private String sampleText;
  67 
  68     private int swatchWidth = 50;
  69 
  70     private Color oldColor = null;
  71 
  72     private JColorChooser getColorChooser() {
  73         return (JColorChooser)SwingUtilities.getAncestorOfClass(
  74                                    JColorChooser.class, this);
  75     }
  76 
  77     public Dimension getPreferredSize() {
  78         JComponent host = getColorChooser();
  79         if (host == null) {
  80             host = this;
  81         }
  82         FontMetrics fm = host.getFontMetrics(getFont());
  83 
  84         int ascent = fm.getAscent();
  85         int height = fm.getHeight();
  86         int width = SwingUtilities2.stringWidth(host, fm, getSampleText());
  87 
  88         int y = height*3 + textGap*3;
  89         int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3;
  90         return new Dimension( x,y );
  91     }
  92 
  93     public void paintComponent(Graphics g) {
  94         if (oldColor == null)
  95             oldColor = getForeground();
  96 
  97         g.setColor(getBackground());
  98         g.fillRect(0,0,getWidth(),getHeight());
  99 
 100         if (this.getComponentOrientation().isLeftToRight()) {
 101             int squareWidth = paintSquares(g, 0);
 102             int textWidth = paintText(g, squareWidth);
 103             paintSwatch(g, squareWidth + textWidth);
 104         } else {
 105             int swatchWidth = paintSwatch(g, 0);
 106             int textWidth = paintText(g, swatchWidth);
 107             paintSquares(g , swatchWidth + textWidth);
 108 
 109         }
 110     }
 111 
 112     private int paintSwatch(Graphics g, int offsetX) {
 113         int swatchX = offsetX;
 114         g.setColor(oldColor);
 115         g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap/2));
 116         g.setColor(getForeground());
 117         g.fillRect(swatchX, (squareSize) + (squareGap/2), swatchWidth, (squareSize) + (squareGap/2) );
 118         return (swatchX+swatchWidth);
 119     }
 120 
 121     private int paintText(Graphics g, int offsetX) {
 122         g.setFont(getFont());
 123         JComponent host = getColorChooser();
 124         if (host == null) {
 125             host = this;
 126         }
 127         FontMetrics fm = SwingUtilities2.getFontMetrics(host, g);
 128 
 129         int ascent = fm.getAscent();
 130         int height = fm.getHeight();
 131         int width = SwingUtilities2.stringWidth(host, fm, getSampleText());
 132 
 133         int textXOffset = offsetX + textGap;
 134 
 135         Color color = getForeground();
 136 
 137         g.setColor(color);
 138 
 139         SwingUtilities2.drawString(host, g, getSampleText(),textXOffset+(textGap/2),
 140                                    ascent+2);
 141 
 142         g.fillRect(textXOffset,
 143                    ( height) + textGap,
 144                    width + (textGap),
 145                    height +2);
 146 
 147         g.setColor(Color.black);
 148         SwingUtilities2.drawString(host, g, getSampleText(),
 149                      textXOffset+(textGap/2),
 150                      height+ascent+textGap+2);
 151 
 152 
 153         g.setColor(Color.white);
 154 
 155         g.fillRect(textXOffset,
 156                    ( height + textGap) * 2,
 157                    width + (textGap),
 158                    height +2);
 159 
 160         g.setColor(color);
 161         SwingUtilities2.drawString(host, g, getSampleText(),
 162                      textXOffset+(textGap/2),
 163                      ((height+textGap) * 2)+ascent+2);
 164 
 165         return width + textGap*3;
 166 
 167     }
 168 
 169     private int paintSquares(Graphics g, int offsetX) {
 170 
 171         int squareXOffset = offsetX;
 172         Color color = getForeground();
 173 
 174         g.setColor(Color.white);
 175         g.fillRect(squareXOffset,0,squareSize,squareSize);
 176         g.setColor(color);
 177         g.fillRect(squareXOffset+innerGap,
 178                    innerGap,
 179                    squareSize - (innerGap*2),
 180                    squareSize - (innerGap*2));
 181         g.setColor(Color.white);
 182         g.fillRect(squareXOffset+innerGap*2,
 183                    innerGap*2,
 184                    squareSize - (innerGap*4),
 185                    squareSize - (innerGap*4));
 186 
 187         g.setColor(color);
 188         g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize);
 189 
 190         g.translate(squareSize+squareGap, 0);
 191         g.setColor(Color.black);
 192         g.fillRect(squareXOffset,0,squareSize,squareSize);
 193         g.setColor(color);
 194         g.fillRect(squareXOffset+innerGap,
 195                    innerGap,
 196                    squareSize - (innerGap*2),
 197                    squareSize - (innerGap*2));
 198         g.setColor(Color.white);
 199         g.fillRect(squareXOffset+innerGap*2,
 200                    innerGap*2,
 201                    squareSize - (innerGap*4),
 202                    squareSize - (innerGap*4));
 203         g.translate(-(squareSize+squareGap), 0);
 204 
 205         g.translate(squareSize+squareGap, squareSize+squareGap);
 206         g.setColor(Color.white);
 207         g.fillRect(squareXOffset,0,squareSize,squareSize);
 208         g.setColor(color);
 209         g.fillRect(squareXOffset+innerGap,
 210                    innerGap,
 211                    squareSize - (innerGap*2),
 212                    squareSize - (innerGap*2));
 213         g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
 214 
 215 
 216 
 217         g.translate((squareSize+squareGap)*2, 0);
 218         g.setColor(Color.white);
 219         g.fillRect(squareXOffset,0,squareSize,squareSize);
 220         g.setColor(color);
 221         g.fillRect(squareXOffset+innerGap,
 222                    innerGap,
 223                    squareSize - (innerGap*2),
 224                    squareSize - (innerGap*2));
 225         g.setColor(Color.black);
 226         g.fillRect(squareXOffset+innerGap*2,
 227                    innerGap*2,
 228                    squareSize - (innerGap*4),
 229                    squareSize - (innerGap*4));
 230         g.translate(-((squareSize+squareGap)*2), 0);
 231 
 232         g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
 233         g.setColor(Color.black);
 234         g.fillRect(squareXOffset,0,squareSize,squareSize);
 235         g.setColor(color);
 236         g.fillRect(squareXOffset+innerGap,
 237                    innerGap,
 238                    squareSize - (innerGap*2),
 239                    squareSize - (innerGap*2));
 240         g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
 241 
 242         return (squareSize*3+squareGap*2);
 243 
 244     }
 245 
 246     private String getSampleText() {
 247         if (this.sampleText == null) {
 248             this.sampleText = UIManager.getString("ColorChooser.sampleText", getLocale());
 249         }
 250         return this.sampleText;
 251     }
 252 }