View Javadoc

1   /*
2    * Created on Mar 12, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.widgets;
13  import org.apache.commons.lang.StringUtils;
14  /***
15   * Describes correctness of data in a widget.
16   * @author Martin Vysny
17   */
18  public final class ValidityMessages {
19  	/***
20  	 * Constructor, sets all three strings to <code>null</code>. Calls
21  	 * <code>this((String)null)</code>.
22  	 */
23  	public ValidityMessages() {
24  		this((String) null);
25  	}
26  	/***
27  	 * Constructor, sets error message.
28  	 * @param error the error message to set.
29  	 */
30  	public ValidityMessages(final String error) {
31  		super();
32  		this.error = error;
33  	}
34  	/***
35  	 * Clones given object.
36  	 * @param other object to clone. If <code>null</code> then all messages
37  	 * will be <code>null</code>.
38  	 */
39  	public ValidityMessages(final ValidityMessages other) {
40  		super();
41  		this.error = other == null ? null : other.error;
42  		this.warning = other == null ? null : other.warning;
43  		this.info = other == null ? null : other.info;
44  	}
45  	/***
46  	 * Error message. If <code>null</code> or empty string then there is no
47  	 * error.
48  	 */
49  	public String error = null;
50  	/***
51  	 * Warning message. If <code>null</code> or empty string then there is no
52  	 * warning.
53  	 */
54  	public String warning = null;
55  	/***
56  	 * Info message. If <code>null</code> or empty string then there is no
57  	 * info.
58  	 */
59  	public String info = null;
60  	/*
61  	 * (non-Javadoc)
62  	 * @see java.lang.Object#toString()
63  	 */
64  	@Override
65  	public String toString() {
66  		StringBuilder result = new StringBuilder();
67  		if (error != null) {
68  			result.append("Error: "); //$NON-NLS-1$
69  			result.append(error);
70  			result.append("; "); //$NON-NLS-1$
71  		}
72  		if (warning != null) {
73  			result.append("Warning: "); //$NON-NLS-1$
74  			result.append(warning);
75  			result.append("; "); //$NON-NLS-1$
76  		}
77  		if (info != null) {
78  			result.append("Info: "); //$NON-NLS-1$
79  			result.append(info);
80  			result.append("; "); //$NON-NLS-1$
81  		}
82  		if (result.length() == 0)
83  			result.append("OK"); //$NON-NLS-1$
84  		return result.toString();
85  	}
86  	/***
87  	 * Returns level of messages.
88  	 * @return level of message, or <code>null</code> if the message does not
89  	 * contain error, warning nor information.
90  	 */
91  	public MessageLevelEnum getLevel() {
92  		if (!StringUtils.isEmpty(error))
93  			return MessageLevelEnum.ERROR;
94  		if (!StringUtils.isEmpty(warning))
95  			return MessageLevelEnum.WARNING;
96  		if (!StringUtils.isEmpty(info))
97  			return MessageLevelEnum.INFO;
98  		return null;
99  	}
100 	/***
101 	 * Checks if two validity messages equals, in terms of changing the
102 	 * displayed text.
103 	 * @param msg1 first messages object.
104 	 * @param msg2 second messages object.
105 	 * @return <code>true</code> if setting <code>other</code> over this
106 	 * will not change the displayed message.
107 	 */
108 	public final static boolean equalsTopLevelMsg(ValidityMessages msg1,
109 			ValidityMessages msg2) {
110 		if (msg1 != null)
111 			return msg1.equalsTopLevelMsg(msg2);
112 		if (msg2 != null)
113 			return msg2.equalsTopLevelMsg(msg1);
114 		return true;
115 	}
116 	/***
117 	 * Checks if two validity messages equals, in terms of changing the
118 	 * displayed text.
119 	 * @param other the other messages object.
120 	 * @return <code>true</code> if setting <code>other</code> over this
121 	 * will not change the displayed message.
122 	 */
123 	public final boolean equalsTopLevelMsg(ValidityMessages other) {
124 		if (this == other)
125 			return true;
126 		final MessageLevelEnum level = getLevel();
127 		if (other == null) {
128 			return (level == null);
129 		}
130 		if (level != other.getLevel())
131 			return false;
132 		if (level == null)
133 			return true;
134 		switch (level) {
135 			case ERROR:
136 				return error.equals(other.error);
137 			case WARNING:
138 				return warning.equals(other.warning);
139 			case INFO:
140 				return info.equals(other.info);
141 		}
142 		// we should have checked all possibilities
143 		throw new AssertionError();
144 	}
145 	/***
146 	 * Checks if given messages contains error.
147 	 * @param messages messages to check
148 	 * @return <code>true</code> if it contains non-empty error message
149 	 */
150 	public static boolean isError(final ValidityMessages messages) {
151 		if (messages == null)
152 			return false;
153 		return !StringUtils.isEmpty(messages.error);
154 	}
155 	/***
156 	 * Sets given field.
157 	 * @param level determines the field.
158 	 * @param message the message.
159 	 * @param overwrite if <code>false</code> and given field is not
160 	 * <code>null</code> then it is not overwritten.
161 	 */
162 	public void setField(final MessageLevelEnum level, final String message,
163 			final boolean overwrite) {
164 		switch (level) {
165 			case ERROR:
166 				if ((error == null) || overwrite)
167 					error = message;
168 				break;
169 			case WARNING:
170 				if ((warning == null) || overwrite)
171 					warning = message;
172 				break;
173 			case INFO:
174 				if ((info == null) || overwrite)
175 					info = message;
176 				break;
177 		}
178 	}
179 	/***
180 	 * Sets field to a value and returns instance of messages object. Creates
181 	 * new instance of messages object if needed (<code>messages==null && message!=null</code>).
182 	 * @param messages current message object. Overwritten if not
183 	 * <code>null</code>.
184 	 * @param level determines the field
185 	 * @param message the message
186 	 * @param overwrite if <code>false</code> and given field is not
187 	 * <code>null</code> then it is not overwritten.
188 	 * @return messages object with given message set.
189 	 */
190 	public static ValidityMessages setField(ValidityMessages messages,
191 			final MessageLevelEnum level, final String message,
192 			final boolean overwrite) {
193 		if (messages == null) {
194 			if (message == null)
195 				return null;
196 			messages = new ValidityMessages();
197 		}
198 		messages.setField(level, message, overwrite);
199 		return messages;
200 	}
201 }