001: /*
002: * $Id: LabelCG.java 3016 2006-11-08 13:01:10Z stephanschuster $
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.wings.plaf.css;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019: import org.wings.SIcon;
020: import org.wings.SResourceIcon;
021: import org.wings.SSlider;
022: import org.wings.event.SParentFrameEvent;
023: import org.wings.event.SParentFrameListener;
024: import org.wings.plaf.css.script.OnHeadersLoadedScript;
025: import org.wings.resource.ResourceManager;
026: import org.wings.session.ScriptManager;
027: import org.wings.SComponent;
028: import org.wings.io.Device;
029: import java.io.IOException;
030: import org.wings.util.SStringBuilder;
031: import org.wings.header.SessionHeaders;
032:
033: /**
034: * CG for SSlider instances.
035: * @author Christian Schyma
036: */
037: public class SliderCG extends AbstractComponentCG implements
038: org.wings.plaf.SliderCG, SParentFrameListener {
039:
040: protected final List headers = new ArrayList();
041:
042: private SIcon horizontalThumb;
043: private SIcon verticalThumb;
044:
045: // used by .SSliderBgHoriz and SSliderBgVert in all.css
046: static {
047: String[] images = new String[] {
048: "org/wings/icons/SliderHoriz.png",
049: "org/wings/icons/SliderVert.png" };
050:
051: for (int x = 0, y = images.length; x < y; x++) {
052: SIcon icon = new SResourceIcon(images[x]);
053: icon.getURL(); // hack to externalize
054: }
055: }
056:
057: /**
058: * the maximum number of pixels the slider thumb can be moved
059: */
060: private final Integer defaultMaxPixelConstraint = 200; // slider bar width - slider thumb width
061:
062: public SliderCG() {
063: setHorizontalThumbIcon((SIcon) ResourceManager.getObject(
064: "SliderCG.horizontalThumbIcon", SIcon.class));
065: setVerticalThumbIcon((SIcon) ResourceManager.getObject(
066: "SliderCG.verticalThumbIcon", SIcon.class));
067:
068: headers
069: .add(Utils
070: .createExternalizedJSHeaderFromProperty(Utils.JS_YUI_SLIDER));
071: }
072:
073: public void writeInternal(final Device device,
074: final SComponent component) throws IOException {
075: String id = component.getName();
076: String thumbId = id + "_sliderthumb";
077: SSlider c = (SSlider) component;
078: Integer maxPixelConstraint = (Integer) component
079: .getClientProperty("maxPixelConstraint");
080: if (maxPixelConstraint == null)
081: maxPixelConstraint = defaultMaxPixelConstraint;
082:
083: // scale factor for converting the pixel offset into a real value
084: double factor = (c.getMaximum() - c.getMinimum())
085: / maxPixelConstraint.floatValue();
086:
087: // render HTML
088: device.print("<div");
089: Utils.optAttribute(device, "id", id);
090: if (HORIZONTAL == c.getOrientation()) {
091: Utils.optAttribute(device, "class", "SSliderBgHoriz");
092: } else if (VERTICAL == c.getOrientation()) {
093: Utils.optAttribute(device, "class", "SSliderBgVert");
094: }
095:
096: device.print(">");
097:
098: device.print("<div");
099: Utils.optAttribute(device, "id", thumbId);
100: Utils.optAttribute(device, "class", "SSliderThumb"); // thumb origin to override table align="center"
101: device.print(">");
102:
103: device.print("<img");
104: if (HORIZONTAL == c.getOrientation()) {
105: Utils.optAttribute(device, "src", horizontalThumb.getURL());
106: } else if (VERTICAL == c.getOrientation()) {
107:
108: Utils.optAttribute(device, "src", verticalThumb.getURL());
109: }
110: device.print(" />");
111:
112: device.print("</div>");
113:
114: String valId = (new SStringBuilder(id).append("_val"))
115: .toString();
116: device.print("<div");
117: if (c.getOrientation() == VERTICAL) {
118: Utils.optAttribute(device, "class", "SSliderValueRight");
119: } else if (c.getOrientation() == HORIZONTAL) {
120: Utils.optAttribute(device, "class", "SSliderValueBottom");
121: }
122: device.print("><input ");
123: Utils.optAttribute(device, "autocomplete", "off");
124: Utils.optAttribute(device, "name", valId);
125: Utils.optAttribute(device, "id", valId);
126: Utils.optAttribute(device, "value", 0);
127: Utils.optAttribute(device, "size", 4);
128: Utils.optAttribute(device, "type", "text");
129: device.print(" readonly></div>");
130:
131: device.print("</div></div>");
132:
133: // prepare script
134: String slider = (new SStringBuilder(id).append("_")
135: .append("slider")).toString();
136:
137: SStringBuilder code = new SStringBuilder("function() {");
138: code.append("var ").append(slider).append(
139: " = YAHOO.widget.Slider.");
140: if (HORIZONTAL == c.getOrientation()) {
141: code.append("getHorizSlider(");
142: } else if (VERTICAL == c.getOrientation()) {
143: code.append("getVertSlider(");
144: }
145:
146: code.append("'" + id + "', ").append("'" + thumbId + "', ")
147: .append("0, ").append(maxPixelConstraint.intValue());
148: if (c.getSnapToTicks()) {
149: code.append(", ").append(c.getMajorTickSpacing() / factor);
150: }
151: code.append("); ");
152:
153: code
154: .append(slider)
155: .append(".setValue(")
156: .append((c.getValue() - c.getMinimum()) / factor)
157: .append(");")
158: .append(slider)
159: .append(
160: ".onChange = function(offset) {document.getElementById('")
161: .append(valId).append("').value = offset * ").append(
162: factor).append("+ ").append(c.getMinimum())
163: .append("};").append("}");
164:
165: ScriptManager.getInstance().addScriptListener(
166: new OnHeadersLoadedScript(code.toString(), false));
167: }
168:
169: public void installCG(final SComponent comp) {
170: super .installCG(comp);
171: comp.addParentFrameListener(this );
172: }
173:
174: public void setHorizontalThumbIcon(SIcon icon) {
175: this .horizontalThumb = icon;
176: }
177:
178: public void setVerticalThumbIcon(SIcon icon) {
179: this .verticalThumb = icon;
180: }
181:
182: public void parentFrameAdded(SParentFrameEvent e) {
183: SessionHeaders.getInstance().registerHeaders(headers);
184: }
185:
186: public void parentFrameRemoved(SParentFrameEvent e) {
187: SessionHeaders.getInstance().deregisterHeaders(headers);
188: }
189:
190: }
|