001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors:
019: * Ezequiel Cuellar <ecuellar@crosslogic.com>
020: * Robert Morris <robertj@morris.net>
021: */
022:
023: package org.swingml;
024:
025: import java.util.*;
026:
027: import org.swingml.component.*;
028: import org.swingml.errors.*;
029: import org.swingml.errors.handlers.*;
030:
031: public class SwingMLModel extends AbstractSwingMLModel implements
032: ISwingMLErrorHandlerContainer {
033:
034: private ColorComponent background;
035: private Map errorHandlers;
036: private String fontName;
037: private String fontSize;
038: private String fontStyle;
039: private ColorComponent foreground;
040: private String icon;
041: private String orientation;
042: private String text;
043: private String tooltip;
044: private boolean visible = true;
045:
046: public SwingMLModel() {
047: super ();
048: }
049:
050: public void addHandler(int errorType, ISwingMLErrorHandler handler) {
051: Integer key = new Integer(errorType);
052:
053: List handlers;
054: if (getErrorHandlers().containsKey(key)) {
055: handlers = (List) getErrorHandlers().get(key);
056: } else {
057: handlers = new ArrayList();
058: }
059: handlers.add(handler);
060:
061: getErrorHandlers().put(key, handlers);
062: }
063:
064: /**
065: * Find the appropriate error handlers for the given group of errors (Should all have same error type) and handle them.
066: * @param errors
067: */
068: private void doHandleErrors(List errors) {
069: if (errors != null && errors.size() > 0) {
070: // Find all registered handlers for this error type
071: int errorType = ((ISwingMLError) errors.get(0)).getType();
072: ISwingMLErrorHandler[] handlers = getHandlers(errorType);
073: ISwingMLErrorHandler handler;
074: if (handlers != null && handlers.length > 0) {
075: for (int y = 0; y < handlers.length; y++) {
076: handler = handlers[y];
077: // Tell this handler to handle this error
078: handler.handle(getContainer(),
079: (ISwingMLError[]) errors
080: .toArray(new ISwingMLError[errors
081: .size()]));
082: }
083: }
084: }
085: }
086:
087: public ColorComponent getBackground() {
088: return background;
089: }
090:
091: public Map getErrorHandlers() {
092: if (errorHandlers == null) {
093: errorHandlers = new HashMap();
094: }
095: return errorHandlers;
096: }
097:
098: public String getFontName() {
099: return fontName;
100: }
101:
102: public String getFontSize() {
103: return fontSize;
104: }
105:
106: public String getFontStyle() {
107: return fontStyle;
108: }
109:
110: public ColorComponent getForeground() {
111: return foreground;
112: }
113:
114: public ISwingMLErrorHandler[] getHandlers(int errorType) {
115: ISwingMLErrorHandler[] result = null;
116:
117: List handlers = (List) getErrorHandlers().get(
118: new Integer(errorType));
119: if (handlers != null) {
120: result = (ISwingMLErrorHandler[]) handlers
121: .toArray(new ISwingMLErrorHandler[handlers.size()]);
122: }
123:
124: if (result == null) {
125: // Use a DefaultErrorHandler
126: result = new ISwingMLErrorHandler[1];
127: result[0] = ErrorHandlerFactory.getDefaultHandler();
128: }
129:
130: return result;
131: }
132:
133: public String getIcon() {
134: return this .icon;
135: }
136:
137: public String getOrientation() {
138: return this .orientation;
139: }
140:
141: public String getText() {
142: return this .text;
143: }
144:
145: public String getTooltip() {
146: return this .tooltip;
147: }
148:
149: /**
150: * Handle the given errors by delegating to the assigned ErrorHandler.
151: */
152: public void handle(ISwingMLError[] errors) {
153: if (errors != null && errors.length > 0) {
154: // group by error type and handle each type as a group
155: List sortedErrors = Arrays.asList(errors);
156: Collections.sort(sortedErrors, new Comparator() {
157:
158: public int compare(Object o1, Object o2) {
159: ISwingMLError error1 = (ISwingMLError) o1;
160: ISwingMLError error2 = (ISwingMLError) o2;
161: if (error1.getType() < error2.getType()) {
162: return -1;
163: } else if (error1.getType() > error2.getType()) {
164: return 1;
165: } else {
166: return 0;
167: }
168: }
169: });
170:
171: // Iterate over all the errors
172: ISwingMLError error;
173: List errorGroup = new ArrayList();
174: int currentErrorType = ISwingMLError.ERROR_UNKNOWN;
175: Iterator schmiterator = sortedErrors.iterator();
176: while (schmiterator.hasNext()) {
177: error = (ISwingMLError) schmiterator.next();
178: if (error.getType() != currentErrorType) {
179: // process the errorGroup and start new
180: doHandleErrors(errorGroup);
181:
182: errorGroup.clear();
183: currentErrorType = error.getType();
184: }
185: errorGroup.add(error);
186: }
187: // handle the last group
188: doHandleErrors(errorGroup);
189: }
190: }
191:
192: public boolean isVisible() {
193: return this .visible;
194: }
195:
196: public void removeHandler(int errorType,
197: ISwingMLErrorHandler handler) {
198: ISwingMLErrorHandler[] handlers = getHandlers(errorType);
199: if (handlers != null && handlers.length > 0) {
200: List newHandlers = new ArrayList();
201: for (int x = 0; x < handlers.length; x++) {
202: if (!handlers[x].equals(handler)) {
203: newHandlers.add(handlers[x]);
204: }
205: }
206: getErrorHandlers().put(new Integer(errorType), newHandlers);
207: }
208: }
209:
210: public void removeHandlers(int errorType) {
211: getErrorHandlers().remove(new Integer(errorType));
212: }
213:
214: public void setBackground(ColorComponent component) {
215: background = component;
216: }
217:
218: public void setErrorHandlers(Map handlers) {
219: this .errorHandlers = handlers;
220: }
221:
222: public void setFontName(String string) {
223: fontName = string;
224: }
225:
226: public void setFontSize(String string) {
227: fontSize = string;
228: }
229:
230: public void setFontStyle(String string) {
231: fontStyle = string;
232: }
233:
234: public void setForeground(ColorComponent component) {
235: foreground = component;
236: }
237:
238: public void setIcon(String anIcon) {
239: this .icon = anIcon;
240: }
241:
242: public void setOrientation(String anOrientation) {
243: this .orientation = anOrientation;
244: }
245:
246: public void setText(String aText) {
247: this .text = aText;
248: }
249:
250: public void setTooltip(String aTooltip) {
251: this .tooltip = aTooltip;
252: }
253:
254: public void setVisible(boolean aVisible) {
255: this .visible = aVisible;
256: }
257:
258: public void validate() {
259: }
260: }
|