ImageSelection extends Selection { public Image replaceWithImage(Image image) { Image mImage =null; if(search instanceof TextNavigation){ 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); 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(); imageContainer = ownerDom.newOdfElement(DrawFrameElement.class); insertOdfElement(imageContainer, index, parentElement); } 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); mImage = Image.getInstanceof(imageElement); mImage.getStyleHandler().setAchorType(AnchorType.AS_CHARACTER); mImage.setName("replace" + System.currentTimeMillis()); } catch (Exception e) { Logger.getLogger(ImageSelection.class.getName()).log(Level.SEVERE, e.getMessage(), e); } } return mImage; } ImageSelection(TextSelection selection); Image replaceWithImage(Image image); Image replaceWithImage(URI imageUri); @Override void cut(); @Override void pasteAtEndOf(Selection positionItem); @Override void pasteAtFrontOf(Selection positionItem);  }
@Test public void testReplaceWithImage() { search = null; search = new TextNavigation("SIMPLE", doc); int i=0; try { ImageSelection nextImageSelection=null; while (search.hasNext()) { TextSelection item= (TextSelection) search.nextSelection(); nextImageSelection=new ImageSelection(item); Paragraph paragraph=sourcedoc.getParagraphByIndex(0, true); TextParagraphElementBase textParaEleBase = paragraph.getOdfElement(); NodeList nodeImages = textParaEleBase.getElementsByTagName("draw:image"); Node nodeImage = nodeImages.item(0); DrawImageElement im = (DrawImageElement)nodeImage; Image ima = Image.getInstanceof(im); Image image = nextImageSelection.replaceWithImage(ima); Assert.assertNotNull(image); if(image.getName().startsWith("replace")){ Assert.assertTrue(true); }else{ Assert.fail(); } String name="simple"+(i++); image.setName(name); Assert.assertEquals(name,image.getName()); } Image image =nextImageSelection.replaceWithImage(image2); image.setName("simpletwice"); Assert.assertEquals("simpletwice",image.getName()); doc.save(ResourceUtilities.newTestOutputFile("TestImageSelectionImageResult.odt")); } catch (Exception e) { Logger.getLogger(ImageSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }
@Test public void testReplaceWithImageURI() { search = null; search = new TextNavigation("SIMPLE", doc); int i=0; try { ImageSelection nextImageSelection=null; while (search.hasNext()) { TextSelection item= (TextSelection) search.nextSelection(); nextImageSelection=new ImageSelection(item); URI imageuri = ResourceUtilities.getURI("image_list_item.png"); Image image = nextImageSelection.replaceWithImage(imageuri); Assert.assertNotNull(image); if(image.getName().startsWith("replace")){ Assert.assertTrue(true); }else{ Assert.fail(); } String name="simple"+(i++); image.setName(name); Assert.assertEquals(name,image.getName()); } URI imageuri = ResourceUtilities.getURI("testA.jpg"); Image image =nextImageSelection.replaceWithImage(imageuri); image.setName("simpletwice"); Assert.assertEquals("simpletwice",image.getName()); doc.save(ResourceUtilities.newTestOutputFile("TestImageSelectionURIResult.odt")); } catch (Exception e) { Logger.getLogger(ImageSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }