From 6bfca28c3d39db6ae3b83eb18e4df28558ade5cc Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 8 Mar 2014 19:52:09 +0000 Subject: [PATCH] When adding a new XPWFRun to a paragraph, update both the Runs and IRuns lists with it git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1575597 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/XWPFParagraph.java | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 526eea7fbe..98b9dccc46 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -337,17 +337,6 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents { return footnoteText.toString(); } - /** - * Appends a new run to this paragraph - * - * @return a new text run - */ - public XWPFRun createRun() { - XWPFRun xwpfRun = new XWPFRun(paragraph.addNewR(), this); - runs.add(xwpfRun); - return xwpfRun; - } - /** * Returns the paragraph alignment which shall be applied to text in this * paragraph. @@ -1186,6 +1175,48 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents { paragraph.setRArray(pos, run); } + /** + * Appends a new run to this paragraph + * + * @return a new text run + */ + public XWPFRun createRun() { + XWPFRun xwpfRun = new XWPFRun(paragraph.addNewR(), this); + runs.add(xwpfRun); + iruns.add(xwpfRun); + return xwpfRun; + } + + /** + * insert a new Run in RunArray + * @param pos + * @return the inserted run + */ + public XWPFRun insertNewRun(int pos){ + if (pos >= 0 && pos <= paragraph.sizeOfRArray()) { + CTR ctRun = paragraph.insertNewR(pos); + XWPFRun newRun = new XWPFRun(ctRun, this); + + // To update the iruns, find where we're going + // in the normal runs, and go in there + int iPos = iruns.size(); + if (pos < runs.size()) { + XWPFRun oldAtPos = runs.get(pos); + int oldAt = iruns.indexOf(oldAtPos); + if (oldAt != -1) { + iPos = oldAt; + } + } + iruns.add(iPos, newRun); + + // Runs itself is easy to update + runs.add(pos, newRun); + + return newRun; + } + return null; + } + /** * this methods parse the paragraph and search for the string searched. * If it finds the string, it will return true and the position of the String @@ -1256,21 +1287,6 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents { return null; } - /** - * insert a new Run in RunArray - * @param pos - * @return the inserted run - */ - public XWPFRun insertNewRun(int pos){ - if (pos >= 0 && pos <= paragraph.sizeOfRArray()) { - CTR ctRun = paragraph.insertNewR(pos); - XWPFRun newRun = new XWPFRun(ctRun, this); - runs.add(pos, newRun); - return newRun; - } - return null; - } - /** * get a Text * @param segment -- 2.39.5