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.Component;
034: import java.awt.Dimension;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.util.Iterator;
038: import java.util.LinkedHashMap;
039: import java.util.LinkedList;
040:
041: import javax.swing.ImageIcon;
042: import javax.swing.JButton;
043: import javax.swing.JComponent;
044: import javax.swing.JLabel;
045: import javax.swing.JTextField;
046:
047: import com.jeta.forms.store.properties.ColorProperty;
048: import com.jeta.open.gui.framework.JETADialog;
049: import com.jeta.open.gui.utils.JETAToolbox;
050: import com.jeta.open.i18n.I18N;
051: import com.jgoodies.forms.layout.CellConstraints;
052: import com.jgoodies.forms.layout.FormLayout;
053:
054: /**
055: * A view that allows the user to select a color that is either constant or
056: * based on the current look and feel.
057: *
058: * This class uses an internal component (JETAColorWell) that is not defined in
059: * the forms runtime. Reflection is used to work with this component to allow
060: * the forms runtime to compile.
061: *
062: * @author Jeff Tassin
063: */
064: public class ColorSelector extends JComponent {
065: /**
066: * The current color property
067: */
068: private ColorProperty m_color_prop = new ColorProperty();
069:
070: /**
071: * A list of ActionListeners that get notified when the color property
072: * changes.
073: */
074: private LinkedList m_listeners;
075:
076: private JTextField m_key_field;
077: private Component m_color_well;
078: private JButton m_definition_btn;
079:
080: private LinkedHashMap m_prepended_colors;
081:
082: private ColorSelectorController m_controller;
083:
084: /**
085: * ctor
086: */
087: public ColorSelector() {
088: try {
089: FormLayout layout = new FormLayout(
090: "center:pref,2dlu,pref,70dlu,24px,10px",
091: "fill:pref");
092: setLayout(layout);
093:
094: CellConstraints cc = new CellConstraints();
095: Class color_well_class = Class
096: .forName("com.jeta.forms.components.colors.JETAColorWell");
097: m_color_well = (Component) color_well_class.newInstance();
098: m_color_well.setName(ColorSelectorNames.ID_COLOR_INKWELL);
099: add(m_color_well, cc.xy(1, 1, CellConstraints.CENTER,
100: CellConstraints.CENTER));
101:
102: add(new JLabel("L&F"), cc.xy(3, 1));
103:
104: m_key_field = new JTextField();
105: m_key_field.setEnabled(false);
106: m_key_field.setName(ColorSelectorNames.ID_COLOR_FIELD);
107: m_key_field.setText(ColorProperty.CONSTANT_COLOR);
108: add(m_key_field, cc.xy(4, 1));
109:
110: JButton btn = new JButton();
111: m_definition_btn = btn;
112: try {
113: btn
114: .setIcon(new ImageIcon(
115: ColorSelector.class
116: .getClassLoader()
117: .getResource(
118: "jeta.resources/images/forms/16x16/palette.png")));
119: btn.setPreferredSize(new Dimension(24, 10));
120: } catch (Exception e) {
121: btn.setText("...");
122: }
123: add(btn, cc.xy(5, 1));
124: m_controller = new ColorSelectorController();
125:
126: } catch (Exception e) {
127: e.printStackTrace();
128: }
129: }
130:
131: /**
132: * Adds an listener that wants to be notified when the color is changed.
133: */
134: public void addActionListener(ActionListener listener) {
135: if (m_listeners == null)
136: m_listeners = new LinkedList();
137:
138: m_listeners.add(listener);
139: }
140:
141: /**
142: * @return the color property
143: */
144: public ColorProperty getColorProperty() {
145: try {
146: ColorProperty cprop = new ColorProperty();
147: cprop.setValue(m_color_prop);
148: cprop.setConstantColor(ColorSelectorUtils
149: .getColor(m_color_well));
150: cprop.setColorKey(getSelectedColorKey());
151: return cprop;
152: } catch (Exception e) {
153: e.printStackTrace();
154: return m_color_prop;
155: }
156: }
157:
158: public String getSelectedColorKey() {
159: return m_key_field.getText();
160: }
161:
162: /**
163: * Notifies all listeners that the ColorProperty has changed
164: */
165: public void notifyListeners() {
166: if (m_listeners != null) {
167: ActionEvent evt = new ActionEvent(this ,
168: ActionEvent.ACTION_PERFORMED, getName());
169: Iterator iter = m_listeners.iterator();
170: while (iter.hasNext()) {
171: ActionListener listener = (ActionListener) iter.next();
172: listener.actionPerformed(evt);
173: }
174: }
175: }
176:
177: /**
178: * Adds a color to the view that is not in the color manager
179: */
180: public void prependColor(String key, Color c) {
181: if (key == null)
182: return;
183:
184: if (m_prepended_colors == null)
185: m_prepended_colors = new LinkedHashMap();
186:
187: m_prepended_colors.put(key, c);
188: }
189:
190: /**
191: * Removes a registered action listener that wants to be notified when the
192: * color is changed.
193: */
194: public void removeActionListener(ActionListener listener) {
195: if (m_listeners != null)
196: m_listeners.remove(listener);
197: }
198:
199: /**
200: * Sets the color property
201: */
202: public void setColorProperty(ColorProperty cprop) {
203: try {
204: /** silence the controller so we don't get combo box events */
205: m_color_prop.setValue(cprop);
206: String color_name = cprop.getColorKey();
207: setSelectedColor(color_name);
208: ColorSelectorUtils.setColor(m_color_well, cprop.getColor());
209: } catch (Exception e) {
210: e.printStackTrace();
211: }
212:
213: }
214:
215: public void setSelectedColor(String cname) {
216: if (cname == null) {
217: m_key_field.setText(ColorProperty.CONSTANT_COLOR);
218: } else {
219: m_key_field.setText(cname);
220: }
221:
222: }
223:
224: /**
225: * Controller for this view
226: */
227: private class ColorSelectorController {
228: private boolean m_silent = false;
229:
230: public ColorSelectorController() {
231: ColorSelectorUtils.addActionListener(m_color_well,
232: new ColorWellClicked());
233: m_definition_btn
234: .addActionListener(new DefinitionListener());
235: }
236:
237: public void setSilent(boolean silent) {
238: m_silent = silent;
239: }
240:
241: public class ColorWellClicked implements ActionListener {
242: public void actionPerformed(ActionEvent evt) {
243: boolean silent = m_silent;
244: if (!silent) {
245: setSilent(true);
246: m_key_field.setText(ColorProperty.CONSTANT_COLOR);
247: m_color_prop.setBrightness(0);
248: m_color_prop
249: .setBrightnessFactor(ColorProperty.DEFAULT_FACTOR);
250: setSilent(silent);
251: notifyListeners();
252: }
253: }
254: }
255:
256: public class DefinitionListener implements ActionListener {
257: public void actionPerformed(ActionEvent evt) {
258: ColorDefinitionView view = new ColorDefinitionView();
259: if (m_prepended_colors != null) {
260: Iterator iter = m_prepended_colors.keySet()
261: .iterator();
262: while (iter.hasNext()) {
263: String cname = (String) iter.next();
264: Color c = (Color) m_prepended_colors.get(cname);
265: view.prependColor(cname, c);
266: }
267: }
268:
269: view.setColorProperty(getColorProperty());
270: JETADialog dlg = JETAToolbox.invokeDialog(view,
271: ColorSelector.this ,
272: I18N.getLocalizedMessage("Color Definition"));
273: if (dlg.isOk()) {
274: setColorProperty(view.getColorProperty());
275: notifyListeners();
276: }
277: }
278: }
279:
280: }
281:
282: }
|