KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > ATEUnderlyingManager


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

38
39 public abstract class ATEUnderlyingManager {
40
41     protected ATEPanel textEditor;
42     protected UnderlyingShape underlyingShape;
43     protected boolean underlying = true;
44
45     public ATEUnderlyingManager(ATEPanel textEditor) {
46         this.textEditor = textEditor;
47         underlyingShape = new UnderlyingShape();
48     }
49
50     public void setUnderlying(boolean flag) {
51         underlying = flag;
52     }
53
54     public void reset() {
55         underlyingShape.reset();
56     }
57
58     public void paint(Graphics g) {
59         if(!underlying)
60             return;
61
62         Graphics2D g2d = (Graphics2D)g;
63         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
64         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
65
66         if(underlyingShape.isReady()) {
67             underlyingShape.draw(g2d);
68             return;
69         }
70
71         underlyingShape.begin();
72
73         render(g);
74
75         underlyingShape.end();
76         underlyingShape.draw(g2d);
77     }
78
79     public abstract void render(Graphics g);
80
81     public static final int SHAPE_SAW_TOOTH = 1;
82     public static final int SHAPE_LINE = 2;
83     public static final int SHAPE_RECT = 3;
84
85     public void drawUnderlineAtIndexes(Graphics g, Color c, int start, int end, int shape) {
86         try {
87             Rectangle r1 = textEditor.textPane.modelToView(start);
88             Rectangle r2 = textEditor.textPane.modelToView(end);
89
90             g.setColor(c);
91
92             switch(shape) {
93                 case SHAPE_SAW_TOOTH: {
94                     int width = r2.x-r1.x;
95                     int triangle_size = 5;
96                     for(int triangle=0; triangle<width/triangle_size; triangle++) {
97                         int x = r1.x+triangle*triangle_size;
98                         int y = r1.y+r1.height-1;
99                         g.drawLine(x, y, x+triangle_size/2, y-triangle_size/2);
100                         g.drawLine(x+triangle_size/2, y-triangle_size/2, x+triangle_size, y);
101
102
103                         underlyingShape.addLine(c, x, y, x+triangle_size/2, y-triangle_size/2);
104                         underlyingShape.addLine(c, x+triangle_size/2, y-triangle_size/2, x+triangle_size, y);
105                     }
106                 }
107                 break;
108
109                 case SHAPE_LINE: {
110                     g.drawLine(r1.x, r1.y+r1.height, r2.x, r2.y+r2.height);
111                     underlyingShape.addLine(c, r1.x, r1.y+r1.height, r2.x, r2.y+r2.height);
112                 }
113                 break;
114
115                 case SHAPE_RECT: {
116                     g.drawRect(r1.x, r1.y, r2.x+r2.width-r1.x, r1.height);
117                     underlyingShape.addRect(c, r1.x, r1.y, r2.x+r2.width, r2.y+r2.height);
118                 }
119                 break;
120             }
121         } catch (BadLocationException JavaDoc e) {
122             // Ignore
123
}
124     }
125
126
127     public static class UnderlyingShape {
128
129         public Map JavaDoc<Color,GeneralPath JavaDoc> shapes = new HashMap JavaDoc<Color, GeneralPath JavaDoc>();
130         public boolean ready = false;
131
132         public void addLine(Color c, int x1, int y1, int x2, int y2) {
133             GeneralPath JavaDoc gp = shapes.get(c);
134             if(gp == null) {
135                 gp = new GeneralPath JavaDoc();
136                 shapes.put(c, gp);
137             }
138             gp.moveTo(x1, y1);
139             gp.lineTo(x2, y2);
140         }
141
142         public void addRect(Color c, int x1, int y1, int x2, int y2) {
143             GeneralPath JavaDoc gp = shapes.get(c);
144             if(gp == null) {
145                 gp = new GeneralPath JavaDoc();
146                 shapes.put(c, gp);
147             }
148             gp.moveTo(x1, y1);
149             gp.lineTo(x2, y1);
150             gp.lineTo(x2, y2);
151             gp.lineTo(x1, y2);
152             gp.lineTo(x1, y1);
153         }
154
155         public void draw(Graphics2D g) {
156             for (Color c : shapes.keySet()) {
157                 g.setColor(c);
158                 g.draw(shapes.get(c));
159             }
160         }
161
162         public void begin() {
163             reset();
164         }
165
166         public void end() {
167             ready = true;
168         }
169
170         public boolean isReady() {
171             return ready;
172         }
173
174         public void reset() {
175             shapes.clear();
176             ready = false;
177         }
178     }
179
180 }
181
Popular Tags