<commit_before>package org.fedorahosted.flies.webtrans.client;

import java.util.ArrayList;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import org.fedorahosted.flies.gwt.model.TransMemory;
import org.fedorahosted.flies.gwt.model.TransUnit;
import org.fedorahosted.flies.gwt.rpc.GetTranslationMemory;
import org.fedorahosted.flies.gwt.rpc.GetTranslationMemoryResult;
import org.fedorahosted.flies.webtrans.client.rpc.CachingDispatchAsync;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;

public class TransMemoryPresenter extends WidgetPresenter<TransMemoryPresenter.Display> {
	private final WorkspaceContext workspaceContext;
	private final CachingDispatchAsync dispatcher;
	private boolean transMemoryVisible = false;
	
	public interface Display extends WidgetDisplay {
		Button getSearchButton();
		TextBox getTmTextBox();
		void createTable(ArrayList<TransMemory> memories);
		void clearResults();
	}

	@Inject
	public TransMemoryPresenter(Display display, EventBus eventBus, CachingDispatchAsync dispatcher, WorkspaceContext workspaceContext) {
		super(display, eventBus);
		this.dispatcher = dispatcher;
		this.workspaceContext = workspaceContext;
	}

	@Override
	public Place getPlace() {
		return null;
	}

	@Override
	protected void onBind() {
		
		display.getSearchButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				display.clearResults();
				final String query = display.getTmTextBox().getText();
				GetTranslationMemory action = new GetTranslationMemory(
						query, 
						workspaceContext.getLocaleId(), false);
				dispatcher.execute(action, new AsyncCallback<GetTranslationMemoryResult>() {
					@Override
					public void onFailure(Throwable caught) {
					}
					@Override
					public void onSuccess(GetTranslationMemoryResult result) {
						ArrayList<TransMemory> memories = result.getMemories();
						display.createTable(memories);
					}
				});
			}
		});
		
		registerHandler(eventBus.addHandler(SelectionEvent.getType(), new SelectionHandler<TransUnit>() {
			@Override
			public void onSelection(SelectionEvent<TransUnit> event) {
				if(transMemoryVisible) {
					//Start automatically fuzzy search
					//FixME: retrieve the query from Source
					final String query = display.getTmTextBox().getText();
					final GetTranslationMemory action = new GetTranslationMemory(
							query, 
							workspaceContext.getLocaleId(), 
							true);
					dispatcher.execute(action, new AsyncCallback<GetTranslationMemoryResult>() {
						@Override
						public void onFailure(Throwable caught) {
							Log.error(caught.getMessage(), caught);
						}
						@Override
						public void onSuccess(GetTranslationMemoryResult result) {
							ArrayList<TransMemory> memories = result.getMemories();
							display.createTable(memories);
						}
					});
				}
			}
		})); 
		
		registerHandler(eventBus.addHandler(VisibilityEvent.getType(), new VisibilityHandler(){
			@Override
			public void onVisibilityChange(VisibilityEvent tabSelectionEvent) {
				transMemoryVisible = tabSelectionEvent.isVisible();
				Log.debug("received visibility=="+transMemoryVisible);
			}
		}));<commit_msg>Always clears old TM results when moving to a different TU, even if TM not visible<commit_after>package org.fedorahosted.flies.webtrans.client;

import java.util.ArrayList;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import org.fedorahosted.flies.gwt.model.TransMemory;
import org.fedorahosted.flies.gwt.model.TransUnit;
import org.fedorahosted.flies.gwt.rpc.GetTranslationMemory;
import org.fedorahosted.flies.gwt.rpc.GetTranslationMemoryResult;
import org.fedorahosted.flies.webtrans.client.rpc.CachingDispatchAsync;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;

public class TransMemoryPresenter extends WidgetPresenter<TransMemoryPresenter.Display> {
	private final WorkspaceContext workspaceContext;
	private final CachingDispatchAsync dispatcher;
	private boolean transMemoryVisible = false;
	
	public interface Display extends WidgetDisplay {
		Button getSearchButton();
		TextBox getTmTextBox();
		void createTable(ArrayList<TransMemory> memories);
		void clearResults();
	}

	@Inject
	public TransMemoryPresenter(Display display, EventBus eventBus, CachingDispatchAsync dispatcher, WorkspaceContext workspaceContext) {
		super(display, eventBus);
		this.dispatcher = dispatcher;
		this.workspaceContext = workspaceContext;
	}

	@Override
	public Place getPlace() {
		return null;
	}

	@Override
	protected void onBind() {
		
		display.getSearchButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				display.clearResults();
				final String query = display.getTmTextBox().getText();
				GetTranslationMemory action = new GetTranslationMemory(
						query, 
						workspaceContext.getLocaleId(), false);
				dispatcher.execute(action, new AsyncCallback<GetTranslationMemoryResult>() {
					@Override
					public void onFailure(Throwable caught) {
					}
					@Override
					public void onSuccess(GetTranslationMemoryResult result) {
						ArrayList<TransMemory> memories = result.getMemories();
						display.createTable(memories);
					}
				});
			}
		});
		
		registerHandler(eventBus.addHandler(SelectionEvent.getType(), new SelectionHandler<TransUnit>() {
			@Override
			public void onSelection(SelectionEvent<TransUnit> event) {
				display.clearResults();
				if(transMemoryVisible) {
					//Start automatically fuzzy search
					//FixME: retrieve the query from Source
					final String query = display.getTmTextBox().getText();
					final GetTranslationMemory action = new GetTranslationMemory(
							query, 
							workspaceContext.getLocaleId(), 
							true);
					dispatcher.execute(action, new AsyncCallback<GetTranslationMemoryResult>() {
						@Override
						public void onFailure(Throwable caught) {
							Log.error(caught.getMessage(), caught);
						}
						@Override
						public void onSuccess(GetTranslationMemoryResult result) {
							ArrayList<TransMemory> memories = result.getMemories();
							display.createTable(memories);
						}
					});
				}
			}
		})); 
		
		registerHandler(eventBus.addHandler(VisibilityEvent.getType(), new VisibilityHandler(){
			@Override
			public void onVisibilityChange(VisibilityEvent tabSelectionEvent) {
				transMemoryVisible = tabSelectionEvent.isVisible();
				Log.debug("received visibility=="+transMemoryVisible);
			}
		}));
{'fraction_non_alphanumeric': 0.09838396897220426, 'fraction_numerical': 0.0, 'mean_word_length': 4.688235294117647, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1093789', 'n_tokens_mistral': 2608, 'n_tokens_neox': 2185, 'n_words': 410}