View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2001-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: Message.java 1605 2008-02-25 21:07:14Z barteo $
26   */
27  package org.microemu.app.ui;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Vector;
32  
33  import org.microemu.log.Logger;
34  
35  /**
36   * 
37   * This class is used to Show error message to user
38   * 
39   * @author vlads
40   *
41   */
42  public class Message {
43  
44  	public static final int ERROR = 0;
45  
46      public static final int INFO = 1;
47  
48      public static final int WARN = 2;
49      
50      private static List listeners = new Vector();
51      
52      static {
53      	Logger.addLogOrigin(Message.class);
54      }
55      
56      /**
57       * Show Error message to user
58       * 
59       * @param title Dialog title
60       * @param text  Message
61       */
62      public static void error(String title, String text) {
63          Logger.error("Message: " + title + ": " + text);
64          callListeners(ERROR, title, text, null);
65      }
66  
67      /**
68       * Show Error message to user
69       * 
70       * @param text  Message
71       */
72      public static void error(String text) {
73          Logger.error("Message: Error: " + text);
74          callListeners(ERROR, "Error", text, null);
75      }
76      
77      /**
78       * Show Error message to user
79       * 
80       * @param title Dialog title
81       * @param text  Message
82       */
83      public static void error(String title, String text, Throwable throwable) {
84          Logger.error("Message: " + title + ": " + text, throwable);
85          callListeners(ERROR, title, text, throwable);
86      }
87  
88      public static void error(String text, Throwable throwable) {
89          Logger.error("Message: Error : " + text, throwable);
90          callListeners(ERROR, "Error", text, throwable);
91      }
92      
93      /**
94       * Show info message to user
95       * 
96       * @param text  Message
97       */
98      public static void info(String text) {
99          Logger.info("Message: info: " + text);
100         callListeners(INFO, "Info", text, null);
101     }
102 
103     /**
104      * Show info message to user
105      * 
106      * @param text  Message
107      */
108     public static void warn(String text) {
109         Logger.warn("Message: warn: " + text);
110         callListeners(INFO, "Warning", text, null);
111     }
112     
113     /**
114      * Here we can butify error message text.
115      * @param throwable
116      * @return
117      */
118     public static String getCauseMessage(Throwable throwable) {
119 		if (throwable.getCause() == null) {
120 			return throwable.toString();
121 		} else {
122 			return getCauseMessage(throwable.getCause());
123 		}
124     }
125     
126     private static void callListeners(int level, String title, String text, Throwable throwable) {
127 		for (Iterator iter = listeners.iterator(); iter.hasNext();) {
128 			MessageListener a = (MessageListener) iter.next();
129 			a.showMessage(level, title, text, throwable);
130 		};
131 	}
132 	
133 	public static void addListener(MessageListener newListener) {
134 		listeners.add(newListener);
135 	}
136 }