Java 类org.eclipse.jface.text.JFaceTextUtil 实例源码

项目:TranskribusSwtGui    文件:ATranscriptionWidget.java   
protected void updateLineStyles() {
        if (!text.isEnabled()) {
            return;
        }

        logger.trace("updating line styles!");

        // set line bullet:
        setLineBulletAndStuff();

        // set global style(s): <-- NO NEED TO DO SO, AS OVERWRITTEN BY setStyleRanges call below!!     
//      StyleRange srDefault = new StyleRange(TrpUtil.getDefaultSWTTextStyle(text.getFont().getFontData()[0], settings));
//      srDefault.start=0;
//      srDefault.length = text.getText().length();
//      text.setStyleRange(srDefault);

        // get specific style ranges for all lines:
        List<StyleRange> allStyles = new ArrayList<>();

        if (true)
        for (int i=JFaceTextUtil.getPartialTopIndex(text); i<=JFaceTextUtil.getPartialBottomIndex(text); ++i) { // only for visible lines       
//      for (int i=0; i<text.getLineCount(); ++i) {
            List<StyleRange> styles = getLineStyleRanges(text.getOffsetAtLine(i));          
            allStyles.addAll(styles);
        }

        // set style ranges:
        try {
            text.setStyleRanges(allStyles.toArray(new StyleRange[0]));
        } catch (IllegalArgumentException e) {
            logger.error("Could not update line styles - skipping");
        }

//      if (true)
//          centerCurrentLine();

        text.redraw();
    }
项目:TranskribusSwtGui    文件:ATranscriptionWidget.java   
/** Makes sure that there is always a line above/below the current line if possible */
private void centerCurrentLine() {
    if (!settings.isCenterCurrentTranscriptionLine())
        return;

    int ci = text.getLineAtOffset(text.getCaretOffset());
    int ti = text.getTopIndex();
    int bi = JFaceTextUtil.getBottomIndex(text);
    if ((ci == ti && ci-1>=0) || (ci == bi && ci-1>=0)) {
        text.setTopIndex(ci-1);
        updateLineStyles();
    }
}
项目:TranskribusSwtGui    文件:LineTranscriptionWidget.java   
@Override protected void onPaintTags(PaintEvent e) {
    if (currentRegionObject == null || currentRegionObject.getTextLine().isEmpty())
        return;

    logger.trace("onPaintTags!");

    Font oldFont = e.gc.getFont();
    // for all visible lines:
    int firstLine = JFaceTextUtil.getPartialTopIndex(text);
    int lastLine = JFaceTextUtil.getPartialBottomIndex(text);
    logger.trace("firstline = "+firstLine+" lastLine = "+lastLine);

    for (int i = firstLine; i <= lastLine; ++i) {
        TrpTextLineType line = getLineObject(i);
        if (line==null) {
            logger.error("Could not paint line tags for line "+i+" - should not happen here!");
            return;
        }

        CustomTagList ctl = line.getCustomTagList();
        int lo = text.getOffsetAtLine(i);
        logger.trace("i = " + i + " lo = " + lo);

        paintTagsFromCustomTagList(e, ctl, lo);
    }
    e.gc.setFont(oldFont); // needed ?? (most probably not)
}
项目:TranskribusSwtGui    文件:WordTranscriptionWidget.java   
@Override protected void onPaintTags(PaintEvent e) {
    if (false) return;

    if (currentRegionObject == null || currentRegionObject.getTextLine().isEmpty())
        return;

    // FIXME??
    Font oldFont = e.gc.getFont();
    // for all visible lines:
    for (int i = JFaceTextUtil.getPartialTopIndex(text); i <= JFaceTextUtil.getPartialBottomIndex(text); ++i) {
        TrpTextLineType line = getLineObject(i);
        if (line==null) {
            logger.error("Could not paint line tags - should not happen here!");
            return;
        }           

        int lo = text.getOffsetAtLine(i);

        TreeMap<TrpWordType, Pair<Integer, Integer>> wordRanges = getWordRanges(line);
        for (TrpWordType word : wordRanges.keySet()) {
            int wo = wordRanges.get(word).getLeft();
            logger.trace("i = " + i + " lo: " + lo + " word: "+word.getId()+" wo: "+wo);
            CustomTagList ctl = word.getCustomTagList();
            paintTagsFromCustomTagList(e, ctl, lo+wo);
        }
    }
    e.gc.setFont(oldFont); // needed ?? (most probably not)
}
项目:TranskribusSwtGui    文件:XmlViewer.java   
private void centerCurrentLine() {
        int ci = text.getLineAtOffset(text.getCaretOffset());
        int ti = text.getTopIndex();
        int bi = JFaceTextUtil.getBottomIndex(text);
//      logger.debug("ci = "+ci+" ti = "+ti+" bi = "+bi);
        if ((ci < ti && ci-1>=0) || (ci > bi && ci-1>=0)) {
//          logger.debug("setting top index to: "+(ci-1));
            text.setTopIndex(ci-1);
        }
    }
项目:APICloud-Studio    文件:CommonLineNumberRulerColumn.java   
/**
 * Returns the viewer's first visible line, even if only partially visible.
 *
 * @return the viewer's first visible line
 */
private int getInclusiveTopIndex() {
    if (fCachedTextWidget != null && !fCachedTextWidget.isDisposed()) {
        return JFaceTextUtil.getPartialTopIndex(fCachedTextViewer);
    }
    return -1;
}
项目:APICloud-Studio    文件:CommonLineNumberRulerColumn.java   
/**
 * Double buffer drawing.
 *
 * @param dest the GC to draw into
 */
private void doubleBufferPaint(GC dest) {

    Point size= fCanvas.getSize();

    if (size.x <= 0 || size.y <= 0)
        return;

    if (fBuffer != null) {
        Rectangle r= fBuffer.getBounds();
        if (r.width != size.x || r.height != size.y) {
            fBuffer.dispose();
            fBuffer= null;
        }
    }
    if (fBuffer == null)
        fBuffer= new Image(fCanvas.getDisplay(), size.x, size.y);

    GC gc= new GC(fBuffer);
    gc.setFont(fCanvas.getFont());
    if (fForeground != null)
        gc.setForeground(fForeground);

    try {
        gc.setBackground(getBackground(fCanvas.getDisplay()));
        gc.fillRectangle(0, 0, size.x, size.y);

        ILineRange visibleLines= JFaceTextUtil.getVisibleModelLines(fCachedTextViewer);
        if (visibleLines == null)
            return;
        fScrollPos= fCachedTextWidget.getTopPixel();
        doPaint(gc, visibleLines);
    } finally {
        gc.dispose();
    }

    dest.drawImage(fBuffer, 0, 0);
}
项目:APICloud-Studio    文件:CommonLineNumberRulerColumn.java   
void doPaint(GC gc, ILineRange visibleLines)
{

    Display display = fCachedTextWidget.getDisplay();

    // draw diff info
    int y = -JFaceTextUtil.getHiddenTopLinePixels(fCachedTextWidget);
    List<LineMap> lineMap = getLineMap(fCachedTextWidget, visibleLines);
    int lineCount = fCachedTextWidget.getLineCount();
    int lineMapSize = lineMap.size();
    for (int i = 0; i < lineMapSize; i++)
    {
        LineMap map = lineMap.get(i);
        int line = map.documentLine;
        int widgetLine = map.widgetLine;
        try
        {
            int lineHeight = fCachedTextWidget.getLineHeight(fCachedTextWidget.getOffsetAtLine(widgetLine));
            paintLine(line, y, lineHeight, gc, display);

            int distanceFromPrevious = 1;
            if (line < lineCount - 1 && i + 1 < lineMapSize)
            {
                distanceFromPrevious = lineMap.get(i + 1).visualLine - map.visualLine;
            }

            y += (lineHeight * distanceFromPrevious);
        }
        catch (Exception e)
        {
            IdeLog.logError(EditorEplPlugin.getDefault(), MessageFormat.format("Error painting line number: {0}", map), e); //$NON-NLS-1$
        }
    }

}
项目:APICloud-Studio    文件:CommonLineNumberRulerColumn.java   
/**
 * Returns the number of lines in the view port.
 *
 * @param textWidget the styled text widget
 * @return the number of lines visible in the view port <code>-1</code> if there's no client
 *         area
 * @deprecated this method should not be used - it relies on the widget using a uniform line
 *             height
 */
static int getVisibleLinesInViewport(StyledText textWidget) {
    if (textWidget != null) {
        Rectangle clArea= textWidget.getClientArea();
        if (!clArea.isEmpty()) {
            int firstPixel= 0;
            int lastPixel= clArea.height - 1; // XXX: what about margins? don't take trims as they include scrollbars
            int first= JFaceTextUtil.getLineIndex(textWidget, firstPixel);
            int last= JFaceTextUtil.getLineIndex(textWidget, lastPixel);
            return last - first;
        }
    }
    return -1;
}
项目:SPLevo    文件:UnifiedDiffRulerColumn.java   
@Override
protected void paintLine(int line, int y, int lineheight, GC gc, Display display) {
    int widgetLine = JFaceTextUtil.modelLineToWidgetLine(getParentRuler().getTextViewer(), line);

    // get the corresponding line number and background color for this column
    String text = "";
    Color bgColor = null;
    if (line < indexedLineNumbersList.size()) {
        bgColor = linesToColorMapping.get(line);
        List<IndexedLineNumber> curLine = indexedLineNumbersList.get(line);
        for (IndexedLineNumber lineNumber : curLine) {
            if (lineNumber.getColumnIndex() == columnIndex) {
                text = createDisplayString(lineNumber.getNumber());
            }
        }
    }

    // get line indentation
    int[] indentations = null;
    try {
        indentations = getIndentations();
    } catch (Exception e) {
        LOGGER.error("Could not get unified column line indentation!", e);
    }

    // calculate proper positioning and draw number
    if (indentations != null) {
        int indentation = indentations[text.length()];
        int baselineBias = getBaselineBias(gc, widgetLine);
        if (bgColor != null) {
            gc.setBackground(bgColor);
            gc.drawString(text, indentation, y + baselineBias, false);
        } else {
            gc.drawString(text, indentation, y + baselineBias, true);
        }
    }
}
项目:codelens-eclipse    文件:ViewZoneChangeAccessor.java   
private int getLineNumber(int widgetLine) {
    return JFaceTextUtil.widgetLine2ModelLine(textViewer, widgetLine) + 1;
}
项目:codelens-eclipse    文件:LineNumberChangeRulerColumnPatch.java   
/**
 * Draws the ruler column.
 *
 * @param gc
 *            the GC to draw into
 * @param visibleLines
 *            the visible model lines
 * @since 3.2
 */
void doPaintPatch(GC gc, ILineRange visibleLines, LineNumberChangeRulerColumn l) {
    Display display = fCachedTextWidget.getDisplay();

    // draw diff info
    int y = -JFaceTextUtil.getHiddenTopLinePixels(fCachedTextWidget);

    // add empty lines if line is wrapped
    boolean isWrapActive = fCachedTextWidget.getWordWrap();

    int lastLine = end(visibleLines);
    for (int line = visibleLines.getStartLine(); line < lastLine; line++) {
        int widgetLine = JFaceTextUtil.modelLineToWidgetLine(fCachedTextViewer, line);
        if (widgetLine == -1)
            continue;

        final int offsetAtLine = fCachedTextWidget.getOffsetAtLine(widgetLine);
        // START PATCH
        // int lineHeight=
        // fCachedTextWidget.getLineHeight(offsetAtLine);
        int lineHeight = JFaceTextUtil.computeLineHeight(fCachedTextWidget, widgetLine, widgetLine + 1, 1);
        // END PATCH
        paintLine(line, y, lineHeight, gc, display, l);

        // increment y position
        if (!isWrapActive) {
            y += lineHeight;
        } else {
            int charCount = fCachedTextWidget.getCharCount();
            if (offsetAtLine == charCount)
                continue;

            // end of wrapped line
            final int offsetEnd = offsetAtLine + fCachedTextWidget.getLine(widgetLine).length();

            if (offsetEnd == charCount)
                continue;

            // use height of text bounding because bounds.width changes
            // on
            // word wrap
            y += fCachedTextWidget.getTextBounds(offsetAtLine, offsetEnd).height;
        }
    }
}
项目:TranskribusSwtGui    文件:ATranscriptionWidget.java   
/**
     * Parses through all visible characters and renders control signs (whitespace, tab, end of line, paragraphs)
     * into the text area without changing the text itself.
     */
    protected void paintControlSigns(PaintEvent e) {
        int firstLine = JFaceTextUtil.getPartialTopIndex(text);
        int lastLine = JFaceTextUtil.getPartialBottomIndex(text);

        int firstCharIndex = text.getOffsetAtLine(firstLine);
        int lastCharIndex = text.getOffsetAtLine(lastLine)+text.getLine(lastLine).length();

        logger.trace("firstCharIndex = "+firstCharIndex+" lastCharIndex = "+lastCharIndex);

        StyleRange sr = null;
        if (!text.getText().isEmpty())
            sr = text.getStyleRangeAtOffset(0);
        for (int i = firstCharIndex; i<=lastCharIndex-1; ++i) {         
            String charStr = text.getText(i, i);

            StyleRange sr1 = text.getStyleRangeAtOffset(i);
            if (sr1 != null)
                sr = sr1;

            logger.trace("charStr = "+charStr);
            String controlChar = null;
            if (charStr.equals(" ")) { // draw whitespace
                final boolean RENDER_WHITESPACE_CONTROL_CHAR_WITH_OVAL = false;
                if (RENDER_WHITESPACE_CONTROL_CHAR_WITH_OVAL) {
                    Rectangle r = text.getTextBounds(i, i);
                    logger.trace("painting whitespace - b = "+r);               
                    e.gc.setBackground(Colors.getSystemColor(SWT.COLOR_RED));

                    e.gc.fillOval(r.x+r.width/2, r.y+r.height/2, r.width/2, r.width/2);
                } else {
                    controlChar = "\u00B7"; 
                }
            } else if (charStr.endsWith("\n")) { // draw end of line or paragraph               
                TrpTextLineType line = getTextLineAtOffset(i);
                logger.trace("line = "+line);

                if (line != null && CustomTagUtil.hasParagraphStructure(line)) {
                    logger.trace("PARAGRAPH CHAR!!");
                    controlChar = "\u00B6"; 
                } else {
                    logger.trace("END OF LINE CHAR!!");
                    controlChar = "\u23CE";
                }
            } else if (charStr.equals("\t")) { // draw tab
                logger.trace("TAB CHAR!!");
                controlChar = "\u21A6";
            }

            // draw control character if set:
            if (controlChar != null) {
//              e.gc.setBackground(Colors.getSystemColor(SWT.COLOR_WHITE));
                e.gc.setForeground(Colors.getSystemColor(SWT.COLOR_RED));

                if (sr != null) {
                    e.gc.setFont(sr.font);
                }
                e.gc.setAntialias(SWT.ON);

                boolean BOLD_CONTROL_SIGN_FONT = false;
                if (BOLD_CONTROL_SIGN_FONT)
                    e.gc.setFont(Fonts.createBoldFont(e.gc.getFont()));

                Point p = text.getLocationAtOffset(i);
                logger.trace("point = "+p);
                e.gc.drawText(controlChar, p.x, p.y, true);
            }

        }   
    }
项目:APICloud-Studio    文件:CommonLineNumberRulerColumn.java   
/**
 * Paints the line. After this method is called the line numbers are painted on top
 * of the result of this method.
 *
 * @param line the line of the document which the ruler is painted for
 * @param y the y-coordinate of the box being painted for <code>line</code>, relative to <code>gc</code>
 * @param lineheight the height of one line (and therefore of the box being painted)
 * @param gc the drawing context the client may choose to draw on.
 * @param display the display the drawing occurs on
 * @since 3.0
 */
protected void paintLine(int line, int y, int lineheight, GC gc, Display display) {
    int widgetLine= JFaceTextUtil.modelLineToWidgetLine(fCachedTextViewer, line);

    String s= createDisplayString(line);
    int indentation= fIndentation[s.length()];
    int baselineBias= getBaselineBias(gc, widgetLine);
    gc.drawString(s, indentation, y + baselineBias, true);
}
项目:APICloud-Studio    文件:CommonLineNumberRulerColumn.java   
/**
 * Returns <code>true</code> if the viewport displays the entire viewer contents, i.e. the
 * viewer is not vertically scrollable.
 *
 * @return <code>true</code> if the viewport displays the entire contents, <code>false</code> otherwise
 * @since 3.2
 */
protected final boolean isViewerEntirelyShown() {
    return JFaceTextUtil.isShowingEntireContents(fCachedTextWidget);
}