001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.feedback;
018:
019: import java.io.Serializable;
020:
021: import org.apache.wicket.Component;
022: import org.apache.wicket.IClusterable;
023:
024: /**
025: * Represents a generic message meant for the end-user/ pages.
026: *
027: * @author Eelco Hillenius
028: * @author Jonathan Locke
029: */
030: public class FeedbackMessage implements IClusterable {
031: private static final long serialVersionUID = 1L;
032:
033: /** Constant for debug level. */
034: public static final int DEBUG = 1;
035:
036: /** Constant for error level. */
037: public static final int ERROR = 4;
038:
039: /** Constant for fatal level. */
040: public static final int FATAL = 5;
041:
042: /** Constant for info level. */
043: public static final int INFO = 2;
044:
045: /**
046: * Constant for an undefined level; note that components might decide not to
047: * render anything when this level is used.
048: */
049: public static final int UNDEFINED = 0;
050:
051: /** Constant for warning level. */
052: public static final int WARNING = 3;
053:
054: /** Levels as strings for debugging. */
055: private static final String[] levelStrings = new String[] {
056: "UNDEFINED", "DEBUG", "INFO", "WARNING", "ERROR", "FATAL" };
057:
058: /**
059: * The message level; can be used by rendering components. Note that what
060: * actually happens with the level indication is totally up to the
061: * components that render messages like these. The default level is
062: * UNDEFINED.
063: */
064: private final int level;
065:
066: /** The actual message. */
067: private final Serializable message;
068:
069: /** The reporting component. */
070: private final Component reporter;
071:
072: /** Whether or not this message has been rendered */
073: private boolean rendered = false;
074:
075: /**
076: * Construct using fields.
077: *
078: * @param reporter
079: * The message reporter
080: * @param message
081: * The actual message
082: * @param level
083: * The level of the message
084: * @param sessionStored
085: * Whether or not this message will be stored in session
086: */
087: public FeedbackMessage(final Component reporter,
088: final Serializable message, final int level) {
089: this .reporter = reporter;
090: this .message = message;
091: this .level = level;
092: }
093:
094: /**
095: * Gets whether or not this message has been rendered
096: *
097: * @return true if this message has been rendered, false otherwise
098: */
099: public final boolean isRendered() {
100: return rendered;
101: }
102:
103: /**
104: * Marks this message as rendered.
105: */
106: public final void markRendered() {
107: this .rendered = true;
108: }
109:
110: /**
111: * Gets the message level; can be used by rendering components. Note that
112: * what actually happens with the level indication is totally up to the
113: * components that render feedback messages.
114: *
115: * @return The message level indicator.
116: */
117: public final int getLevel() {
118: return level;
119: }
120:
121: /**
122: * Gets the current level as a String
123: *
124: * @return The current level as a String
125: */
126: public String getLevelAsString() {
127: return levelStrings[getLevel()];
128: }
129:
130: /**
131: * Gets the actual message.
132: *
133: * @return the message.
134: */
135: public final Serializable getMessage() {
136: return message;
137: }
138:
139: /**
140: * Gets the reporting component.
141: *
142: * @return the reporting component.
143: */
144: public final Component getReporter() {
145: return reporter;
146: }
147:
148: /**
149: * Gets whether the current level is DEBUG or up.
150: *
151: * @return whether the current level is DEBUG or up.
152: */
153: public final boolean isDebug() {
154: return isLevel(DEBUG);
155: }
156:
157: /**
158: * Gets whether the current level is ERROR or up.
159: *
160: * @return whether the current level is ERROR or up.
161: */
162: public final boolean isError() {
163: return isLevel(ERROR);
164: }
165:
166: /**
167: * Gets whether the current level is FATAL or up.
168: *
169: * @return whether the current level is FATAL or up.
170: */
171: public final boolean isFatal() {
172: return isLevel(FATAL);
173: }
174:
175: /**
176: * Gets whether the current level is INFO or up.
177: *
178: * @return whether the current level is INFO or up.
179: */
180: public final boolean isInfo() {
181: return isLevel(INFO);
182: }
183:
184: /**
185: * Returns whether this level is greater than or equal to the given level.
186: *
187: * @param level
188: * the level
189: * @return whether this level is greater than or equal to the given level
190: */
191: public final boolean isLevel(int level) {
192: return (getLevel() >= level);
193: }
194:
195: /**
196: * Gets whether the current level is UNDEFINED.
197: *
198: * @return whether the current level is UNDEFINED.
199: */
200: public final boolean isUndefined() {
201: return (getLevel() == UNDEFINED);
202: }
203:
204: /**
205: * Gets whether the current level is WARNING or up.
206: *
207: * @return whether the current level is WARNING or up.
208: */
209: public final boolean isWarning() {
210: return isLevel(WARNING);
211: }
212:
213: /**
214: * @see java.lang.Object#toString()
215: */
216: public String toString() {
217: return "[FeedbackMessage message = \""
218: + getMessage()
219: + "\", reporter = "
220: + ((getReporter() == null) ? "null" : getReporter()
221: .getId()) + ", level = " + getLevelAsString()
222: + "]";
223: }
224: }
|