/*
* SwingML Copyright (C) 2002 SwingML Team
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option) any
* later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Authors:
* Ezequiel Cuellar <ecuellar@crosslogic.com>
* Robert Morris <robertj@morris.net>
*/
package org.swingml;
import java.util.*;
import org.swingml.component.*;
import org.swingml.errors.*;
import org.swingml.errors.handlers.*;
public class SwingMLModel extends AbstractSwingMLModel implements ISwingMLErrorHandlerContainer {
private ColorComponent background;
private Map errorHandlers;
private String fontName;
private String fontSize;
private String fontStyle;
private ColorComponent foreground;
private String icon;
private String orientation;
private String text;
private String tooltip;
private boolean visible = true;
public SwingMLModel () {
super();
}
public void addHandler (int errorType, ISwingMLErrorHandler handler) {
Integer key = new Integer(errorType);
List handlers;
if (getErrorHandlers().containsKey(key)) {
handlers = (List) getErrorHandlers().get(key);
} else {
handlers = new ArrayList();
}
handlers.add(handler);
getErrorHandlers().put(key, handlers);
}
/**
* Find the appropriate error handlers for the given group of errors (Should all have same error type) and handle them.
* @param errors
*/
private void doHandleErrors (List errors) {
if (errors != null && errors.size() > 0) {
// Find all registered handlers for this error type
int errorType = ((ISwingMLError) errors.get(0)).getType();
ISwingMLErrorHandler[] handlers = getHandlers(errorType);
ISwingMLErrorHandler handler;
if (handlers != null && handlers.length > 0) {
for (int y = 0; y < handlers.length; y++) {
handler = handlers[y];
// Tell this handler to handle this error
handler.handle(getContainer(), (ISwingMLError[]) errors.toArray(new ISwingMLError[errors.size()]));
}
}
}
}
public ColorComponent getBackground () {
return background;
}
public Map getErrorHandlers () {
if (errorHandlers == null) {
errorHandlers = new HashMap();
}
return errorHandlers;
}
public String getFontName () {
return fontName;
}
public String getFontSize () {
return fontSize;
}
public String getFontStyle () {
return fontStyle;
}
public ColorComponent getForeground () {
return foreground;
}
public ISwingMLErrorHandler[] getHandlers (int errorType) {
ISwingMLErrorHandler[] result = null;
List handlers = (List) getErrorHandlers().get(new Integer(errorType));
if (handlers != null) {
result = (ISwingMLErrorHandler[]) handlers.toArray(new ISwingMLErrorHandler[handlers.size()]);
}
if (result == null) {
// Use a DefaultErrorHandler
result = new ISwingMLErrorHandler[1];
result[0] = ErrorHandlerFactory.getDefaultHandler();
}
return result;
}
public String getIcon () {
return this.icon;
}
public String getOrientation () {
return this.orientation;
}
public String getText () {
return this.text;
}
public String getTooltip () {
return this.tooltip;
}
/**
* Handle the given errors by delegating to the assigned ErrorHandler.
*/
public void handle (ISwingMLError[] errors) {
if (errors != null && errors.length > 0) {
// group by error type and handle each type as a group
List sortedErrors = Arrays.asList(errors);
Collections.sort(sortedErrors, new Comparator() {
public int compare (Object o1, Object o2) {
ISwingMLError error1 = (ISwingMLError) o1;
ISwingMLError error2 = (ISwingMLError) o2;
if (error1.getType() < error2.getType()) {
return -1;
} else if (error1.getType() > error2.getType()) {
return 1;
} else {
return 0;
}
}
});
// Iterate over all the errors
ISwingMLError error;
List errorGroup = new ArrayList();
int currentErrorType = ISwingMLError.ERROR_UNKNOWN;
Iterator schmiterator = sortedErrors.iterator();
while (schmiterator.hasNext()) {
error = (ISwingMLError) schmiterator.next();
if (error.getType() != currentErrorType) {
// process the errorGroup and start new
doHandleErrors(errorGroup);
errorGroup.clear();
currentErrorType = error.getType();
}
errorGroup.add(error);
}
// handle the last group
doHandleErrors(errorGroup);
}
}
public boolean isVisible () {
return this.visible;
}
public void removeHandler (int errorType, ISwingMLErrorHandler handler) {
ISwingMLErrorHandler[] handlers = getHandlers(errorType);
if (handlers != null && handlers.length > 0) {
List newHandlers = new ArrayList();
for (int x = 0; x < handlers.length; x++) {
if (!handlers[x].equals(handler)) {
newHandlers.add(handlers[x]);
}
}
getErrorHandlers().put(new Integer(errorType), newHandlers);
}
}
public void removeHandlers (int errorType) {
getErrorHandlers().remove(new Integer(errorType));
}
public void setBackground (ColorComponent component) {
background = component;
}
public void setErrorHandlers (Map handlers) {
this.errorHandlers = handlers;
}
public void setFontName (String string) {
fontName = string;
}
public void setFontSize (String string) {
fontSize = string;
}
public void setFontStyle (String string) {
fontStyle = string;
}
public void setForeground (ColorComponent component) {
foreground = component;
}
public void setIcon (String anIcon) {
this.icon = anIcon;
}
public void setOrientation (String anOrientation) {
this.orientation = anOrientation;
}
public void setText (String aText) {
this.text = aText;
}
public void setTooltip (String aTooltip) {
this.tooltip = aTooltip;
}
public void setVisible (boolean aVisible) {
this.visible = aVisible;
}
public void validate () {}
}
|