01: /*
02: * uDig - User Friendly Desktop Internet GIS client
03: * http://udig.refractions.net
04: * (C) 2004, Refractions Research Inc.
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package net.refractions.udig.ui;
18:
19: import net.refractions.udig.internal.ui.UiPlugin;
20: import net.refractions.udig.ui.internal.Messages;
21:
22: import org.eclipse.jface.viewers.ICellEditorValidator;
23: import org.geotools.feature.AttributeType;
24: import org.geotools.feature.Feature;
25: import org.geotools.feature.FeatureType;
26:
27: /**
28: * Validates that an input is a legal input
29: *
30: * @author jeichar
31: * @since 0.3
32: */
33: public class AttributeValidator implements ICellEditorValidator {
34: private final AttributeType attributeType;
35: private final FeatureType featureType;
36: private Object[] values;
37: private int indexof;
38:
39: /**
40: * Creates a new instance of AttributeValidator
41: *
42: * @param attributeType The AttributeType that the new instance will validate
43: * @param featureType the featureType that contains the attributeType.
44: */
45: public AttributeValidator(AttributeType attributeType,
46: FeatureType featureType) {
47: this .attributeType = attributeType;
48: this .featureType = featureType;
49: values = new Object[featureType.getAttributeCount()];
50: for (int i = 0; i < featureType.getAttributeCount(); i++) {
51: AttributeType attributeType2 = featureType
52: .getAttributeType(i);
53: if (attributeType2 == attributeType) {
54: this .indexof = i;
55: }
56: values[i] = attributeType2.createDefaultValue();
57: }
58: }
59:
60: @SuppressWarnings("unchecked")
61: public String isValid(Object value) {
62: if (value == null
63: || (value instanceof String && ((String) value)
64: .equals(""))) { //$NON-NLS-1$
65: if (!attributeType.isNillable())
66: return Messages.AttributeValidator_missingAtt1
67: + attributeType.getName()
68: + Messages.AttributeValidator_missingAtt2;
69: else
70: return null;
71: }
72: if (!attributeType.getType().isAssignableFrom(value.getClass())) {
73: return Messages.AttributeValidator_wrongType
74: + attributeType.getType().getSimpleName();
75: }
76: values[indexof] = value;
77: try {
78: Feature feature = featureType.create(values);
79: if (attributeType.getRestriction() != null
80: && !attributeType.getRestriction()
81: .contains(feature))
82: return Messages.AttributeValidator_restriction
83: + attributeType.getRestriction();
84: } catch (Throwable e1) {
85: UiPlugin
86: .log(
87: "Tried to create a feature for validating the attribute value but something went wrong (this may not be an error)", e1); //$NON-NLS-1$
88: }
89:
90: try {
91: attributeType.validate(value);
92: return null;
93: } catch (Throwable e) {
94: return e.getLocalizedMessage();
95: }
96: }
97: }
|