001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.colorchooser;
020:
021: import java.awt.Color;
022: import java.awt.Dimension;
023: import java.awt.Graphics;
024: import java.awt.Insets;
025: import java.awt.event.MouseEvent;
026:
027: import javax.swing.UIManager;
028:
029: import com.jeta.forms.store.properties.ColorHolder;
030: import com.jeta.open.gui.framework.JETAPanel;
031: import com.jeta.swingbuilder.store.RecentColorsModel;
032:
033: /**
034: * This panel is used to display recent colors selected in the JETAColorChooser.
035: * It is needed because the JColorChooser has a curiously poor design.
036: *
037: * @author Jeff Tassin
038: */
039: public class RecentSwatchPanel extends JETAPanel {
040: /**
041: * Used to store the recent colors between application invocations.
042: */
043: private RecentColorsModel m_colors;
044:
045: /**
046: * The size in pixels of each swatch
047: */
048: private Dimension m_swatch_size;
049:
050: private static final int SWATCH_COLUMNS = 30;
051: private static final int SWATCH_ROWS = 2;
052:
053: /**
054: * A small space between each swatch.
055: */
056: protected Dimension m_gap;
057:
058: /**
059: * The default color for a swatch if one has not been defined.
060: */
061: private ColorHolder m_default_color;
062:
063: public RecentSwatchPanel() {
064: Color default_color = UIManager
065: .getColor("ColorChooser.swatchesDefaultRecentColor");
066: if (default_color == null)
067: default_color = UIManager.getColor("control");
068: if (default_color == null)
069: default_color = Color.gray;
070:
071: m_default_color = new ColorHolder(default_color);
072: m_colors = RecentColorsModel.createInstance(SWATCH_ROWS
073: * SWATCH_COLUMNS);
074:
075: m_swatch_size = UIManager
076: .getDimension("ColorChooser.swatchesRecentSwatchSize");
077: if (m_swatch_size == null)
078: m_swatch_size = new Dimension(10, 10);
079:
080: m_gap = new Dimension(1, 1);
081:
082: setToolTipText("");
083: setRequestFocusEnabled(false);
084: setBorder(javax.swing.BorderFactory
085: .createLineBorder(Color.black));
086: }
087:
088: public boolean isFocusTraversable() {
089: return false;
090: }
091:
092: public void paintComponent(Graphics g) {
093: Insets insets = getInsets();
094: g.setColor(Color.white);
095: g.fillRect(0, 0, getWidth(), getHeight());
096: for (int row = 0; row < SWATCH_ROWS; row++) {
097: for (int column = 0; column < SWATCH_COLUMNS; column++) {
098: g.setColor(getColorForCell(column, row).getColor());
099: int x = insets.left + column
100: * (m_swatch_size.width + m_gap.width);
101: int y = insets.top + row
102: * (m_swatch_size.height + m_gap.height);
103: g.fillRect(x, y, m_swatch_size.width,
104: m_swatch_size.height);
105: g.setColor(Color.black);
106: g.drawLine(x + m_swatch_size.width - 1, y, x
107: + m_swatch_size.width - 1, y
108: + m_swatch_size.height - 1);
109: g.drawLine(x, y + m_swatch_size.height - 1, x
110: + m_swatch_size.width - 1, y
111: + m_swatch_size.height - 1);
112: }
113: }
114:
115: g.setColor(Color.white);
116: int width = SWATCH_COLUMNS
117: * (m_swatch_size.width + m_gap.width) - 2;
118: int height = SWATCH_ROWS
119: * (m_swatch_size.height + m_gap.height) - 2;
120: g.drawLine(insets.left, insets.top, insets.left + width,
121: insets.top);
122: g.drawLine(insets.left, insets.top, insets.left, insets.top
123: + height);
124: g.drawLine(insets.left, insets.top + height, insets.left
125: + width, insets.top + height);
126: g.drawLine(insets.left + width, insets.top,
127: insets.left + width, insets.top + height);
128: }
129:
130: public Dimension getPreferredSize() {
131: Insets insets = getInsets();
132: int width = SWATCH_COLUMNS
133: * (m_swatch_size.width + m_gap.width) - 1 + insets.left
134: + insets.right;
135: int height = SWATCH_ROWS
136: * (m_swatch_size.height + m_gap.height) - 1
137: + insets.top + insets.bottom;
138: return new Dimension(width, height);
139: }
140:
141: public String getToolTipText(MouseEvent e) {
142: ColorHolder color = getColorForLocation(e.getX(), e.getY());
143: return color.getRed() + ", " + color.getGreen() + ", "
144: + color.getBlue();
145: }
146:
147: public ColorHolder getColorForLocation(int x, int y) {
148: int column = x / (m_swatch_size.width + m_gap.width);
149: int row = y / (m_swatch_size.height + m_gap.height);
150: return getColorForCell(column, row);
151: }
152:
153: private ColorHolder getColorForCell(int column, int row) {
154: return m_colors.getColor((row * SWATCH_COLUMNS) + column,
155: m_default_color);
156: }
157:
158: public void setMostRecentColor(Color c) {
159: m_colors.setRecentColor(new ColorHolder(c));
160: repaint();
161: }
162:
163: /**
164: * Saves the recent color settings to the application store
165: */
166: public void saveSettings() {
167: RecentColorsModel.save(m_colors);
168: }
169: }
|