#ODFTOOLKIT-410# implemented by Raimund Hölle - Set table:error-message for content-validation to activate actual validation

git-svn-id: https://svn.apache.org/repos/asf/incubator/odf/trunk@1714431 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/simple/src/main/java/org/odftoolkit/simple/table/Cell.java b/simple/src/main/java/org/odftoolkit/simple/table/Cell.java
index 5ab46a4..79f390e 100644
--- a/simple/src/main/java/org/odftoolkit/simple/table/Cell.java
+++ b/simple/src/main/java/org/odftoolkit/simple/table/Cell.java
@@ -39,6 +39,7 @@
 import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;

 import org.odftoolkit.odfdom.dom.attribute.fo.FoTextAlignAttribute;

 import org.odftoolkit.odfdom.dom.attribute.office.OfficeValueTypeAttribute;

+import org.odftoolkit.odfdom.dom.attribute.table.TableMessageTypeAttribute;

 import org.odftoolkit.odfdom.dom.element.OdfStylableElement;

 import org.odftoolkit.odfdom.dom.element.OdfStyleBase;

 import org.odftoolkit.odfdom.dom.element.dc.DcCreatorElement;

@@ -53,6 +54,7 @@
 import org.odftoolkit.odfdom.dom.element.table.TableContentValidationElement;

 import org.odftoolkit.odfdom.dom.element.table.TableContentValidationsElement;

 import org.odftoolkit.odfdom.dom.element.table.TableCoveredTableCellElement;

+import org.odftoolkit.odfdom.dom.element.table.TableErrorMessageElement;

 import org.odftoolkit.odfdom.dom.element.table.TableHelpMessageElement;

 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement;

 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElementBase;

@@ -2383,6 +2385,10 @@
 	/**

 	 * Specifies the allowed values of this cell in a list. Any value out of

 	 * this list is invalid.

+   * <p>

+   * NOTE: The validity list is per default only used for a drop down box

+   * to select possible values. Validation error messages can be enabled

+   * by {@code setInputErrorMessage()}.

 	 * <p>

 	 * NOTE: Now, the validity rule does not take effect when a cell value

 	 * is updated by Simple ODF API yet.

@@ -2390,6 +2396,8 @@
 	 * @param values

 	 *            the list of allowed values.

 	 * @since 0.6

+   *

+   * @see #setInputErrorMessage(String, String, String)

 	 */

 	public void setValidityList(List<String> values) {

 		try {

@@ -2440,6 +2448,53 @@
 		}

 	}

 

+  /**

+   * Sets the title and text of the message box which will

+   * be displayed if if invalid input is entered into the cell.

+   * <p>

+   * This message has only effect if a content validation is

+   * active for the cell, e. g. by setting a validation list.

+   *

+   * @param title Title of the message box. May be {@code null} for using

+   *              default title.

+   * @param text Text of the message box. May be {@code null} for using

+   *             default message.

+   * @param messageType The severity of the message. If {@code null}, the

+   *                    default is used ("stop"). Allowed values are enumerated

+   *                    by {@code TableMessageTypeAttribute.Value}: {@code "stop"}

+   *                    (Default), {@code "warning"}, {@code "information"}.

+   * @since 0.8.2

+   *

+   * @see #setValidityList(List)

+   * @see TableMessageTypeAttribute.Value

+   */

+  public void setInputErrorMessage(String title, String text, String messageType) {

+    try {

+      TableContentValidationElement validationElement = getContentValidationEle();

+      TableErrorMessageElement errorMessageElement = OdfElement.findFirstChildNode(TableErrorMessageElement.class,

+          validationElement);

+      if (errorMessageElement != null) {

+        validationElement.removeChild(errorMessageElement);

+      }

+      errorMessageElement = validationElement.newTableErrorMessageElement();

+      errorMessageElement.setTableDisplayAttribute(true);

+      if (title != null) {

+        errorMessageElement.setTableTitleAttribute(title);

+      }

+      if (text != null) {

+         errorMessageElement.newTextPElement().setTextContent(text);

+      }

+      if (messageType == null) {

+        messageType = TableMessageTypeAttribute.DEFAULT_VALUE;

+      }

+      errorMessageElement.setTableMessageTypeAttribute(messageType);

+

+    } catch (Exception e) {

+      Logger.getLogger(Cell.class.getName()).log(Level.SEVERE, null, e);

+    }

+  }

+

+

 	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 	//////////////////////////////////////// private methods ///////////////////////////////////////////////////////////

 	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

diff --git a/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java b/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
index 6921725..58b2ea2 100644
--- a/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
+++ b/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
@@ -1432,6 +1432,24 @@
 		}

 	}

 

+  @Test

+  public void testSetValidityListAndInputErrorMessage() {

+    try {

+      int rowindex = 3, columnindex = 2;

+      Table table = odsdoc.getTableByName("Sheet2");

+      Cell fcell = table.getCellByPosition(columnindex, rowindex);

+      List<String> values = new ArrayList<String>(Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri"));

+      fcell.setValidityList(values);

+      fcell.setInputErrorMessage("Error message", "This cell only allows working days.", null);

+      fcell.setStringValue("Tue");

+      Assert.assertNotNull(fcell.getOdfElement().getTableContentValidationNameAttribute());

+      saveods();

+    } catch (Exception e) {

+      Logger.getLogger(TableCellTest.class.getName()).log(Level.SEVERE, null, e);

+      Assert.fail(e.getMessage());

+    }

+  }

+

 	private void compareResults(Element element, String input, String[] output) {

 		int i;

 		int nSpaces;