001: /*
002: * Copyright 2000,2006 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013:
014: package org.wingx;
015:
016: import java.awt.Color;
017: import javax.swing.event.ChangeEvent;
018: import org.wings.LowLevelEventListener;
019: import org.wings.SComponent;
020: import org.wings.ReloadManager;
021: import org.wings.SForm;
022: import org.wings.event.SAjaxChangeListener;
023: import org.wings.session.Session;
024: import org.wings.session.SessionManager;
025: import org.wingx.plaf.css.ColorPickerCG;
026:
027: /**
028: * A component to choose a color. Adopted and enhanced from the YUI library examples
029: * (see http://developer.yahoo.com/yui/slider/).
030: */
031: public class XColorPicker extends SComponent implements
032: LowLevelEventListener, XColorPickerInterface {
033:
034: /**
035: * red RGB part
036: */
037: private int red = 255;
038:
039: /**
040: * green RGB part
041: */
042: private int green = 255;
043:
044: /**
045: * blue RGB part
046: */
047: private int blue = 255;
048:
049: /**
050: * @see #setImmediateUpdate(boolean b)
051: */
052: private boolean immediateUpdate = true;
053:
054: /**
055: * Ajax request timeout in ms
056: */
057: private int timeout = 5000;
058:
059: private ReloadManager reloadManager = SessionManager.getSession()
060: .getReloadManager();
061:
062: /**
063: * Creates a color picker with the default color (white) and immediate
064: * updates.
065: * @see #setImmediateUpdate(boolean b)
066: */
067: public XColorPicker() {
068: ((ColorPickerCG) getCG()).prepareIds(this );
069: }
070:
071: /**
072: * Creates a color picker with the given color set as default and
073: * immediate updates.
074: * @param red
075: * @param green
076: * @param blue
077: * @see #setImmediateUpdate(boolean b)
078: */
079: public XColorPicker(int red, int green, int blue) {
080: this ();
081: this .red = red;
082: this .green = green;
083: this .blue = blue;
084: }
085:
086: /**
087: * Creates a color picker with the given color and immediate update
088: * option set.
089: * @param red
090: * @param green
091: * @param blue
092: * @param immediateUpdate see setImmediateUpdate(boolean b)
093: */
094: public XColorPicker(int red, int green, int blue,
095: boolean immediateUpdate) {
096: this (red, green, blue);
097: this .immediateUpdate = immediateUpdate;
098: }
099:
100: /**
101: * @return color selected by user
102: */
103: public Color getSelectedColor() {
104: return new Color(red, green, blue);
105: }
106:
107: public boolean setSelectedColor(int red, int green, int blue) {
108: Color oldColor = this .getSelectedColor();
109:
110: this .red = red;
111: this .green = green;
112: this .blue = blue;
113:
114: Color newColor = this .getSelectedColor();
115:
116: if (newColor != oldColor) {
117: fireStateChanged();
118: if (this .immediateUpdate
119: && reloadManager.getDirtyFrames().contains(
120: this .getParentFrame()))
121: return true;
122: }
123:
124: return false;
125: }
126:
127: /**
128: * If true (which is the default) all dirty components will be requested to
129: * refresh after a change of this component.
130: * @see #setSelectedColor(int red, int green, int blue)
131: */
132: public void setImmediateUpdate(boolean b) {
133: this .immediateUpdate = b;
134: }
135:
136: /**
137: * @see #setImmediateUpdate(boolean b)
138: */
139: public boolean isImmediateUpdate() {
140: return this .immediateUpdate;
141: }
142:
143: /**
144: * Adds the specified <code>SAjaxChangeListener</code> to the color picker.
145: * @param l the <code>SAjaxChangeListener</code> to add
146: */
147: public void addAjaxChangeListener(SAjaxChangeListener l) {
148: addEventListener(SAjaxChangeListener.class, l);
149: }
150:
151: /**
152: * Removes a <code>SAjaxChangeListener</code> from the color picker.
153: *
154: * @param l the <code>SAjaxChangeListener</code> to remove
155: */
156: public void removeAjaxChangeListener(SAjaxChangeListener l) {
157: removeEventListener(SAjaxChangeListener.class, l);
158: }
159:
160: /**
161: * Returns an array of all the <code>SAjaxChangeListener</code>s added
162: * to this color picker with <code>addAjaxChangeListener</code>.
163: *
164: * @return all of the <code>SAjaxChangeListener</code>s added or an empty
165: * array if no listeners have been added
166: */
167: public SAjaxChangeListener[] getAjaxChangeListeners() {
168: return (SAjaxChangeListener[]) getListeners(SAjaxChangeListener.class);
169: }
170:
171: public boolean hasAjaxChangeListener() {
172: return (getListenerCount(SAjaxChangeListener.class) > 0) ? true
173: : false;
174: }
175:
176: /**
177: * Notifies all listeners that have registered interest in
178: * <code>ChangeEvent</code>s.
179: */
180: protected void fireStateChanged() {
181: SAjaxChangeListener[] listeners = (SAjaxChangeListener[]) getListeners(SAjaxChangeListener.class);
182: for (int i = 0; i < listeners.length; i++) {
183: SAjaxChangeListener listener = listeners[i];
184: listener.stateChanged(new ChangeEvent(this ));
185: }
186: }
187:
188: public void fireFinalEvents() {
189: fireStateChanged();
190: }
191:
192: public void fireIntermediateEvents() {
193: // nothing to do
194: }
195:
196: public boolean isEpochCheckEnabled() {
197: return true;
198: }
199:
200: public void processLowLevelEvent(String action, String[] values) {
201: processKeyEvents(values);
202: if (action.endsWith("_keystroke"))
203: return;
204:
205: Color oldColor = this .getSelectedColor();
206:
207: String rAction = (String) this
208: .getClientProperty("pickerRvalId");
209: String gAction = (String) this
210: .getClientProperty("pickerGvalId");
211: String bAction = (String) this
212: .getClientProperty("pickerBvalId");
213:
214: int newRed = this .red;
215: int newGreen = this .green;
216: int newBlue = this .blue;
217:
218: if (action.compareTo(rAction) == 0) {
219: newRed = Integer.parseInt(values[0]);
220: } else if (action.compareTo(gAction) == 0) {
221: newGreen = Integer.parseInt(values[0]);
222: } else if (action.compareTo(bAction) == 0) {
223: newBlue = Integer.parseInt(values[0]);
224: }
225:
226: Color newColor = new Color(newRed, newGreen, newBlue);
227:
228: if (oldColor != newColor) {
229: this .red = newRed;
230: this .green = newGreen;
231: this .blue = newBlue;
232: SForm.addArmedComponent(this );
233: }
234:
235: }
236:
237: /**
238: * Set Ajax request timeout.
239: * @param timeout in ms
240: */
241: public void setTimeout(int timeout) {
242: int oldTimeout = this .timeout;
243: this .timeout = timeout;
244: reloadIfChange(oldTimeout, timeout);
245: }
246:
247: /**
248: * @see org.wingx.XSuggest#setTimeout(int timeout)
249: */
250: public int getTimeout() {
251: return timeout;
252: }
253:
254: }
|