001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.faces.application;
030:
031: import java.util.*;
032:
033: public class FacesMessage implements java.io.Serializable {
034: public static final Severity SEVERITY_INFO = new Severity("info", 0);
035: public static final Severity SEVERITY_WARN = new Severity("warn", 1);
036: public static final Severity SEVERITY_ERROR = new Severity("error",
037: 2);
038: public static final Severity SEVERITY_FATAL = new Severity("fatal",
039: 3);
040:
041: public static final String FACES_MESSAGES = "javax.faces.Messages";
042:
043: public static final List VALUES;
044: public static final Map VALUES_MAP;
045:
046: private Severity severity;
047: private String summary;
048: private String detail;
049:
050: public FacesMessage() {
051: this .severity = SEVERITY_INFO;
052: }
053:
054: public FacesMessage(String summary) {
055: this .severity = SEVERITY_INFO;
056: this .summary = summary;
057: }
058:
059: public FacesMessage(String summary, String detail) {
060: this .severity = SEVERITY_INFO;
061: this .summary = summary;
062: this .detail = detail;
063: }
064:
065: public FacesMessage(Severity severity, String summary, String detail) {
066: this .severity = severity;
067: this .summary = summary;
068: this .detail = detail;
069: }
070:
071: /**
072: * Return the localized detail text. If no localized detail text has been
073: * defined for this message, return the localized summary text instead.
074: * @return
075: */
076: public String getDetail() {
077: if (this .detail == null)
078: return this .summary;
079:
080: return this .detail;
081: }
082:
083: public void setDetail(String detail) {
084: this .detail = detail;
085: }
086:
087: public Severity getSeverity() {
088: return this .severity;
089: }
090:
091: public void setSeverity(Severity severity) {
092: this .severity = severity;
093: }
094:
095: public String getSummary() {
096: return this .summary;
097: }
098:
099: public void setSummary(String summary) {
100: this .summary = summary;
101: }
102:
103: public static class Severity implements Comparable {
104: private String _name;
105: private int _value;
106:
107: Severity(String name, int value) {
108: _name = name;
109: _value = value;
110: }
111:
112: public int getOrdinal() {
113: return _value;
114: }
115:
116: public int compareTo(Object other) {
117: if (!(other instanceof Severity))
118: return -1;
119:
120: Severity severity = (Severity) other;
121:
122: if (_value < severity._value)
123: return -1;
124: else if (severity._value < _value)
125: return 1;
126: else
127: return 0;
128: }
129:
130: public String toString() {
131: return _name;
132: }
133: }
134:
135: static {
136: ArrayList<Severity> list = new ArrayList<Severity>();
137:
138: list.add(SEVERITY_INFO);
139: list.add(SEVERITY_WARN);
140: list.add(SEVERITY_ERROR);
141: list.add(SEVERITY_FATAL);
142:
143: VALUES = Collections.unmodifiableList(list);
144:
145: HashMap<String, Severity> map = new HashMap<String, Severity>();
146:
147: map.put(SEVERITY_INFO.toString(), SEVERITY_INFO);
148: map.put(SEVERITY_WARN.toString(), SEVERITY_WARN);
149: map.put(SEVERITY_ERROR.toString(), SEVERITY_ERROR);
150: map.put(SEVERITY_FATAL.toString(), SEVERITY_FATAL);
151:
152: VALUES_MAP = Collections.unmodifiableMap(map);
153: }
154:
155: public String toString() {
156: if (this .detail != null)
157: return "FacesMessage[" + this .severity + ",\""
158: + this .summary + "\",\"" + this .detail + "\"]";
159: else if (this .summary != null)
160: return "FacesMessage[" + this .severity + ",\""
161: + this .summary + "\"]";
162: else
163: return "FacesMessage[" + this .severity + "]";
164: }
165: }
|