0N/A/*
2362N/A * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.colorchooser;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.text.*;
0N/Aimport java.awt.*;
0N/Aimport java.awt.image.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.beans.PropertyChangeEvent;
0N/Aimport java.beans.PropertyChangeListener;
0N/Aimport java.io.Serializable;
0N/Aimport sun.swing.SwingUtilities2;
0N/A
0N/A
0N/A/**
0N/A * The standard preview panel for the color chooser.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @author Steve Wilson
0N/A * @see JColorChooser
0N/A */
0N/Aclass DefaultPreviewPanel extends JPanel {
0N/A
0N/A private int squareSize = 25;
0N/A private int squareGap = 5;
0N/A private int innerGap = 5;
0N/A
0N/A
0N/A private int textGap = 5;
0N/A private Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
0N/A private String sampleText;
0N/A
0N/A private int swatchWidth = 50;
0N/A
0N/A private Color oldColor = null;
0N/A
0N/A private JColorChooser getColorChooser() {
0N/A return (JColorChooser)SwingUtilities.getAncestorOfClass(
0N/A JColorChooser.class, this);
0N/A }
0N/A
0N/A public Dimension getPreferredSize() {
0N/A JComponent host = getColorChooser();
0N/A if (host == null) {
0N/A host = this;
0N/A }
0N/A FontMetrics fm = host.getFontMetrics(getFont());
0N/A
0N/A int ascent = fm.getAscent();
0N/A int height = fm.getHeight();
0N/A int width = SwingUtilities2.stringWidth(host, fm, getSampleText());
0N/A
0N/A int y = height*3 + textGap*3;
0N/A int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3;
0N/A return new Dimension( x,y );
0N/A }
0N/A
0N/A public void paintComponent(Graphics g) {
0N/A if (oldColor == null)
0N/A oldColor = getForeground();
0N/A
0N/A g.setColor(getBackground());
0N/A g.fillRect(0,0,getWidth(),getHeight());
0N/A
0N/A if (this.getComponentOrientation().isLeftToRight()) {
0N/A int squareWidth = paintSquares(g, 0);
0N/A int textWidth = paintText(g, squareWidth);
0N/A paintSwatch(g, squareWidth + textWidth);
0N/A } else {
0N/A int swatchWidth = paintSwatch(g, 0);
0N/A int textWidth = paintText(g, swatchWidth);
0N/A paintSquares(g , swatchWidth + textWidth);
0N/A
0N/A }
0N/A }
0N/A
0N/A private int paintSwatch(Graphics g, int offsetX) {
0N/A int swatchX = offsetX;
0N/A g.setColor(oldColor);
0N/A g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap/2));
0N/A g.setColor(getForeground());
0N/A g.fillRect(swatchX, (squareSize) + (squareGap/2), swatchWidth, (squareSize) + (squareGap/2) );
0N/A return (swatchX+swatchWidth);
0N/A }
0N/A
0N/A private int paintText(Graphics g, int offsetX) {
0N/A g.setFont(getFont());
0N/A JComponent host = getColorChooser();
0N/A if (host == null) {
0N/A host = this;
0N/A }
0N/A FontMetrics fm = SwingUtilities2.getFontMetrics(host, g);
0N/A
0N/A int ascent = fm.getAscent();
0N/A int height = fm.getHeight();
0N/A int width = SwingUtilities2.stringWidth(host, fm, getSampleText());
0N/A
0N/A int textXOffset = offsetX + textGap;
0N/A
0N/A Color color = getForeground();
0N/A
0N/A g.setColor(color);
0N/A
0N/A SwingUtilities2.drawString(host, g, getSampleText(),textXOffset+(textGap/2),
0N/A ascent+2);
0N/A
0N/A g.fillRect(textXOffset,
0N/A ( height) + textGap,
0N/A width + (textGap),
0N/A height +2);
0N/A
0N/A g.setColor(Color.black);
0N/A SwingUtilities2.drawString(host, g, getSampleText(),
0N/A textXOffset+(textGap/2),
0N/A height+ascent+textGap+2);
0N/A
0N/A
0N/A g.setColor(Color.white);
0N/A
0N/A g.fillRect(textXOffset,
0N/A ( height + textGap) * 2,
0N/A width + (textGap),
0N/A height +2);
0N/A
0N/A g.setColor(color);
0N/A SwingUtilities2.drawString(host, g, getSampleText(),
0N/A textXOffset+(textGap/2),
0N/A ((height+textGap) * 2)+ascent+2);
0N/A
0N/A return width + textGap*3;
0N/A
0N/A }
0N/A
0N/A private int paintSquares(Graphics g, int offsetX) {
0N/A
0N/A int squareXOffset = offsetX;
0N/A Color color = getForeground();
0N/A
0N/A g.setColor(Color.white);
0N/A g.fillRect(squareXOffset,0,squareSize,squareSize);
0N/A g.setColor(color);
0N/A g.fillRect(squareXOffset+innerGap,
0N/A innerGap,
0N/A squareSize - (innerGap*2),
0N/A squareSize - (innerGap*2));
0N/A g.setColor(Color.white);
0N/A g.fillRect(squareXOffset+innerGap*2,
0N/A innerGap*2,
0N/A squareSize - (innerGap*4),
0N/A squareSize - (innerGap*4));
0N/A
0N/A g.setColor(color);
0N/A g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize);
0N/A
0N/A g.translate(squareSize+squareGap, 0);
0N/A g.setColor(Color.black);
0N/A g.fillRect(squareXOffset,0,squareSize,squareSize);
0N/A g.setColor(color);
0N/A g.fillRect(squareXOffset+innerGap,
0N/A innerGap,
0N/A squareSize - (innerGap*2),
0N/A squareSize - (innerGap*2));
0N/A g.setColor(Color.white);
0N/A g.fillRect(squareXOffset+innerGap*2,
0N/A innerGap*2,
0N/A squareSize - (innerGap*4),
0N/A squareSize - (innerGap*4));
0N/A g.translate(-(squareSize+squareGap), 0);
0N/A
0N/A g.translate(squareSize+squareGap, squareSize+squareGap);
0N/A g.setColor(Color.white);
0N/A g.fillRect(squareXOffset,0,squareSize,squareSize);
0N/A g.setColor(color);
0N/A g.fillRect(squareXOffset+innerGap,
0N/A innerGap,
0N/A squareSize - (innerGap*2),
0N/A squareSize - (innerGap*2));
0N/A g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
0N/A
0N/A
0N/A
0N/A g.translate((squareSize+squareGap)*2, 0);
0N/A g.setColor(Color.white);
0N/A g.fillRect(squareXOffset,0,squareSize,squareSize);
0N/A g.setColor(color);
0N/A g.fillRect(squareXOffset+innerGap,
0N/A innerGap,
0N/A squareSize - (innerGap*2),
0N/A squareSize - (innerGap*2));
0N/A g.setColor(Color.black);
0N/A g.fillRect(squareXOffset+innerGap*2,
0N/A innerGap*2,
0N/A squareSize - (innerGap*4),
0N/A squareSize - (innerGap*4));
0N/A g.translate(-((squareSize+squareGap)*2), 0);
0N/A
0N/A g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
0N/A g.setColor(Color.black);
0N/A g.fillRect(squareXOffset,0,squareSize,squareSize);
0N/A g.setColor(color);
0N/A g.fillRect(squareXOffset+innerGap,
0N/A innerGap,
0N/A squareSize - (innerGap*2),
0N/A squareSize - (innerGap*2));
0N/A g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
0N/A
0N/A return (squareSize*3+squareGap*2);
0N/A
0N/A }
0N/A
0N/A private String getSampleText() {
0N/A if (this.sampleText == null) {
0N/A this.sampleText = UIManager.getString("ColorChooser.sampleText", getLocale());
0N/A }
0N/A return this.sampleText;
0N/A }
0N/A}