summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/terminal/CompositeErrorMessage.java
blob: 7d47c1ea11cb1c5f41b57ee8181568631e95d09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* *************************************************************************
 
                               IT Mill Toolkit 

               Development of Browser User Interfaces Made Easy

                    Copyright (C) 2000-2006 IT Mill Ltd
                     
   *************************************************************************

   This product is distributed under commercial license that can be found
   from the product package on license/license.txt. Use of this product might 
   require purchasing a commercial license from IT Mill Ltd. For guidelines 
   on usage, see license/licensing-guidelines.html

   *************************************************************************
   
   For more information, contact:
   
   IT Mill Ltd                           phone: +358 2 4802 7180
   Ruukinkatu 2-4                        fax:   +358 2 4802 7181
   20540, Turku                          email:  info@itmill.com
   Finland                               company www: www.itmill.com
   
   Primary source for information and releases: www.itmill.com

   ********************************************************************** */

package com.itmill.toolkit.terminal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/** Class for combining multiple error messages together.
 *
 * @author  IT Mill Ltd
 * @version @VERSION@
 * @since 3.0
 */
public class CompositeErrorMessage implements ErrorMessage {

	/** Array of all the errors */
	private List errors;

	/** Level of the error */
	private int level;

	/** Constructor for CompositeErrorMessage.
	 * 
	 * @param errorMessages Array of error messages that are listed togeter. 
	 * Nulls are ignored, but at least one message is required.
	 * @throws NullPointerException if errorMessages is null.
	 * 	 * @throws IllegalArgumentException if the array was empty. 	 
	  */
	public CompositeErrorMessage(ErrorMessage[] errorMessages) {
		errors = new ArrayList(errorMessages.length);
		level = Integer.MIN_VALUE;

		for (int i = 0; i < errorMessages.length; i++) {
			addErrorMessage(errorMessages[i]);
		}

		if (errors.size() == 0)
			throw new IllegalArgumentException("Composite error message must have at least one error");

	}

	/** Constructor for CompositeErrorMessage.
	 * @param errorMessages Collection of error messages that are listed
	 * togeter. At least one message is required.
	 * @throws NullPointerException if the collection is null.
	 * @throws IllegalArgumentException if the collection was empty.
	 */
	public CompositeErrorMessage(Collection errorMessages) {
		errors = new ArrayList(errorMessages.size());
		level = Integer.MIN_VALUE;

		for (Iterator i = errorMessages.iterator(); i.hasNext();) {
			addErrorMessage((ErrorMessage) i.next());
		}

		if (errors.size() == 0)
			throw new IllegalArgumentException("Composite error message must have at least one error");
	}

	/** The error level is the largest error level in 
	 * @see com.itmill.toolkit.terminal.ErrorMessage#getErrorLevel()
	 */
	public final int getErrorLevel() {
		return level;
	}

	/** Add a error message into this composite message.
	 *  Updates the level field.
	 * @param error The error message to be added. Duplicate errors are ignored.
	 */
	private void addErrorMessage(ErrorMessage error) {
		if (error != null && !errors.contains(error)) {
			this.errors.add(error);
			int l = error.getErrorLevel();
			if (l > level)
				level = l;
		}
	}

	/** Get Error Iterator. */
	public Iterator iterator() {
		return errors.iterator();
	}

	public void paint(PaintTarget target) throws PaintException {

		if (errors.size() == 1)
			 ((ErrorMessage) errors.iterator().next()).paint(target);
		else {
			target.startTag("error");

			if (level > 0 && level <= ErrorMessage.INFORMATION)
				target.addAttribute("level", "info");
			else if (level <= ErrorMessage.WARNING)
				target.addAttribute("level", "warning");
			else if (level <= ErrorMessage.ERROR)
				target.addAttribute("level", "error");
			else if (level <= ErrorMessage.CRITICAL)
				target.addAttribute("level", "critical");
			else
				target.addAttribute("level", "system");

			// Paint all the exceptions
			for (Iterator i = errors.iterator(); i.hasNext();) {
				((ErrorMessage) i.next()).paint(target);
			}

			target.endTag("error");
		}
	}

	/* Documented in super interface */
	public void addListener(RepaintRequestListener listener) {
	}

	/* Documented in super interface */
	public void removeListener(RepaintRequestListener listener) {
	}

	/* Documented in super interface */
	public void requestRepaint() {
	}

	/* Documented in super interface */
	public void requestRepaintRequests() {
	}

	/** Returns a comma separated list of the error messages.
	 * @return String, comma separated list of error messages.
	 */
	public String toString() {
		String retval = "[";
		int pos = 0;
		for (Iterator i = errors.iterator(); i.hasNext();) {
			if (pos > 0)
				retval += ",";
			pos++;
			retval += i.next().toString();
		}
		retval += "]";

		return retval;
	}
}