001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.form.control;
016:
017: import org.araneaframework.InputData;
018: import org.araneaframework.OutputData;
019: import org.araneaframework.Path;
020: import org.araneaframework.Scope;
021: import org.araneaframework.Widget;
022: import org.araneaframework.core.Assert;
023: import org.araneaframework.core.BaseApplicationWidget;
024: import org.araneaframework.core.BaseWidget;
025: import org.araneaframework.http.HttpInputData;
026: import org.araneaframework.uilib.form.Control;
027: import org.araneaframework.uilib.form.FormElement;
028: import org.araneaframework.uilib.form.FormElementContext;
029:
030: /**
031: * This class is a control generalization that provides methods common to all HTML form controls.
032: * The methods include XML output and error handling.
033: *
034: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
035: *
036: */
037: public abstract class BaseControl extends BaseApplicationWidget
038: implements java.io.Serializable, Control {
039: //*******************************************************************
040: // FIELDS
041: //*******************************************************************
042:
043: protected Object value;
044: protected Object innerData;
045: protected boolean isReadFromRequest = false;
046:
047: private FormElementContext feCtx;
048:
049: //*********************************************************************
050: //* PUBLIC METHODS
051: //*********************************************************************
052:
053: /**
054: * Returns the value of the control (value read from the request). Type of value depends on the
055: * type of control.
056: *
057: * @return Returns the value of the control (value read from the request).
058: */
059: public Object getRawValue() {
060: return value;
061: }
062:
063: /**
064: * Sets the raw control value (as it was read from request/written to response).
065: * It is usually set by {@link org.araneaframework.uilib.form.Converter} when
066: * value of {@link FormElement} that owns this {@link BaseControl} changes.
067: *
068: * @param value control value.
069: * @see #getRawValue()
070: */
071: public void setRawValue(Object value) {
072: BaseControl.this .value = value;
073: }
074:
075: /**
076: * Returns {@link ViewModel}.
077: * @return {@link ViewModel}.
078: * @throws Exception
079: */
080: public Object getViewModel() throws Exception {
081: return new ViewModel();
082: }
083:
084: public void setFormElementCtx(FormElementContext formElementContext) {
085: this .feCtx = formElementContext;
086: }
087:
088: public FormElementContext getFormElementCtx() {
089: return feCtx;
090: }
091:
092: /**
093: * By default the control is considered read if it has a not null data read from request.
094: */
095: public boolean isRead() {
096: return isReadFromRequest;
097: }
098:
099: //*********************************************************************
100: //* OVERRIDABLE METHODS
101: //*********************************************************************
102:
103: public void convertAndValidate() {
104: convert();
105: validate();
106: }
107:
108: public void convert() {
109: }
110:
111: public void validate() {
112: }
113:
114: protected void readFromRequest(HttpInputData request) {
115: }
116:
117: //*********************************************************************
118: //* INTERNAL METHODS
119: //*********************************************************************
120:
121: protected void init() throws Exception {
122: super .init();
123:
124: Assert
125: .notNull(
126: this ,
127: getFormElementCtx(),
128: "Form element context must be assigned to the control before it can be initialized! "
129: + "Make sure that the control is associated with a form element!");
130:
131: }
132:
133: protected void action(Path path, InputData input, OutputData output)
134: throws Exception {
135: if (!isDisabled())
136: super .action(path, input, output);
137: }
138:
139: protected void update(InputData input) throws Exception {
140: super .update(input);
141:
142: if (!isDisabled())
143: readFromRequest((HttpInputData) input);
144: }
145:
146: protected void handleEvent(InputData input) throws Exception {
147: if (!isDisabled())
148: super .handleEvent(input);
149: }
150:
151: /**
152: * Returns control label.
153: *
154: * @return control label.
155: */
156: protected String getLabel() {
157: return feCtx.getLabel();
158: }
159:
160: /**
161: * * Returns whether the control is mandatory, that is must be inserted by user.
162: *
163: * @return whether the control is mandatory, that is must be inserted by user.
164: */
165: protected boolean isMandatory() {
166: return feCtx.isMandatory();
167: }
168:
169: protected void addError(String error) {
170: feCtx.addError(error);
171: }
172:
173: /**
174: * Returns whether the control is disabled.
175: * @return whether the control is disabled
176: *
177: * @since 1.1 this method is public and part of {@link Control} interface
178: */
179: public boolean isDisabled() {
180: return feCtx.isDisabled();
181: }
182:
183: protected boolean isValid() {
184: return feCtx.isValid();
185: }
186:
187: public Widget.Interface _getWidget() {
188: return new WidgetImpl();
189: }
190:
191: protected class WidgetImpl extends BaseWidget.WidgetImpl {
192: public void update(InputData input) {
193: isReadFromRequest = false;
194: super .update(input);
195: }
196: }
197:
198: //*********************************************************************
199: //* VIEW MODEL
200: //*********************************************************************
201:
202: /**
203: * Represents a general control view model.
204: *
205: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
206: */
207: public class ViewModel implements Control.ViewModel {
208: protected String controlType;
209: protected boolean mandatory;
210: protected boolean disabled;
211: protected String label;
212:
213: /**
214: * Takes an outer class snapshot.
215: */
216: public ViewModel() {
217: String className = BaseControl.this .getClass().getName();
218: // Recognizes Controls that are defined as (anonymous) nested classes.
219: // Prior to 1.5 getDeclaringClass() does not exist, so just look for '$'.
220: if (className.indexOf('$') != -1)
221: className = BaseControl.this .getClass().getSuperclass()
222: .getName();
223: className = className
224: .substring(className.lastIndexOf(".") + 1);
225: this .controlType = className;
226:
227: this .mandatory = BaseControl.this .isMandatory();
228: this .disabled = BaseControl.this .isDisabled();
229:
230: this .label = BaseControl.this .getLabel();
231: }
232:
233: /** @since 1.1 */
234: public Scope getScope() {
235: return BaseControl.this .getScope();
236: }
237:
238: /**
239: * Returns control type.
240: * @return control type.
241: */
242: public String getControlType() {
243: return controlType;
244: }
245:
246: /**
247: * Returns whether the control is mandatory.
248: * @return whether the control is mandatory.
249: */
250: public boolean isMandatory() {
251: return mandatory;
252: }
253:
254: /**
255: * Returns control label.
256: * @return control label.
257: */
258: public String getLabel() {
259: return label;
260: }
261:
262: /**
263: * Returns whether the control is disabled.
264: * @return whether the control is disabled.
265: */
266: public boolean isDisabled() {
267: return disabled;
268: }
269: }
270: }
|