001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2004 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.swing.components.validators;
019:
020: import java.text.*;
021:
022: import javax.swing.*;
023:
024: import net.ar.webonswing.*;
025: import net.ar.webonswing.helpers.*;
026: import net.ar.webonswing.swing.components.validators.contributors.*;
027:
028: public class JCompareValidator extends JValidator {
029: protected Operation operation;
030: protected Type type;
031:
032: protected Object valueToCompare;
033: protected JComponent componentToCompare;
034:
035: public JCompareValidator() {
036: WosFramework.assignContributor(this ,
037: CompareValidatorUIContributor.class);
038: }
039:
040: public JCompareValidator(JComponent aComponentToValidate,
041: String aMessage, String aGroupMessage,
042: boolean hasRemoteValidation, Object aValueToCompare,
043: JCompareValidator.Operation anOperation,
044: JCompareValidator.Type aType) {
045: this (aComponentToValidate, aMessage, aGroupMessage,
046: hasRemoteValidation, anOperation, aType);
047: valueToCompare = aValueToCompare;
048: }
049:
050: public JCompareValidator(JComponent aComponentToValidate,
051: String aMessage, String aGroupMessage,
052: boolean hasRemoteValidation,
053: JComponent aComponentToCompare,
054: JCompareValidator.Operation anOperation,
055: JCompareValidator.Type aType) {
056: this (aComponentToValidate, aMessage, aGroupMessage,
057: hasRemoteValidation, anOperation, aType);
058: componentToCompare = aComponentToCompare;
059: }
060:
061: public JCompareValidator(JComponent aComponentToValidate,
062: String aMessage, String aGroupMessage,
063: boolean hasRemoteValidation, JComponent aComponentToCompare) {
064: this (aComponentToValidate, aMessage, aGroupMessage,
065: hasRemoteValidation, JCompareValidator.Operation.equal,
066: JCompareValidator.Type.STRING);
067: componentToCompare = aComponentToCompare;
068: }
069:
070: public JCompareValidator(JComponent aComponentToValidate,
071: String aMessage, String aGroupMessage,
072: boolean hasRemoteValidation,
073: JCompareValidator.Operation anOperation,
074: JCompareValidator.Type aType) {
075: super (aComponentToValidate, aMessage, aGroupMessage,
076: hasRemoteValidation);
077:
078: operation = anOperation;
079: type = aType;
080:
081: WosFramework.assignContributor(this ,
082: CompareValidatorUIContributor.class);
083: }
084:
085: protected boolean performValidation() {
086: try {
087: Comparable v1 = type
088: .getComparable(getComponentValue(componentToValidate));
089:
090: if (componentToCompare != null || valueToCompare != null) {
091: Comparable v2 = type
092: .getComparable(componentToCompare != null ? getComponentValue(componentToCompare)
093: : valueToCompare);
094: return operation.compare(v1, v2);
095: } else
096: return true;
097:
098: } catch (RuntimeException e) {
099: return false;
100: }
101: }
102:
103: public static class Operation {
104: protected int[] matchValues;
105: protected String name;
106:
107: public static Operation equal = new Operation(new int[] { 0, 0,
108: 0 }, "equal");
109: public static Operation notEqual = new Operation(new int[] {
110: -1, 1, 1 }, "not equal");
111: public static Operation greaterThan = new Operation(new int[] {
112: 1, 1, 1 }, "greater than");
113: public static Operation greaterThanEqual = new Operation(
114: new int[] { 1, 0, 0 }, "greater than equal");
115: public static Operation lessThan = new Operation(new int[] {
116: -1, -1, -1 }, "less than");
117: public static Operation lessThanEqual = new Operation(
118: new int[] { -1, 0, 0 }, "less than equal");
119: public static Operation dataTypeCheck = new Operation(
120: new int[] { -1, 0, 1 }, "data type check");
121:
122: public Operation(int[] match, String aName) {
123: matchValues = match;
124: name = aName;
125: }
126:
127: public boolean compare(Comparable aValue,
128: Comparable aCompareValue) {
129: int comparison = aValue.compareTo(aCompareValue);
130:
131: if (comparison != 0)
132: comparison = comparison / Math.abs(comparison);
133:
134: return comparison == matchValues[0]
135: || comparison == matchValues[1]
136: || comparison == matchValues[2];
137: }
138:
139: public int[] getMatchValues() {
140: return matchValues;
141: }
142:
143: public void setMatchValues(int[] aIs) {
144: matchValues = aIs;
145: }
146:
147: public String toString() {
148: return name;
149: }
150: }
151:
152: public static abstract class Type {
153: public static Type STRING = new Type() {
154: public Comparable createComparableFromString(String aValue) {
155: return aValue;
156: }
157:
158: public String toString() {
159: return "string";
160: }
161: };
162:
163: public static Type INTEGER = new Type() {
164: public Comparable createComparableFromString(String aValue) {
165: return new Integer(aValue);
166: }
167:
168: public String toString() {
169: return "integer";
170: }
171: };
172:
173: public static Type DOUBLE = new Type() {
174: public Comparable createComparableFromString(String aValue) {
175: return new Double(aValue);
176: }
177:
178: public String toString() {
179: return "double";
180: }
181: };
182:
183: public static Type DATE = DATE("dd/MM/yyyy HH:mm:ss");
184:
185: public static Type DATE(final String aPattern) {
186: return new Type() {
187: protected SimpleDateFormat format = new SimpleDateFormat(
188: aPattern);
189:
190: public Comparable createComparableFromString(
191: String aValue) {
192: try {
193: return format.parse(aValue);
194: } catch (ParseException e) {
195: throw new WebOnSwingException(e);
196: }
197: }
198:
199: protected String createStringFromComparable(
200: Object aValue) {
201: return format.format(aValue);
202: }
203:
204: public String getPattern() {
205: return format.toPattern();
206: }
207:
208: public String toString() {
209: return "date:" + format.toPattern();
210: }
211: };
212: }
213:
214: protected abstract Comparable createComparableFromString(
215: String aValue);
216:
217: protected String createStringFromComparable(Object aValue) {
218: return aValue.toString();
219: }
220:
221: public Comparable getComparable(Object aValue) {
222: if (aValue instanceof String)
223: return createComparableFromString((String) aValue);
224: else if (aValue instanceof Comparable)
225: return (Comparable) aValue;
226: else
227: throw new WebOnSwingException(
228: "The value to compare does not implement Comparable interface");
229: }
230:
231: public String getString(Object aValue) {
232: if (aValue instanceof String)
233: return (String) aValue;
234: else if (aValue instanceof Comparable)
235: return createStringFromComparable(aValue);
236: else
237: throw new WebOnSwingException(
238: "The value to compare does not implement Comparable interface");
239: }
240: }
241:
242: public JComponent getComponentToCompare() {
243: return componentToCompare;
244: }
245:
246: public Operation getOperation() {
247: return operation;
248: }
249:
250: public void setComponentToCompare(JComponent aComponent) {
251: componentToCompare = aComponent;
252: }
253:
254: public void setOperation(Operation aOperation) {
255: operation = aOperation;
256: }
257:
258: public Type getType() {
259: return type;
260: }
261:
262: public void setType(Type aType) {
263: type = aType;
264: }
265:
266: public Object getValueToCompare() {
267: return valueToCompare;
268: }
269:
270: public void setValueToCompare(Object aObject) {
271: valueToCompare = aObject;
272: }
273: }
|