incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fh...@apache.org
Subject svn commit: r1503564 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/table/Cell.java test/java/org/odftoolkit/simple/table/TableCellTest.java
Date Tue, 16 Jul 2013 05:57:31 GMT
Author: fhopf
Date: Tue Jul 16 05:57:31 2013
New Revision: 1503564

URL: http://svn.apache.org/r1503564
Log:
ODFTOOLKIT-358: Added method to set datetime value on table cell

Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Cell.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Cell.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Cell.java?rev=1503564&r1=1503563&r2=1503564&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Cell.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Cell.java Tue Jul
16 05:57:31 2013
@@ -135,6 +135,8 @@ public class Cell extends Component impl
 	 * The default time format of table cell.
 	 */
 	private static final String DEFAULT_TIME_FORMAT = "'PT'HH'H'mm'M'ss'S'";
+	// example format: 2002-05-30T09:30:10
+	private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
 	/**
 	 * The default cell back color of table cell.
 	 */
@@ -1016,12 +1018,30 @@ public class Cell extends Component impl
 	 *             is not "date".
 	 */
 	public Calendar getDateValue() {
+		return getOfficeDateValue(DEFAULT_DATE_FORMAT);
+	}
+	
+	/**
+	 * Get the cell date time value (date and time) as Calendar.
+	 * <p>
+	 * Throw IllegalArgumentException if the cell type is not "date".
+	 * 
+	 * @return the Calendar value of cell
+	 * @throws IllegalArgumentException
+	 *             an IllegalArgumentException will be thrown, if the cell type
+	 *             is not "date".
+	 */
+	public Calendar getDateTimeValue() {
+		return getOfficeDateValue(DEFAULT_DATE_TIME_FORMAT);
+	}
+	
+	private Calendar getOfficeDateValue(String pattern) {
 		if (getTypeAttr() == OfficeValueTypeAttribute.Value.DATE) {
 			String dateStr = mCellElement.getOfficeDateValueAttribute();
 			if (dateStr == null) {
 				return null;
 			}
-			Date date = parseString(dateStr, DEFAULT_DATE_FORMAT);
+			Date date = parseString(dateStr, pattern);
 			Calendar calender = Calendar.getInstance();
 			calender.setTime(date);
 			return calender;
@@ -1038,17 +1058,29 @@ public class Cell extends Component impl
 	 *            type.
 	 */
 	public void setDateValue(Calendar date) {
+		setOfficeDateValue(date, DEFAULT_DATE_FORMAT);
+	}
+	
+	/**
+	 * Sets the cell value as a date with second precision and the value type to be "date".
+	 * @param date 
+	 */
+	public void setDateTimeValue(Calendar date) {
+		setOfficeDateValue(date, DEFAULT_DATE_TIME_FORMAT);
+	}
+
+	private void setOfficeDateValue(Calendar date, String pattern) {
 		if (date == null) {
 			throw new IllegalArgumentException("date shouldn't be null.");
 		}
 		splitRepeatedCells();
 		setTypeAttr(OfficeValueTypeAttribute.Value.DATE);
-		SimpleDateFormat simpleFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
+		SimpleDateFormat simpleFormat = new SimpleDateFormat(pattern);
 		String svalue = simpleFormat.format(date.getTime());
 		mCellElement.setOfficeDateValueAttribute(svalue);
 		setDisplayTextContent(svalue, null);
 	}
-
+	
 	/**
 	 * Set the cell style name. When lots of cells have the same style features,
 	 * the user can configuration the first one and set the other's style name

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java?rev=1503564&r1=1503563&r2=1503564&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
(original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
Tue Jul 16 05:57:31 2013
@@ -581,6 +581,29 @@ public class TableCellTest {
 		fcell = table.getCellByPosition(columnindex, rowindex);
 		Assert.assertEquals(0, fcell.getDateValue().compareTo(expectedCalendar));
 	}
+	
+	@Test
+	public void testDateTimeValue() {
+		int rowindex = 7, columnindex = 7;
+		Table table = odsdoc.getTableByName("Sheet1");
+		Cell fcell = table.getCellByPosition(columnindex, rowindex);
+		try {
+			fcell.setDateTimeValue(null);
+			Assert.fail("Null date time value should throw IllegalArgumentException");
+		} catch (IllegalArgumentException ie) {
+			Assert.assertEquals("date shouldn't be null.", ie.getMessage());
+		}
+
+		Calendar expectedCalendar = new GregorianCalendar(2010, 1, 30, 23, 12, 56);
+		fcell.setDateTimeValue(expectedCalendar);
+		saveods();
+		// reload
+		loadOutputSpreadsheet();
+		table = odsdoc.getTableByName("Sheet1");
+		fcell = table.getCellByPosition(columnindex, rowindex);
+		Assert.assertEquals(expectedCalendar, fcell.getDateTimeValue());
+	}
+	
 
         @Test
         public void testGetFromEmptyDateValue() throws Exception {



Mime
View raw message