Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add DiffMatchPatchLabel; use in Translation Memory view
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Dec 6, 2011
1 parent c53316f commit 53869df
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 78 deletions.
Expand Up @@ -38,7 +38,7 @@ public interface Display extends WidgetDisplay

HasText getTmTextBox();

void createTable(ArrayList<TranslationMemoryItem> memories);
void createTable(String query, ArrayList<TranslationMemoryItem> memories);

void startProcessing();

Expand Down Expand Up @@ -88,7 +88,7 @@ public void showResultsFor(TransUnit transUnit)
showResults(query, searchType);
}

private void showResults(String query, GetTranslationMemory.SearchType searchType)
private void showResults(final String query, GetTranslationMemory.SearchType searchType)
{
display.startProcessing();
final GetTranslationMemory action = new GetTranslationMemory(query, workspaceContext.getWorkspaceId().getLocaleId(), searchType);
Expand All @@ -104,7 +104,7 @@ public void onFailure(Throwable caught)
public void onSuccess(GetTranslationMemoryResult result)
{
ArrayList<TranslationMemoryItem> memories = result.getMemories();
display.createTable(memories);
display.createTable(query, memories);
}
});
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.customware.gwt.presenter.client.EventBus;

import org.zanata.webtrans.client.events.TransMemoryCopyEvent;
import org.zanata.webtrans.client.ui.DiffMatchPatchLabel;
import org.zanata.webtrans.client.ui.HighlightingLabel;
import org.zanata.webtrans.shared.model.TranslationMemoryItem;

Expand Down Expand Up @@ -150,7 +151,7 @@ public void stopProcessing()
}

@Override
public void createTable(ArrayList<TranslationMemoryItem> memories)
public void createTable(String query, ArrayList<TranslationMemoryItem> memories)
{
// TODO most of this should be in TransMemoryPresenter
clearResults();
Expand All @@ -166,7 +167,7 @@ public void createTable(ArrayList<TranslationMemoryItem> memories)
final String targetMessage = memory.getTarget();
final int similarity = memory.getSimilarityPercent();

resultTable.setWidget(row, SOURCE_COL, new HighlightingLabel(sourceMessage));
resultTable.setWidget(row, SOURCE_COL, new DiffMatchPatchLabel(query, sourceMessage));
resultTable.setWidget(row, TARGET_COL, new HighlightingLabel(targetMessage));
resultTable.setText(row, SIMILARITY_COL, similarity + "%");

Expand Down

This file was deleted.

@@ -0,0 +1,55 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.ui;

import com.google.gwt.user.client.ui.HTML;

public class DiffMatchPatchLabel extends HTML
{
private String original;
private String plainText;

public DiffMatchPatchLabel()
{
}

public DiffMatchPatchLabel(String orig, String text)
{
super();
this.original = orig;
setText(text);
}

@Override
public String getText()
{
return plainText;
}

@Override
public void setText(String text)
{
this.plainText = text;
String diff = Highlighting.diffAsHtml(original, plainText);
setHTML(diff);
}

}
@@ -0,0 +1,117 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.ui;

import com.google.gwt.user.client.Element;

public class Highlighting
{

public static native void syntaxHighlight(String text, Element elem)/*-{
elem.innerHTML = '';
$wnd.highlightText(text, elem);
}-*/;

public static native void searchHighlight(String searchTerm, Element elem)/*-{
// the highlightStartTag and highlightEndTag parameters are optional
var bodyText = elem.innerHTML;
var highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
var highlightEndTag = "</font>";
// find all occurences of the search term in the given text,
// and add some "highlight" tags to them (we're not using a
// regular expression search, because we want to filter out
// matches that occur within HTML tags and script blocks, so
// we have to do a little extra validation)
var newText = "";
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcBodyText = bodyText.toLowerCase();
while (bodyText.length > 0) {
i = lcBodyText.indexOf(lcSearchTerm, i + 1);
if (i < 0) {
newText += bodyText;
bodyText = "";
} else {
// skip anything inside an HTML tag
if (bodyText.lastIndexOf(">", i) >= bodyText
.lastIndexOf("<", i)) {
// skip anything inside a <script> block
if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText
.lastIndexOf("<script", i)) {
newText += bodyText.substring(0, i) + highlightStartTag
+ bodyText.substr(i, searchTerm.length)
+ highlightEndTag;
bodyText = bodyText.substr(i + searchTerm.length);
lcBodyText = bodyText.toLowerCase();
i = -1;
}
}
}
elem.innerHTML = newText;
}
}-*/;

public static native String diffAsHtml(String text1, String text2)/*-{
if (!$wnd.diffMatchPatch) {
$wnd.diffMatchPatch = new $wnd.diff_match_patch();
$wnd.diffMatchPatch.Diff_Timeout = 0.2;
// modified diff_prettyHtml() from diff_match_patch.js
$wnd.diffMatchPatch.prototype.diff_prettyHtml = function(diffs) {
var html = [];
var pattern_amp = /&/g;
var pattern_lt = /</g;
var pattern_gt = />/g;
var pattern_para = /\n/g;
for ( var x = 0; x < diffs.length; x++) {
var op = diffs[x][0]; // Operation (insert, delete, equal)
var data = diffs[x][1]; // Text of change.
var text = data.replace(pattern_amp, '&amp;').replace(
pattern_lt, '&lt;').replace(pattern_gt, '&gt;')
.replace(pattern_para, '&para;<br>');
switch (op) {
case DIFF_INSERT:
html[x] = '<ins class="diff-insert">' + text + '</ins>';
break;
case DIFF_DELETE:
html[x] = '<del class="diff-delete">' + text + '</del>';
break;
case DIFF_EQUAL:
html[x] = '<span class="diff-equal">' + text
+ '</span>';
break;
}
}
return html.join('');
};
}
var dmp = $wnd.diffMatchPatch;
var d = dmp.diff_main(text1, text2);
dmp.diff_cleanupSemantic(d);
return dmp.diff_prettyHtml(d);
}-*/;

}
Expand Up @@ -56,13 +56,13 @@ private void highlight()
{
Element element = getElement();
String text = plainText == null ? "" : plainText.replaceAll("\n", \n");
CodeMirror.doHighlight(text, element);
Highlighting.syntaxHighlight(text, element);
}

public void highlightSearch(String search)
{
Element element = getElement();
CodeMirror.highlightSearch(search, element);
Highlighting.searchHighlight(search, element);
}

}
Expand Up @@ -440,6 +440,20 @@ tr.ApprovedStateDecoration td.TableEditorCell-Target .TableEditorContent-Edit {
font-weight: bold
}

.diff-equal {
background:yellow;
}

.diff-insert {
background:#e6ffe6;
text-decoration: none;
}

.diff-delete {
background:#ffe6e6;
text-decoration: none;
}

.DocumentListTable {
width: 100%;
font-size:16px;
Expand Down
Expand Up @@ -12,6 +12,8 @@
<script src="CodeMirror-0.67/js/tokenize.js" type="text/javascript"></script>
<script src="CodeMirror-0.67/js/parsexml.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="CodeMirror-0.67/css/xmlcolors.css"/>

<script src="diff_match_patch/javascript/diff_match_patch.js" type="text/javascript"></script>

<!-- -->
<!-- This script loads your compiled module. -->
Expand Down

0 comments on commit 53869df

Please sign in to comment.