1 package org.antlr.works.ate; 2 3 import javax.swing.text.BadLocationException ; 4 import java.awt.*; 5 import java.awt.geom.GeneralPath ; 6 import java.util.HashMap ; 7 import java.util.Map ; 8 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 e) { 122 } 124 } 125 126 127 public static class UnderlyingShape { 128 129 public Map <Color,GeneralPath > shapes = new HashMap <Color, GeneralPath >(); 130 public boolean ready = false; 131 132 public void addLine(Color c, int x1, int y1, int x2, int y2) { 133 GeneralPath gp = shapes.get(c); 134 if(gp == null) { 135 gp = new GeneralPath (); 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 gp = shapes.get(c); 144 if(gp == null) { 145 gp = new GeneralPath (); 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 |