use of net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher 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();
}
}
}
Aggregations