FocusedRange focusedRange = editor.getSelectionHelper().getSelectionRange();
if (focusedRange != null) {
tmpCursor = focusedRange.getFocus();
}
final int cursorLoc = tmpCursor;
AttachmentPopupView attachmentView = new AttachmentPopupWidget();
attachmentView.init(new Listener() {
@Override
public void onShow() {
}
@Override
public void onHide() {
}
@Override
public void onDone(String encodedWaveRef, String attachmentId, String fullFileName) {
// Insert a file name linking to the attachment URL.
int lastSlashPos = fullFileName.lastIndexOf("/");
int lastBackSlashPos = fullFileName.lastIndexOf("\\");
String fileName = fullFileName;
if (lastSlashPos != -1) {
fileName = fullFileName.substring(lastSlashPos + 1, fullFileName.length());
} else if (lastBackSlashPos != -1) {
fileName = fullFileName.substring(lastBackSlashPos + 1, fullFileName.length());
}
XmlStringBuilder xml = XmlStringBuilder.createFromXmlString(fileName);
int to = -1;
int docSize = editor.getDocument().size();
if (cursorLoc != -1) {
// Insert the attachment at the cursor location.
CMutableDocument doc = editor.getDocument();
Point<ContentNode> point = doc.locate(cursorLoc);
doc.insertXml(point, xml);
} else {
LineContainers.appendLine(editor.getDocument(), xml);
}
// Calculate the link length for the attachment.
to = cursorLoc + editor.getDocument().size() - docSize;
String linkValue =
GWT.getHostPageBaseURL() + "attachment/" + attachmentId + "?fileName="
+ fileName + "&waveRef=" + encodedWaveRef;
EditorAnnotationUtil.setAnnotationOverRange(editor.getDocument(),
editor.getCaretAnnotations(), Link.KEY, linkValue, cursorLoc, to);
// Store the attachment information as annotations to allow
// robots detect and process them.
EditorAnnotationUtil.setAnnotationOverRange(editor.getDocument(),
editor.getCaretAnnotations(), "attachment/id", attachmentId, cursorLoc, to);
EditorAnnotationUtil.setAnnotationOverRange(editor.getDocument(),
editor.getCaretAnnotations(), "attachment/fileName", fileName, cursorLoc, to);
}
});
attachmentView.setAttachmentId(attachmentIdGenerator.newAttachmentId());
attachmentView.setWaveRef(waveRefToken);
attachmentView.show();
}
});
}