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 ; 8 import java.awt.event.MouseEvent ; 9 import java.awt.event.MouseMotionAdapter ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 42 43 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 <ATEAnalysisItem> items) { 107 for(Iterator <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 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 { 133 134 public int getIndexOfFirstErrors(Point p) { 135 int[] types = getAnalysisManager().getAvailableTypes(); 136 for(int type=0; type<types.length; type++) { 137 List <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 151 152 public void mousePressed(MouseEvent 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 e) { 161 overlay.hide(); 162 } 163 } 164 165 168 169 protected class ColumnMouseMotionAdapter extends MouseMotionAdapter { 170 171 public void mouseMoved(MouseEvent e) { 172 ATEAnalysisManager manager = getAnalysisManager(); 173 if(manager == null) 174 return; 175 176 StringBuffer sb = new StringBuffer (); 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 getItemDescriptionsAtPoint(Point point) { 195 StringBuffer sb = new StringBuffer (); 196 ATEAnalysisManager manager = getAnalysisManager(); 197 if(manager != null) { 198 int[] types = manager.getAvailableTypes(); 199 for(int type=0; type<types.length; type++) { 200 List <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 |