001: package org.wingx.plaf.css;
002:
003: import java.text.DateFormat;
004: import java.text.Format;
005: import java.text.SimpleDateFormat;
006: import java.util.ArrayList;
007: import java.util.Calendar;
008: import java.util.Collections;
009: import java.util.Date;
010: import java.util.GregorianCalendar;
011: import java.util.List;
012:
013: import org.wings.SDimension;
014: import org.wings.SFormattedTextField;
015: import org.wings.SResourceIcon;
016: import org.wings.header.Header;
017: import org.wings.header.SessionHeaders;
018: import org.wings.io.Device;
019: import org.wings.plaf.Update;
020: import org.wings.plaf.css.AbstractComponentCG;
021: import org.wings.plaf.css.AbstractUpdate;
022: import org.wings.plaf.css.UpdateHandler;
023: import org.wings.plaf.css.Utils;
024: import org.wings.plaf.css.script.OnHeadersLoadedScript;
025: import org.wings.session.ScriptManager;
026: import org.wings.util.SStringBuilder;
027: import org.wingx.XCalendar;
028:
029: /**
030: * @author <a href="mailto:e.habicht@thiesen.com">Erik Habicht</a>
031: * @author Stephan Schuster
032: */
033: public class CalendarCG extends AbstractComponentCG<XCalendar>
034: implements org.wingx.plaf.CalendarCG<XCalendar> {
035:
036: private static final long serialVersionUID = 1L;
037:
038: protected final static List<Header> headers;
039:
040: static {
041: String[] images = new String[] {
042: "org/wingx/calendar/calcd.gif",
043: "org/wingx/calendar/cally.gif",
044: "org/wingx/calendar/calry.gif" };
045:
046: for (int x = 0, y = images.length; x < y; x++) {
047: new SResourceIcon(images[x]).getId();
048: }
049:
050: List<Header> headerList = new ArrayList<Header>();
051: headerList
052: .add(Utils
053: .createExternalizedCSSHeaderFromProperty(Utils.CSS_YUI_ASSETS_CALENDAR));
054: headerList
055: .add(Utils
056: .createExternalizedJSHeaderFromProperty(Utils.JS_YUI_CALENDAR));
057: headerList
058: .add(Utils
059: .createExternalizedJSHeader("org/wingx/calendar/calendar.js"));
060: headers = Collections.unmodifiableList(headerList);
061: }
062:
063: public CalendarCG() {
064: }
065:
066: public void installCG(XCalendar component) {
067: super .installCG(component);
068: SessionHeaders.getInstance().registerHeaders(headers);
069: }
070:
071: public void uninstallCG(XCalendar component) {
072: super .uninstallCG(component);
073: SessionHeaders.getInstance().deregisterHeaders(headers);
074: }
075:
076: public void writeInternal(Device device, XCalendar component)
077: throws java.io.IOException {
078: final String idPrefix = "_cal_";
079: final String idComponent = component.getName();
080: final String idValue = idPrefix + idComponent + "_val";
081: final String idButton = idPrefix + idComponent + "_btn";
082: final String idContainer = idPrefix + idComponent + "_con";
083: final String idCalendar = idPrefix + idComponent + "_cal";
084:
085: SFormattedTextField fTextField = component
086: .getFormattedTextField();
087: SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
088: dateFormat.setTimeZone(component.getTimeZone());
089:
090: device.print("<table");
091: Utils.writeAllAttributes(device, component);
092: device.print("><tr><td class=\"tf\" width=\"*\"");
093: int oversizePadding = Utils.calculateHorizontalOversize(
094: fTextField, true);
095: if (oversizePadding != 0)
096: Utils.optAttribute(device, "oversize", oversizePadding);
097: device.print('>');
098:
099: SDimension preferredSize = component.getPreferredSize();
100: if (preferredSize != null && preferredSize.getWidth() != null
101: && "auto".equals(preferredSize.getWidth()))
102: fTextField.setPreferredSize(SDimension.FULLWIDTH);
103: fTextField.setEnabled(component.isEnabled());
104: fTextField.write(device);
105:
106: device.print("\n</td><td class=\"bu\" width=\"1\">\n");
107:
108: device.print("<input type=\"hidden\" id=\"").print(idValue)
109: .print("\" name=\"").print(idValue)
110: .print("\" value=\"").print(
111: format(dateFormat, component.getDate())).print(
112: "\">\n");
113: device.print("<img id=\"").print(idButton).print("\" src=\"")
114: .print(component.getEditIcon().getURL()).print(
115: "\" />\n");
116: device
117: .print("<div class=\"container\" id=\"")
118: .print(idContainer)
119: .print(
120: "\"><div class=\"hd\"></div><div class=\"bd\"><div class=\"calendar\" id=\"")
121: .print(idCalendar).print("\"></div></div></div></td>");
122:
123: writeTableSuffix(device, component);
124:
125: if (component.isEnabled()) {
126: SimpleDateFormat format_months_long = new SimpleDateFormat(
127: "MMMMM");
128: format_months_long.setTimeZone(component.getTimeZone());
129: SimpleDateFormat format_weekdays_short = new SimpleDateFormat(
130: "EE");
131: format_weekdays_short.setTimeZone(component.getTimeZone());
132:
133: SStringBuilder script = new SStringBuilder(
134: "new wingS.calendar.XCalendar(")
135: .append('"')
136: .append(component.getName())
137: .append("\",\"")
138: .append(component.getParentFrame().getName())
139: .append("\", {iframe:false,")
140: .append("months_long:")
141: .append(createMonthsString(format_months_long))
142: .append(',')
143: .append("weekdays_short:")
144: .append(createWeekdaysString(format_weekdays_short))
145: .append(',')
146: .append("start_weekday:")
147: .append(
148: (Calendar.getInstance().getFirstDayOfWeek() - 1))
149: .append("}").append(");");
150:
151: ScriptManager.getInstance().addScriptListener(
152: new OnHeadersLoadedScript(script.toString(), true));
153: }
154:
155: }
156:
157: private String createMonthsString(Format format) {
158: SStringBuilder stringBuilder = new SStringBuilder();
159: stringBuilder.append('[');
160: Calendar cal = new GregorianCalendar();
161: cal.set(Calendar.MONTH, Calendar.JANUARY);
162: for (int x = 0, y = 12; x < y; x++) {
163: stringBuilder.append('"');
164: stringBuilder.append(format.format(cal.getTime()));
165: stringBuilder.append("\",");
166: cal.add(Calendar.MONTH, 1);
167: }
168: stringBuilder.deleteCharAt(stringBuilder.length() - 1);
169: stringBuilder.append(']');
170: return stringBuilder.toString();
171: }
172:
173: private String createWeekdaysString(Format format) {
174: SStringBuilder stringBuilder = new SStringBuilder();
175: stringBuilder.append('[');
176: Calendar cal = new GregorianCalendar();
177: cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
178: for (int x = 0, y = 7; x < y; x++) {
179: stringBuilder.append('"');
180: stringBuilder.append(format.format(cal.getTime()));
181: stringBuilder.append("\",");
182: cal.add(Calendar.DAY_OF_WEEK, 1);
183: }
184: stringBuilder.deleteCharAt(stringBuilder.length() - 1);
185: stringBuilder.append(']');
186: return stringBuilder.toString();
187: }
188:
189: private String format(DateFormat dateFormat, Date date) {
190: return date != null ? dateFormat.format(date) : "";
191: }
192:
193: public Update getHiddenUpdate(XCalendar cal, Date date) {
194: return new HiddenUpdate(cal, date);
195: }
196:
197: protected static class HiddenUpdate extends
198: AbstractUpdate<XCalendar> {
199:
200: private Date date;
201: private SimpleDateFormat format;
202:
203: public HiddenUpdate(XCalendar cal, Date date) {
204: super (cal);
205: this .date = date;
206: this .format = new SimpleDateFormat("MM/dd/yyyy");
207: }
208:
209: public Handler getHandler() {
210: UpdateHandler handler = new UpdateHandler("value");
211: handler.addParameter(component.getName() + "val");
212: handler.addParameter(date == null ? "" : format
213: .format(date));
214: return handler;
215: }
216: }
217:
218: }
|