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

Commit

Permalink
Browse files Browse the repository at this point in the history
rhbz723084 - can load from bookmark
  • Loading branch information
Patrick Huang committed Mar 14, 2013
1 parent 97b3e28 commit aba0adf
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 116 deletions.
@@ -1,17 +1,21 @@
package org.zanata.webtrans.client.events;

import org.zanata.webtrans.client.service.GetTransUnitActionContext;
import org.zanata.webtrans.client.service.NavigationService;
import org.zanata.webtrans.shared.model.TransUnitId;
import com.google.gwt.event.shared.GwtEvent;

public class BookmarkedTextFlowEvent extends GwtEvent<BookmarkedTextFlowEventHandler>
public class BookmarkedTextFlowEvent extends GwtEvent<BookmarkedTextFlowEventHandler> implements NavigationService.UpdateContextCommand
{
public static Type<BookmarkedTextFlowEventHandler> TYPE = new Type<BookmarkedTextFlowEventHandler>();

private TransUnitId textFlowId;
private int offset;
private TransUnitId targetTransUnitId;

public BookmarkedTextFlowEvent(TransUnitId textFlowId)
public BookmarkedTextFlowEvent(int offset, TransUnitId textFlowId)
{
this.textFlowId = textFlowId;
this.offset = offset;
this.targetTransUnitId = textFlowId;
}

public Type<BookmarkedTextFlowEventHandler> getAssociatedType()
Expand All @@ -24,8 +28,19 @@ protected void dispatch(BookmarkedTextFlowEventHandler handler)
handler.onBookmarkableTextFlow(this);
}

public TransUnitId getTextFlowId()
@Override
public GetTransUnitActionContext updateContext(GetTransUnitActionContext currentContext)
{
return textFlowId;
return currentContext.changeOffset(offset).changeTargetTransUnitId(targetTransUnitId);
}

public int getOffset()
{
return offset;
}

public TransUnitId getTargetTransUnitId()
{
return targetTransUnitId;
}
}
Expand Up @@ -14,7 +14,6 @@ public class DocumentSelectionEvent extends GwtEvent<DocumentSelectionHandler> i
*/
private static Type<DocumentSelectionHandler> TYPE;
private final DocumentId document;
private String findMessage;

/**
* Gets the type associated with this event.
Expand All @@ -26,27 +25,16 @@ public static Type<DocumentSelectionHandler> getType()
return TYPE != null ? TYPE : (TYPE = new Type<DocumentSelectionHandler>());
}

public DocumentSelectionEvent(DocumentId document, String findMessage)
{
this.document = document;
this.findMessage = findMessage;
}

public DocumentSelectionEvent(DocumentId documentId)
{
this(documentId, null);
this.document = documentId;
}

public DocumentId getDocumentId()
{
return document;
}

public String getFindMessage()
{
return findMessage;
}

@Override
protected void dispatch(DocumentSelectionHandler handler)
{
Expand All @@ -63,6 +51,6 @@ public GwtEvent.Type<DocumentSelectionHandler> getAssociatedType()
public GetTransUnitActionContext updateContext(GetTransUnitActionContext currentContext)
{
Preconditions.checkNotNull(currentContext, "current context can not be null");
return currentContext.changeDocument(document).changeFindMessage(findMessage);
return currentContext.changeDocument(document);
}
}
@@ -0,0 +1,18 @@
package org.zanata.webtrans.client.events;

import com.google.gwt.event.shared.GwtEvent;

public class InitEditorEvent extends GwtEvent<InitEditorEventHandler>
{
public static Type<InitEditorEventHandler> TYPE = new Type<InitEditorEventHandler>();

public Type<InitEditorEventHandler> getAssociatedType()
{
return TYPE;
}

protected void dispatch(InitEditorEventHandler handler)
{
handler.onInitEditor(this);
}
}
@@ -0,0 +1,8 @@
package org.zanata.webtrans.client.events;

import com.google.gwt.event.shared.EventHandler;

public interface InitEditorEventHandler extends EventHandler
{
void onInitEditor(InitEditorEvent event);
}
Expand Up @@ -3,6 +3,7 @@
import org.zanata.webtrans.client.presenter.MainView;

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

/**
* Encapsulates a string token of key-value pairs for GWT history operations.
Expand Down Expand Up @@ -513,16 +514,15 @@ public boolean isProjectSearchInTarget()

public void setTextFlowId(String textFlowId)
{
Long id;
try
String textFlow = Strings.nullToEmpty(textFlowId);
if (textFlow.matches("^\\d+$"))
{
id = Long.valueOf(textFlowId);
this.textFlowId = Long.valueOf(textFlow);
}
catch (Exception e)
else
{
id = null;
this.textFlowId = null;
}
this.textFlowId = id;
}

public Long getTextFlowId()
Expand Down
@@ -0,0 +1,66 @@
package org.zanata.webtrans.client.service;

import org.zanata.webtrans.client.presenter.UserConfigHolder;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.TransUnitId;
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Singleton
public class GetTransUnitActionContextHolder
{
private GetTransUnitActionContext context;
private UserConfigHolder configHolder;

@Inject
public GetTransUnitActionContextHolder(UserConfigHolder configHolder)
{
this.configHolder = configHolder;
}

protected boolean isContextInitialized()
{
return context != null;
}

protected GetTransUnitActionContext initContext(DocumentId documentId, String findMessage, TransUnitId targetTransUnitId)
{
// @formatter:off
context = new GetTransUnitActionContext(documentId)
.changeCount(configHolder.getState().getEditorPageSize())
.changeFindMessage(findMessage)
.changeFilterNeedReview(configHolder.getState().isFilterByNeedReview())
.changeFilterTranslated(configHolder.getState().isFilterByTranslated())
.changeFilterUntranslated(configHolder.getState().isFilterByUntranslated())
.changeTargetTransUnitId(targetTransUnitId);
// @formatter:on

return context;
}

public GetTransUnitActionContext getContext()
{
return context;
}

public GetTransUnitActionContext changeOffset(int targetOffset)
{
context = context.changeOffset(targetOffset);
return context;
}

public GetTransUnitActionContext changeTargetTransUnitId(TransUnitId transUnitId)
{
context.changeTargetTransUnitId(transUnitId);
return context;
}

public GetTransUnitActionContext updateContext(GetTransUnitActionContext newContext)
{
context = newContext;
return context;
}
}
@@ -1,24 +1,24 @@
package org.zanata.webtrans.client.service;

import static com.google.common.base.Objects.equal;
import net.customware.gwt.presenter.client.EventBus;

import org.zanata.webtrans.client.events.BookmarkedTextFlowEvent;
import org.zanata.webtrans.client.events.DocumentSelectionEvent;
import org.zanata.webtrans.client.events.FindMessageEvent;
import org.zanata.webtrans.client.events.InitEditorEvent;
import org.zanata.webtrans.client.history.HistoryToken;
import org.zanata.webtrans.client.presenter.AppPresenter;
import org.zanata.webtrans.client.presenter.DocumentListPresenter;
import org.zanata.webtrans.client.presenter.SearchResultsPresenter;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.TransUnitId;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import net.customware.gwt.presenter.client.EventBus;
import static com.google.common.base.Objects.*;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
Expand All @@ -30,16 +30,25 @@ public class HistoryEventHandlerService implements ValueChangeHandler<String>
private final DocumentListPresenter documentListPresenter;
private final AppPresenter appPresenter;
private final SearchResultsPresenter searchResultsPresenter;
private final GetTransUnitActionContextHolder getTransUnitActionContextHolder;
private final ModalNavigationStateHolder modalStateHolder;
// initial state
private HistoryToken currentHistoryState = new HistoryToken();

@Inject
public HistoryEventHandlerService(EventBus eventBus, DocumentListPresenter documentListPresenter, AppPresenter appPresenter, SearchResultsPresenter searchResultsPresenter)
// @formatter:off
public HistoryEventHandlerService(EventBus eventBus, DocumentListPresenter documentListPresenter,
AppPresenter appPresenter, SearchResultsPresenter searchResultsPresenter,
GetTransUnitActionContextHolder getTransUnitActionContextHolder,
ModalNavigationStateHolder modalStateHolder)
// @formatter:on
{
this.eventBus = eventBus;
this.documentListPresenter = documentListPresenter;
this.appPresenter = appPresenter;
this.searchResultsPresenter = searchResultsPresenter;
this.getTransUnitActionContextHolder = getTransUnitActionContextHolder;
this.modalStateHolder = modalStateHolder;
}

@Override
Expand All @@ -51,9 +60,17 @@ public void onValueChange(ValueChangeEvent<String> event)
processForDocumentListPresenter(newHistoryToken);
processForProjectWideSearch(newHistoryToken);

// AppPresenter process need to happen before transFilter. We want certain events happen in order:
// DocumentSelectionEvent -> FindMessageEvent -> BookmarkedTextFlowEvent
processForAppPresenter(newHistoryToken);
DocumentId documentId = documentListPresenter.getDocumentId(newHistoryToken.getDocumentPath());
if (!getTransUnitActionContextHolder.isContextInitialized() && documentId != null)
{
// if editor is not yet initialized, we want to load document with search and target trans unit all at once
Long textFlowId = newHistoryToken.getTextFlowId();
TransUnitId transUnitId = textFlowId == null ? null : new TransUnitId(textFlowId);
getTransUnitActionContextHolder.initContext(documentId, newHistoryToken.getSearchText(), transUnitId);
eventBus.fireEvent(new InitEditorEvent());
}

processForAppPresenter(documentId);
processForTransFilter(newHistoryToken);
processForBookmarkedTextFlow(newHistoryToken);

Expand All @@ -74,9 +91,8 @@ protected void processForDocumentListPresenter(HistoryToken token)
}
}

protected void processForAppPresenter(HistoryToken token)
protected void processForAppPresenter(DocumentId docId)
{
DocumentId docId = documentListPresenter.getDocumentId(token.getDocumentPath());
if (docId != null && !equal(appPresenter.getSelectedDocIdOrNull(), docId))
{
appPresenter.selectDocument(docId);
Expand All @@ -85,7 +101,7 @@ protected void processForAppPresenter(HistoryToken token)

if (docId != null)
{
eventBus.fireEvent(new DocumentSelectionEvent(docId, token.getSearchText()));
eventBus.fireEvent(new DocumentSelectionEvent(docId));
}
}

Expand Down Expand Up @@ -123,10 +139,22 @@ protected void processForProjectWideSearch(HistoryToken token)

protected void processForBookmarkedTextFlow(HistoryToken token)
{
if (!equal(token.getTextFlowId(), currentHistoryState.getTextFlowId()) && token.getTextFlowId() != null)
if (equal(token.getTextFlowId(), currentHistoryState.getTextFlowId()) || token.getTextFlowId() == null || modalStateHolder.getPageCount() == 0)
{
// target text flow id hasn't changed,
// or no text flow id in history,
// or modal navigation state holder is not yet initialized (which means InitEditorEvent will be handling this.
// See onValueChange(ValueChangeEvent<String>))
return;
}

TransUnitId transUnitId = new TransUnitId(token.getTextFlowId());
int targetPage = modalStateHolder.getTargetPage(transUnitId);
if (targetPage != NavigationService.UNDEFINED)
{
Log.info("[gwt-history] bookmarkable text flow has changed: " + token.getTextFlowId());
eventBus.fireEvent(new BookmarkedTextFlowEvent(new TransUnitId(token.getTextFlowId())));
Log.info("[gwt-history] bookmarked text flow. Target page: " + targetPage + ", target TU id: " + transUnitId);
int offset = targetPage * getTransUnitActionContextHolder.getContext().getCount();
eventBus.fireEvent(new BookmarkedTextFlowEvent(offset, transUnitId));
}
}
}

0 comments on commit aba0adf

Please sign in to comment.