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

Commit

Permalink
Merge branch 'refactor' into rhbz757621
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Dec 9, 2011
2 parents 5c9fdfe + adea24c commit 24434e2
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 185 deletions.
Expand Up @@ -18,20 +18,33 @@ public class HistoryToken
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";

public static final String KEY_DOC_FILTER_OPTION = "filtertype";
public static final String VALUE_DOC_FILTER_EXACT = "exact";
public static final String VALUE_DOC_FILTER_INEXACT = "substr";
// public static final String VALUE_DOC_FILTER_INEXACT = "substr";

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

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

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

/**
* Generate a history token from the given token string
Expand All @@ -42,89 +55,68 @@ public static HistoryToken fromTokenString(String token)
{
HistoryToken historyToken = new HistoryToken();

String[] pair;
if (token == null || token.length() == 0)
{
return historyToken;
}

try
for (String pairString : token.split(PAIR_SEPARATOR))
{
for (String pairString : token.split(PAIR_SEPARATOR))
String[] pair = pairString.split(DELIMITER_K_V);
String key;
String value;
try
{
pair = pairString.split(DELIMITER_K_V);
String key = pair[0];
String value = pair[1];
key = pair[0];
value = pair[1];
}
catch (ArrayIndexOutOfBoundsException e)
{
continue;
}

if (key == HistoryToken.KEY_DOCUMENT)
{
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 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)
if (key == HistoryToken.KEY_DOCUMENT)
{
historyToken.setDocumentPath(value);
}
else if (key == HistoryToken.KEY_VIEW)
{
if (value.equals(VALUE_EDITOR_VIEW))
{
historyToken.setDocFilterText(value);
historyToken.setView(AppPresenter.Display.MainView.Editor);
}
// else default will be used
}
else if (key == HistoryToken.KEY_DOC_FILTER_OPTION)
{
if (value == VALUE_DOC_FILTER_EXACT)
historyToken.setDocFilterExact(true);
// else default used
}
else if (key == HistoryToken.KEY_DOC_FILTER_TEXT)
{
historyToken.setDocFilterText(value);
}

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

}
}
catch (IllegalArgumentException e)
{
throw new IllegalArgumentException("token must be a list of key-value pairs in the form key1:value1,key2:value2,...", e);
}

return historyToken;
}

public boolean hasDocumentPath()
{
return fullDocPath != null && fullDocPath.length() > 0;
}

public String getDocumentPath()
{
return fullDocPath;
}

public void setDocumentPath(String fullDocPath)
{
this.fullDocPath = fullDocPath;
}

public boolean hasView()
{
return view != null;
if (fullDocPath == null)
this.fullDocPath = DEFAULT_DOCUMENT_PATH;
else
this.fullDocPath = fullDocPath;
}

public AppPresenter.Display.MainView getView()
Expand All @@ -134,40 +126,35 @@ public AppPresenter.Display.MainView getView()

public void setView(AppPresenter.Display.MainView view)
{
this.view = view;
}

public boolean hasDocFilterExact()
{
return docFilterExact != null;
if (view == null)
this.view = DEFAULT_VIEW;
else
this.view = view;
}

public Boolean getDocFilterExact()
{
return docFilterExact;
}

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

public boolean hasDocFilterText()
{
return docFilterText != null;
}

public String getDocFilterText()
{
return docFilterText;
}

public void setDocFilterText(String value)
{
this.docFilterText = value;
if (value == null || value.length() == 0)
this.docFilterText = DEFAULT_DOC_FILTER_TEXT;
else
this.docFilterText = value;
}


/**
* @return a token string for use with
* {@link com.google.gwt.user.client.History}
Expand All @@ -177,43 +164,36 @@ public String toTokenString()
String token = "";
boolean first = true;

if (hasView())
if (view != DEFAULT_VIEW)
{
if (first)
first = false;
else
token += PAIR_SEPARATOR;
token += KEY_VIEW + DELIMITER_K_V;
if (view == AppPresenter.Display.MainView.Editor)
{
token += VALUE_EDITOR_VIEW;
}
else if (view == AppPresenter.Display.MainView.Documents)
{
token += VALUE_DOCLIST_VIEW;
}
// editor is the only non-default view
token += KEY_VIEW + DELIMITER_K_V + VALUE_EDITOR_VIEW;
}

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

if (hasDocFilterExact())
if (docFilterExact != DEFAULT_DOC_FILTER_EXACT)
{
if (first)
first = false;
else
token += PAIR_SEPARATOR;
token += KEY_DOC_FILTER_OPTION + DELIMITER_K_V;
token += docFilterExact ? VALUE_DOC_FILTER_EXACT : VALUE_DOC_FILTER_INEXACT;
// exact is the only non-default filter value
token += KEY_DOC_FILTER_OPTION + DELIMITER_K_V + VALUE_DOC_FILTER_EXACT;
}

if (hasDocFilterText())
if (!docFilterText.equals(DEFAULT_DOC_FILTER_TEXT))
{
if (first)
first = false;
Expand Down
Expand Up @@ -319,16 +319,13 @@ 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 (token.hasDocumentPath() && (selectedDocument == null || !selectedDocument.getId().equals(docId)))
if (docId != null && (selectedDocument == null || !selectedDocument.getId().equals(docId)))
{
Log.info("Firing document selection event");
try
Expand All @@ -342,7 +339,13 @@ private void processHistoryEvent(ValueChangeEvent<String> event)
Log.info("Fired document selection event for " + docId.getId());
}

if (token.hasView() && token.getView() != display.getCurrentView())
// if there is no valid document, don't show the editor
if (docId == null)
{
token.setView(MainView.Documents);
}

if (token.getView() != display.getCurrentView())
{
if (display.getCurrentView().equals(MainView.Editor))
{
Expand All @@ -357,27 +360,13 @@ 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);
}

// 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
if (token.hasView() && token.getView().equals(MainView.Editor))
{
// update toggle link with alternate view, or doc list if no doc is
// loaded
if (docId == null || token.getView().equals(MainView.Editor))
token.setView(MainView.Documents);
}
else
{ // doclist is default
token.setView(MainView.Editor);
}
((Anchor) display.getDocumentsLink()).setHref("#" + token.toTokenString());

}

}

0 comments on commit 24434e2

Please sign in to comment.