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());
}
/*
* From UploadToolbarAction in Walkaround
* @author hearnden@google.com (David Hearnden)
*/
CMutableDocument doc = editor.getDocument();
FocusedContentRange selection = editor.getSelectionHelper().getSelectionPoints();
Point<ContentNode> point;
if (selection != null) {
point = selection.getFocus();
} else {
// Focus was probably lost. Bring it back.
editor.focus(false);
selection = editor.getSelectionHelper().getSelectionPoints();
if (selection != null) {
point = selection.getFocus();
} else {
// Still no selection. Oh well, put it at the end.
point = doc.locate(doc.size() - 1);
}
}
XmlStringBuilder content = ImageThumbnail.constructXml(attachmentId, fileName);
ImageThumbnailWrapper thumbnail = ImageThumbnailWrapper.of(doc.insertXml(point, content));
thumbnail.setAttachmentId(attachmentId);
}
});
attachmentView.setAttachmentId(attachmentIdGenerator.newAttachmentId());
attachmentView.setWaveRef(waveRefToken);
attachmentView.show();
}
});
}