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.constraint;
016:
017: import org.araneaframework.Environment;
018: import org.araneaframework.core.AraneaRuntimeException;
019: import org.araneaframework.core.Assert;
020: import org.araneaframework.core.NoSuchEnvironmentEntryException;
021: import org.araneaframework.uilib.form.FormElement;
022: import org.araneaframework.uilib.form.FormElementContext;
023:
024: /**
025: * {@link org.araneaframework.uilib.form.Constraint} that is associated with
026: * some {@link org.araneaframework.uilib.form.FormElement}.
027: *
028: * @author Taimo Peelo (taimo@araneaframework.org)
029: */
030: public abstract class BaseFieldConstraint extends BaseConstraint {
031: private FormElement field;
032:
033: public BaseFieldConstraint() {
034: }
035:
036: // Constraints environment should always be set to field environment.
037: // however there is no guarantee that field has been initialized when
038: // constructor is called, so the environment may be missing crucial entries.
039: // Just setting field constraint works (then constraints environment is set when
040: // field is initialized). That would break constraint previously set however.
041: public BaseFieldConstraint(FormElement field) {
042: Assert.notNullParam(this , field, "field");
043: this .field = field;
044: }
045:
046: /**
047: * Returns the {@link FormElement} that
048: * this {@link org.araneaframework.uilib.form.Constraint} is constraining.
049: *
050: * @return constrained {@link FormElement}
051: */
052: protected FormElementContext getField() {
053: if (field != null)
054: return field;
055:
056: FormElementContext result;
057: try {
058: result = (FormElementContext) getEnvironment()
059: .requireEntry(FormElementContext.class);
060: } catch (NoSuchEnvironmentEntryException e) {
061: throw new FieldConstraintException(
062: Assert.this ToString(this )
063: + " could not determine FormElementContext, this is probably caused by applying field constraint to something other than FormElement.",
064: e);
065: }
066: return result;
067: }
068:
069: public Environment getEnvironment() {
070: if (field == null)
071: return super .getEnvironment();
072: return field.getConstraintEnvironment();
073: }
074:
075: protected String getLabel() {
076: return getField().getLabel();
077: }
078:
079: protected Object getValue() {
080: return getField().getValue();
081: }
082:
083: public boolean isRead() {
084: return getField().isRead();
085: }
086:
087: public boolean isDisabled() {
088: return getField().isDisabled();
089: }
090:
091: public boolean isMandatory() {
092: return getField().isMandatory();
093: }
094:
095: /**
096: * Exception thrown when {@link org.araneaframework.uilib.form.FormElement} associated with
097: * {@link BaseFieldConstraint} could not be determined.
098: */
099: public static class FieldConstraintException extends
100: AraneaRuntimeException {
101: public FieldConstraintException() {
102: super ();
103: }
104:
105: public FieldConstraintException(String message, Throwable cause) {
106: super (message, cause);
107: }
108:
109: public FieldConstraintException(String message) {
110: super (message);
111: }
112:
113: public FieldConstraintException(Throwable cause) {
114: super(cause);
115: }
116: }
117: }
|