001: /*
002: * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.widget;
030:
031: import com.caucho.util.L10N;
032:
033: import java.io.IOException;
034: import java.util.Locale;
035: import java.util.logging.Logger;
036:
037: public class FieldWidget extends WidgetContainer {
038: private static L10N L = new L10N(FieldWidget.class);
039:
040: static protected final Logger log = Logger
041: .getLogger(FieldWidget.class.getName());
042:
043: private LocaleString _displayName;
044: private LocaleString _shortDescription;
045:
046: public FieldWidget() {
047: }
048:
049: public FieldWidget(String id) {
050: super (id);
051: }
052:
053: public FieldWidget(Widget parent) {
054: super (parent);
055: }
056:
057: public FieldWidget(Widget parent, String id) {
058: super (parent, id);
059: }
060:
061: /**
062: * The name to display
063: */
064: public void setDisplayName(String displayName) {
065: _displayName = LocaleString.add(_displayName, displayName);
066: }
067:
068: /**
069: * The name to display
070: */
071: public void addDisplayName(LocaleString displayName) {
072: _displayName = LocaleString.add(_displayName, displayName);
073: }
074:
075: /**
076: * The name to display
077: */
078: public String getDisplayName(Locale locale) {
079: return LocaleString.get(_displayName, locale);
080: }
081:
082: /**
083: * The name to display
084: */
085: public String getDisplayName() {
086: return LocaleString.get(_displayName);
087: }
088:
089: /**
090: * A short description, up to one sentence in length.
091: */
092: public void setShortDescription(String shortDescription) {
093: _shortDescription = LocaleString.add(_shortDescription,
094: shortDescription);
095: }
096:
097: /**
098: * A short description, up to one sentence in length.
099: */
100: public void addShortDescription(LocaleString shortDescription) {
101: _shortDescription = LocaleString.add(_shortDescription,
102: shortDescription);
103: }
104:
105: /**
106: * A short description, up to one sentence in length.
107: */
108: public String getShortDescription() {
109: return LocaleString.get(_shortDescription);
110: }
111:
112: /**
113: * A short description, up to one sentence in length.
114: */
115: public String getShortDescription(Locale locale) {
116: return LocaleString.get(_shortDescription, locale);
117: }
118:
119: protected String calculateId(Widget child) {
120: String id = null;
121:
122: int size = size();
123:
124: if (size == 0)
125: id = "value";
126: else
127: id = "value" + size;
128:
129: return id;
130: }
131:
132: public void init() throws WidgetException {
133: super .init();
134:
135: if (getDisplayName() == null)
136: throw new IllegalStateException(L.l("`{0}' is required",
137: "display-name"));
138: }
139:
140: protected FieldWidgetState createState(WidgetConnection connection)
141: throws WidgetException {
142: return new FieldWidgetState();
143: }
144:
145: protected WidgetState decodeChild(WidgetConnection connection,
146: WidgetState this State, Widget child) throws WidgetException {
147: WidgetState childState = super .decodeChild(connection,
148: this State, child);
149:
150: FieldWidgetState state = (FieldWidgetState) this State;
151:
152: state.setChildWithValue(childState);
153:
154: return childState;
155: }
156:
157: public void renderTextHtml(WidgetConnection connection,
158: FieldWidgetState widgetState) throws WidgetException,
159: IOException {
160: WidgetWriter writer = connection.getWriter();
161:
162: Locale locale = connection.getLocale();
163:
164: String displayName = getDisplayName(locale);
165: String shortDescription = getShortDescription(locale);
166:
167: writer.startElement("div", true);
168: writer.writeAttribute("id", getClientId());
169: writer.writeAttribute("class", getCssClass());
170:
171: // name
172:
173: writer.startElement("div", true);
174: writer.writeAttribute("id", getClientId());
175: writer.writeAttribute("class", "name");
176:
177: if (shortDescription != null)
178: writer.writeAttribute("title", shortDescription);
179:
180: if (displayName != null)
181: writer.writeText(displayName);
182:
183: writer.endElement("div", true);
184:
185: // XXX:
186: // warnings
187: // errors
188:
189: // value
190:
191: writer.startElement("div", true);
192: writer.writeAttribute("id", getClientId());
193: writer.writeAttribute("class", "value");
194:
195: super .renderChildren(connection, widgetState);
196:
197: writer.endElement("div", true);
198:
199: //
200:
201: writer.endElement("div", true);
202: }
203: }
|