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

Commit

Permalink
rhbz844820, rhbz730878 - enable highlight search term in code mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Sep 17, 2012
1 parent af53a22 commit c266eae
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 27 deletions.
Expand Up @@ -21,6 +21,7 @@
package org.zanata.webtrans.client.editor.table;

import com.allen_sauer.gwt.log.client.Log;
import com.google.common.base.Strings;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
Expand Down Expand Up @@ -228,4 +229,20 @@ private native void setCodeMirrorCursorPos(int cursorIndex) /*-{
editor.setCursor(pos);
}-*/;

public void highlight(String term)
{
if (useCodeMirrorFlag && !Strings.isNullOrEmpty(term))
{
codeMirrorHighlight(term);
}
}

private native void codeMirrorHighlight(String term) /*-{
var editor = this.@org.zanata.webtrans.client.editor.table.EditorTextArea::codeMirrorEditor;
var searchCursor = editor.getSearchCursor(term, {line: 0, ch: 0}, true);
while(searchCursor.findNext())
{
editor.markText(searchCursor.from(), searchCursor.to(), "CodeMirror-searching");
}
}-*/;
}
Expand Up @@ -49,7 +49,7 @@ public interface TargetContentsDisplay extends WidgetDisplay, IsWidget, HasTrans

void setToMode(ToggleEditor.ViewMode viewMode);

void setFindMessage(String findMessage);
void highlightSearch(String findMessage);

List<String> getCachedTargets();

Expand Down
Expand Up @@ -533,15 +533,15 @@ public void onFocus(TransUnitId id, int editorIndex)
@Override
public boolean isUsingCodeMirror()
{
return configuration.isUseCodeMirrorEditor();
return configHolder.isUseCodeMirrorEditor();
}

@Override
public void onCancel(TransUnitId transUnitId)
{
ensureRowSelection(transUnitId);
display.updateCachedAndInEditorTargets(display.getCachedTargets());
display.setFindMessage(findMessage);
display.highlightSearch(findMessage);
setFocus();
}

Expand Down Expand Up @@ -743,7 +743,7 @@ public void highlightSearch(String message)
findMessage = message;
for (TargetContentsDisplay targetContentsDisplay : displayList)
{
targetContentsDisplay.setFindMessage(message);
targetContentsDisplay.highlightSearch(message);
}
}

Expand Down Expand Up @@ -807,7 +807,7 @@ public void updateTargets(TransUnitId transUnitId, List<String> targets)
if (display != null)
{
display.updateCachedAndInEditorTargets(targets);
display.setFindMessage(findMessage);
display.highlightSearch(findMessage);
}
}
}
Expand Up @@ -83,7 +83,6 @@ interface Binder extends UiBinder<HorizontalPanel, TargetContentsView>
SimplePanel undoContainer;

private HorizontalPanel rootPanel;
private String findMessage;
private ArrayList<ToggleEditor> editors;
private Listener listener;

Expand Down Expand Up @@ -165,7 +164,7 @@ public void setValue(TransUnit transUnit)
int rowIndex = 0;
for (String target : cachedTargets)
{
Editor editor = new Editor(target, findMessage, rowIndex, listener, transUnit.getId());
Editor editor = new Editor(target, rowIndex, listener, transUnit.getId());
editorGrid.setWidget(rowIndex, 0, editor);
editors.add(editor);
rowIndex++;
Expand Down Expand Up @@ -222,9 +221,12 @@ public void onHistoryClick(ClickEvent event)
}

@Override
public void setFindMessage(String findMessage)
public void highlightSearch(String findMessage)
{
this.findMessage = findMessage;
for (ToggleEditor editor : editors)
{
editor.highlightSearch(findMessage);
}
}

@Override
Expand Down
Expand Up @@ -54,14 +54,14 @@

import com.allen_sauer.gwt.log.client.Log;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.inject.Inject;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class TransUnitEditPresenter extends WidgetPresenter<TransUnitEditDisplay> implements
TransUnitSelectionHandler,
FindMessageHandler,
FilterViewEventHandler,
FilterViewConfirmationDisplay.Listener,
NavigationController.PageDataChangeListener,
Expand Down Expand Up @@ -107,7 +107,6 @@ public TransUnitEditPresenter(TransUnitEditDisplay display, EventBus eventBus, N
@Override
protected void onBind()
{
eventBus.addHandler(FindMessageEvent.getType(), this);
eventBus.addHandler(FilterViewEvent.getType(), this);
eventBus.addHandler(TransUnitSelectionEvent.getType(), this);
eventBus.addHandler(TableRowSelectedEvent.TYPE, this);
Expand Down Expand Up @@ -141,13 +140,6 @@ public void goToPage(int pageNumber)
navigationController.gotoPage(pageNumber - 1);
}

@Override
public void onFindMessage(FindMessageEvent event)
{
sourceContentsPresenter.highlightSearch(event.getMessage());
targetContentsPresenter.highlightSearch(event.getMessage());
}

@Override
public void onFilterView(FilterViewEvent event)
{
Expand Down Expand Up @@ -242,6 +234,13 @@ else if (updateType == TransUnitUpdated.UpdateType.WebEditorSaveFuzzy)
}
}

@Override
public void highlightSearch(String findMessage)
{
sourceContentsPresenter.highlightSearch(findMessage);
targetContentsPresenter.highlightSearch(findMessage);
}

@Override
public void onRowSelected(int rowIndexOnPage)
{
Expand Down
Expand Up @@ -60,6 +60,7 @@
import com.allen_sauer.gwt.log.client.Log;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
Expand Down Expand Up @@ -170,6 +171,7 @@ public void onSuccess(GetTransUnitListResult result)
eventBus.fireEvent(new PageChangeEvent(navigationService.getCurrentPage()));
isLoadingTU = false;
finishLoading();
highlightSearch();
}
});
}
Expand All @@ -191,6 +193,14 @@ private void finishLoading()
}
}

private void highlightSearch()
{
if (!Strings.isNullOrEmpty(context.getFindMessage()))
{
pageDataChangeListener.highlightSearch(context.getFindMessage());
}
}

private void requestNavigationIndex(GetTransUnitActionContext context)
{
Log.info("requesting navigation index: " + context);
Expand Down Expand Up @@ -327,6 +337,7 @@ public void onReloadPage(ReloadPageEvent event)
pageDataChangeListener.showDataForCurrentPage(pageModel.getData());
isLoadingTU = false;
finishLoading();
highlightSearch();
}

public void execute(UpdateContextCommand command)
Expand Down Expand Up @@ -409,5 +420,7 @@ public static interface PageDataChangeListener
void showDataForCurrentPage(List<TransUnit> transUnits);

void refreshView(TransUnit updatedTransUnit, EditorClientId editorClientId, TransUnitUpdated.UpdateType updateType);

void highlightSearch(String findMessage);
}
}
@@ -1,5 +1,6 @@
package org.zanata.webtrans.client.ui;

import com.google.common.base.Strings;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
Expand Down Expand Up @@ -27,7 +28,7 @@ public CodeMirrorReadOnlyWidget()
}

// see http://codemirror.net/doc/manual.html#usage
public native JavaScriptObject initCodeMirror(Element element) /*-{
private native JavaScriptObject initCodeMirror(Element element) /*-{
var self = this;
var codeMirrorEditor = $wnd.CodeMirror.fromTextArea(element, {
lineNumbers: true,
Expand All @@ -49,15 +50,39 @@ public String getText()
public void setText(String text)
{
textArea.setValue(text);
codeMirror = initCodeMirror(textArea);
if (codeMirror == null)
{
codeMirror = initCodeMirror(textArea);
}
content = text;
}

public native void refresh() /*-{
var codeMirror = this.@org.zanata.webtrans.client.ui.CodeMirrorReadOnlyWidget::codeMirror;
codeMirror.refresh();
if (codeMirror)
{
codeMirror.refresh();
}
}-*/;

public void highlight(String term)
{
if (!Strings.isNullOrEmpty(term))
{
codeMirrorHighlight(term);
}
}

private native void codeMirrorHighlight(String term) /*-{
var codeMirror = this.@org.zanata.webtrans.client.ui.CodeMirrorReadOnlyWidget::codeMirror;
var searchCursor = codeMirror.getSearchCursor(term, {line: 0, ch: 0}, true);
while(searchCursor.findNext())
{
codeMirror.markText(searchCursor.from(), searchCursor.to(), "CodeMirror-searching");
}
}-*/;


interface CodeMirrorWidgetUiBinder extends UiBinder<Widget, CodeMirrorReadOnlyWidget>
{
}
Expand Down
Expand Up @@ -30,7 +30,6 @@

public class Editor extends Composite implements ToggleEditor
{
private String findMessage;
private TargetContentsDisplay.Listener listener;

interface EditorUiBinder extends UiBinder<Widget, Editor>
Expand All @@ -48,6 +47,8 @@ interface Styles extends CssResource
String hasValidationError();

String copyButton();

String targetWrapper();
}

private static EditorUiBinder uiBinder = GWT.create(EditorUiBinder.class);
Expand Down Expand Up @@ -112,9 +113,8 @@ public void run()
}
};

public Editor(String displayString, String findMessage, int index, final TargetContentsDisplay.Listener listener, TransUnitId id)
public Editor(String displayString, int index, final TargetContentsDisplay.Listener listener, TransUnitId id)
{
this.findMessage = findMessage;
this.listener = listener;
this.index = index;
this.id = id;
Expand Down Expand Up @@ -305,6 +305,12 @@ public void clearTranslatorList()
translatorList.clear();
}

@Override
public void highlightSearch(String findMessage)
{
textArea.highlight(findMessage);
}

@Override
public void removeTranslator(String name, String color)
{
Expand Down
Expand Up @@ -117,8 +117,8 @@ public void refresh()
sourceContent.refresh();
}

public void highlightSearch(String search)
public void highlightSearch(String searchTerm)
{
// TODO need to highlight search term
sourceContent.highlight(searchTerm);
}
}
Expand Up @@ -22,6 +22,8 @@ public interface ToggleEditor extends IsWidget, HasText, HasUpdateValidationWarn

void clearTranslatorList();

void highlightSearch(String findMessage);

static enum ViewMode
{
VIEW, EDIT
Expand Down
Expand Up @@ -273,7 +273,7 @@ public void onCancelCanResetTextBack()
verify(display, atLeastOnce()).getId();
verify(display).getCachedTargets();
verify(display).updateCachedAndInEditorTargets(targets);
verify(display).setFindMessage(anyString());
verify(display).highlightSearch(anyString());
}

@Test
Expand Down

0 comments on commit c266eae

Please sign in to comment.