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 height = fm.getHeight();
  85         int width = SwingUtilities2.getTextUIDrawing(host)
  86                 .getStringWidth(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.getTextUIDrawing(host)
 132                 .getStringWidth(host, fm, getSampleText());
 133 
 134         int textXOffset = offsetX + textGap;
 135 
 136         Color color = getForeground();
 137 
 138         g.setColor(color);
 139 
 140         SwingUtilities2.getTextUIDrawing(host)
 141                 .drawString(host, g, getSampleText(),textXOffset+(textGap/2),
 142                             ascent+2);
 143 
 144         g.fillRect(textXOffset,
 145                    ( height) + textGap,
 146                    width + (textGap),
 147                    height +2);
 148 
 149         g.setColor(Color.black);
 150         SwingUtilities2.getTextUIDrawing(host)
 151                 .drawString(host, g, getSampleText(),
 152                             textXOffset+(textGap/2),
 153                             height+ascent+textGap+2);
 154 
 155 
 156         g.setColor(Color.white);
 157 
 158         g.fillRect(textXOffset,
 159                    ( height + textGap) * 2,
 160                    width + (textGap),
 161                    height +2);
 162 
 163         g.setColor(color);
 164         SwingUtilities2.getTextUIDrawing(host)
 165                 .drawString(host, g, getSampleText(),
 166                             textXOffset+(textGap/2),
 167                             ((height+textGap) * 2)+ascent+2);
 168 
 169         return width + textGap*3;
 170 
 171     }
 172 
 173     private int paintSquares(Graphics g, int offsetX) {
 174 
 175         int squareXOffset = offsetX;
 176         Color color = getForeground();
 177 
 178         g.setColor(Color.white);
 179         g.fillRect(squareXOffset,0,squareSize,squareSize);
 180         g.setColor(color);
 181         g.fillRect(squareXOffset+innerGap,
 182                    innerGap,
 183                    squareSize - (innerGap*2),
 184                    squareSize - (innerGap*2));
 185         g.setColor(Color.white);
 186         g.fillRect(squareXOffset+innerGap*2,
 187                    innerGap*2,
 188                    squareSize - (innerGap*4),
 189                    squareSize - (innerGap*4));
 190 
 191         g.setColor(color);
 192         g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize);
 193 
 194         g.translate(squareSize+squareGap, 0);
 195         g.setColor(Color.black);
 196         g.fillRect(squareXOffset,0,squareSize,squareSize);
 197         g.setColor(color);
 198         g.fillRect(squareXOffset+innerGap,
 199                    innerGap,
 200                    squareSize - (innerGap*2),
 201                    squareSize - (innerGap*2));
 202         g.setColor(Color.white);
 203         g.fillRect(squareXOffset+innerGap*2,
 204                    innerGap*2,
 205                    squareSize - (innerGap*4),
 206                    squareSize - (innerGap*4));
 207         g.translate(-(squareSize+squareGap), 0);
 208 
 209         g.translate(squareSize+squareGap, squareSize+squareGap);
 210         g.setColor(Color.white);
 211         g.fillRect(squareXOffset,0,squareSize,squareSize);
 212         g.setColor(color);
 213         g.fillRect(squareXOffset+innerGap,
 214                    innerGap,
 215                    squareSize - (innerGap*2),
 216                    squareSize - (innerGap*2));
 217         g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
 218 
 219 
 220 
 221         g.translate((squareSize+squareGap)*2, 0);
 222         g.setColor(Color.white);
 223         g.fillRect(squareXOffset,0,squareSize,squareSize);
 224         g.setColor(color);
 225         g.fillRect(squareXOffset+innerGap,
 226                    innerGap,
 227                    squareSize - (innerGap*2),
 228                    squareSize - (innerGap*2));
 229         g.setColor(Color.black);
 230         g.fillRect(squareXOffset+innerGap*2,
 231                    innerGap*2,
 232                    squareSize - (innerGap*4),
 233                    squareSize - (innerGap*4));
 234         g.translate(-((squareSize+squareGap)*2), 0);
 235 
 236         g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
 237         g.setColor(Color.black);
 238         g.fillRect(squareXOffset,0,squareSize,squareSize);
 239         g.setColor(color);
 240         g.fillRect(squareXOffset+innerGap,
 241                    innerGap,
 242                    squareSize - (innerGap*2),
 243                    squareSize - (innerGap*2));
 244         g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
 245 
 246         return (squareSize*3+squareGap*2);
 247 
 248     }
 249 
 250     private String getSampleText() {
 251         if (this.sampleText == null) {
 252             this.sampleText = UIManager.getString("ColorChooser.sampleText", getLocale());
 253         }
 254         return this.sampleText;
 255     }
 256 }