View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program 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
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.util;
21  
22  import java.awt.Color;
23  import java.io.File;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  
27  import ui.panel.MessagePanel;
28  
29  public class MessageLogger {
30  
31  	public static final int DEFAULT_MESSAGE = 0;
32  
33  	public static final int ERROR_MESSAGE = 1;
34  
35  	public static final int INFO_MESSAGE = 2;
36  
37  	public static final int DEBUG_MESSAGE = 3;
38  
39  	/***
40  	 * Comment for <code>messagePanel</code> This is the text panel in wich
41  	 * every application message will be printed.
42  	 */
43  	private static MessagePanel messagePanel;
44  
45  	/***
46  	 * Comment for <code>fileWriter</code> This helps to write every message
47  	 * in a certain file.
48  	 */
49  	private static FileWriter fileWriter = null;
50  
51  	/***
52  	 * Comment for <code>fileLogEnabled</code> States if the log on file must
53  	 * be enabled or not.
54  	 */
55  	private static boolean fileLogEnabled = false;
56  
57  	/***
58  	 * Comment for <code>logFile</code> This is the default log file name.
59  	 */
60  	private static final String logFile = "Log.txt"; //$NON-NLS-1$
61  
62  	/***
63  	 * Comment for <code>logFileClosed</code> States if the log file is closed
64  	 * or not.
65  	 */
66  	private static boolean logFileClosed = false;
67  
68  	/***
69  	 * This is a constructor.
70  	 */
71  	public MessageLogger() {
72  		if (fileLogEnabled)
73  			openLogFile();
74  	}
75  
76  	/***
77  	 * This method simply opens the log file instanciating the
78  	 * <code>fileWriter</code>.
79  	 */
80  	private static void openLogFile() {
81  		try {
82  			File file = new File(logFile);
83  			if (file.exists())
84  				file.delete();
85  			fileWriter = new FileWriter(file, false);
86  		} catch (IOException e) {
87  			// TODO Auto-generated catch block
88  			println(e.getMessage(), ERROR_MESSAGE);
89  		}
90  	}
91  
92  	/*
93  	 * public static synchronized void println(Object obj, int messageType) { if
94  	 * (logFileClosed) { openLogFile(); logFileClosed = false; } if (obj !=
95  	 * null) println(obj.toString(), messageType); else println(null,
96  	 * messageType); }
97  	 */
98  	/***
99  	 * This method prints on every enabled message component the input
100 	 * <code>text</code>.
101 	 * 
102 	 * @param text
103 	 *            The text to print.
104 	 * @param messageType
105 	 *            The text's message type.
106 	 */
107 	private static synchronized void println(String text, int messageType) {
108 		if (messagePanel != null) {
109 			switch (messageType) {
110 			case DEFAULT_MESSAGE:
111 				getMessagePanel().addMessage(text, Color.BLACK);
112 				break;
113 			case ERROR_MESSAGE:
114 				getMessagePanel().addMessage(text, Color.RED);
115 				break;
116 			case INFO_MESSAGE:
117 				getMessagePanel().addMessage(text, Color.GREEN);
118 				break;
119 			case DEBUG_MESSAGE:
120 				getMessagePanel().addMessage(text, Color.BLUE);
121 				break;
122 
123 			}
124 		}
125 		System.out.println(text);
126 		try {
127 			if (fileLogEnabled)
128 				fileWriter.write(text + "\n"); //$NON-NLS-1$
129 		} catch (IOException e) {
130 			// TODO Auto-generated catch block
131 			println(e.getMessage(), ERROR_MESSAGE);
132 		}
133 	}
134 
135 	/***
136 	 * This method simply clears every enabled message component and closes the
137 	 * fileWriter if the file logging is enabled.
138 	 */
139 	public static synchronized void clear() {
140 		if (getMessagePanel() != null)
141 			getMessagePanel().removeAll();
142 		try {
143 			if (fileLogEnabled && !logFileClosed) {
144 				fileWriter.close();
145 				logFileClosed = true;
146 			}
147 		} catch (IOException e) {
148 			// TODO Auto-generated catch block
149 			println(e.getMessage(), ERROR_MESSAGE);
150 		}
151 	}
152 
153 	/***
154 	 * @return Returns the messagePanel.
155 	 */
156 	public static MessagePanel getMessagePanel() {
157 		if (messagePanel == null) {
158 			messagePanel = new MessagePanel();
159 			messagePanel.setBackground(Color.WHITE);
160 		}
161 		return messagePanel;
162 	}
163 }