001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal;
016:
017: import org.apache.tapestry.Asset;
018: import org.apache.tapestry.BaseValidationDecorator;
019: import org.apache.tapestry.Field;
020: import org.apache.tapestry.MarkupWriter;
021: import org.apache.tapestry.ValidationTracker;
022: import org.apache.tapestry.dom.Element;
023: import org.apache.tapestry.ioc.Messages;
024: import org.apache.tapestry.services.Environment;
025: import org.apache.tapestry.services.ValidationMessagesSource;
026:
027: /**
028: * Default implementation that writes an attribute into fields or labels that are in error.
029: */
030: public final class DefaultValidationDecorator extends
031: BaseValidationDecorator {
032: private final Environment _environment;
033:
034: private Asset _iconAsset;
035:
036: private Messages _validationMessages;
037:
038: /**
039: * @param environment
040: * used to locate objects and services during the render
041: * @param validationMessages
042: * obtained from {@link ValidationMessagesSource}, used to obtain the label for the
043: * icon
044: * @param iconAsset
045: * asset for an icon that will be displayed after each field (marked with the
046: * "t-invisible" CSS class, if the field is not in error)
047: */
048: public DefaultValidationDecorator(final Environment environment,
049: Messages validationMessages, Asset iconAsset) {
050: _environment = environment;
051: _validationMessages = validationMessages;
052: _iconAsset = iconAsset;
053: }
054:
055: @Override
056: public void insideField(Field field) {
057: if (inError(field))
058: addErrorClassToCurrentElement();
059: }
060:
061: @Override
062: public void insideLabel(Field field, Element element) {
063: if (field == null)
064: return;
065:
066: if (inError(field))
067: addErrorClass(element);
068: }
069:
070: @Override
071: public void afterField(Field field) {
072: String iconId = field.getClientId() + ":icon";
073:
074: MarkupWriter writer = _environment
075: .peekRequired(MarkupWriter.class);
076:
077: String cssClass = inError(field) ? "t-error-icon"
078: : "t-error-icon t-invisible";
079:
080: writer.element("img", "src", _iconAsset.toClientURL(), "alt",
081: _validationMessages.get("icon-label"), "class",
082: cssClass, "id", iconId);
083: writer.end();
084: }
085:
086: private boolean inError(Field field) {
087: ValidationTracker tracker = _environment
088: .peekRequired(ValidationTracker.class);
089:
090: return tracker.inError(field);
091: }
092:
093: private void addErrorClassToCurrentElement() {
094: MarkupWriter writer = _environment
095: .peekRequired(MarkupWriter.class);
096:
097: Element element = writer.getElement();
098:
099: addErrorClass(element);
100: }
101:
102: private void addErrorClass(Element element) {
103: String current = element.getAttribute("class");
104:
105: String newValue = current == null ? InternalConstants.TAPESTRY_ERROR_CLASS
106: : current + " "
107: + InternalConstants.TAPESTRY_ERROR_CLASS;
108:
109: element.forceAttributes("class", newValue);
110: }
111:
112: }
|