Search in sources :

Example 46 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project eclipse.platform.text by eclipse.

the class OverviewRuler method toLineNumbers.

/**
 * Translates a given y-coordinate of this ruler into the corresponding
 * document lines. The number of lines depends on the concrete scaling
 * given as the ration between the height of this ruler and the length
 * of the document.
 *
 * @param y_coordinate the y-coordinate
 * @param annotationRect <code>true</code> to only consider the position of a drawn annotation rectangle,
 * 	<code>false</code> to consider the whole line
 * @return the corresponding document lines as {firstLine, lastLine}, or {-1, -1} if no lines correspond to the y-coordinate
 */
private int[] toLineNumbers(int y_coordinate, boolean annotationRect) {
    // this is the inverse of #computeY(int, WidgetInfos)
    WidgetInfos infos = new WidgetInfos(fTextViewer.getTextWidget(), fCanvas);
    if (y_coordinate >= infos.writable || y_coordinate >= infos.bounds.height || y_coordinate < 0)
        return new int[] { -1, -1 };
    if (annotationRect && ANNOTATION_HEIGHT >= infos.bounds.height / infos.maxLines)
        annotationRect = false;
    int[] lines = new int[2];
    int[] pixels;
    int pixelEnd = Math.min(infos.bounds.height, y_coordinate + ANNOTATION_HEIGHT / 2 + 1);
    if (annotationRect) {
        pixels = new int[] { pixelEnd };
    } else {
        int pixelStart = Math.max(y_coordinate - ANNOTATION_HEIGHT / 2 + 1, 0);
        pixels = new int[] { pixelStart, pixelEnd };
    }
    if (infos.bounds.height > infos.writable || infos.invisibleLines <= 0) {
        // yy = Math.max(0, (2 * startLine + 1) * infos.writable / (infos.maxLines * 2) - infos.bounds.y);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) ((pixels[i] + infos.bounds.y) * infos.maxLines / (double) infos.writable);
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("static y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    } else if (y_coordinate < infos.thumbHeight / 2) {
        // yy= (int) (startLine * infos.thumbHeight / infos.visibleLines);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) (pixels[i] * infos.visibleLines / infos.thumbHeight);
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("start  y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    } else if (infos.bounds.height - 1 - infos.thumbHeight / 2 < y_coordinate) {
        // yy= (int) (infos.bounds.height-1 - (double)infos.thumbHeight / 2 + (startLine - (infos.maxLines - infos.visibleLines / 2) + 1) * infos.thumbHeight / infos.visibleLines);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) ((pixels[i] - (infos.bounds.height - 1) + (double) infos.thumbHeight / 2) * infos.visibleLines / infos.thumbHeight - 1 + (infos.maxLines - infos.visibleLines / 2));
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("end    y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    } else {
        // yy= (int) ((double)infos.thumbHeight/2 + (startLine + 1 - infos.visibleLines / 2) * (infos.bounds.height-1 - infos.thumbHeight) / infos.invisibleLines);
        for (int i = 0; i < pixels.length; i++) lines[i] = (int) ((pixels[i] - (double) infos.thumbHeight / 2) * infos.invisibleLines / (infos.bounds.height - 1 - infos.thumbHeight) - 1 + infos.visibleLines / 2);
        if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            System.out.println("middle y: " + y_coordinate + " => [" + lines[0] + ", " + lines[1] + "]");
    }
    if (y_coordinate < ANNOTATION_HEIGHT && y_coordinate < infos.bounds.y)
        lines[0] = 0;
    else if (lines[0] < 0)
        lines[0] = 0;
    if (annotationRect) {
        int y0 = computeY(lines[0], infos);
        if (y_coordinate < y0 || y0 + ANNOTATION_HEIGHT < y_coordinate) {
            lines[0] = -1;
            lines[1] = -1;
            return lines;
        } else {
            lines[1] = lines[0];
        }
    }
    if (lines[1] > infos.maxLines)
        lines[1] = infos.maxLines;
    if (fTextViewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension = (ITextViewerExtension5) fTextViewer;
        lines[0] = extension.widgetLine2ModelLine(lines[0]);
        lines[1] = extension.widgetLine2ModelLine(lines[1]);
    } else {
        try {
            IRegion visible = fTextViewer.getVisibleRegion();
            int lineNumber = fTextViewer.getDocument().getLineOfOffset(visible.getOffset());
            lines[0] += lineNumber;
            lines[1] += lineNumber;
        } catch (BadLocationException x) {
        }
    }
    if (DEBUG_TO_DOCUMENT_LINE_NUMBER)
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        System.out.println("result: [" + lines[0] + ", " + lines[1] + "]");
    return lines;
}
Also used : ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 47 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project webtools.sourceediting by eclipse.

the class StructuredTextEditor method gotoMatchingBracket.

/**
 * Jumps to the matching bracket.
 */
void gotoMatchingBracket() {
    ICharacterPairMatcher matcher = createCharacterPairMatcher();
    if (matcher == null)
        return;
    ISourceViewer sourceViewer = getSourceViewer();
    IDocument document = sourceViewer.getDocument();
    if (document == null)
        return;
    IRegion selection = getSignedSelection(sourceViewer);
    int selectionLength = Math.abs(selection.getLength());
    if (selectionLength > 1) {
        setStatusLineErrorMessage(SSEUIMessages.GotoMatchingBracket_error_invalidSelection);
        sourceViewer.getTextWidget().getDisplay().beep();
        return;
    }
    int sourceCaretOffset = selection.getOffset() + selection.getLength();
    IRegion region = matcher.match(document, sourceCaretOffset);
    if (region == null) {
        setStatusLineErrorMessage(SSEUIMessages.GotoMatchingBracket_error_noMatchingBracket);
        sourceViewer.getTextWidget().getDisplay().beep();
        return;
    }
    int offset = region.getOffset();
    int length = region.getLength();
    if (length < 1)
        return;
    int anchor = matcher.getAnchor();
    // go to after the match if matching to the right
    int targetOffset = (ICharacterPairMatcher.RIGHT == anchor) ? offset : offset + length;
    boolean visible = false;
    if (sourceViewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
        visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
    } else {
        IRegion visibleRegion = sourceViewer.getVisibleRegion();
        // http://dev.eclipse.org/bugs/show_bug.cgi?id=34195
        visible = (targetOffset >= visibleRegion.getOffset() && targetOffset <= visibleRegion.getOffset() + visibleRegion.getLength());
    }
    if (!visible) {
        setStatusLineErrorMessage(SSEUIMessages.GotoMatchingBracket_error_bracketOutsideSelectedElement);
        sourceViewer.getTextWidget().getDisplay().beep();
        return;
    }
    if (selection.getLength() < 0)
        targetOffset -= selection.getLength();
    if (sourceViewer != null) {
        sourceViewer.setSelectedRange(targetOffset, selection.getLength());
        sourceViewer.revealRange(targetOffset, selection.getLength());
    }
}
Also used : ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) ICharacterPairMatcher(org.eclipse.jface.text.source.ICharacterPairMatcher) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point)

Example 48 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project webtools.sourceediting by eclipse.

the class OpenFileHyperlinkTracker method repairRepresentation.

private void repairRepresentation(boolean redrawAll) {
    if (fActiveRegion == null)
        return;
    int offset = fActiveRegion.getOffset();
    int length = fActiveRegion.getLength();
    fActiveRegion = null;
    ITextViewer viewer = getTextViewer();
    if (viewer != null) {
        resetCursor(viewer);
        // Remove underline
        if (viewer instanceof ITextViewerExtension5) {
            ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
            offset = extension.modelOffset2WidgetOffset(offset);
        } else {
            offset -= viewer.getVisibleRegion().getOffset();
        }
        try {
            StyledText text = viewer.getTextWidget();
            // need clearBackground to be true for paint event to be fired
            text.redrawRange(offset, length, true);
        } catch (IllegalArgumentException x) {
            Logger.logException(x);
        }
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Point(org.eclipse.swt.graphics.Point) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 49 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project webtools.sourceediting by eclipse.

the class OpenFileHyperlinkTracker method paintControl.

/*
	 * @see PaintListener#paintControl(PaintEvent)
	 */
public void paintControl(PaintEvent event) {
    if (fActiveRegion == null)
        return;
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    StyledText text = viewer.getTextWidget();
    if (text == null || text.isDisposed())
        return;
    int offset = 0;
    int length = 0;
    if (viewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
        IRegion widgetRange = extension.modelRange2WidgetRange(fActiveRegion);
        if (widgetRange == null)
            return;
        offset = widgetRange.getOffset();
        length = widgetRange.getLength();
    } else {
        IRegion region = viewer.getVisibleRegion();
        if (!includes(region, fActiveRegion))
            return;
        offset = fActiveRegion.getOffset() - region.getOffset();
        length = fActiveRegion.getLength();
    }
    // support for bidi
    Point minLocation = getMinimumLocation(text, offset, length);
    Point maxLocation = getMaximumLocation(text, offset, length);
    int x1 = minLocation.x;
    int x2 = minLocation.x + maxLocation.x - minLocation.x - 1;
    int y = minLocation.y + text.getLineHeight() - 1;
    GC gc = event.gc;
    if (fColor != null && !fColor.isDisposed())
        gc.setForeground(fColor);
    gc.drawLine(x1, y, x2, y);
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Point(org.eclipse.swt.graphics.Point) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 50 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project webtools.sourceediting by eclipse.

the class Highlighter method getDocumentRangeFromWidgetRange.

/**
 * Adjust the given widget offset and length so that they are the
 * textviewer document's offset and length, taking into account what is
 * actually visible in the document.
 *
 * @param offset
 * @param length
 * @return a region containing the offset and length within the
 *         textviewer's document or null if the offset is not within the
 *         document
 */
private IRegion getDocumentRangeFromWidgetRange(int offset, int length) {
    IRegion styleRegion = null;
    ITextViewer viewer = getTextViewer();
    if (viewer instanceof ITextViewerExtension5) {
        // get document range, taking into account folding regions in
        // viewer
        ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
        styleRegion = extension.widgetRange2ModelRange(new Region(offset, length));
    } else {
        // get document range, taking into account viewer visible region
        // get visible region in viewer
        IRegion vr = null;
        if (viewer != null)
            vr = viewer.getVisibleRegion();
        else
            vr = new Region(0, getDocument().getLength());
        // care
        if (offset <= vr.getLength()) {
            // Adjust the offset to be within visible region
            styleRegion = new Region(offset + vr.getOffset(), length);
        }
    }
    return styleRegion;
}
Also used : ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) ITextViewer(org.eclipse.jface.text.ITextViewer)

Aggregations

ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)58 Point (org.eclipse.swt.graphics.Point)39 IRegion (org.eclipse.jface.text.IRegion)38 StyledText (org.eclipse.swt.custom.StyledText)23 Region (org.eclipse.jface.text.Region)17 BadLocationException (org.eclipse.jface.text.BadLocationException)14 IDocument (org.eclipse.jface.text.IDocument)14 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)7 ITextViewer (org.eclipse.jface.text.ITextViewer)6 Rectangle (org.eclipse.swt.graphics.Rectangle)6 Position (org.eclipse.jface.text.Position)5 GC (org.eclipse.swt.graphics.GC)5 StyleRange (org.eclipse.swt.custom.StyleRange)4 ITextViewerExtension2 (org.eclipse.jface.text.ITextViewerExtension2)3 ITypedRegion (org.eclipse.jface.text.ITypedRegion)3 ITextSelection (org.eclipse.jface.text.ITextSelection)2 Color (org.eclipse.swt.graphics.Color)2 Image (org.eclipse.swt.graphics.Image)2 Display (org.eclipse.swt.widgets.Display)2 Entry (java.util.Map.Entry)1