Bug 568341 - [Model2Doc][Docx] Model2Doc must provide the framework to
generate a Microsoft Word docx file from a Document Structure model

* generate table
WARNING: missing caption

Signed-off-by: Pauline DEVILLE <pauline.deville@cea.fr>
Change-Id: I72120eb301ee4315fb5a2ef12cb72b7d6e13445e
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/resources/Template.dotx b/plugins/docx/org.eclipse.papyrus.model2doc.docx/resources/Template.dotx
index 63c2a22..51db51c 100755
--- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/resources/Template.dotx
+++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/resources/Template.dotx
Binary files differ
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 b756721..3a4db79 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
@@ -19,13 +19,20 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
 
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
 import org.apache.poi.xwpf.usermodel.XWPFRun;
+import org.apache.poi.xwpf.usermodel.XWPFTable;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.papyrus.model2doc.core.author.IAuthor;
 import org.eclipse.papyrus.model2doc.core.builtintypes.AbstractTable;
+import org.eclipse.papyrus.model2doc.core.builtintypes.Cell;
 import org.eclipse.papyrus.model2doc.core.builtintypes.IFileReference;
+import org.eclipse.papyrus.model2doc.core.builtintypes.Row;
+import org.eclipse.papyrus.model2doc.core.builtintypes.TextCell;
 import org.eclipse.papyrus.model2doc.core.generatorconfiguration.IDocumentGeneratorConfiguration;
 import org.eclipse.papyrus.model2doc.core.generatorconfiguration.operations.GeneratorConfigurationOperations;
 import org.eclipse.papyrus.model2doc.core.transcription.CoverPage;
@@ -142,8 +149,44 @@
 
 	@Override
 	public void writeTable(AbstractTable table) {
-		// TODO Auto-generated method stub
+		// do some check
+		int rowsNumber = table.getRowsNumber();
+		int colNumbers = table.getColumnsNumber();
 
+		if (rowsNumber <= 0 || colNumbers <= 0) {
+			return;
+		}
+
+		final List<Cell> allCells = table.getAllCells();
+
+		final int totalCells = allCells.size();
+		if (rowsNumber * colNumbers != totalCells) {
+			Activator.log.warn(NLS.bind("The number of cells in the table is not as excepted. We won't manage the table {0}.", table.getCaption())); //$NON-NLS-1$
+			return;
+		}
+
+		Activator.log.info(NLS.bind("Start the creation of the table {0}", table.getCaption())); //$NON-NLS-1$
+		Activator.log.info(NLS.bind("--This table have {0} columns and {1} rows", colNumbers, rowsNumber)); //$NON-NLS-1$
+
+		// create and fill the table
+		XWPFTable xwpfTable = document.createTable(rowsNumber, colNumbers);
+		styleService.applyTableStyle(xwpfTable, document);
+		Iterator<Row> rowIter = table.getRows().iterator();
+		int rowNumber = 0;
+		while (rowIter.hasNext()) {
+			Row row = rowIter.next();
+			Iterator<Cell> cellIter = row.getCells().iterator();
+			int cellNumber = 0;
+			while (cellIter.hasNext()) {
+				Cell cell = cellIter.next();
+				if (cell instanceof TextCell) {
+					TextCell textCell = (TextCell) cell;
+					xwpfTable.getRow(rowNumber).getCell(cellNumber).setText(textCell.getText());
+				}
+				cellNumber++;
+			}
+			rowNumber++;
+		}
 	}
 
 	@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 6d8003e..540ae0d 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
@@ -24,21 +24,6 @@
 	 */
 	// public static final String PAGESTYLES_FAMILY = "PageStyles"; //$NON-NLS-1$
 	//
-	// /**
-	// * Style property sets.
-	// */
-	// public static final String STANDARD_PROPERTYSET = "Standard"; //$NON-NLS-1$
-	//
-	// /**
-	// * Style properties.
-	// */
-	// public static final String PARASTYLENAME_PROPERTY = "ParaStyleName"; //$NON-NLS-1$
-	//
-	// public static final String NUMBERINGSTYLENAME_PROPERTY = "NumberingStyleName"; //$NON-NLS-1$
-	//
-	// public static final String PARAADJUST_PROPERTY = "ParaAdjust"; //$NON-NLS-1$
-	//
-	// public static final String CHARPOSTURE_PROPERTY = "CharPosture"; //$NON-NLS-1$
 
 	/**
 	 * Style values.
@@ -51,6 +36,8 @@
 
 	public static final String HEADING_STYLE_VALUE = "Heading"; //$NON-NLS-1$
 
+	public static final String TABLE_STYLE_VALUE = "Grilledutableau"; //$NON-NLS-1$
+
 	// public static final String LIST_1_STYLE_VALUE = "List 1"; //$NON-NLS-1$
 
 	/**
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 2b87880..efdfcf0 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
@@ -13,7 +13,9 @@
  *****************************************************************************/
 package org.eclipse.papyrus.model2doc.docx.services;
 
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+import org.apache.poi.xwpf.usermodel.XWPFTable;
 
 public interface StyleService {
 
@@ -39,6 +41,16 @@
 	boolean applySectionTitleStyle(XWPFParagraph paragraph, int sectionLevel);
 
 	/**
+	 * Apply the style to the table
+	 *
+	 * @param table
+	 *            the table on which the style will be applied
+	 * @param document
+	 *            the document owning the table
+	 */
+	boolean applyTableStyle(XWPFTable table, XWPFDocument document);
+
+	/**
 	 * Apply the style given as parameter styleName
 	 *
 	 * @param paragraph
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleServiceImpl.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleServiceImpl.java
index dfbd5fe..3b6d437 100755
--- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleServiceImpl.java
+++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/services/StyleServiceImpl.java
@@ -15,22 +15,65 @@
 
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+import org.apache.poi.xwpf.usermodel.XWPFTable;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.papyrus.model2doc.docx.Activator;
 import org.eclipse.papyrus.model2doc.docx.internal.util.StyleConstants;
 
 public class StyleServiceImpl implements StyleService {
 
+	/**
+	 *
+	 * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#applyDocumentMainTitleStyle(org.apache.poi.xwpf.usermodel.XWPFParagraph)
+	 *
+	 * @param paragraph
+	 * @return
+	 */
 	@Override
 	public boolean applyDocumentMainTitleStyle(XWPFParagraph paragraph) {
 		String style = getDocumentMainTitleStyle();
 		return applyStyle(paragraph, style);
 	}
 
+	/**
+	 *
+	 * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#applySectionTitleStyle(org.apache.poi.xwpf.usermodel.XWPFParagraph, int)
+	 *
+	 * @param paragraph
+	 * @param sectionLevel
+	 * @return
+	 */
 	@Override
 	public boolean applySectionTitleStyle(XWPFParagraph paragraph, int sectionLevel) {
 		String style = getSectionTitleStyle(sectionLevel);
 		return applyStyle(paragraph, style);
 	}
 
+	/**
+	 * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#applyTableStyle(org.apache.poi.xwpf.usermodel.XWPFTable, XWPFDocument)
+	 *
+	 * @param table
+	 * @return
+	 */
+	@Override
+	public boolean applyTableStyle(XWPFTable table, XWPFDocument document) {
+		String styleName = StyleConstants.TABLE_STYLE_VALUE;
+		if (document.getStyles().styleExist(styleName)) {
+			table.setStyleID(styleName);
+			return true;
+		}
+		Activator.log.warn(NLS.bind("the style {0} does not exist in the template file", styleName)); //$NON-NLS-1$
+		return false;
+	}
+
+	/**
+	 *
+	 * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#applyStyle(org.apache.poi.xwpf.usermodel.XWPFParagraph, java.lang.String)
+	 *
+	 * @param paragraph
+	 * @param styleName
+	 * @return
+	 */
 	@Override
 	public boolean applyStyle(XWPFParagraph paragraph, String styleName) {
 		XWPFDocument document = paragraph.getDocument();
@@ -38,19 +81,39 @@
 			paragraph.setStyle(styleName);
 			return true;
 		}
+		Activator.log.warn(NLS.bind("the style {0} does not exist in the template file", styleName)); //$NON-NLS-1$
 		return false;
 	}
 
+	/**
+	 *
+	 * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#getDocumentMainTitleStyle()
+	 *
+	 * @return
+	 */
 	@Override
 	public String getDocumentMainTitleStyle() {
 		return StyleConstants.TITLE_STYLE_VALUE;
 	}
 
+	/**
+	 *
+	 * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#getSectionTitleStyle(int)
+	 *
+	 * @param sectionLevel
+	 * @return
+	 */
 	@Override
 	public String getSectionTitleStyle(int sectionLevel) {
 		return getHeadingStyleValue() + sectionLevel;
 	}
 
+	/**
+	 *
+	 * @see org.eclipse.papyrus.model2doc.docx.services.StyleService#getHeadingStyleValue()
+	 *
+	 * @return
+	 */
 	@Override
 	public String getHeadingStyleValue() {
 		return StyleConstants.HEADING_STYLE_VALUE;