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

Commit

Permalink
rhbz785034 Ignore TM results which arrive after selected row changes
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Feb 14, 2012
1 parent 934be3a commit ea7c85e
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 22 deletions.
Expand Up @@ -34,13 +34,14 @@ public class TransMemoryPresenter extends WidgetPresenter<TransMemoryPresenter.D
{
private final WorkspaceContext workspaceContext;
private final CachingDispatchAsync dispatcher;
private GetTranslationMemory currentRequest;

public interface Display extends WidgetDisplay
{
HasValue<Boolean> getExactButton();

HasClickHandlers getSearchButton();

HasValue<SearchType> getSearchType();

HasText getTmTextBox();

void createTable(String query, ArrayList<TranslationMemoryGlossaryItem> memories);
Expand Down Expand Up @@ -69,14 +70,14 @@ public TransMemoryPresenter(Display display, EventBus eventBus, CachingDispatchA
@Override
protected void onBind()
{
display.getSearchType().setValue(SearchType.FUZZY);
display.getSearchButton().addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
String query = display.getTmTextBox().getText();
GetTranslationMemory.SearchType searchType = display.getExactButton().getValue() ? SearchType.EXACT : SearchType.RAW;
showResults(query, searchType);
showResults(query, display.getSearchType().getValue());
}
});

Expand Down Expand Up @@ -131,6 +132,7 @@ private void showResults(final String query, GetTranslationMemory.SearchType sea
{
display.startProcessing();
final GetTranslationMemory action = new GetTranslationMemory(query, workspaceContext.getWorkspaceId().getLocaleId(), searchType);
currentRequest = action;
dispatcher.execute(action, new AsyncCallback<GetTranslationMemoryResult>()
{
@Override
Expand All @@ -142,8 +144,17 @@ public void onFailure(Throwable caught)
@Override
public void onSuccess(GetTranslationMemoryResult result)
{
if (!result.getRequest().equals(currentRequest))
{
Log.info("ignoring old TM result for query: " + result.getRequest().getQuery());
return;
}
Log.info("received TM result for query: " + currentRequest.getQuery());
display.getTmTextBox().setText(currentRequest.getQuery());
display.getSearchType().setValue(currentRequest.getSearchType());
ArrayList<TranslationMemoryGlossaryItem> memories = result.getMemories();
display.createTable(query, memories);
currentRequest = null;
}
});
}
Expand Down
@@ -0,0 +1,38 @@
/*
* 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.resources;

import com.google.gwt.i18n.client.LocalizableResource.DefaultLocale;
import com.google.gwt.i18n.client.LocalizableResource.Generate;

@DefaultLocale
@Generate(format = "com.google.gwt.i18n.rebind.format.PropertiesFormat")
public interface EnumMessages extends com.google.gwt.i18n.client.Messages
{
@DefaultMessage("Phrase")
String searchTypeExact();

@DefaultMessage("Fuzzy")
String searchTypeFuzzy();

@DefaultMessage("Lucene")
String searchTypeRaw();
}
@@ -0,0 +1,61 @@
/*
* Copyright 2011, 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 java.util.Arrays;

import com.google.gwt.text.shared.Renderer;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SimpleKeyProvider;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public class EnumListBox<E extends Enum<?>> extends ValueListBox<E>
{

public EnumListBox(Class<E> clazz, Renderer<E> renderer)
{
this(clazz.getEnumConstants(), renderer);
}

public EnumListBox(E[] values, Renderer<E> renderer)
{
this(values, renderer, new SimpleKeyProvider<E>());
}

public EnumListBox(E[] values, Renderer<E> renderer, ProvidesKey<E> keyProvider)
{
super(renderer, keyProvider);
init(values);
}

private void init(E[] values)
{
// this avoids the automatic null entry in the acceptableValues array
setValue(values[0]);
setAcceptableValues(Arrays.asList(values));
}

}
@@ -0,0 +1,36 @@
package org.zanata.webtrans.client.ui;

import com.google.gwt.text.shared.AbstractRenderer;

/**
* Translates enum entries. Use setEmptyValue() if you want to have a custom empty value. Default empty value is "".
*
* @param <T>
* an enumeration entry which is to be registered in {@link Translations}
* @author Els Dessin http://stackoverflow.com/a/4931399/14379
*/
public class EnumRenderer<T extends Enum<?>> extends AbstractRenderer<T>
{
private String emptyValue = "";

/**
* Subclasses can override to return localised strings
*/
@Override
public String render(T object)
{
if (object == null)
return emptyValue;
return object.toString();
}

public String getEmptyValue()
{
return emptyValue;
}

public void setEmptyValue(String emptyValue)
{
this.emptyValue = emptyValue;
}
}
@@ -0,0 +1,36 @@
package org.zanata.webtrans.client.ui;

import org.zanata.webtrans.client.resources.EnumMessages;
import org.zanata.webtrans.shared.rpc.GetTranslationMemory.SearchType;

import com.google.inject.Inject;

/**
* Translates SearchType entries.
*/
public class SearchTypeRenderer extends EnumRenderer<SearchType>
{
private final EnumMessages messages;

@Inject
public SearchTypeRenderer(EnumMessages messages)
{
this.messages = messages;
}

@Override
public String render(SearchType st)
{
switch (st)
{
case EXACT:
return messages.searchTypeExact();
case FUZZY:
return messages.searchTypeFuzzy();
case RAW:
return messages.searchTypeRaw();
default:
return getEmptyValue();
}
}
}
Expand Up @@ -11,8 +11,11 @@
import org.zanata.webtrans.client.resources.Resources;
import org.zanata.webtrans.client.resources.UiMessages;
import org.zanata.webtrans.client.ui.DiffMatchPatchLabel;
import org.zanata.webtrans.client.ui.EnumListBox;
import org.zanata.webtrans.client.ui.HighlightingLabel;
import org.zanata.webtrans.client.ui.SearchTypeRenderer;
import org.zanata.webtrans.shared.model.TranslationMemoryGlossaryItem;
import org.zanata.webtrans.shared.rpc.GetTranslationMemory.SearchType;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
Expand All @@ -27,13 +30,13 @@
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

Expand All @@ -57,12 +60,12 @@ interface TransMemoryViewUiBinder extends UiBinder<Widget, TransMemoryView>
@UiField
TextBox tmTextBox;

@UiField
CheckBox phraseButton;

@UiField
Button searchButton;

@UiField(provided = true)
ValueListBox<SearchType> searchType;

@UiField
Button clearButton;

Expand All @@ -84,11 +87,11 @@ interface TransMemoryViewUiBinder extends UiBinder<Widget, TransMemoryView>
private UiMessages messages;

@Inject
public TransMemoryView(final UiMessages messages, Resources resources)
public TransMemoryView(final UiMessages messages, SearchTypeRenderer searchTypeRenderer, Resources resources)
{
this.resources = resources;
searchType = new EnumListBox<SearchType>(SearchType.class, searchTypeRenderer);
initWidget(uiBinder.createAndBindUi(this));
phraseButton.setText(messages.phraseButtonLabel());
clearButton.setText(messages.clearButtonLabel());
searchButton.setText(messages.searchButtonLabel());
this.messages = messages;
Expand Down Expand Up @@ -124,15 +127,15 @@ void onClearButtonClicked(ClickEvent event)
}

@Override
public HasValue<Boolean> getExactButton()
public Button getSearchButton()
{
return phraseButton;
return searchButton;
}

@Override
public Button getSearchButton()
public HasValue<SearchType> getSearchType()
{
return searchButton;
return searchType;
}

public TextBox getTmTextBox()
Expand Down
Expand Up @@ -14,7 +14,7 @@
<g:layer top="0px" height="30px">
<g:HorizontalPanel>
<g:TextBox ui:field="tmTextBox" />
<g:CheckBox ui:field="phraseButton"/>
<g:ValueListBox ui:field="searchType"/>
<g:Button ui:field="searchButton"/>
<g:Button ui:field="clearButton"/>
</g:HorizontalPanel>
Expand Down
Expand Up @@ -185,7 +185,7 @@ private int compare(float a, float b)
Collections.sort(results, comp);

log.info("Returning {0} TM matches for \"{1}\"", results.size(), abbrev);
return new GetTranslationMemoryResult(results);
return new GetTranslationMemoryResult(action, results);
}

@Override
Expand Down
Expand Up @@ -55,4 +55,60 @@ public String getQuery()
return query;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((localeId == null) ? 0 : localeId.hashCode());
result = prime * result + ((query == null) ? 0 : query.hashCode());
result = prime * result + ((searchType == null) ? 0 : searchType.hashCode());
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (!(obj instanceof GetTranslationMemory))
{
return false;
}
GetTranslationMemory other = (GetTranslationMemory) obj;
if (localeId == null)
{
if (other.localeId != null)
{
return false;
}
}
else if (!localeId.equals(other.localeId))
{
return false;
}
if (query == null)
{
if (other.query != null)
{
return false;
}
}
else if (!query.equals(other.query))
{
return false;
}
if (searchType != other.searchType)
{
return false;
}
return true;
}

}

0 comments on commit ea7c85e

Please sign in to comment.