Search in sources :

Example 6 with TextLayout

use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.

the class SourceColunmCellRenderer method paint.

/**
	 * {@inheritDoc}
	 */
public void paint(GC gc, Object value) {
    GridItem item = (GridItem) value;
    gc.setFont(item.getFont(getColumn()));
    boolean drawBackground = true;
    boolean drawAsSelected = isSelected();
    if (isCellSelected()) {
        drawAsSelected = true;
    }
    gc.setForeground(item.getForeground(getColumn()));
    if (drawAsSelected) {
        gc.setBackground((Color) item.getParent().getData("selectedBgColor"));
    } else {
        if (item.getParent().isEnabled()) {
            Color back = item.getBackground(getColumn());
            if (back != null) {
                gc.setBackground(back);
            } else {
                drawBackground = false;
            }
        } else {
            gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        }
    }
    if (drawBackground) {
        gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
    }
    String text = segmentViewer.getTextWidget().getText();
    // 创建 TextLayout
    TextLayout layout = getTextLayout(gc, item, getColumn(), false, false);
    String displayStr = layout.getText();
    // 附加内部标记样式前,先和源文比较,不一样的地方着色实现
    if (!tuSrcText.equals(displayStr)) {
        // TextStyle style = new TextStyle(layout.getFont(), colorConfigBean.getSrcDiffFgColor(),
        // colorConfigBean.getSrcDiffBgColor());
        // List<Position> diff = Comparator.Compare(tuSrcText, displayStr);
        // for (Iterator<Position> iterator = diff.iterator(); iterator.hasNext();) {
        // Position position = iterator.next();
        // layout.setStyle(style, position.offset, position.length);
        // }
        TextStyle diffStyle = new TextStyle(layout.getFont(), null, diffColor);
        // Increase or
        TextStyle InOrDecreaseStyle = new TextStyle(layout.getFont(), null, inOrDecreaseColor);
        // decrease
        TextStyle positionStyle = new TextStyle(layout.getFont(), null, positionColor);
        TextDiffMatcher dmp = new TextDiffMatcher();
        LinkedList<Diff> diffs = dmp.diff_main(text, displayStr);
        dmp.diff_cleanupEfficiency(diffs);
        boolean pFlg = positionDiff(diffs, text, displayStr);
        if (pFlg) {
            int insTextStart = 0;
            int delTextStart = 0;
            for (int i = 0; i < diffs.size(); i++) {
                Diff f = diffs.get(i);
                if (f.operation == Operation.EQUAL) {
                    int l = f.text.length() - 1;
                    insTextStart += l;
                    delTextStart += l;
                    continue;
                } else if (f.operation == Operation.DELETE && drawAsSelected) {
                    String delText = f.text;
                    Matcher m = PlaceHolderEditModeBuilder.PATTERN.matcher(delText);
                    TextStyle style = positionStyle;
                    boolean flg = m.find();
                    if (flg && delText.length() != 1) {
                        int start = text.indexOf(delText, delTextStart);
                        delTextStart = start + delText.length();
                        int mark = 0;
                        List<StyleRange> rangeList = new ArrayList<StyleRange>();
                        do {
                            int off = m.start();
                            StyleRange range = new StyleRange(style);
                            range.start = start + mark;
                            range.length = off - mark;
                            mark = m.end();
                            rangeList.add(range);
                        } while (m.find());
                        if (mark < delText.length()) {
                            StyleRange range = new StyleRange(style);
                            range.start = start + mark;
                            range.length = delText.length() - mark;
                            rangeList.add(range);
                        }
                        for (StyleRange range : rangeList) {
                            segmentViewer.getTextWidget().setStyleRange(range);
                        }
                    } else if (!flg) {
                        int start = text.indexOf(delText, delTextStart);
                        delTextStart = start + delText.length();
                        StyleRange range = new StyleRange(style);
                        range.start = start;
                        range.length = delText.length();
                        segmentViewer.getTextWidget().setStyleRange(range);
                    }
                } else if (f.operation == Operation.INSERT) {
                    String insText = f.text;
                    int start = displayStr.indexOf(insText, insTextStart);
                    insTextStart += insText.length();
                    layout.setStyle(positionStyle, start, start + insText.length() - 1);
                }
            }
        } else {
            int insTextStart = 0;
            int delTextStart = 0;
            for (int i = 0; i < diffs.size(); i++) {
                Diff f = diffs.get(i);
                if (f.operation == Operation.EQUAL) {
                    int l = f.text.length() - 1;
                    insTextStart += l;
                    delTextStart += l;
                    continue;
                } else if (f.operation == Operation.DELETE) {
                    String delText = f.text;
                    boolean isDiff = false;
                    if (i + 1 < diffs.size() && diffs.get(i + 1).operation == Operation.INSERT) {
                        // 库和当前句子存在不一致部分
                        i += 1;
                        // 处理库
                        f = diffs.get(i);
                        String insText = f.text;
                        int start = displayStr.indexOf(insText, insTextStart);
                        layout.setStyle(diffStyle, start, start + insText.length() - 1);
                        insTextStart = start + insText.length();
                        isDiff = true;
                    }
                    // 处理当前句子
                    if (drawAsSelected) {
                        Matcher m = PlaceHolderEditModeBuilder.PATTERN.matcher(delText);
                        TextStyle style = isDiff ? diffStyle : InOrDecreaseStyle;
                        boolean flg = m.find();
                        if (flg && delText.length() != 1) {
                            int start = text.indexOf(delText, delTextStart);
                            delTextStart = start + delText.length();
                            int mark = 0;
                            List<StyleRange> rangeList = new ArrayList<StyleRange>();
                            do {
                                int off = m.start();
                                StyleRange range = new StyleRange(style);
                                range.start = start + mark;
                                range.length = off - mark;
                                mark = m.end();
                                rangeList.add(range);
                            } while (m.find());
                            if (mark < delText.length()) {
                                StyleRange range = new StyleRange(style);
                                range.start = start + mark;
                                range.length = delText.length() - mark;
                                rangeList.add(range);
                            }
                            for (StyleRange range : rangeList) {
                                segmentViewer.getTextWidget().setStyleRange(range);
                            }
                        } else if (!flg) {
                            int start = text.indexOf(delText, delTextStart);
                            delTextStart = start + delText.length();
                            StyleRange range = new StyleRange(style);
                            range.start = start;
                            range.length = delText.length();
                            segmentViewer.getTextWidget().setStyleRange(range);
                        }
                    }
                } else if (f.operation == Operation.INSERT) {
                    // 库多出来的
                    String insText = f.text;
                    int start = displayStr.indexOf(insText, insTextStart);
                    insTextStart += insText.length();
                    layout.setStyle(InOrDecreaseStyle, start, start + insText.length() - 1);
                }
            }
        }
    }
    // 添加标记样式,并创建标记
    attachInnertTagStyle(gc, layout, false);
    try {
        int y = getBounds().y + textTopMargin + topMargin;
        y += getVerticalAlignmentAdjustment(layout.getBounds().height, getBounds().height);
        if (item.getParent().isAutoHeight()) {
            int textHeight = topMargin + textTopMargin;
            // fix Bug #3116 库匹配面板--显示的匹配有截断 by Jason
            // for (int cnt = 0; cnt < layout.getLineCount(); cnt++)
            // textHeight += layout.getLineBounds(cnt).height;
            textHeight += layout.getBounds().height;
            textHeight += textBottomMargin + bottomMargin;
            item.setData("itemHeight", textHeight);
        }
        Point selection = copyEnable.getSelectionRange(getColumn(), item);
        if (selection == null || selection.x == selection.y) {
            layout.draw(gc, getBounds().x + leftMargin, y);
        } else {
            int x = getBounds().x + leftMargin;
            int start = Math.max(0, selection.x);
            int end = Math.min(displayStr.length(), selection.y);
            layout.draw(gc, x, y, start, end - 1, getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT), getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
        }
        drawInnerTag(gc, layout);
        if (item.getParent().getLinesVisible()) {
            if (isCellSelected()) {
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
            } else {
                gc.setForeground(item.getParent().getLineColor());
            }
            gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
            gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
        }
    } finally {
        if (layout != null) {
            layout.dispose();
        }
    }
}
Also used : Diff(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher.Diff) Matcher(java.util.regex.Matcher) TextDiffMatcher(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout) GridItem(org.eclipse.nebula.widgets.grid.GridItem) TextStyle(org.eclipse.swt.graphics.TextStyle) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TextDiffMatcher(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher)

Example 7 with TextLayout

use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.

the class TargetColunmCellRenderer method paint.

/**
	 * {@inheritDoc}
	 */
public void paint(GC gc, Object value) {
    GridItem item = (GridItem) value;
    gc.setFont(item.getFont(getColumn()));
    boolean drawBackground = true;
    boolean drawAsSelected = isSelected();
    if (isCellSelected()) {
        drawAsSelected = true;
    }
    gc.setForeground(item.getForeground(getColumn()));
    if (drawAsSelected) {
        gc.setBackground((Color) item.getParent().getData("selectedBgColor"));
    } else {
        if (item.getParent().isEnabled()) {
            Color back = item.getBackground(getColumn());
            if (back != null) {
                gc.setBackground(back);
            } else {
                drawBackground = false;
            }
        } else {
            gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        }
    }
    if (drawBackground) {
        gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
    }
    TextLayout layout = getTextLayout(gc, item, getColumn(), true, false);
    String displayStr = layout.getText();
    try {
        int y = getBounds().y + textTopMargin + topMargin;
        y += getVerticalAlignmentAdjustment(layout.getBounds().height, getBounds().height);
        if (item.getParent().isAutoHeight()) {
            int textHeight = topMargin + textTopMargin;
            // fix Bug #3116 库匹配面板--显示的匹配有截断 by Jason
            // for (int cnt = 0; cnt < layout.getLineCount(); cnt++)
            // textHeight += layout.getLineBounds(cnt).height;
            textHeight += layout.getBounds().height;
            textHeight += textBottomMargin + bottomMargin;
            int heigth = (Integer) item.getData("itemHeight");
            textHeight = Math.max(textHeight, heigth);
            if (textHeight != item.getHeight()) {
                item.setHeight(textHeight);
                item.setData("itemHeight", textHeight);
            }
        }
        // Point selection = copyEnable.getSelectionRange(getColumn(), getRow() - 1); // row based zero ,get row
        // first row is 1
        Point selection = copyEnable.getSelectionRange(getColumn(), item);
        if (selection == null || selection.x == selection.y) {
            layout.draw(gc, getBounds().x + leftMargin, y);
        } else {
            // textLayout.draw(gc, getBounds().x + leftMargin, y);
            int x = getBounds().x + leftMargin;
            int start = Math.max(0, selection.x);
            int end = Math.min(displayStr.length(), selection.y);
            layout.draw(gc, x, y, start, end - 1, getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT), getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
        }
        drawInnerTag(gc, layout);
        if (item.getParent().getLinesVisible()) {
            if (isCellSelected()) {
                // XXX: should be user definable?
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
            } else {
                gc.setForeground(item.getParent().getLineColor());
            }
            gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
            gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
        }
    } finally {
        if (layout != null) {
            layout.dispose();
        }
    }
}
Also used : GridItem(org.eclipse.nebula.widgets.grid.GridItem) Color(org.eclipse.swt.graphics.Color) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 8 with TextLayout

use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.

the class TypeColunmCellRenderer method computeSize.

/**
	 * {@inheritDoc}
	 */
public Point computeSize(GC gc, int wHint, int hHint, Object value) {
    GridItem item = (GridItem) value;
    gc.setFont(item.getFont(getColumn()));
    int x = 0;
    x += leftMargin;
    int y = 0;
    Image image = item.getImage(getColumn());
    if (image != null) {
        y = topMargin + image.getBounds().height + bottomMargin;
        x += image.getBounds().width + 3;
    }
    // MOPR-DND
    // MOPR: replaced this code (to get correct preferred height for cells in word-wrap columns)
    //
    // x += gc.stringExtent(item.getText(column)).x + rightMargin;
    //
    // y = Math.max(y,topMargin + gc.getFontMetrics().getHeight() + bottomMargin);
    //
    // with this code:
    int textHeight = 0;
    if (!isWordWrap()) {
        x += gc.textExtent(item.getText(getColumn())).x + rightMargin;
        textHeight = topMargin + textTopMargin + gc.getFontMetrics().getHeight() + textBottomMargin + bottomMargin;
    } else {
        int plainTextWidth;
        if (wHint == SWT.DEFAULT)
            plainTextWidth = getBounds().width - x - rightMargin;
        else
            plainTextWidth = wHint - x - rightMargin;
        TextLayout currTextLayout = new TextLayout(gc.getDevice());
        currTextLayout.setFont(gc.getFont());
        currTextLayout.setText(item.getText(getColumn()));
        currTextLayout.setAlignment(getAlignment());
        currTextLayout.setWidth(plainTextWidth < 1 ? 1 : plainTextWidth);
        x += plainTextWidth + rightMargin;
        textHeight += topMargin + textTopMargin;
        for (int cnt = 0; cnt < currTextLayout.getLineCount(); cnt++) textHeight += currTextLayout.getLineBounds(cnt).height;
        textHeight += textBottomMargin + bottomMargin;
        currTextLayout.dispose();
    }
    y = Math.max(y, textHeight);
    return new Point(x, y);
}
Also used : GridItem(org.eclipse.nebula.widgets.grid.GridItem) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 9 with TextLayout

use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.

the class XGridCellRenderer method getTextLayout.

public TextLayout getTextLayout(GC gc, GridItem item, int columnIndex, boolean innerTagStyled, boolean drawInnerTag) {
    TextLayout layout = new TextLayout(gc.getDevice());
    layout.setFont(font);
    layout.setTabs(new int[] { tabWidth });
    innerTagFactory.reset();
    String displayStr = "";
    try {
        displayStr = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag(item.getText(columnIndex)));
    } catch (NullPointerException e) {
        return null;
    }
    layout.setText(displayStr);
    int width = getBounds().width - leftMargin - rightMargin;
    layout.setWidth(width < 1 ? 1 : width);
    layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
    layout.setAlignment(SWT.LEFT);
    layout.setOrientation(item.getParent().getOrientation());
    if (displayStr.length() != 0 && innerTagStyled) {
        attachInnertTagStyle(gc, layout, drawInnerTag);
    }
    return layout;
}
Also used : Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 10 with TextLayout

use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.

the class XGridCellRenderer method setFont.

/**
	 * 设置字体
	 * @param font
	 *            ;
	 */
public void setFont(Font font) {
    TextLayout layout = new TextLayout(Display.getDefault());
    layout.setFont(font);
    StringBuffer tabBuffer = new StringBuffer(tabSize);
    for (int i = 0; i < tabSize; i++) {
        tabBuffer.append(' ');
    }
    layout.setText(tabBuffer.toString());
    tabWidth = layout.getBounds().width;
    layout.dispose();
    this.font = font;
}
Also used : Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Aggregations

TextLayout (org.eclipse.swt.graphics.TextLayout)26 Point (org.eclipse.swt.graphics.Point)19 GridItem (org.eclipse.nebula.widgets.grid.GridItem)13 Color (org.eclipse.swt.graphics.Color)10 Image (org.eclipse.swt.graphics.Image)10 Rectangle (org.eclipse.swt.graphics.Rectangle)10 DisposeEvent (org.eclipse.swt.events.DisposeEvent)9 DisposeListener (org.eclipse.swt.events.DisposeListener)9 TextStyle (org.eclipse.swt.graphics.TextStyle)7 Font (org.eclipse.swt.graphics.Font)4 Matcher (java.util.regex.Matcher)3 ArrayList (java.util.ArrayList)2 Pattern (java.util.regex.Pattern)2 InnerTagBean (net.heartsome.cat.common.innertag.InnerTagBean)2 XliffInnerTagFactory (net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory)2 GridColumn (org.eclipse.nebula.widgets.grid.GridColumn)2 StyleRange (org.eclipse.swt.custom.StyleRange)2 RGB (org.eclipse.swt.graphics.RGB)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1