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

Commit

Permalink
add default document path
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Dec 9, 2011
1 parent 79474bd commit 3dc5049
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 128 deletions.
Expand Up @@ -27,11 +27,24 @@ public class HistoryToken
public static final String VALUE_DOC_FILTER_EXACT = "exact";
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,84 +55,78 @@ public static HistoryToken fromTokenString(String token)
{
HistoryToken historyToken = new HistoryToken();

String[] pair;

try
if (token == null || token.length() == 0)
{
return historyToken;
}

String[] pair;
for (String pairString : token.split(PAIR_SEPARATOR))
{
for (String pairString : token.split(PAIR_SEPARATOR))
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 (key == HistoryToken.KEY_DOCUMENT)
{
historyToken.setDocumentPath(value);
}
else if (key == HistoryToken.KEY_VIEW)
{
if (value.equals(VALUE_EDITOR_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);
}
historyToken.setView(AppPresenter.Display.MainView.Editor);
}
else if (key == HistoryToken.KEY_DOC_FILTER_OPTION)
else if (value.equals(VALUE_DOCLIST_VIEW))
{
if (value == VALUE_DOC_FILTER_EXACT)
historyToken.setDocFilterExact(true);
else if (value == VALUE_DOC_FILTER_INEXACT)
historyToken.setDocFilterExact(false);
historyToken.setView(AppPresenter.Display.MainView.Documents);
}
else if (key == HistoryToken.KEY_DOC_FILTER_TEXT)
{
historyToken.setDocFilterText(value);
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);
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;
if (fullDocPath == null)
this.fullDocPath = DEFAULT_DOCUMENT_PATH;
else
this.fullDocPath = fullDocPath;
}

public boolean hasView()
Expand Down Expand Up @@ -194,13 +201,13 @@ else if (view == AppPresenter.Display.MainView.Documents)
}
}

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())
Expand Down
Expand Up @@ -319,6 +319,8 @@ private void adjustStats(TranslationStats statsObject, TransUnitUpdatedEvent Upd

private void processHistoryEvent(ValueChangeEvent<String> event)
{
try
{

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

Expand All @@ -328,7 +330,7 @@ private void processHistoryEvent(ValueChangeEvent<String> event)

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 Down Expand Up @@ -378,6 +380,12 @@ else if (!token.hasView())
}
((Anchor) display.getDocumentsLink()).setHref("#" + token.toTokenString());

}
catch (Throwable t)
{
Log.error("exception while responding to history token", t);
}

}

}
Expand Up @@ -135,21 +135,14 @@ public void onSelection(SelectionEvent<DocumentInfo> event)

// prevent feedback loops between history and selection
boolean isNewSelection;
if (token.hasDocumentPath())
DocumentId docId = getDocumentId(token.getDocumentPath());
if (docId == null)
{
try
{
isNewSelection = event.getSelectedItem().getId().getId() != getDocumentId(token.getDocumentPath()).getId();
}
catch (Throwable t)
{
Log.info("got exception determining whether selection is new", t);
isNewSelection = false;
}
isNewSelection = true;
}
else
{
isNewSelection = true;
isNewSelection = docId.equals(event.getSelectedItem().getId());
}

if (isNewSelection)
Expand Down Expand Up @@ -211,73 +204,80 @@ public void onValueChange(ValueChangeEvent<Boolean> event)
@Override
public void onValueChange(ValueChangeEvent<String> event)
{
boolean filterChanged = false;
HistoryToken token = HistoryToken.fromTokenString(event.getValue());
if (token.hasDocFilterText())
try
{
// update textbox to match new history state
if (!token.getDocFilterText().equals(display.getFilterTextBox().getValue()))
boolean filterChanged = false;
HistoryToken token = HistoryToken.fromTokenString(event.getValue());
if (token.hasDocFilterText())
{
display.getFilterTextBox().setValue(token.getDocFilterText(), true);
// update textbox to match new history state
if (!token.getDocFilterText().equals(display.getFilterTextBox().getValue()))
{
display.getFilterTextBox().setValue(token.getDocFilterText(), true);
}

boolean patternChanged;
if (currentHistoryState == null)
patternChanged = true;
else
patternChanged = !token.getDocFilterText().equals(currentHistoryState.getDocFilterText());
if (patternChanged)
{
filter.setPattern(token.getDocFilterText());
filterChanged = true;
}
}

boolean patternChanged;
if (currentHistoryState == null)
patternChanged = true;
else
patternChanged = !token.getDocFilterText().equals(currentHistoryState.getDocFilterText());
if (patternChanged)
{
filter.setPattern(token.getDocFilterText());
filterChanged = true;
if (currentHistoryState != null && currentHistoryState.hasDocFilterText())
{
// not using default
filter.setPattern("");
filterChanged = true;
}
// else was already using blank filter
}
}
else
{
if (currentHistoryState != null && currentHistoryState.hasDocFilterText())
{
// not using default
filter.setPattern("");
filterChanged = true;
}
// else was already using blank filter
}

if (token.hasDocFilterExact())
{
// update checkbox to match new history state
if (token.getDocFilterExact() != display.getExactSearchCheckbox().getValue())
if (token.hasDocFilterExact())
{
display.getExactSearchCheckbox().setValue(token.getDocFilterExact());
// update checkbox to match new history state
if (token.getDocFilterExact() != display.getExactSearchCheckbox().getValue())
{
display.getExactSearchCheckbox().setValue(token.getDocFilterExact());
}

boolean flagChanged;
if (currentHistoryState == null)
flagChanged = true;
else
flagChanged = !token.getDocFilterExact().equals(currentHistoryState.getDocFilterExact());

if (flagChanged)
{
filter.setFullText(token.getDocFilterExact());
filterChanged = true;
}
}

boolean flagChanged;
if (currentHistoryState == null)
flagChanged = true;
else
flagChanged = !token.getDocFilterExact().equals(currentHistoryState.getDocFilterExact());

if (flagChanged)
{
filter.setFullText(token.getDocFilterExact());
filterChanged = true;
if (currentHistoryState != null && currentHistoryState.hasDocFilterExact() && currentHistoryState.getDocFilterExact() == true)
{
// not using default
filter.setFullText(false);
filterChanged = true;
}
// else was already using substring match
}

currentHistoryState = token;

if (filterChanged)
runFilter();
}
else
catch (Throwable t)
{
if (currentHistoryState != null && currentHistoryState.hasDocFilterExact() && currentHistoryState.getDocFilterExact() == true)
{
// not using default
filter.setFullText(false);
filterChanged = true;
}
// else was already using substring match
Log.error("exception with doclistpresenter responding to histroy token", t);
}

currentHistoryState = token;

if (filterChanged)
runFilter();
}
});

Expand Down Expand Up @@ -483,11 +483,15 @@ public DocumentInfo getDocumentInfo(DocumentId docId)
*
* @param fullPathAndName document path + document name
* @return the id for the document, or null if the document is not in the
* document list
* document list or there is no document list
*/
public DocumentId getDocumentId(String fullPathAndName)
{
return idsByPath.get(fullPathAndName);
if (idsByPath != null)
{
return idsByPath.get(fullPathAndName);
}
return null;
}

private void clearSelection()
Expand Down

0 comments on commit 3dc5049

Please sign in to comment.