001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.presentation.portlet.widgets.model;
051:
052: import org.jaffa.datatypes.Currency;
053: import org.jaffa.datatypes.*;
054: import org.jaffa.metadata.*;
055: import java.util.*;
056: import org.apache.log4j.*;
057: import org.jaffa.datatypes.exceptions.UnknownDataTypeRuntimeException;
058: import org.jaffa.presentation.portlet.widgets.model.exceptions.DataTypeMismatchRuntimeException;
059:
060: /** Model for the EditBox widget.
061: */
062: public class EditBoxModel extends WidgetModel {
063: private static Logger log = Logger.getLogger(EditBoxModel.class);
064:
065: // Default Rules For TextBox
066: private String m_stringCase = FieldMetaData.MIXED_CASE;
067: private int m_maxLen = 50;
068: private int m_minLen = 0;
069: private String m_dataType = Defaults.STRING;
070: private FieldMetaData m_meta = null;
071: private Boolean m_mandatory = null;
072: private boolean m_modelChanged = false;
073:
074: // Internal Context
075: private String m_value = null;
076:
077: /** Default constructor.
078: */
079: public EditBoxModel() {
080: }
081:
082: /** Creates a model..
083: * @param meta The meta object to use for getting the layout info.
084: * This may throw the UnknownDataTypeRuntimeException in case the input 'meta' object has an unsupported DataType.
085: */
086: public EditBoxModel(FieldMetaData meta) {
087: this (null, meta);
088: }
089:
090: /** Creates a model initialized to the input value.
091: * @param obj The initial value.
092: */
093: public EditBoxModel(Object obj) {
094: this (obj, null);
095: }
096:
097: /** Creates a model initialized to the input value.
098: * @param obj The initial value.
099: * @param meta The meta object to use for getting the layout info.
100: * This may throw the UnknownDataTypeRuntimeException in case the input 'meta' object has an unsupported DataType.
101: * This may throw the DataTypeMismatchRuntimeException in case there is a mismatch in the Datatypes of the inputs.
102: */
103: public EditBoxModel(Object obj, FieldMetaData meta) {
104: m_meta = meta;
105: if (obj != null) {
106: // Make sure this is a real datatype
107: m_dataType = Defaults.getDataType(obj);
108: if (m_dataType == null) {
109: String str = "Unsupported Data Type : "
110: + obj.getClass().getName();
111: log.error(str);
112: throw new UnknownDataTypeRuntimeException(str);
113: }
114: } else if (meta != null) {
115: m_dataType = meta.getDataType();
116: } else {
117: String str = "Unknown Data Type: Null parameters passed";
118: log.error(str);
119: throw new UnknownDataTypeRuntimeException(str);
120: }
121:
122: // Assign the maxLen only if the meta object is passed
123: // Otherwise let it remain the default value 50
124: if (meta != null)
125: m_maxLen = meta.getWidth();
126:
127: // Try to infer default meta data if none was passed
128: if (meta == null) {
129: if (obj instanceof DateOnly)
130: meta = new DateOnlyFieldMetaData();
131: else if (obj instanceof DateTime)
132: meta = new DateTimeFieldMetaData();
133: else if (obj instanceof Long)
134: meta = new IntegerFieldMetaData();
135: else if (obj instanceof Double)
136: meta = new DecimalFieldMetaData();
137: else if (obj instanceof Currency)
138: meta = new CurrencyFieldMetaData();
139: else
140: // Default to string
141: meta = new StringFieldMetaData();
142: }
143:
144: // Make sure Object and MetaData are the same data type!
145: if (!m_dataType.equals(meta.getDataType())) {
146: String str = "Object Data Type Mismatch. Object = "
147: + obj.getClass().getName() + " is '" + m_dataType
148: + "', MetaData = '" + meta.getDataType() + "'";
149: log.error(str);
150: throw new DataTypeMismatchRuntimeException(str);
151: }
152:
153: // If a string, get the string meta data properties out
154: if (m_dataType.equals(Defaults.STRING)) {
155: StringFieldMetaData metaStr = (StringFieldMetaData) meta;
156: if (metaStr.getCaseType() != null)
157: m_stringCase = metaStr.getCaseType();
158:
159: // Set the object value using the string formatter.
160: if (obj == null)
161: m_value = "";
162: else
163: m_value = Formatter.format((String) obj);
164:
165: } else if (m_dataType.equals(Defaults.DATEONLY)) {
166: // Set the object value using the string formatter.
167: DateOnlyFieldMetaData metaDO = (DateOnlyFieldMetaData) meta;
168: // @todo
169: if (obj == null)
170: m_value = "";
171: else
172: m_value = Formatter.format((DateOnly) obj, metaDO
173: .getLayout());
174: } else if (m_dataType.equals(Defaults.DATETIME)) {
175: // Set the object value using the string formatter.
176: DateTimeFieldMetaData metaDT = (DateTimeFieldMetaData) meta;
177: // @todo
178: if (obj == null)
179: m_value = "";
180: else
181: m_value = Formatter.format((DateTime) obj, metaDT
182: .getLayout());
183: } else if (m_dataType.equals(Defaults.INTEGER)) {
184: // Set the object value using the Integer formatter.
185: IntegerFieldMetaData metaInt = (IntegerFieldMetaData) meta;
186: if (obj == null)
187: m_value = "";
188: else
189: m_value = Formatter.format((Long) obj, metaInt
190: .getLayout());
191: } else if (m_dataType.equals(Defaults.DECIMAL)) {
192: // Set the object value using the Decimal formatter.
193: DecimalFieldMetaData metaDec = (DecimalFieldMetaData) meta;
194: if (obj == null)
195: m_value = "";
196: else
197: m_value = Formatter.format((Double) obj, metaDec
198: .getLayout());
199: } else if (m_dataType.equals(Defaults.CURRENCY)) {
200: // Set the object value using the Decimal formatter.
201: CurrencyFieldMetaData metaCurr = (CurrencyFieldMetaData) meta;
202: if (obj == null)
203: m_value = "";
204: else
205: m_value = Formatter.format((Currency) obj, metaCurr
206: .getLayout());
207: } else {
208: // Default : Get the string version of the model.
209: if (obj == null)
210: m_value = "";
211: else
212: m_value = obj.toString();
213: }
214:
215: if (log.isDebugEnabled())
216: log.debug("Initialize Widget To Value: " + m_value);
217: }
218:
219: /** Getter for property value.
220: * @return Value of property value.
221: */
222: public String getValue() {
223: return m_value;
224: }
225:
226: /** Setter for property value.
227: * @param value New value of property value.
228: */
229: public void setValue(String value) {
230: if (m_stringCase != null && value != null) {
231: if (m_stringCase.equals(FieldMetaData.UPPER_CASE))
232: value = value.toUpperCase();
233: else if (m_stringCase.equals(FieldMetaData.LOWER_CASE))
234: value = value.toLowerCase();
235: } else if (value == null)
236: value = "";
237: // Only update it and flag it if changed
238: if ((value == null && m_value != null)
239: || (value != null && !value.equals(m_value))) {
240: m_value = value;
241: setModelChanged(true);
242:
243: }
244: }
245:
246: /** Change the state of the model internally */
247: private void setModelChanged(boolean setState) {
248: m_modelChanged = setState;
249: }
250:
251: /** See if model has changed, in the process reset the changed flag.
252: * @return true if the model was changed.
253: */
254: public boolean isModelChanged() {
255: boolean modified = m_modelChanged;
256: m_modelChanged = false;
257: return modified;
258: }
259:
260: /** Setter for property stringCase.
261: * @param stringCase New value of property stringCase.
262: */
263: public void setStringCase(String stringCase) {
264: m_stringCase = stringCase;
265: }
266:
267: /** Getter for property stringCase.
268: * @return Value of property stringCase.
269: */
270: public String getStringCase() {
271: return m_stringCase;
272: }
273:
274: /** Setter for property maxLen.
275: * @param maxLen New value of property maxLen.
276: */
277: public void setMaxLen(int maxLen) {
278: m_maxLen = maxLen;
279: }
280:
281: /** Getter for property maxLen.
282: * @return Value of property maxLen.
283: */
284: public int getMaxLen() {
285: return m_maxLen;
286: }
287:
288: /** Setter for property minLen.
289: * @param minLen New value of property minLen.
290: */
291: public void setMinLen(int minLen) {
292: m_minLen = minLen;
293: }
294:
295: /** Getter for property minLen.
296: * @return Value of property minLen.
297: */
298: public int getMinLen() {
299: return m_minLen;
300: }
301:
302: /** Getter for property dataType.
303: * @return Value of property dataType.
304: */
305: public String getDataType() {
306: return m_dataType;
307: }
308:
309: /** Getter for property metaData.
310: * @return Value of property metaData.
311: */
312: public FieldMetaData getMetaData() {
313: return m_meta;
314: }
315:
316: /** Getter for property mandatory.
317: * @return Value of property mandatory.
318: */
319: public boolean isMandatory() {
320: if (m_mandatory != null)
321: return m_mandatory.booleanValue();
322: else if (m_meta != null && m_meta.isMandatory() != null)
323: return m_meta.isMandatory().booleanValue();
324: else
325: return false;
326: }
327:
328: /** Setter for property mandatory.
329: * @param meta New value of property mandatory.
330: */
331: public void setMandatory(boolean mandatory) {
332: m_mandatory = new Boolean(mandatory);
333: }
334:
335: }
|