All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jface.text.MarginPainter Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text;


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;


/**
 * Paints a vertical line (margin line) after a given column respecting the text
 * viewer's font.
 * 

* Clients usually instantiate and configure objects of this class.

*

* This class is not intended to be subclassed.

* * @since 2.1 * @noextend This class is not intended to be subclassed by clients. */ public class MarginPainter implements IPainter, PaintListener { /** The widget of the text viewer */ private StyledText fTextWidget; /** The column after which to paint the line, default value 80 */ private int fMarginWidth= 80; /** The color in which to paint the line */ private Color fColor; /** The line style of the line to be painted, default value SWT.LINE_SOLID */ private int fLineStyle= SWT.LINE_SOLID; /** The line width of the line to be painted, default value 1 */ private int fLineWidth= 0; // NOTE: 0 means width is 1 but with optimized performance /** The cached x-offset of the fMarginWidth for the current font */ private int fCachedWidgetX= -1; /** The active state of this painter */ private boolean fIsActive= false; /** * Creates a new painter for the given text viewer. * * @param textViewer the text viewer */ public MarginPainter(ITextViewer textViewer) { fTextWidget= textViewer.getTextWidget(); } /** * Sets the column after which to draw the margin line. * * @param width the column */ public void setMarginRulerColumn(int width) { fMarginWidth= width; initialize(); } /** * Sets the line style of the margin line. * * @param lineStyle a SWT style constant describing the line style */ public void setMarginRulerStyle(int lineStyle) { fLineStyle= lineStyle; } /** * Sets the line width of the margin line. * * @param lineWidth the line width */ public void setMarginRulerWidth(int lineWidth) { if (lineWidth == 1) lineWidth= 0; // NOTE: 0 means width is 1 but with optimized performance fLineWidth= lineWidth; } /** * Sets the color of the margin line. Must be called before paint is called the first time. * * @param color the color */ public void setMarginRulerColor(Color color) { fColor= color; } /** * Initializes this painter, by flushing and recomputing all caches and causing * the widget to be redrawn. Must be called explicitly when font of text widget changes. */ public void initialize() { computeWidgetX(); fTextWidget.redraw(); } /** * Computes and remembers the x-offset of the margin column for the * current widget font. */ private void computeWidgetX() { GC gc= new GC(fTextWidget); int pixels= gc.getFontMetrics().getAverageCharWidth(); gc.dispose(); fCachedWidgetX= pixels * fMarginWidth; } @Override public void deactivate(boolean redraw) { if (fIsActive) { fIsActive= false; fCachedWidgetX= -1; fTextWidget.removePaintListener(this); if (redraw) fTextWidget.redraw(); } } @Override public void dispose() { fTextWidget= null; } @Override public void paint(int reason) { if (!fIsActive) { fIsActive= true; fTextWidget.addPaintListener(this); if (fCachedWidgetX == -1) computeWidgetX(); fTextWidget.redraw(); } else if (CONFIGURATION == reason || INTERNAL == reason) fTextWidget.redraw(); } @Override public void paintControl(PaintEvent e) { if (fTextWidget != null) { int x= fCachedWidgetX - fTextWidget.getHorizontalPixel(); if (x >= 0) { Rectangle area= fTextWidget.getClientArea(); e.gc.setForeground(fColor); e.gc.setLineStyle(fLineStyle); e.gc.setLineWidth(fLineWidth); e.gc.drawLine(x, 0, x, area.height); } } } @Override public void setPositionManager(IPaintPositionManager manager) { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy