Coverage details for ui.util.MessageLogger

LineHitsSource
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  
290public 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      */
490    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      */
550    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      */
660    private static boolean logFileClosed = false;
67  
68     /**
69      * This is a constructor.
70      */
710    public MessageLogger() {
720        if (fileLogEnabled)
730            openLogFile();
740    }
75  
76     /**
77      * This method simply opens the log file instanciating the
78      * <code>fileWriter</code>.
79      */
80     private static void openLogFile() {
81         try {
820            File file = new File(logFile);
830            if (file.exists())
840                file.delete();
850            fileWriter = new FileWriter(file, false);
860        } catch (IOException e) {
87             // TODO Auto-generated catch block
880            println(e.getMessage(), ERROR_MESSAGE);
890        }
900    }
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) {
1080        if (messagePanel != null) {
1090            switch (messageType) {
110             case DEFAULT_MESSAGE:
1110                getMessagePanel().addMessage(text, Color.BLACK);
1120                break;
113             case ERROR_MESSAGE:
1140                getMessagePanel().addMessage(text, Color.RED);
1150                break;
116             case INFO_MESSAGE:
1170                getMessagePanel().addMessage(text, Color.GREEN);
1180                break;
119             case DEBUG_MESSAGE:
1200                getMessagePanel().addMessage(text, Color.BLUE);
121                 break;
122  
123             }
124         }
1250        System.out.println(text);
126         try {
1270            if (fileLogEnabled)
1280                fileWriter.write(text + "\n"); //$NON-NLS-1$
1290        } catch (IOException e) {
130             // TODO Auto-generated catch block
1310            println(e.getMessage(), ERROR_MESSAGE);
1320        }
1330    }
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() {
1400        if (getMessagePanel() != null)
1410            getMessagePanel().removeAll();
142         try {
1430            if (fileLogEnabled && !logFileClosed) {
1440                fileWriter.close();
1450                logFileClosed = true;
146             }
1470        } catch (IOException e) {
148             // TODO Auto-generated catch block
1490            println(e.getMessage(), ERROR_MESSAGE);
1500        }
1510    }
152  
153     /**
154      * @return Returns the messagePanel.
155      */
156     public static MessagePanel getMessagePanel() {
1570        if (messagePanel == null) {
1580            messagePanel = new MessagePanel();
1590            messagePanel.setBackground(Color.WHITE);
160         }
1610        return messagePanel;
162     }
163 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.