001: /*
002: * $Id: CSSLookAndFeel.java 3059 2006-11-14 20:41:26Z cjschyma $
003: * Copyright 2000,2005 wingS development team.
004: *
005: * This file is part of wingS (http://wingsframework.org).
006: *
007: * wingS is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014:
015: package org.wingx.plaf.css;
016:
017: import java.awt.Color;
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.HashSet;
021: import java.util.List;
022:
023: import org.wings.SComponent;
024: import org.wings.SIcon;
025: import org.wings.SResourceIcon;
026: import org.wings.event.SParentFrameEvent;
027: import org.wings.event.SParentFrameListener;
028: import org.wings.header.SessionHeaders;
029: import org.wings.io.Device;
030: import org.wings.plaf.css.AbstractComponentCG;
031: import org.wings.plaf.css.Utils;
032: import org.wings.plaf.css.dwr.CallableManager;
033: import org.wings.plaf.css.script.OnHeadersLoadedScript;
034: import org.wings.session.ScriptManager;
035: import org.wings.util.SStringBuilder;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.wingx.XColorPicker;
039: import org.wingx.XColorPickerInterface;
040:
041: public class ColorPickerCG extends AbstractComponentCG implements
042: org.wingx.plaf.ColorPickerCG, SParentFrameListener {
043:
044: private final transient static Log log = LogFactory
045: .getLog(ColorPickerCG.class);
046:
047: protected final List headers = new ArrayList();
048:
049: // these graphics are needed for hsvcolorpicker.css and hsvcolorpicker_lt_ie7.css
050: static {
051: String[] images = new String[] {
052: "org/wingx/colorpicker/images/select.gif",
053: "org/wingx/colorpicker/images/hline.png",
054: "org/wingx/colorpicker/images/hue.png",
055: "org/wingx/colorpicker/images/pickerbg.png" };
056:
057: for (int x = 0, y = images.length; x < y; x++) {
058: SIcon icon = new SResourceIcon(images[x]);
059: icon.getURL();
060: }
061: }
062:
063: public ColorPickerCG() {
064: // TODO export to properties file!
065: headers
066: .add(Utils
067: .createExternalizedJSHeaderFromProperty(Utils.JS_YUI_SLIDER));
068: headers
069: .add(Utils
070: .createExternalizedCSSHeader("org/wingx/colorpicker/hsvcolorpicker.css"));
071: headers
072: .add(Utils
073: .createExternalizedJSHeader("org/wingx/colorpicker/hsvcolorpicker.js"));
074: headers
075: .add(Utils
076: .createExternalizedJSHeader("org/wingx/colorpicker/color.js"));
077: }
078:
079: /**
080: * Preparation of DOM element ids. Every id has to be individual and
081: * therefore the component id is used upfront.
082: */
083: public void prepareIds(SComponent component) {
084: log.debug("preparing ids");
085:
086: String id = component.getName();
087:
088: component.putClientProperty("pickerPanelId", id);
089: component.putClientProperty("pickerHandleId",
090: (new SStringBuilder(id).append("_pickerHandle"))
091: .toString());
092: component.putClientProperty("pickerId", (new SStringBuilder(id)
093: .append("_pickerDiv")).toString());
094: component.putClientProperty("pickerSwatchId",
095: (new SStringBuilder(id).append("_pickerSwatch"))
096: .toString());
097: component.putClientProperty("selectorId", (new SStringBuilder(
098: id).append("_selector")).toString());
099: component.putClientProperty("hueBackgroundId",
100: (new SStringBuilder(id).append("_hueBg")).toString());
101: component.putClientProperty("hueThumbId", (new SStringBuilder(
102: id).append("_hueThumb")).toString());
103: component.putClientProperty("pickerFormName",
104: (new SStringBuilder(id).append("_pickerform"))
105: .toString());
106:
107: component.putClientProperty("pickerRvalId",
108: (new SStringBuilder(id).append("_pickerrval"))
109: .toString());
110: component.putClientProperty("pickerGvalId",
111: (new SStringBuilder(id).append("_pickergval"))
112: .toString());
113: component.putClientProperty("pickerBvalId",
114: (new SStringBuilder(id).append("_pickerbval"))
115: .toString());
116:
117: component.putClientProperty("pickerHvalId",
118: (new SStringBuilder(id).append("_pickerhval"))
119: .toString());
120: component.putClientProperty("pickerSvalId",
121: (new SStringBuilder(id).append("_pickersval"))
122: .toString());
123: component.putClientProperty("pickerVvalId",
124: (new SStringBuilder(id).append("_pickervval"))
125: .toString());
126:
127: component.putClientProperty("pickerHexValId",
128: (new SStringBuilder(id).append("_pickerhexval"))
129: .toString());
130: }
131:
132: protected void printPicker(Device device, SComponent component)
133: throws IOException {
134: device.print("<div");
135: Utils.optAttribute(device, "id", (String) component
136: .getClientProperty("pickerId"));
137: Utils.optAttribute(device, "class", "hsvcolorpicker pickerDiv");
138: device.print(">");
139: device.print("<div");
140: Utils.optAttribute(device, "class", "hsvcolorpicker pickerbg");
141: device.print("></div>");
142: device.print("<div");
143: Utils.optAttribute(device, "id", (String) component
144: .getClientProperty("selectorId"));
145: Utils.optAttribute(device, "class", "hsvcolorpicker selector");
146: device.print("></div>");
147: device.print("</div>");
148: }
149:
150: private void printHueSlider(Device device, SComponent component)
151: throws IOException {
152: device.print("<div");
153: Utils.optAttribute(device, "id", (String) component
154: .getClientProperty("hueBackgroundId"));
155: Utils.optAttribute(device, "class", "hsvcolorpicker hueBg");
156: device.print(">");
157: device.print("<div");
158: Utils.optAttribute(device, "id", (String) component
159: .getClientProperty("hueThumbId"));
160: Utils.optAttribute(device, "class", "hsvcolorpicker hueThumb");
161: device.print("></div>");
162: device.print("</div>");
163: }
164:
165: private void printPickerValues(Device device, SComponent component)
166: throws IOException {
167: device.print("<div");
168: Utils.optAttribute(device, "class",
169: "hsvcolorpicker pickervaldiv");
170: device.print(">");
171: device.print("<br />");
172: printValueInputField(device, (String) component
173: .getClientProperty("pickerRvalId"), "R", 3);
174: printValueInputField(device, (String) component
175: .getClientProperty("pickerHvalId"), "H", 3);
176: device.print("<br />");
177: printValueInputField(device, (String) component
178: .getClientProperty("pickerGvalId"), "G", 3);
179: printValueInputField(device, (String) component
180: .getClientProperty("pickerSvalId"), "S", 3);
181: device.print("<br />");
182: printValueInputField(device, (String) component
183: .getClientProperty("pickerBvalId"), "B", 3);
184: printValueInputField(device, (String) component
185: .getClientProperty("pickerVvalId"), "V", 3);
186: device.print("<br />");
187: device.print("<br />");
188: printValueInputField(device, (String) component
189: .getClientProperty("pickerHexValId"), "#", 6);
190: device.print("<br />");
191: device.print("</div>");
192: }
193:
194: private void printPickerSwatch(Device device, SComponent component)
195: throws IOException {
196: device.print("<div");
197: Utils.optAttribute(device, "id", (String) component
198: .getClientProperty("pickerSwatchId"));
199: Utils.optAttribute(device, "class",
200: "hsvcolorpicker pickerSwatch");
201: device.print("> </div>");
202: }
203:
204: private void printPickerDNDHandle(Device device,
205: SComponent component) throws IOException {
206: device.print("<h4");
207: Utils.optAttribute(device, "id", (String) component
208: .getClientProperty("pickerHandleId"));
209: device.print("> </h4>");
210: }
211:
212: public void writeInternal(Device device, SComponent component)
213: throws IOException {
214: // render HTML
215: device.print("<div");
216: Utils.optAttribute(device, "id", (String) component
217: .getClientProperty("pickerPanelId"));
218: Utils.optAttribute(device, "class", "hsvcolorpicker dragPanel");
219: device.print(">");
220: printPickerDNDHandle(device, component);
221: printPicker(device, component);
222: printHueSlider(device, component);
223: printPickerValues(device, component);
224: printPickerSwatch(device, component);
225: device.print("</div>");
226:
227: // prepare script
228: XColorPicker picker = (XColorPicker) component;
229: Color c = picker.getSelectedColor();
230: SStringBuilder code = new SStringBuilder("function() {");
231: code.append("new wingS.HSVColorPicker(").append(
232: component.getClientProperty("DWR_JS_OBJECT")).append(
233: ", ").append(picker.getTimeout()).append(", ").append(
234: "'").append(
235: (String) component.getClientProperty("pickerPanelId"))
236: .append("', ").append("'").append(
237: (String) component
238: .getClientProperty("pickerHandleId"))
239: .append("', ").append("'").append(
240: (String) component
241: .getClientProperty("pickerId")).append(
242: "', ").append("'").append(
243: (String) component
244: .getClientProperty("pickerSwatchId"))
245: .append("', ").append("'").append(
246: (String) component
247: .getClientProperty("pickerHvalId"))
248: .append("', ").append("'").append(
249: (String) component
250: .getClientProperty("pickerSvalId"))
251: .append("', ").append("'").append(
252: (String) component
253: .getClientProperty("pickerVvalId"))
254: .append("', ").append("'").append(
255: (String) component
256: .getClientProperty("pickerRvalId"))
257: .append("', ").append("'").append(
258: (String) component
259: .getClientProperty("pickerGvalId"))
260: .append("', ").append("'").append(
261: (String) component
262: .getClientProperty("pickerBvalId"))
263: .append("', ").append("'").append(
264: (String) component
265: .getClientProperty("pickerHexValId"))
266: .append("', ").append("'").append(
267: (String) component
268: .getClientProperty("selectorId"))
269: .append("', ").append("'").append(
270: (String) component
271: .getClientProperty("hueBackgroundId"))
272: .append("', ").append("'").append(
273: (String) component
274: .getClientProperty("hueThumbId"))
275: .append("', ").append("'").append(c.getRed()).append(
276: "', ").append("'").append(c.getGreen()).append(
277: "', ").append("'").append(c.getBlue()).append(
278: "');").append("}");
279:
280: ScriptManager.getInstance().addScriptListener(
281: new OnHeadersLoadedScript(code.toString(), false));
282: }
283:
284: private void printValueInputField(Device device, String inputId,
285: String prefix, int size) throws IOException {
286: device.print(prefix);
287: device.print(" <input");
288: Utils.optAttribute(device, "class", "hsvcolorpicker");
289: Utils.optAttribute(device, "name", inputId);
290: Utils.optAttribute(device, "id", inputId);
291: device.print(" type='text' value='0' size='" + size
292: + "' maxlength='" + size + "' readonly>");
293: }
294:
295: public void installCG(final SComponent comp) {
296: super .installCG(comp);
297: comp.addParentFrameListener(this );
298: }
299:
300: public void parentFrameAdded(SParentFrameEvent e) {
301: SessionHeaders.getInstance().registerHeaders(headers);
302:
303: SComponent c = e.getComponent();
304: String DWR_JS_OBJECT = c.getName() + "_colorpicker";
305: c.putClientProperty("DWR_JS_OBJECT", DWR_JS_OBJECT);
306:
307: // expose methods to JavaScript by using DWR
308: CallableManager.getInstance().registerCallable(DWR_JS_OBJECT,
309: c, XColorPickerInterface.class);
310: }
311:
312: public void parentFrameRemoved(SParentFrameEvent e) {
313: SessionHeaders.getInstance().deregisterHeaders(headers);
314:
315: String DWR_JS_OBJECT = (String) e.getComponent()
316: .getClientProperty("DWR_JS_OBJECT");
317: CallableManager.getInstance().unregisterCallable(DWR_JS_OBJECT);
318: }
319:
320: }
|