KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > analysis > ATEAnalysisColumn


1 package org.antlr.works.ate.analysis;
2
3 import org.antlr.works.ate.ATEPanel;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.event.MouseAdapter JavaDoc;
8 import java.awt.event.MouseEvent JavaDoc;
9 import java.awt.event.MouseMotionAdapter JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 /*
13
14 [The "BSD licence"]
15 Copyright (c) 2005 Jean Bovet
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
20 are met:
21
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 3. The name of the author may not be used to endorse or promote products
28 derived from this software without specific prior written permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 */

42
43 /** This class handles the analysis column located at the right of the ATEPanel.
44  * It purposes is to display information about errors/warnings in the text.
45  * It uses the ATEAnalysisManager to find the errors/warnings to display.
46  */

47
48 public class ATEAnalysisColumn extends JPanel {
49
50     protected ATEPanel textEditor;
51     protected ATEAnalysisColumnOverlay overlay;
52     protected ColumnAnalysisBox analysisBox;
53
54     protected int topOffset = 30;
55     protected int bottomOffset = 50;
56     protected int lineCount;
57
58     public ATEAnalysisColumn(ATEPanel textEditor) {
59         this.textEditor = textEditor;
60
61         setFocusable(false);
62
63         analysisBox = new ColumnAnalysisBox();
64         overlay = new ATEAnalysisColumnOverlay(textEditor.getParentFrame(), this);
65
66         addMouseMotionListener(new ColumnMouseMotionAdapter());
67         addMouseListener(new ColumnMouseAdapter());
68     }
69
70     public ATEAnalysisManager getAnalysisManager() {
71         return textEditor.getAnalysisManager();
72     }
73
74     public Rectangle getDrawingBounds() {
75         Rectangle r = getBounds();
76         r.y = topOffset;
77         r.height -= topOffset + bottomOffset;
78         return r;
79     }
80
81     public Rectangle composeIndicatorRectangle(int line, int coarse) {
82         Rectangle r = getDrawingBounds();
83         float position = (float)line / lineCount;
84         int y = (int)(r.y + r.height * position);
85         return new Rectangle(3, y-coarse, r.width-6, 2+2*coarse);
86     }
87
88     public void paint(Graphics g) {
89         super.paint(g);
90
91         ATEAnalysisManager manager = getAnalysisManager();
92         if(manager == null)
93             return;
94
95         lineCount = manager.getLinesCount();
96
97         Graphics2D g2d = (Graphics2D)g;
98         int[] types = manager.getAvailableTypes();
99         for(int type=0; type<types.length; type++) {
100             paintStrips(g2d, manager.getItemsForType(type));
101         }
102
103         analysisBox.paint(g);
104     }
105
106     protected void paintStrips(Graphics2D g, List JavaDoc<ATEAnalysisItem> items) {
107         for(Iterator JavaDoc<ATEAnalysisItem> iter = items.iterator(); iter.hasNext(); ) {
108             ATEAnalysisItem item = iter.next();
109             g.setColor(item.color);
110             g.fill(composeIndicatorRectangle(item.line, 0));
111         }
112     }
113
114     /** This class is used to draw the little analysis colored box at the top
115      * of the analysis column.
116      */

117
118     protected class ColumnAnalysisBox {
119
120         protected final Rectangle r = new Rectangle(2, 2, 14, 14);
121
122         public ColumnAnalysisBox() {
123         }
124
125         public void paint(Graphics g) {
126             BorderFactory.createEtchedBorder().paintBorder(ATEAnalysisColumn.this, g, r.x, r.y, r.width, r.height);
127             g.setColor(getAnalysisManager().getAnalysisColor());
128             g.fillRect(r.x+2, r.y+2, r.width-5, r.height-5);
129         }
130     }
131
132     protected class ColumnMouseAdapter extends MouseAdapter JavaDoc {
133
134         public int getIndexOfFirstErrors(Point p) {
135             int[] types = getAnalysisManager().getAvailableTypes();
136             for(int type=0; type<types.length; type++) {
137                 List JavaDoc<ATEAnalysisItem> items = getAnalysisManager().getItemsForType(type);
138                 for(int item=0; item<items.size(); item++) {
139                     ATEAnalysisItem ai = items.get(item);
140                     if(composeIndicatorRectangle(ai.line, 2).contains(p)) {
141                         return ai.index;
142                     }
143                 }
144             }
145             return -1;
146         }
147
148         /** Jump into the text at the location of the current error/warning under
149          * the mouse.
150          */

151
152         public void mousePressed(MouseEvent JavaDoc e) {
153             int index = getIndexOfFirstErrors(e.getPoint());
154             if(index > -1) {
155                 overlay.hide();
156                 textEditor.setCaretPosition(index);
157             }
158         }
159
160         public void mouseExited(MouseEvent JavaDoc e) {
161             overlay.hide();
162         }
163     }
164
165     /** This class is used to display any tooltip describing the errors/warnings
166      * under the cursor position.
167      */

168
169     protected class ColumnMouseMotionAdapter extends MouseMotionAdapter JavaDoc {
170
171         public void mouseMoved(MouseEvent JavaDoc e) {
172             ATEAnalysisManager manager = getAnalysisManager();
173             if(manager == null)
174                 return;
175
176             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
177             if(analysisBox.r.contains(e.getPoint())) {
178                 sb.append(manager.getAnalysisDescription());
179             } else {
180                 sb.append(getItemDescriptionsAtPoint(e.getPoint()));
181             }
182
183             if(sb.length() > 0) {
184                 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
185                 overlay.setLocation(e.getPoint());
186                 overlay.setText(sb.toString());
187                 overlay.display();
188             } else {
189                 setCursor(Cursor.getDefaultCursor());
190                 overlay.hide();
191             }
192         }
193
194         protected String JavaDoc getItemDescriptionsAtPoint(Point point) {
195             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
196             ATEAnalysisManager manager = getAnalysisManager();
197             if(manager != null) {
198                 int[] types = manager.getAvailableTypes();
199                 for(int type=0; type<types.length; type++) {
200                     List JavaDoc<ATEAnalysisItem> items = manager.getItemsForType(type);
201                     for(int item=0; item<items.size(); item++) {
202                         ATEAnalysisItem ai = items.get(item);
203                         if(composeIndicatorRectangle(ai.line, 2).contains(point)) {
204                             sb.append(ai.description);
205                             sb.append("\n");
206                         }
207                     }
208                 }
209             }
210             return sb.toString();
211         }
212     }
213 }
214
Popular Tags