001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing.colorchooser;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.FontMetrics;
025: import java.awt.Graphics;
026: import java.awt.Point;
027: import java.awt.Polygon;
028:
029: import javax.swing.JPanel;
030: import javax.swing.UIManager;
031:
032: import org.apache.harmony.x.swing.Utilities;
033:
034: class PreviewPanel extends JPanel {
035:
036: private static final int BIG_SQUARE_SIZE = 13;
037: private static final int SMALL_SQUARE_SIZE = 7;
038:
039: private static final int TOP_OFFSET = 10;
040: private static final int TEXT_OFFSET = 10;
041:
042: private int sampleWidth;
043: private int sampleHeight;
044:
045: private int swatchSize;
046: private int swatchOffset;
047:
048: private Polygon oldColorPoly = new Polygon(new int[] {
049: swatchOffset, swatchOffset + swatchSize,
050: swatchOffset + swatchSize }, new int[] { TOP_OFFSET,
051: TOP_OFFSET, TOP_OFFSET + swatchSize }, 3);
052: private Polygon newColorPoly = new Polygon(new int[] {
053: swatchOffset, swatchOffset, swatchOffset + swatchSize },
054: new int[] { TOP_OFFSET, TOP_OFFSET + swatchSize,
055: TOP_OFFSET + swatchSize }, 3);
056:
057: private Color previousColor;
058:
059: public Dimension getPreferredSize() {
060: initPanelSize();
061: return new Dimension(sampleWidth * 2 + TEXT_OFFSET * 3
062: + swatchSize, 64);
063: }
064:
065: public void paint(final Graphics graphics) {
066: Color oldColor = graphics.getColor();
067:
068: if (previousColor == null) {
069: previousColor = getForeground();
070: }
071:
072: super .paint(graphics);
073: initPanelSize();
074: paintTextSamples(graphics);
075: paintSwatch(graphics);
076: paintRectSamples(graphics);
077:
078: graphics.setColor(oldColor);
079: }
080:
081: private void paintTextSamples(final Graphics graphics) {
082: Color color = getForeground();
083: String textSample = UIManager
084: .getString("ColorChooser.sampleText");
085:
086: graphics.setColor(color);
087: graphics.fillRect(TEXT_OFFSET, TOP_OFFSET, sampleWidth,
088: sampleHeight);
089: graphics.fillRect(TEXT_OFFSET + sampleWidth + 1, TOP_OFFSET,
090: sampleWidth, sampleHeight);
091: graphics.setColor(Color.WHITE);
092: graphics.fillRect(TEXT_OFFSET, TOP_OFFSET + sampleHeight + 1,
093: sampleWidth, sampleHeight);
094:
095: Point stringPosition = getStringPlacement(textSample,
096: TEXT_OFFSET, TOP_OFFSET, sampleWidth - 25, sampleHeight);
097: graphics.drawString(textSample, stringPosition.x,
098: stringPosition.y);
099: stringPosition = getStringPlacement(textSample, TEXT_OFFSET
100: + sampleWidth + 25, TOP_OFFSET, sampleWidth - 25,
101: sampleHeight);
102: graphics.setColor(Color.BLACK);
103: graphics.drawString(textSample, stringPosition.x,
104: stringPosition.y);
105: graphics.setColor(color);
106: stringPosition = getStringPlacement(textSample, TEXT_OFFSET,
107: TOP_OFFSET + sampleHeight, sampleWidth - 25,
108: sampleHeight);
109: graphics.drawString(textSample, stringPosition.x,
110: stringPosition.y);
111: stringPosition = getStringPlacement(textSample, TEXT_OFFSET
112: + sampleWidth + 25, TOP_OFFSET + sampleHeight,
113: sampleWidth - 25, sampleHeight);
114: graphics.drawString(textSample, stringPosition.x,
115: stringPosition.y);
116:
117: graphics.setColor(Color.BLACK);
118: graphics.drawRect(TEXT_OFFSET, TOP_OFFSET, sampleWidth * 2,
119: sampleHeight * 2);
120: graphics.drawLine(TEXT_OFFSET + sampleWidth, TOP_OFFSET,
121: TEXT_OFFSET + sampleWidth, TOP_OFFSET + sampleHeight
122: * 2);
123: graphics.drawLine(TEXT_OFFSET, TOP_OFFSET + sampleHeight,
124: TEXT_OFFSET + sampleWidth * 2, TOP_OFFSET
125: + sampleHeight);
126: }
127:
128: private void paintRectSamples(final Graphics graphics) {
129: Color color = getForeground();
130:
131: graphics.setColor(Color.WHITE);
132: int bigSquareRelativeY = (sampleHeight - BIG_SQUARE_SIZE + 1) / 2;
133: graphics.fillRect(TEXT_OFFSET + sampleWidth - 19, TOP_OFFSET
134: + bigSquareRelativeY, BIG_SQUARE_SIZE, BIG_SQUARE_SIZE);
135:
136: graphics.setColor(Color.BLACK);
137: graphics.fillRect(TEXT_OFFSET + sampleWidth + 6, TOP_OFFSET
138: + bigSquareRelativeY, BIG_SQUARE_SIZE, BIG_SQUARE_SIZE);
139: graphics.fillRect(TEXT_OFFSET + sampleWidth - 16, TOP_OFFSET
140: + sampleHeight + bigSquareRelativeY + SMALL_SQUARE_SIZE
141: / 2, SMALL_SQUARE_SIZE, SMALL_SQUARE_SIZE);
142:
143: graphics.setColor(color);
144: graphics.fillRect(TEXT_OFFSET + sampleWidth - 19, TOP_OFFSET
145: + sampleHeight + bigSquareRelativeY, BIG_SQUARE_SIZE,
146: BIG_SQUARE_SIZE);
147: graphics.fillRect(TEXT_OFFSET + sampleWidth + 6, TOP_OFFSET
148: + sampleHeight + bigSquareRelativeY, BIG_SQUARE_SIZE,
149: BIG_SQUARE_SIZE);
150:
151: graphics.setColor(Color.WHITE);
152: graphics.fillRect(TEXT_OFFSET + sampleWidth + 9, TOP_OFFSET
153: + sampleHeight + bigSquareRelativeY + SMALL_SQUARE_SIZE
154: / 2, SMALL_SQUARE_SIZE, SMALL_SQUARE_SIZE);
155:
156: graphics.setColor(Color.BLACK);
157: graphics.fillRect(TEXT_OFFSET + sampleWidth - 16, TOP_OFFSET
158: + sampleHeight + bigSquareRelativeY + SMALL_SQUARE_SIZE
159: / 2, SMALL_SQUARE_SIZE, SMALL_SQUARE_SIZE);
160:
161: graphics.setColor(color);
162: graphics.fillRect(TEXT_OFFSET + sampleWidth + 9, TOP_OFFSET
163: + bigSquareRelativeY + SMALL_SQUARE_SIZE / 2,
164: SMALL_SQUARE_SIZE, SMALL_SQUARE_SIZE);
165: graphics.fillRect(TEXT_OFFSET + sampleWidth - 16, TOP_OFFSET
166: + bigSquareRelativeY + SMALL_SQUARE_SIZE / 2,
167: SMALL_SQUARE_SIZE, SMALL_SQUARE_SIZE);
168:
169: }
170:
171: private void paintSwatch(final Graphics graphics) {
172: Color color = getForeground();
173:
174: graphics.setColor(previousColor);
175: graphics.fillPolygon(oldColorPoly);
176: graphics.setColor(color);
177: graphics.fillPolygon(newColorPoly);
178: graphics.setColor(Color.BLACK);
179: graphics.drawRect(swatchOffset, TOP_OFFSET, swatchSize,
180: swatchSize);
181: }
182:
183: private Point getStringPlacement(final String string, final int x,
184: final int y, final int width, final int height) {
185: FontMetrics fm = Utilities.getFontMetrics(this );
186: Dimension size = Utilities.getStringSize(string, fm);
187:
188: return new Point(x + (width - size.width) / 2, y
189: + fm.getHeight() + (height - fm.getHeight()) / 2);
190: }
191:
192: private void initPanelSize() {
193: String textSample = UIManager
194: .getString("ColorChooser.sampleText");
195: Dimension stringSize = Utilities.getStringSize(textSample, this
196: .getFontMetrics(this .getFont()));
197: sampleWidth = stringSize.width + BIG_SQUARE_SIZE + 20;
198: sampleHeight = stringSize.height + 4;
199:
200: swatchSize = sampleHeight * 2;
201: swatchOffset = sampleWidth * 2 + TEXT_OFFSET * 2;
202:
203: oldColorPoly = new Polygon(new int[] { swatchOffset,
204: swatchOffset + swatchSize, swatchOffset + swatchSize },
205: new int[] { TOP_OFFSET, TOP_OFFSET,
206: TOP_OFFSET + swatchSize }, 3);
207: newColorPoly = new Polygon(new int[] { swatchOffset,
208: swatchOffset, swatchOffset + swatchSize }, new int[] {
209: TOP_OFFSET, TOP_OFFSET + swatchSize,
210: TOP_OFFSET + swatchSize }, 3);
211: }
212: }
|