Search in sources :

Example 1 with Diff

use of net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher.Diff 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 2 with Diff

use of net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher.Diff in project translationstudio8 by heartsome.

the class SourceColunmCellRenderer method positionDiff.

private boolean positionDiff(LinkedList<Diff> diffs, String text1, String text2) {
    if (diffs.size() > 2 && text1.length() == text2.length()) {
        // 判断位置不一样
        LinkedList<Diff> delDiff = new LinkedList<Diff>();
        LinkedList<Diff> insDiff = new LinkedList<Diff>();
        for (int i = 0; i < diffs.size(); i++) {
            Diff f = diffs.get(i);
            if (f.operation == Operation.DELETE) {
                delDiff.add(f);
            } else if (f.operation == Operation.INSERT) {
                insDiff.add(f);
            }
        }
        if (delDiff.size() == 0 || insDiff.size() == 0) {
            return false;
        }
        label: for (int i = 0; i < delDiff.size(); i++) {
            Diff df = delDiff.get(i);
            for (int j = 0; j < insDiff.size(); j++) {
                Diff idf = insDiff.get(j);
                if (df.text.trim().equals(idf.text.trim())) {
                    insDiff.remove(j);
                    continue label;
                }
            }
            return false;
        }
        return true;
    }
    return false;
}
Also used : Diff(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher.Diff) LinkedList(java.util.LinkedList) Point(org.eclipse.swt.graphics.Point)

Aggregations

LinkedList (java.util.LinkedList)2 Diff (net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher.Diff)2 Point (org.eclipse.swt.graphics.Point)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 TextDiffMatcher (net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher)1 GridItem (org.eclipse.nebula.widgets.grid.GridItem)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 Color (org.eclipse.swt.graphics.Color)1 TextLayout (org.eclipse.swt.graphics.TextLayout)1 TextStyle (org.eclipse.swt.graphics.TextStyle)1