001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.components.colors;
031:
032: import java.awt.Color;
033: import java.awt.Dimension;
034: import java.awt.Graphics;
035: import java.awt.Rectangle;
036: import java.awt.event.ActionEvent;
037: import java.awt.event.ActionListener;
038: import java.awt.event.MouseAdapter;
039: import java.awt.event.MouseEvent;
040: import java.util.Iterator;
041: import java.util.LinkedList;
042:
043: import javax.swing.JColorChooser;
044: import javax.swing.JComponent;
045: import javax.swing.SwingUtilities;
046: import javax.swing.border.BevelBorder;
047:
048: import com.jeta.open.i18n.I18N;
049: import com.jeta.open.registry.JETARegistry;
050:
051: /**
052: * This class is used to display a small rectangular button on a panel. The
053: * button is used to select a color for those dialogs that support color
054: * selection/preferences.
055: *
056: * @author Jeff Tassin
057: */
058: public class JETAColorWell extends JComponent {
059: /**
060: * The current color
061: */
062: private Color m_color;
063:
064: /**
065: * A list of ActionListener objects that get notified when the color
066: * changes.
067: */
068: private LinkedList m_listeners;
069:
070: /**
071: * ctor
072: */
073: public JETAColorWell() {
074: initialize(null);
075: }
076:
077: /**
078: * ctor
079: */
080: public JETAColorWell(Color color) {
081: initialize(color);
082: }
083:
084: /**
085: * Adds an listener that wants to be notified when the color is changed.
086: */
087: public void addActionListener(ActionListener listener) {
088: if (m_listeners == null)
089: m_listeners = new LinkedList();
090: if (!m_listeners.contains(listener))
091: m_listeners.add(listener);
092: }
093:
094: /**
095: * Returns the currently selected color
096: *
097: * @return the color for this ink well
098: */
099: public Color getColor() {
100: return m_color;
101: }
102:
103: /**
104: * Initializes the color well
105: */
106: protected void initialize(Color color) {
107: if (color == null)
108: m_color = Color.black;
109: else
110: m_color = color;
111:
112: setBorder(javax.swing.BorderFactory
113: .createBevelBorder(BevelBorder.LOWERED));
114: Dimension d = new Dimension(16, 16);
115: setSize(d);
116: setPreferredSize(d);
117: addMouseListener(new ColorWellListener());
118: }
119:
120: /**
121: * Notifies any listeners that the color has changed.
122: */
123: protected void notifyListeners(ActionEvent evt) {
124: if (m_listeners != null) {
125: Iterator iter = m_listeners.iterator();
126: while (iter.hasNext()) {
127: ActionListener listener = (ActionListener) iter.next();
128: listener.actionPerformed(evt);
129: }
130: }
131: }
132:
133: /**
134: * Paints the component
135: */
136: public void paintComponent(Graphics g) {
137: java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
138: g2.setPaint(getColor());
139: Rectangle rect = g.getClipBounds();
140: g2.fillRect(rect.x, rect.y, rect.width, rect.height);
141: }
142:
143: /**
144: * Removes a listener that was previous added with addListener
145: */
146: public void removeActionListener(ActionListener listener) {
147: if (m_listeners != null)
148: m_listeners.remove(listener);
149: }
150:
151: /**
152: * Sets the color for this ink well
153: *
154: * @param color
155: * the color to set
156: */
157: public void setColor(Color color) {
158: m_color = color;
159: repaint();
160: }
161:
162: /**
163: * Handler for changing color
164: */
165: public class ColorWellListener extends MouseAdapter {
166: public void mouseClicked(MouseEvent evt) {
167: JETAColorWell comp = (JETAColorWell) evt.getSource();
168:
169: Color result = null;
170: ColorChooserFactory factory = (ColorChooserFactory) JETARegistry
171: .lookup(ColorChooserFactory.COMPONENT_ID);
172: if (factory != null) {
173: result = factory.showColorChooser(comp, I18N
174: .getLocalizedMessage("Color Chooser"), comp
175: .getColor());
176: } else {
177: result = JColorChooser.showDialog(SwingUtilities
178: .getWindowAncestor(comp), I18N
179: .getLocalizedMessage("Color Chooser"), comp
180: .getColor());
181: }
182:
183: if (result != null) {
184: comp.setColor(result);
185: comp.notifyListeners(new ActionEvent(
186: JETAColorWell.this,
187: ActionEvent.ACTION_PERFORMED, comp.getName()));
188: }
189: }
190: }
191:
192: }
|