TextNavigation textSearch = (TextNavigation) search;
int leftLength = textSelection.getText().length();
int index = textSelection.getIndex();
mIsInserted = false;
OdfElement parentElement = textSelection.getContainerElement();
OdfFileDom ownerDom = (OdfFileDom) parentElement.getOwnerDocument();
int nodeLength = TextExtractor.getText(
this.textSelection.getContainerElement()).length();
if (this.textSelection.getText().length() != nodeLength)
this.textSelection.setSelectionReplaced(true);
try {
if (imageContainer == null) {
delete(index, leftLength, parentElement);
// PrepareContainer
imageContainer = ownerDom.newOdfElement(DrawFrameElement.class);
insertOdfElement(imageContainer, index, parentElement);
} else {
NodeList nodeImages = imageContainer.getElementsByTagName("draw:image");
Node nodeImage = nodeImages.item(0);
DrawImageElement im = (DrawImageElement) nodeImage;
Image oldimage = Image.getInstanceof(im);
oldimage.remove();
// PrepareContainer
imageContainer = ownerDom.newOdfElement(DrawFrameElement.class);
insertOdfElement(imageContainer, index, parentElement);
}
// Insert Image resource to package
DrawImageElement imageElement = imageContainer.newDrawImageElement();
InputStream is = image.getImageInputStream();
String internalPath = image.getInternalPath();
String mediaType = OdfFileEntry.getMediaTypeString(internalPath);
OdfSchemaDocument mOdfSchemaDoc = (OdfSchemaDocument) ownerDom.getDocument();
String packagePath = Image.getPackagePath(mOdfSchemaDoc,internalPath);
mOdfSchemaDoc.getPackage().insert(is, packagePath, mediaType);
packagePath = packagePath.replaceFirst(ownerDom.getDocument().getDocumentPath(), "");
Image.configureInsertedImage((OdfSchemaDocument) ownerDom.getDocument(), imageElement,packagePath, false);
// get image object
mImage = Image.getInstanceof(imageElement);
mImage.getStyleHandler().setAchorType(AnchorType.AS_CHARACTER);
mImage.setName("replace" + System.currentTimeMillis());