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

Commit

Permalink
Revert "add defaults to history token, needs debugging"
Browse files Browse the repository at this point in the history
it was too buggy, did it more carefully in another branch that will be merged after this revert.

This reverts commit 1fe8483.
  • Loading branch information
davidmason committed Dec 9, 2011
1 parent 1fe8483 commit 5c9fdfe
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 113 deletions.
Expand Up @@ -12,14 +12,13 @@
*/
public class HistoryToken
{

private static final String DELIMITER_K_V = ":";
private static final String PAIR_SEPARATOR = ";";

public static final String KEY_DOCUMENT = "doc";

public static final String KEY_VIEW = "view";
// public static final String VALUE_DOCLIST_VIEW = "list";
public static final String VALUE_DOCLIST_VIEW = "list";
public static final String VALUE_EDITOR_VIEW = "doc";

public static final String KEY_DOC_FILTER_TEXT = "filter";
Expand All @@ -28,29 +27,14 @@ public class HistoryToken
public static final String VALUE_DOC_FILTER_EXACT = "exact";
public static final String VALUE_DOC_FILTER_INEXACT = "substr";

// defaults
private static final String DEFAULT_DOCUMENT_PATH = "";
private static final String DEFAULT_DOC_FILTER_TEXT = "";
private static final boolean DEFAULT_DOC_FILTER_EXACT = false;
private static final AppPresenter.Display.MainView DEFAULT_VIEW = AppPresenter.Display.MainView.Documents;

private AppPresenter.Display.MainView view;
private String fullDocPath;
private boolean docFilterExact;
private String docFilterText;
private AppPresenter.Display.MainView view = null;
private String fullDocPath = null;
private Boolean docFilterExact = null;
private String docFilterText = null;

public HistoryToken()
{
view = DEFAULT_VIEW;
fullDocPath = DEFAULT_DOCUMENT_PATH;
docFilterText = DEFAULT_DOC_FILTER_TEXT;
docFilterExact = DEFAULT_DOC_FILTER_EXACT;
}

/**
* Generates a history token from the given token string. Default values will
* be used for any keys that are not present or do not have a valid value
* associated with them.
* Generate a history token from the given token string
*
* @param token A GWT history token in the form key1:value1,key2:value2,...
*/
Expand All @@ -70,25 +54,46 @@ public static HistoryToken fromTokenString(String token)

if (key == HistoryToken.KEY_DOCUMENT)
{
if (value != null && value.length() > 0)
try
{
historyToken.setDocumentPath((value));
}
catch (NullPointerException e)
{
historyToken.setDocumentPath(null);
}
catch (NumberFormatException e)
{
historyToken.setDocumentPath(null);
}
}
else if (key == HistoryToken.KEY_VIEW)
{
if (value.equals(VALUE_EDITOR_VIEW))
{
historyToken.setView(AppPresenter.Display.MainView.Editor);
// else assume document list
}
else if (key == HistoryToken.KEY_DOC_FILTER_TEXT)
{
if (value != null && value.length() > 0)
historyToken.setDocFilterText(value);
}
else if (value.equals(VALUE_DOCLIST_VIEW))
{
historyToken.setView(AppPresenter.Display.MainView.Documents);
}
else
{ // invalid view
historyToken.setView(null);
}
}
else if (key == HistoryToken.KEY_DOC_FILTER_OPTION)
{
if (value == VALUE_DOC_FILTER_EXACT)
historyToken.setDocFilterExact(true);
else if (value == VALUE_DOC_FILTER_INEXACT)
historyToken.setDocFilterExact(false);
}
else if (key == HistoryToken.KEY_DOC_FILTER_TEXT)
{
historyToken.setDocFilterText(value);
}

else
Log.info("unrecognised history key: " + key);

Expand All @@ -102,14 +107,13 @@ else if (key == HistoryToken.KEY_DOC_FILTER_OPTION)
return historyToken;
}

/**
*
* @return the document path, may be an empty string, will not be null
*/
public boolean hasDocumentPath()
{
return fullDocPath != null && fullDocPath.length() > 0;
}

public String getDocumentPath()
{
if (fullDocPath == null)
return DEFAULT_DOCUMENT_PATH;
return fullDocPath;
}

Expand All @@ -118,14 +122,13 @@ public void setDocumentPath(String fullDocPath)
this.fullDocPath = fullDocPath;
}

/**
*
* @return the current view, will never return null
*/
public boolean hasView()
{
return view != null;
}

public AppPresenter.Display.MainView getView()
{
if (view == null)
return DEFAULT_VIEW;
return view;
}

Expand All @@ -134,29 +137,28 @@ public void setView(AppPresenter.Display.MainView view)
this.view = view;
}

/**
*
* @return true if document filter should accept only an exact match
*/
public boolean getDocFilterExact()
public boolean hasDocFilterExact()
{
return docFilterExact != null;
}

public Boolean getDocFilterExact()
{
return docFilterExact;
}

public void setDocFilterExact(boolean exactMatch)
public void setDocFilterExact(Boolean exactMatch)
{
docFilterExact = exactMatch;
}

/**
*
* @return the string against which to filter the document list. May be an
* empty string, will never be null.
*/
public boolean hasDocFilterText()
{
return docFilterText != null;
}

public String getDocFilterText()
{
if (docFilterText == null)
return DEFAULT_DOC_FILTER_TEXT;
return docFilterText;
}

Expand All @@ -167,9 +169,6 @@ public void setDocFilterText(String value)


/**
* Generates a token string to represent this {@link HistoryToken}. Fields
* that have their default value are not included in the string.
*
* @return a token string for use with
* {@link com.google.gwt.user.client.History}
*/
Expand All @@ -178,41 +177,43 @@ public String toTokenString()
String token = "";
boolean first = true;

if (getView() != DEFAULT_VIEW)
if (hasView())
{
if (first)
first = false;
else
token += PAIR_SEPARATOR;
token += KEY_VIEW + DELIMITER_K_V;
// this conditional is unnecessary
if (view == AppPresenter.Display.MainView.Editor)
{
token += VALUE_EDITOR_VIEW;
}
else if (view == AppPresenter.Display.MainView.Documents)
{
token += VALUE_DOCLIST_VIEW;
}
}

if (!getDocumentPath().equals(DEFAULT_DOCUMENT_PATH))
if (hasDocumentPath())
{
if (first)
first = false;
else
token += PAIR_SEPARATOR;
token += KEY_DOCUMENT + DELIMITER_K_V + fullDocPath;
token += KEY_DOCUMENT + DELIMITER_K_V + fullDocPath.toString();
}

if (getDocFilterExact() != DEFAULT_DOC_FILTER_EXACT)
if (hasDocFilterExact())
{
if (first)
first = false;
else
token += PAIR_SEPARATOR;
token += KEY_DOC_FILTER_OPTION + DELIMITER_K_V;
// this is redundant if defaults don't change
token += docFilterExact ? VALUE_DOC_FILTER_EXACT : VALUE_DOC_FILTER_INEXACT;
}

if (!getDocFilterText().equals(DEFAULT_DOC_FILTER_TEXT))
if (hasDocFilterText())
{
if (first)
first = false;
Expand All @@ -223,15 +224,4 @@ public String toTokenString()

return token;
}

public Object clone()
{
HistoryToken newToken = new HistoryToken();
newToken.view = this.view;
newToken.fullDocPath = this.fullDocPath;
newToken.docFilterText = this.docFilterText;
newToken.docFilterExact = this.docFilterExact;

return newToken;
}
}
Expand Up @@ -143,9 +143,6 @@ enum StatsType
private final TranslationStats selectedDocumentStats = new TranslationStats();
private final TranslationStats projectStats = new TranslationStats();

// used to determine whether history state has changed
private HistoryToken currentHistoryState = null;

private static final String WORKSPACE_TITLE_QUERY_PARAMETER_KEY = "title";

@Inject
Expand Down Expand Up @@ -322,20 +319,30 @@ private void adjustStats(TranslationStats statsObject, TransUnitUpdatedEvent Upd

private void processHistoryEvent(ValueChangeEvent<String> event)
{

// TODO keep track of previous history token like in DocumentListPresenter

Log.info("Responding to history token: " + event.getValue());

HistoryToken token = HistoryToken.fromTokenString(event.getValue());

DocumentId docId = documentListPresenter.getDocumentId(token.getDocumentPath());

if (docId != null && (selectedDocument == null || !selectedDocument.getId().equals(docId)))
if (token.hasDocumentPath() && (selectedDocument == null || !selectedDocument.getId().equals(docId)))
{
Log.info("Firing document selection event for document " + docId.getId());
eventBus.fireEvent(new DocumentSelectionEvent(docId));
Log.info("Firing document selection event");
try
{
eventBus.fireEvent(new DocumentSelectionEvent(docId));
}
catch (Throwable t)
{
Log.info("got exception from document selection event", t);
}
Log.info("Fired document selection event for " + docId.getId());
}


if (token.getView() != currentHistoryState.getView())
if (token.hasView() && token.getView() != display.getCurrentView())
{
if (display.getCurrentView().equals(MainView.Editor))
{
Expand All @@ -350,20 +357,27 @@ private void processHistoryEvent(ValueChangeEvent<String> event)
}
display.showInMainView(token.getView());
}
// TODO set defaults in history rather than having this block.
else if (!token.hasView())
{
// default view.
display.showInMainView(MainView.Documents);
}

currentHistoryState = token;
// TODO use a cloned token below when the current token is stored. Ok to
// modify current token for now. (add clone method when doing this)

// update toggle link with alternate view latest history state
HistoryToken toggleToken = (HistoryToken) token.clone();
if (toggleToken.getView().equals(MainView.Editor))
if (token.hasView() && token.getView().equals(MainView.Editor))
{
toggleToken.setView(MainView.Documents);
token.setView(MainView.Documents);
}
else
{
toggleToken.setView(MainView.Editor);
{ // doclist is default
token.setView(MainView.Editor);
}
((Anchor) display.getDocumentsLink()).setHref("#" + toggleToken.toTokenString());
((Anchor) display.getDocumentsLink()).setHref("#" + token.toTokenString());

}

}

0 comments on commit 5c9fdfe

Please sign in to comment.