001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * 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, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.form;
017
018 import java.util.Iterator;
019
020 import org.springframework.binding.validation.ValidationMessage;
021 import org.springframework.binding.validation.ValidationResults;
022 import org.springframework.binding.validation.ValidationResultsModel;
023 import org.springframework.richclient.dialog.Messagable;
024 import org.springframework.util.Assert;
025
026 /**
027 * An implementation of ValidationResultsReporter that reports only a single
028 * message from the configured validation results model to the associated
029 * message receiver. More details of the searching process can be found in the
030 * {@link #getValidationMessage(ValidationResults)} method.
031 *
032 * @author Keith Donald
033 * @author Jan Hoskens
034 */
035 public class SimpleValidationResultsReporter implements ValidationResultsReporter {
036
037 /** ResultsModel containing the messages. */
038 private ValidationResultsModel resultsModel;
039
040 /** Recipient for the message. */
041 private Messagable messageReceiver;
042
043 /**
044 * Constructor.
045 *
046 * @param resultsModel ValidationResultsModel to monitor and report on.
047 * @param messageReceiver The receiver for validation messages.
048 */
049 public SimpleValidationResultsReporter(ValidationResultsModel resultsModel, Messagable messageReceiver) {
050 Assert.notNull(resultsModel, "resultsModel is required");
051 Assert.notNull(messageReceiver, "messagePane is required");
052 this.resultsModel = resultsModel;
053 this.messageReceiver = messageReceiver;
054 init();
055 }
056
057 /**
058 * Initialize listener and trigger a first-time check.
059 */
060 private void init() {
061 resultsModel.addValidationListener(this);
062 validationResultsChanged(null);
063 }
064
065 /**
066 * Clear the messageReceiver.
067 */
068 public void clearErrors() {
069 messageReceiver.setMessage(null);
070 }
071
072 /**
073 * Handle a change in the validation results model. Update the message
074 * receiver based on our current results model state.
075 */
076 public void validationResultsChanged(ValidationResults results) {
077 if (resultsModel.getMessageCount() == 0) {
078 messageReceiver.setMessage(null);
079 }
080 else {
081 ValidationMessage message = getValidationMessage(resultsModel);
082 messageReceiver.setMessage(message);
083 }
084 }
085
086 /**
087 * <p>
088 * Get the message that should be reported.
089 * </p>
090 *
091 * Searching takes following rules into account:
092 * <ul>
093 * <li>Severity of the selected message is the most severe one (INFO <
094 * WARNING < ERROR).</li>
095 * <li>Timestamp of the selected message is the most recent one of the
096 * result of the previous rule.</li>
097 * </ul>
098 *
099 * Any custom Severities will be placed in order according to their given
100 * magnitude.
101 *
102 * @param resultsModel Search this model to find the message.
103 * @return the message to display on the Messagable.
104 */
105 protected ValidationMessage getValidationMessage(ValidationResults resultsModel) {
106 ValidationMessage validationMessage = null;
107 for (Iterator i = resultsModel.getMessages().iterator(); i.hasNext();) {
108 ValidationMessage tmpMessage = (ValidationMessage) i.next();
109 if (validationMessage == null
110 || (validationMessage.getSeverity().compareTo(tmpMessage.getSeverity()) < 0)
111 || ((validationMessage.getTimestamp() < tmpMessage.getTimestamp()) && (validationMessage
112 .getSeverity() == tmpMessage.getSeverity()))) {
113 validationMessage = tmpMessage;
114 }
115 }
116 return validationMessage;
117 }
118
119 /**
120 * @see org.springframework.richclient.form.ValidationResultsReporter#hasErrors()
121 */
122 public boolean hasErrors() {
123 return resultsModel.getHasErrors();
124 }
125 }