1 11 package org.eclipse.jface.text.source; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.custom.StyledText; 15 import org.eclipse.swt.events.MouseEvent; 16 import org.eclipse.swt.events.MouseListener; 17 import org.eclipse.swt.events.MouseMoveListener; 18 import org.eclipse.swt.events.PaintEvent; 19 import org.eclipse.swt.events.PaintListener; 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.graphics.GC; 23 import org.eclipse.swt.widgets.Canvas; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Display; 27 28 import org.eclipse.core.runtime.Assert; 29 30 import org.eclipse.jface.resource.JFaceResources; 31 32 import org.eclipse.jface.text.ITextListener; 33 import org.eclipse.jface.text.ITextViewer; 34 import org.eclipse.jface.text.IViewportListener; 35 import org.eclipse.jface.text.JFaceTextUtil; 36 import org.eclipse.jface.text.TextEvent; 37 38 39 72 public abstract class AbstractRulerColumn implements IVerticalRulerColumn, IVerticalRulerInfo, IVerticalRulerInfoExtension { 73 private static final int DEFAULT_WIDTH= 12; 74 private static final int DEFAULT_TEXT_INSET= 2; 75 76 79 private final class MouseHandler implements MouseListener, MouseMoveListener { 80 81 84 public void mouseUp(MouseEvent event) { 85 } 86 87 90 public void mouseDown(MouseEvent event) { 91 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y); 92 } 93 94 97 public void mouseDoubleClick(MouseEvent event) { 98 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y); 99 } 100 101 104 public void mouseMove(MouseEvent event) { 105 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y); 106 } 107 } 108 109 112 private final class InternalListener implements IViewportListener, ITextListener { 113 114 117 public void viewportChanged(int topPixel) { 118 int delta= topPixel - fLastTopPixel; 119 if (scrollVertical(delta)) 120 fCanvas.update(); } 122 123 126 public void textChanged(TextEvent event) { 127 133 if (!event.getViewerRedrawState()) 134 return; 135 136 if (fWasShowingEntireContents || event.getDocumentEvent() == null || JFaceTextUtil.isShowingEntireContents(fStyledText)) 137 redraw(); 138 } 139 } 140 141 142 143 144 private final InternalListener fInternalListener= new InternalListener(); 145 146 private final MouseHandler fMouseHandler= new MouseHandler(); 147 148 152 153 154 private CompositeRuler fParentRuler; 155 156 private Canvas fCanvas; 157 158 private ITextViewer fTextViewer; 159 160 private StyledText fStyledText; 161 162 163 164 165 private int fLastTopPixel= -1; 166 167 private boolean fWasShowingEntireContents= false; 168 169 170 171 172 private int fWidth= DEFAULT_WIDTH; 173 174 private int fTextInset= DEFAULT_TEXT_INSET; 175 176 private Color fBackground; 177 178 private Font fFont; 179 180 private IAnnotationModel fModel; 181 182 private IAnnotationHover fHover; 183 184 187 protected AbstractRulerColumn() { 188 } 189 190 194 public Control createControl(CompositeRuler parentRuler, Composite parentControl) { 195 Assert.isLegal(parentControl != null); 196 Assert.isLegal(parentRuler != null); 197 Assert.isLegal(fParentRuler == null); 199 fParentRuler= parentRuler; 200 201 fTextViewer= getParentRuler().getTextViewer(); 202 fTextViewer.addViewportListener(fInternalListener); 203 fTextViewer.addTextListener(fInternalListener); 204 205 fStyledText= fTextViewer.getTextWidget(); 206 207 fCanvas= new Canvas(parentControl, getCanvasStyle()); 208 209 fCanvas.setBackground(getDefaultBackground()); 210 fCanvas.setFont(getFont()); 211 212 fCanvas.addPaintListener(new PaintListener() { 213 public void paintControl(PaintEvent event) { 214 AbstractRulerColumn.this.paintControl(event); 215 } 216 }); 217 218 fCanvas.addMouseListener(fMouseHandler); 219 fCanvas.addMouseMoveListener(fMouseHandler); 220 221 return fCanvas; 222 } 223 224 234 protected int getCanvasStyle() { 235 return SWT.NO_BACKGROUND; 236 } 237 238 241 public final Control getControl() { 242 return fCanvas; 243 } 244 245 251 protected final void setWidth(int width) { 252 Assert.isLegal(width >= 0); 253 if (fWidth != width) { 254 fWidth= width; 255 CompositeRuler composite= getParentRuler(); 256 if (composite != null) 257 composite.relayout(); 258 } 259 } 260 261 264 public final int getWidth() { 265 return fWidth; 266 } 267 268 274 protected final CompositeRuler getParentRuler() { 275 return fParentRuler; 276 } 277 278 283 public final void setFont(Font font) { 284 if (fFont != font) { 285 fFont= font; 286 redraw(); 287 } 288 } 289 290 296 protected final Font getFont() { 297 if (fFont != null) 298 return fFont; 299 if (fStyledText != null && !fStyledText.isDisposed()) 300 return fStyledText.getFont(); 301 return JFaceResources.getTextFont(); 302 } 303 304 309 protected final void setTextInset(int textInset) { 310 if (textInset != fTextInset) { 311 fTextInset= textInset; 312 redraw(); 313 } 314 } 315 316 322 protected final int getTextInset() { 323 return fTextInset; 324 } 325 326 329 public void setModel(IAnnotationModel model) { 330 if (fModel != model) { 331 fModel= model; 332 redraw(); 333 } 334 } 335 336 339 public final IAnnotationModel getModel() { 340 return fModel; 341 } 342 343 351 protected final void setDefaultBackground(Color background) { 352 if (fBackground != background) { 353 fBackground= background; 354 if (fCanvas != null && !fCanvas.isDisposed()) 355 fCanvas.setBackground(getDefaultBackground()); 356 redraw(); 357 } 358 } 359 360 365 protected final Color getDefaultBackground() { 366 if (fBackground != null) 367 return fBackground; 368 if (fStyledText != null && !fStyledText.isDisposed()) 369 return fStyledText.getBackground(); 370 Display display; 371 if (fCanvas != null && !fCanvas.isDisposed()) 372 display= fCanvas.getDisplay(); 373 else 374 display= Display.getCurrent(); 375 if (display != null) 376 return display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); 377 return null; 378 } 379 380 385 protected final void setHover(IAnnotationHover hover) { 386 if (fHover != hover) 387 fHover= hover; 388 } 389 390 393 public IAnnotationHover getHover() { 394 return fHover; 395 } 396 397 405 public void dispose() { 406 if (fTextViewer != null) { 407 fTextViewer.removeViewportListener(fInternalListener); 408 fTextViewer.removeTextListener(fInternalListener); 409 fTextViewer= null; 410 } 411 412 if (fStyledText != null) 413 fStyledText= null; 414 415 if (fCanvas != null) { 416 fCanvas.dispose(); 417 fCanvas= null; 418 } 419 } 420 421 424 public final void redraw() { 425 if (fCanvas != null && !fCanvas.isDisposed()) 426 fCanvas.redraw(); 427 } 428 429 434 protected final void redraw(ILineRange lines) { 435 if (fCanvas == null || fCanvas.isDisposed()) 436 return; 437 int firstModelLine= lines.getStartLine(); 438 int lastModelLine= firstModelLine + lines.getNumberOfLines(); 439 int firstWidgetLine= JFaceTextUtil.modelLineToWidgetLine(fTextViewer, firstModelLine); 440 int lastWidgetLine= JFaceTextUtil.modelLineToWidgetLine(fTextViewer, lastModelLine); 441 442 int from= Math.max(0, fStyledText.getLinePixel(firstWidgetLine)); 443 int to= Math.min(fCanvas.getSize().y, fStyledText.getLinePixel(lastWidgetLine + 1)); 445 fCanvas.redraw(0, from, fWidth, to - from, false); 446 } 447 448 453 private void paintControl(PaintEvent event) { 454 if (fTextViewer == null) 455 return; 456 fWasShowingEntireContents= JFaceTextUtil.isShowingEntireContents(fStyledText); 457 fLastTopPixel= fStyledText.getTopPixel(); 458 459 ILineRange lines= computeDirtyWidgetLines(event); 460 GC gc= event.gc; 461 paint(gc, lines); 462 463 if ((fCanvas.getStyle() & SWT.NO_BACKGROUND) != 0) { 464 int firstEmpty= Math.max(event.y, fStyledText.getLinePixel(fStyledText.getLineCount())); 466 int lastEmpty= event.y + event.height; 467 if (lastEmpty > firstEmpty) { 468 gc.setBackground(getDefaultBackground()); 469 gc.fillRectangle(0, firstEmpty, getWidth(), lastEmpty - firstEmpty); 470 } 471 } 472 } 473 474 480 private ILineRange computeDirtyWidgetLines(PaintEvent event) { 481 int firstLine= fStyledText.getLineIndex(event.y); 482 int lastLine= fStyledText.getLineIndex(event.y + event.height - 1); 483 return new LineRange(firstLine, lastLine - firstLine + 1); 484 } 485 486 498 protected void paint(GC gc, ILineRange lines) { 499 final int firstLine= lines.getStartLine(); 500 final int lastLine= firstLine + lines.getNumberOfLines(); 501 for (int line= firstLine; line < lastLine; line++) { 502 int modelLine= JFaceTextUtil.widgetLine2ModelLine(fTextViewer, line); 503 if (modelLine == -1) 504 continue; 505 int linePixel= fStyledText.getLinePixel(line); 506 int lineHeight= fStyledText.getLineHeight(fStyledText.getOffsetAtLine(line)); 507 paintLine(gc, modelLine, line, linePixel, lineHeight); 508 } 509 } 510 511 526 protected void paintLine(GC gc, int modelLine, int widgetLine, int linePixel, int lineHeight) { 527 gc.setBackground(computeBackground(modelLine)); 528 gc.fillRectangle(0, linePixel, getWidth(), lineHeight); 529 String text= computeText(modelLine); 530 if (text != null) { 531 gc.setForeground(computeForeground(modelLine)); 532 gc.drawString(text, getTextInset(), linePixel, true); 533 } 534 } 535 536 546 protected String computeText(int line) { 547 return null; 548 } 549 550 561 protected Color computeBackground(int line) { 562 return getDefaultBackground(); 563 } 564 565 576 protected Color computeForeground(int line) { 577 return fStyledText.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY); 578 } 579 580 583 public final int getLineOfLastMouseButtonActivity() { 584 return getParentRuler().getLineOfLastMouseButtonActivity(); 585 } 586 587 590 public final int toDocumentLineNumber(int y_coordinate) { 591 return getParentRuler().toDocumentLineNumber(y_coordinate); 592 } 593 594 597 public void addVerticalRulerListener(IVerticalRulerListener listener) { 598 throw new UnsupportedOperationException (); 599 } 600 601 604 public void removeVerticalRulerListener(IVerticalRulerListener listener) { 605 throw new UnsupportedOperationException (); 606 } 607 608 616 private boolean scrollVertical(int pixels) { 617 if (pixels == 0 || fCanvas == null || fCanvas.isDisposed()) 618 return false; 619 620 final int width= getWidth(); 621 final int clientAreaHeight= fStyledText.getClientArea().height; 622 final int topMargin= 0; 623 final int leftMargin= 0; 624 final int bottomMargin= 0; 625 626 if (pixels > 0) { 627 int sourceY= topMargin + pixels; 629 int scrollHeight= clientAreaHeight - sourceY - bottomMargin; 630 if (scrollHeight > 0) 631 fCanvas.scroll(leftMargin, topMargin, leftMargin, sourceY, width, scrollHeight, true); 633 if (sourceY > scrollHeight) { 634 int redrawY= Math.max(0, topMargin + scrollHeight); 636 int redrawHeight= Math.min(clientAreaHeight, pixels - scrollHeight); 637 fCanvas.redraw(leftMargin, redrawY, width, redrawHeight, true); 638 } 639 } else { 640 int destinationY= topMargin - pixels; 642 int scrollHeight= clientAreaHeight - destinationY - bottomMargin; 643 if (scrollHeight > 0) 644 fCanvas.scroll(leftMargin, destinationY, leftMargin, topMargin, width, scrollHeight, true); 646 if (destinationY > scrollHeight) { 647 int redrawY= Math.max(0, topMargin + scrollHeight); 649 int redrawHeight= Math.min(clientAreaHeight, -pixels - scrollHeight); 650 fCanvas.redraw(leftMargin, redrawY, width, redrawHeight, true); 651 } 652 } 653 return true; 654 } 655 } 656 | Popular Tags |