View Javadoc

1   /*
2    * JBCS - A JBuilder Plugin for Checkstyle
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Copyright © 2003
19   * Henri Tremblay
20   */
21  package com.henri.jbcs;
22  
23  import javax.swing.JOptionPane;
24  
25  import com.borland.primetime.ide.Browser;
26  import com.borland.primetime.ide.Message;
27  import com.borland.primetime.ide.MessageCategory;
28  
29  public class MessageHandler {
30  
31     private static final boolean DEBUG = Boolean.getBoolean("checkstyle.debug");
32  
33     /***
34      * The Jbuilder message category for debugging
35      */
36     private static final MessageCategory MSG_CATEGORY_DEBUG = DEBUG ?
37        new MessageCategory("Checkstyle - DEBUG") : null;
38  
39     /*** The Jbuilder message category */
40     private static final MessageCategory MSG_CATEGORY = new MessageCategory("Checkstyle") {
41        public boolean categoryWillPromptOnClose() {
42           return CheckStyleOpenTool.isRunning();
43        }
44     };
45  
46     /*** Internal reference to the current browser */
47     private Browser m_browser = null;
48  
49     /***
50      * MessageHandler for a specific Browser. Messages will appear in this Browser.
51      * @param browser Browser
52      */
53     public MessageHandler(Browser browser) {
54        m_browser = browser;
55     }
56  
57     /***
58      * MessageHandler just writing on the currently active browser.
59      */
60     public MessageHandler() {
61        this(Browser.getActiveBrowser());
62     }
63  
64     public void clearMessages() {
65        m_browser.getMessageView().clearMessages(MSG_CATEGORY);
66     }
67  
68     /***
69      * Add a message at the root of the message pane
70      * @param msg Message to be added
71      * @return The Jbuilder message added
72      */
73     public Message addMessage(String msg) {
74        return addMessage(msg, null);
75     }
76  
77     /***
78      * Add a message as a child of the parent message in the message pane
79      * @param msgText Message to be added
80      * @param parent Parent of the message. If null, the message will be put at the root
81      * @return The Jbuilder message added
82      */
83     public Message addMessage(String msgText, Message parent) {
84        Message msg = new Message(msgText);
85        m_browser.getMessageView().addMessage(MSG_CATEGORY, parent, msg, true);
86        return msg;
87     }
88  
89     /***
90      * Write a message in the status bar
91      * @param msg Message to write
92      */
93     public void writeStatusBarMessage(String msg) {
94        m_browser.getStatusView().setText(msg);
95     }
96  
97     /***
98      * Will add the list of messages as child of the parent messages
99      * @param parent Parent message, if null messages will be added at the root
100     * @param msgs List of messages to add
101     */
102    public void addMessages(Message parent, Message[] msgs) {
103       m_browser.getMessageView().addMessages(MSG_CATEGORY, parent, msgs, true);
104    }
105 
106    /***
107     * Log debug info.
108     *
109     * @param msg  The message to log.
110     */
111    public void debug(String msg) {
112       if(DEBUG) {
113          if(m_browser == null) {
114             m_browser = Browser.getActiveBrowser();
115          }
116          m_browser.getMessageView().addMessage(MSG_CATEGORY_DEBUG, msg);
117       }
118    }
119 
120    /***
121     * Log Error conditions.
122     *
123     * @param msg  The message to log.
124     * @param t    Error.
125     */
126    public void error(String msg, Throwable t) {
127       if(DEBUG) {
128          debug(msg);
129          t.printStackTrace();
130       }
131       JOptionPane.showMessageDialog(m_browser, msg, "JBCS Error", JOptionPane.ERROR_MESSAGE);
132       System.err.println(msg);
133    }
134 }