View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2001-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: Message.java 957 2007-02-18 18:10:27Z vlads $
21   */
22  package org.microemu.app.ui;
23  
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Vector;
27  
28  import org.microemu.log.Logger;
29  
30  /**
31   * 
32   * This class is used to Show error message to user
33   * 
34   * @author vlads
35   *
36   */
37  public class Message {
38  
39  	public static final int ERROR = 0;
40  
41      public static final int INFO = 1;
42  
43      public static final int WARN = 2;
44      
45      private static List listeners = new Vector();
46      
47      static {
48      	Logger.addLogOrigin(Message.class);
49      }
50      
51      /**
52       * Show Error message to user
53       * 
54       * @param title Dialog title
55       * @param text  Message
56       */
57      public static void error(String title, String text) {
58          Logger.error("Message: " + title + ": " + text);
59          callListeners(ERROR, title, text, null);
60      }
61  
62      /**
63       * Show Error message to user
64       * 
65       * @param text  Message
66       */
67      public static void error(String text) {
68          Logger.error("Message: Error: " + text);
69          callListeners(ERROR, "Error", text, null);
70      }
71      
72      /**
73       * Show Error message to user
74       * 
75       * @param title Dialog title
76       * @param text  Message
77       */
78      public static void error(String title, String text, Throwable throwable) {
79          Logger.error("Message: " + title + ": " + text, throwable);
80          callListeners(ERROR, title, text, throwable);
81      }
82  
83      public static void error(String text, Throwable throwable) {
84          Logger.error("Message: Error : " + text, throwable);
85          callListeners(ERROR, "Error", text, throwable);
86      }
87      
88      /**
89       * Show info message to user
90       * 
91       * @param text  Message
92       */
93      public static void info(String text) {
94          Logger.info("Message: info: " + text);
95          callListeners(INFO, "Info", text, null);
96      }
97  
98      /**
99       * Show info message to user
100      * 
101      * @param text  Message
102      */
103     public static void warn(String text) {
104         Logger.warn("Message: warn: " + text);
105         callListeners(INFO, "Warning", text, null);
106     }
107     
108     /**
109      * Here we can butify error message text.
110      * @param throwable
111      * @return
112      */
113     public static String getCauseMessage(Throwable throwable) {
114 		if (throwable.getCause() == null) {
115 			return throwable.toString();
116 		} else {
117 			return getCauseMessage(throwable.getCause());
118 		}
119     }
120     
121     private static void callListeners(int level, String title, String text, Throwable throwable) {
122 		for (Iterator iter = listeners.iterator(); iter.hasNext();) {
123 			MessageListener a = (MessageListener) iter.next();
124 			a.showMessage(level, title, text, throwable);
125 		};
126 	}
127 	
128 	public static void addListener(MessageListener newListener) {
129 		listeners.add(newListener);
130 	}
131 }