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 java.util.HashMap;
24  import java.util.Map;
25  
26  import java.awt.Color;
27  
28  import com.borland.primetime.editor.EditorPane;
29  import com.borland.primetime.editor.LineMark;
30  import com.borland.primetime.ide.Browser;
31  import com.borland.primetime.ide.Message;
32  import com.borland.primetime.node.TextFileNode;
33  import com.borland.primetime.viewer.TextNodeViewer;
34  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
35  
36  /***
37   * This class is to display the checkstyle message to JBuilder message view.
38   *
39   * @author Henri Tremblay
40   * @version $Revision: 1.2 $ $Date: 2005/07/27 23:18:56 $
41   */
42  public class CheckStyleMessage extends Message
43  {
44     /***
45      * Mark to highlight a line containing a Checkstyle error
46      */
47     private static final LineMark MARK = new EditorPane.HighlightMark();
48  
49     /***
50      * List of colors per severity. Key is the SeverityLevel, value is Color
51      */
52     private static final Map SEVERITY_COLOR = new HashMap();
53  
54     /***
55      * Parsed file
56      */
57     private TextFileNode m_fileNode;
58  
59     /***
60      * Line containing the error
61      */
62     private int m_line;
63  
64     static {
65        SEVERITY_COLOR.put(SeverityLevel.INFO, Color.BLUE);
66        SEVERITY_COLOR.put(SeverityLevel.WARNING, Color.BLACK);
67        SEVERITY_COLOR.put(SeverityLevel.ERROR, Color.RED);
68     }
69  
70     /***
71      * Constructor.
72      * @param node The node reference to the file
73      * @param line The line number of the message
74      * @param message The message itself
75      * @param level Checkstyle severity level for the error
76      */
77     public CheckStyleMessage(TextFileNode node, int line, String message, SeverityLevel level) {
78        super(line + ": " + message);
79        this.m_line = line;
80        m_fileNode = node;
81        foreground = (Color) SEVERITY_COLOR.get(level);
82     }
83  
84     /***
85      * @see Message
86      * @param browser
87      */
88     public void selectAction(Browser browser) {
89        displayResult(browser, false);
90     }
91  
92     /***
93      * @see Message
94      * @param browser
95      */
96     public void messageAction(Browser browser) {
97        displayResult(browser, true);
98     }
99  
100    /***
101     * Highlight the corresponding line.
102     * @param browser The browser
103     * @param requestFocus This function will do nothing if
104     * <code>requestFocus</code> is false.
105     */
106    private void displayResult(Browser browser, boolean requestFocus) {
107       try {
108          // If the node is not open, open it
109          if(!browser.isOpenNode(m_fileNode)) {
110             browser.openNodes(new TextFileNode[] { m_fileNode }, m_fileNode);
111          }
112 
113          browser.setActiveNode(m_fileNode, requestFocus);
114          TextNodeViewer viewer = (TextNodeViewer) browser.getViewerOfType(
115             m_fileNode, TextNodeViewer.class);
116          browser.setActiveViewer(m_fileNode, viewer, requestFocus);
117          EditorPane editor = viewer.getEditor();
118          editor.gotoPosition(m_line, 1, false, EditorPane.CENTER_IF_NEAR_EDGE);
119          if(requestFocus || m_line == 0) {
120             editor.requestFocus();
121          }
122          else {
123             editor.setTemporaryMark(m_line, MARK);
124          }
125       }
126       catch(Exception ex) {
127          ex.printStackTrace();
128       }
129    }
130 }