Bug 569658 - [Model2Doc][Docx] The docx generator should be able to generate bullet list * create a CustomXWPFParagraph to be able to set the numIlvl value * add numbering in the template Signed-off-by: Pauline DEVILLE <pauline.deville@cea.fr> Change-Id: Ifecb36deb164832a994854b49f3c24a0d1f6a4e0
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/poi/CustomXWPFDocument.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/poi/CustomXWPFDocument.java index 2e0a15c..a5a48b9 100755 --- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/poi/CustomXWPFDocument.java +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/poi/CustomXWPFDocument.java
@@ -109,4 +109,17 @@ return table; } + /** + * Appends a new {@link CustomXWPFParagraph} to this document + * + * @return the new customXWPFParagraph + */ + @Override + public XWPFParagraph createParagraph() { + XWPFParagraph p = new CustomXWPFParagraph(getDocument().getBody().addNewP(), this); + bodyElements.add(p); + paragraphs.add(p); + return p; + } + }
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/poi/CustomXWPFParagraph.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/poi/CustomXWPFParagraph.java new file mode 100755 index 0000000..21ad468 --- /dev/null +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/poi/CustomXWPFParagraph.java
@@ -0,0 +1,58 @@ +/***************************************************************************** + * Copyright (c) 2020 CEA LIST and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.model2doc.docx.internal.poi; + +import java.math.BigInteger; + +import org.apache.poi.xwpf.usermodel.IBody; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; + +/** + * This custom implementation allow to add some missing methods in the apache api + */ +public class CustomXWPFParagraph extends XWPFParagraph { + + /** + * Constructor. + * + * @param prgrph + * @param part + */ + public CustomXWPFParagraph(CTP prgrph, IBody part) { + super(prgrph, part); + } + + /** + * set numIlvl of Paragraph + * + * @param itemLevel + * the new value of the numIlvl + */ + public void setItemLevel(int itemLevel) { + if (getCTP().getPPr() == null) { + getCTP().addNewPPr(); + } + if (getCTP().getPPr().getNumPr() == null) { + getCTP().getPPr().addNewNumPr(); + } + if (getCTP().getPPr().getNumPr().getIlvl() == null) { + getCTP().getPPr().getNumPr().addNewIlvl(); + } + getCTP().getPPr().getNumPr().getIlvl().setVal(BigInteger.valueOf(itemLevel)); + } + +}
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java index 92b6024..986fd47 100755 --- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java
@@ -115,6 +115,18 @@ } /** + * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#applyListStyle(org.apache.poi.xwpf.usermodel.XWPFParagraph) + * + * @param paragraph + * @return + */ + @Override + public boolean applyListStyle(XWPFParagraph paragraph) { + String style = getListStyleValue(); + return applyStyle(paragraph, style); + } + + /** * * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#applyStyle(org.apache.poi.xwpf.usermodel.XWPFParagraph, java.lang.String) * @@ -177,4 +189,14 @@ public String getCaptionStyleValue() { return StyleConstants.CAPTION_STYLE_VALUE; } + + /** + * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#getListStyleValue() + * + * @return + */ + @Override + public String getListStyleValue() { + return StyleConstants.LIST_STYLE_VALUE; + } }
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java index cd2d1a2..d2a5f9a 100755 --- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java
@@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -36,11 +37,15 @@ import org.eclipse.core.runtime.Assert; import org.eclipse.osgi.util.NLS; import org.eclipse.papyrus.model2doc.core.author.IAuthor; +import org.eclipse.papyrus.model2doc.core.builtintypes.AbstractList; import org.eclipse.papyrus.model2doc.core.builtintypes.AbstractTable; +import org.eclipse.papyrus.model2doc.core.builtintypes.BasicList; import org.eclipse.papyrus.model2doc.core.builtintypes.Cell; import org.eclipse.papyrus.model2doc.core.builtintypes.IFileReference; +import org.eclipse.papyrus.model2doc.core.builtintypes.ListItem; import org.eclipse.papyrus.model2doc.core.builtintypes.Row; import org.eclipse.papyrus.model2doc.core.builtintypes.TextCell; +import org.eclipse.papyrus.model2doc.core.builtintypes.TextListItem; import org.eclipse.papyrus.model2doc.core.generatorconfiguration.IDocumentGeneratorConfiguration; import org.eclipse.papyrus.model2doc.core.generatorconfiguration.operations.GeneratorConfigurationOperations; import org.eclipse.papyrus.model2doc.core.styles.BooleanNamedStyle; @@ -51,6 +56,7 @@ import org.eclipse.papyrus.model2doc.docx.Activator; import org.eclipse.papyrus.model2doc.docx.Messages; import org.eclipse.papyrus.model2doc.docx.internal.poi.CustomXWPFDocument; +import org.eclipse.papyrus.model2doc.docx.internal.poi.CustomXWPFParagraph; import org.eclipse.papyrus.model2doc.docx.internal.poi.CustomXWPFTable; import org.eclipse.papyrus.model2doc.docx.internal.services.StyleServiceImpl; import org.eclipse.papyrus.model2doc.docx.internal.util.ImageUtils; @@ -200,9 +206,44 @@ } @Override - public void writeList(org.eclipse.papyrus.model2doc.core.builtintypes.AbstractList list, boolean processRichText) { - // TODO Auto-generated method stub + public void writeList(AbstractList abstractList, boolean processRichText) { + if (false == abstractList instanceof BasicList) { + Activator.log.warn(NLS.bind("The list of type {0} is not supported by {1}.", abstractList.eClass().getName(), getClass().getName())); //$NON-NLS-1$ + return; + } + final BasicList basicList = (BasicList) abstractList; + final Iterator<ListItem> iter = basicList.getItems().iterator(); + while (iter.hasNext()) { + writeListItem(iter.next()); + } + } + /** + * Write the list for the given item + * + * @param listItem + * the item to write + */ + private void writeListItem(ListItem listItem) { + final int itemLevel = listItem.getLevel(); + if (false == listItem instanceof TextListItem) { + Activator.log.warn(NLS.bind("The list item of type {0} is not supported by {1}.", listItem.eClass().getName(), getClass().getName())); //$NON-NLS-1$ + return; + } + XWPFParagraph paragraph = document.createParagraph(); + paragraph.setNumID(BigInteger.ONE); // this correspond to the id of the abstractNum which is in the numbering file it start at 1 + if (paragraph instanceof CustomXWPFParagraph) { + ((CustomXWPFParagraph) paragraph).setItemLevel(itemLevel - 1); + } + XWPFRun run = paragraph.createRun(); + run.setText(((TextListItem) listItem).getText()); + + styleService.applyListStyle(paragraph); + + final Iterator<ListItem> subItemIterator = listItem.getSubItems().iterator(); + while (subItemIterator.hasNext()) { + writeListItem(subItemIterator.next()); + } } @Override
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/util/StyleConstants.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/util/StyleConstants.java index 395ccb6..814f676 100755 --- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/util/StyleConstants.java +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/util/StyleConstants.java
@@ -38,6 +38,8 @@ public static final String NO_HEADER_STYLE_VALUE = "TableWithoutHeader"; //$NON-NLS-1$ + public static final String LIST_STYLE_VALUE = "Paragraphedeliste"; //$NON-NLS-1$ + /** * Constructor. *
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleService.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleService.java index b1d4a49..e31d6bf 100755 --- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleService.java +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleService.java
@@ -54,6 +54,15 @@ boolean applyTableStyle(XWPFTable xwpfTable, XWPFDocument document, AbstractTable tableDescription); /** + * Apply the list style + * + * @param paragraph + * the paragraph on which the list style will be applied + * @return true if the style has been find, false otherwise + */ + boolean applyListStyle(XWPFParagraph paragraph); + + /** * Apply the caption style to the paragraph * * @param paragraph @@ -105,4 +114,11 @@ */ String getCaptionStyleValue(); + /** + * Get the list style name + * + * @return the list style name + */ + String getListStyleValue(); + } \ No newline at end of file
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/templates/PapyrusTemplate.dotx b/plugins/docx/org.eclipse.papyrus.model2doc.docx/templates/PapyrusTemplate.dotx index 898eb40..ef14463 100755 --- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/templates/PapyrusTemplate.dotx +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/templates/PapyrusTemplate.dotx Binary files differ