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.converter;
016:
017: import java.util.Set;
018: import org.araneaframework.Environment;
019: import org.araneaframework.core.Assert;
020: import org.araneaframework.uilib.form.Converter;
021: import org.araneaframework.uilib.form.FormElementContext;
022:
023: /**
024: * This class is the base class for form converters. The converters' task is to convert the value
025: * of form {@link org.araneaframework.uilib.form.Control} to the value of {@link org.araneaframework.uilib.form.FormElement}
026: * {@link org.araneaframework.uilib.form.Data} and back.
027: *
028: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
029: */
030: public abstract class BaseConverter implements java.io.Serializable,
031: Converter {
032:
033: private FormElementContext feCtx;
034:
035: //*********************************************************************
036: //* PUBLIC METHODS
037: //*********************************************************************
038:
039: /**
040: * This method converts the data from one type to another. If the data is <code>null</code>
041: * then <code>null</code> is returned. Otherwise {@link #convertNotNull(Object)}method is used
042: * for actual conversion.
043: *
044: * @param data Data to convert.
045: * @return Converted data.
046: */
047: public Object convert(Object data) {
048: Assert
049: .notNull(
050: this ,
051: getFormElementCtx(),
052: "Form element context must be assigned to the converter before it can function! "
053: + "Make sure that the converter is associated with a form element!");
054:
055: if (data == null)
056: return null;
057:
058: return convertNotNull(data);
059: }
060:
061: /**
062: * This method converts the data from one type to another (though the types are exchanged in
063: * comparison with {@link #convert(Object)}). If the data is <code>null</code> then <code>null</code>
064: * is returned. Otherwise {@link #reverseConvertNotNull(Object)}method is used for actual
065: * conversion.
066: *
067: * @param data Data to convert.
068: * @return Converted data.
069: */
070: public Object reverseConvert(Object data) {
071: Assert
072: .notNull(
073: this ,
074: getFormElementCtx(),
075: "Form element context must be assigned to the converter before it can function! "
076: + "Make sure that the converter is associated with a form element!");
077:
078: if (data == null)
079: return null;
080:
081: return reverseConvertNotNull(data);
082: }
083:
084: public void setFormElementCtx(FormElementContext feCtx) {
085: this .feCtx = feCtx;
086: }
087:
088: public FormElementContext getFormElementCtx() {
089: return this .feCtx;
090: }
091:
092: //*********************************************************************
093: //* PROTECTED METHODS
094: //*********************************************************************
095:
096: protected void addError(String error) {
097: feCtx.addError(error);
098: }
099:
100: protected void addErrors(Set errors) {
101: feCtx.addErrors(errors);
102: }
103:
104: protected Environment getEnvironment() {
105: return feCtx.getEnvironment();
106: }
107:
108: protected String getLabel() {
109: return feCtx.getLabel();
110: }
111:
112: //*********************************************************************
113: //* ABSTRACT INTERFACE METHODS
114: //*********************************************************************
115:
116: /**
117: * This method should return a new converter, of the same type that the class that overrides it,
118: * however freshly initialized.
119: *
120: * @return a new converter, of the same type that the class that overrides it, however freshly
121: * initialized.
122: */
123: public abstract Converter newConverter();
124:
125: //*********************************************************************
126: //* ABSTRACT IMPLEMENTATION METHODS
127: //*********************************************************************
128:
129: /**
130: * This method should convert the data from one type to another. It may assume that the <code>data</code>
131: * is never <code>null</code>.
132: *
133: * @param data Data to convert.
134: * @return Converted data.
135: */
136: protected abstract Object convertNotNull(Object data);
137:
138: /**
139: * This method should convert the data from one type to another. It may assume that the <code>data</code>
140: * is never <code>null</code>. The types of data are reversed in comparison to
141: * {@link #convertNotNull(Object)}.
142: *
143: * @param data Data to convert.
144: * @return Converted data.
145: */
146: protected abstract Object reverseConvertNotNull(Object data);
147:
148: }
|