001: package org.swingml.errors.handlers;
002:
003: import java.awt.*;
004: import java.util.*;
005:
006: import javax.swing.*;
007:
008: import org.swingml.errors.*;
009:
010: /**
011: * @author CrossLogic
012: */
013: public class DefaultErrorHandler implements ISwingMLErrorHandler {
014:
015: private Map parameters = new HashMap();
016:
017: protected DefaultErrorHandler() {
018: }
019:
020: public void addParameter(String name, String value) {
021: getParameters().put(name, value);
022: }
023:
024: /**
025: * Return the Dialog type that should be shown for the given error.
026: */
027: private int getMessageType(ISwingMLError error) {
028: // default to ERROR message
029: int result = JOptionPane.ERROR_MESSAGE;
030: switch (error.getType()) {
031: case (ISwingMLError.MESSAGE):
032: result = JOptionPane.INFORMATION_MESSAGE;
033: break;
034: case (ISwingMLError.ERROR_BUSINESS_LOGIC):
035: result = JOptionPane.ERROR_MESSAGE;
036: break;
037: case (ISwingMLError.WARNING):
038: result = JOptionPane.WARNING_MESSAGE;
039: break;
040: }
041: return result;
042: }
043:
044: public String getParameter(String name) {
045: return getParameters().get(name) != null ? (String) getParameters()
046: .get(name)
047: : null;
048: }
049:
050: public Map getParameters() {
051: if (parameters == null) {
052: parameters = new HashMap();
053: }
054: return parameters;
055: }
056:
057: /**
058: * render a dialog with the error messages in it.
059: *
060: * **Warning** - Do not call the handle(container, errors[]) method, as subclasses may be calling this method via a super.handle() call.
061: * Calling the other handle() method may result in an infinite loop.
062: */
063: public void handle(Container container, final ISwingMLError error) {
064: if (error != null) {
065: Runnable showDialog = new Runnable() {
066:
067: public void run() {
068: JOptionPane.showMessageDialog(new JFrame(), error
069: .getText(), "GUIDE", getMessageType(error));
070: }
071: };
072: Thread aThread = new Thread(showDialog);
073: aThread.start();
074: }
075: }
076:
077: /**
078: * render a dialog with the error messages in it.
079: */
080: public void handle(Container container, ISwingMLError[] errors) {
081: if (errors != null) {
082: String errorString = "";
083: ISwingMLError error;
084: for (int x = 0; x < errors.length; x++) {
085: error = errors[x];
086: errorString += error.getText() + "\n";
087: }
088: final String result = errorString;
089: final int type = getMessageType(errors[0]);
090: Runnable showDialog = new Runnable() {
091:
092: public void run() {
093: JOptionPane.showMessageDialog(new JFrame(), result,
094: "GUIDE", type);
095: }
096: };
097: Thread aThread = new Thread(showDialog);
098: aThread.start();
099:
100: }
101: }
102:
103: public void removeParameter(String name) {
104: if (name != null && getParameters().containsKey(name)) {
105: getParameters().remove(name);
106: }
107: }
108:
109: public void setParameters(Map params) {
110: this.parameters = params;
111: }
112: }
|