* @param toReplace
* list of TransUnits to replace
*/
private void fireReplaceTextEvent(List<TransUnitReplaceInfo> toReplace) {
if (!userWorkspaceContext.hasEditTranslationAccess()) {
eventBus.fireEvent(new NotificationEvent(Severity.Warning, messages
.youAreNotAllowedToModifyTranslations()));
return;
}
if (toReplace.isEmpty()) {
eventBus.fireEvent(new NotificationEvent(Severity.Warning, messages
.noTextFlowsSelected()));
return;
}
List<TransUnit> transUnits = new ArrayList<TransUnit>();
for (TransUnitReplaceInfo info : toReplace) {
switch (info.getReplaceState()) {
case NotReplaced:
transUnits.add(info.getTransUnit());
setReplaceState(info, ReplacementState.Replacing);
info.setPreviewState(PreviewState.Hide);
refreshInfoDisplay(info);
break;
}
}
if (transUnits.isEmpty()) {
eventBus.fireEvent(new NotificationEvent(Severity.Warning, messages
.noReplacementsToMake()));
return;
}
final String searchText = display.getFilterTextBox().getValue();
final String replacement = display.getReplacementTextBox().getValue();
boolean caseSensitive = display.getCaseSensitiveChk().getValue();
ReplaceText action =
new ReplaceText(transUnits, searchText, replacement,
caseSensitive);
dispatcher.execute(action, new AsyncCallback<UpdateTransUnitResult>() {
@Override
public void onFailure(Throwable e) {
Log.error("[SearchResultsPresenter] Replace text failure " + e,
e);
eventBus.fireEvent(new NotificationEvent(Severity.Error,
messages.replaceTextFailure()));
// may want to change state from 'replacing' (possibly add error
// state)
}
@Override
public void onSuccess(final UpdateTransUnitResult result) {
final List<TransUnitUpdateInfo> updateInfoList =
processSuccessfulReplacements(result
.getUpdateInfoList());
if (updateInfoList.isEmpty()) {
eventBus.fireEvent(new NotificationEvent(Info, messages
.noReplacementsToMake()));
return;
}
String message;
if (updateInfoList.size() == 1) {
TransUnitUpdateInfo info = updateInfoList.get(0);
String text = info.getTransUnit().getTargets().get(0);
String truncatedText =
text.substring(
0,
(text.length() <= TRUNCATED_TARGET_LENGTH ? text
.length() : TRUNCATED_TARGET_LENGTH));
int oneBasedRowIndex =
info.getTransUnit().getRowIndex() + 1;
String docName = docPaths.get(info.getDocumentId().getId());
message =
messages.replacedTextInOneTextFlow(searchText,
replacement, docName, oneBasedRowIndex,
truncatedText);
} else {
message =
messages.replacedTextInMultipleTextFlows(
searchText, replacement,
updateInfoList.size());
}
final UndoLink undoLink = undoLinkProvider.get();
undoLink.prepareUndoFor(result);
undoLink.setUndoCallback(new UndoLink.UndoCallback() {
@Override
public void preUndo() {
executePreUndo(updateInfoList);
}
@Override
public void postUndoSuccess() {
executePostSucess(result);
}
});
NotificationEvent event =
new NotificationEvent(Info, message, undoLink);
eventBus.fireEvent(event);
}
});
}