You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/10/03 19:47:06 UTC
svn commit: r821375 - in /incubator/pivot/trunk:
core/src/org/apache/pivot/util/ demos/src/org/apache/pivot/demos/roweditor/
wtk/src/org/apache/pivot/wtk/ wtk/src/org/apache/pivot/wtk/content/
wtk/src/org/apache/pivot/wtk/skin/ wtk/src/org/apache/pivot...
Author: tvolkert
Date: Sat Oct 3 17:47:06 2009
New Revision: 821375
URL: http://svn.apache.org/viewvc?rev=821375&view=rev
Log:
PIVOT-310 :: Make CalendarDate a struct-like class
Modified:
incubator/pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java
incubator/pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/CustomTableRow.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Calendar.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/CalendarButton.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/CalendarDateSpinnerData.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/CalendarButtonSkin.java
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java
Modified: incubator/pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java?rev=821375&r1=821374&r2=821375&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java Sat Oct 3 17:47:06 2009
@@ -31,9 +31,20 @@
public class CalendarDate implements Comparable<CalendarDate>, Serializable {
private static final long serialVersionUID = 3974393986540543704L;
- private int year;
- private int month;
- private int day;
+ /**
+ * The year field. (e.g. <tt>2008</tt>).
+ */
+ public final int year;
+
+ /**
+ * The month field, 0-based. (e.g. <tt>2</tt> for March).
+ */
+ public final int month;
+
+ /**
+ * The day of the month, 0-based. (e.g. <tt>14</tt> for the 15th).
+ */
+ public final int day;
private static final int[] MONTH_LENGTHS = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
@@ -76,41 +87,6 @@
* The day of the month, 0-based. (e.g. <tt>14</tt> for the 15th)
*/
public CalendarDate(int year, int month, int day) {
- set(year, month, day);
- }
-
- /**
- * Creates a new date representing the specified date string. The date
- * string must be in the <tt>ISO 8601</tt> "calendar date" format,
- * which is <tt>[YYYY]-[MM]-[DD]</tt>.
- *
- * @param date
- * A string in the form of <tt>[YYYY]-[MM]-[DD]</tt>. (e.g. 2008-07-23)
- */
- public CalendarDate(String date) {
- Pattern pattern = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
- Matcher matcher = pattern.matcher(date);
-
- if (!matcher.matches()) {
- throw new IllegalArgumentException("Invalid date format: " + date);
- }
-
- String year = matcher.group(1);
- String month = matcher.group(2);
- String day = matcher.group(3);
-
- set(Integer.parseInt(year), Integer.parseInt(month) - 1,
- Integer.parseInt(day) - 1);
- }
-
- /**
- * Sets the date.
- *
- * @param year
- * @param month
- * @param day
- */
- public void set(int year, int month, int day) {
if (year <= GREGORIAN_CUTOVER_YEAR || year > 9999) {
throw new IllegalArgumentException("Invalid year: " + year);
}
@@ -136,63 +112,6 @@
}
/**
- * Gets the year field. (e.g. <tt>2008</tt>).
- *
- * @return
- * This calendar date's <tt>year</tt> field
- */
- public int getYear() {
- return year;
- }
-
- /**
- * Sets the year field.
- *
- * @param year
- */
- public void setYear(int year) {
- set(year, month, day);
- }
-
- /**
- * Gets the month field, 0-based. (e.g. <tt>2</tt> for March).
- *
- * @return
- * This calendar date's <tt>month</tt> field
- */
- public int getMonth() {
- return month;
- }
-
- /**
- * Sets the month field.
- *
- * @param month
- */
- public void setMonth(int month) {
- set(year, month, day);
- }
-
- /**
- * Gets the day of the month, 0-based. (e.g. <tt>14</tt> for the 15th).
- *
- * @return
- * This calendar date's <tt>day</tt> field
- */
- public int getDay() {
- return day;
- }
-
- /**
- * Sets the day field.
- *
- * @param day
- */
- public void setDay(int day) {
- set(year, month, day);
- }
-
- /**
* Compares this calendar date with another calendar date.
*
* @param calendarDate
@@ -334,4 +253,28 @@
return buf.toString();
}
+
+ /**
+ * Creates a new date representing the specified date string. The date
+ * string must be in the <tt>ISO 8601</tt> "calendar date" format,
+ * which is <tt>[YYYY]-[MM]-[DD]</tt>.
+ *
+ * @param date
+ * A string in the form of <tt>[YYYY]-[MM]-[DD]</tt>. (e.g. 2008-07-23)
+ */
+ public static CalendarDate forString(String date) {
+ Pattern pattern = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
+ Matcher matcher = pattern.matcher(date);
+
+ if (!matcher.matches()) {
+ throw new IllegalArgumentException("Invalid date format: " + date);
+ }
+
+ String year = matcher.group(1);
+ String month = matcher.group(2);
+ String day = matcher.group(3);
+
+ return new CalendarDate(Integer.parseInt(year), Integer.parseInt(month) - 1,
+ Integer.parseInt(day) - 1);
+ }
}
Modified: incubator/pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/CustomTableRow.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/CustomTableRow.java?rev=821375&r1=821374&r2=821375&view=diff
==============================================================================
--- incubator/pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/CustomTableRow.java (original)
+++ incubator/pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/CustomTableRow.java Sat Oct 3 17:47:06 2009
@@ -36,7 +36,7 @@
}
public final void setDate(String calendarDate) {
- setDate(new CalendarDate(calendarDate));
+ setDate(CalendarDate.forString(calendarDate));
}
public ExpenseType getType() {
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Calendar.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Calendar.java?rev=821375&r1=821374&r2=821375&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Calendar.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/Calendar.java Sat Oct 3 17:47:06 2009
@@ -110,7 +110,7 @@
}
private Calendar(CalendarDate calendarDate) {
- this(calendarDate.getYear(), calendarDate.getMonth());
+ this(calendarDate.year, calendarDate.month);
}
public Calendar(int year, int month) {
@@ -196,7 +196,7 @@
throw new IllegalArgumentException("selectedDate is null.");
}
- setSelectedDate(new CalendarDate(selectedDate));
+ setSelectedDate(CalendarDate.forString(selectedDate));
}
/**
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/CalendarButton.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/CalendarButton.java?rev=821375&r1=821374&r2=821375&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/CalendarButton.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/CalendarButton.java Sat Oct 3 17:47:06 2009
@@ -157,7 +157,7 @@
throw new IllegalArgumentException("selectedDate is null.");
}
- setSelectedDate(new CalendarDate(selectedDate));
+ setSelectedDate(CalendarDate.forString(selectedDate));
}
/**
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/CalendarDateSpinnerData.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/CalendarDateSpinnerData.java?rev=821375&r1=821374&r2=821375&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/CalendarDateSpinnerData.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/CalendarDateSpinnerData.java Sat Oct 3 17:47:06 2009
@@ -107,15 +107,15 @@
throw new IllegalArgumentException("lowerBound must be before upperBound.");
}
- calendar = new GregorianCalendar(lowerBound.getYear(), lowerBound.getMonth(),
- lowerBound.getDay() + 1);
+ calendar = new GregorianCalendar(lowerBound.year, lowerBound.month,
+ lowerBound.day + 1);
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendarIndex = 0;
// Calculate our length and cache it, since it is guaranteed to
// remain fixed
- GregorianCalendar upperBoundCalendar = new GregorianCalendar(upperBound.getYear(),
- upperBound.getMonth(), upperBound.getDay() + 1);
+ GregorianCalendar upperBoundCalendar = new GregorianCalendar(upperBound.year,
+ upperBound.month, upperBound.day + 1);
upperBoundCalendar.setTimeZone(TimeZone.getTimeZone("GMT"));
long lowerBoundMilliseconds = calendar.getTimeInMillis();
long upperBoundMilliseconds = upperBoundCalendar.getTimeInMillis();
@@ -192,11 +192,7 @@
public int indexOf(CalendarDate item) {
long currentMilliseconds = calendar.getTimeInMillis();
- int year = item.getYear();
- int month = item.getMonth();
- int day = item.getDay() + 1;
-
- GregorianCalendar tmpCalendar = new GregorianCalendar(year, month, day);
+ GregorianCalendar tmpCalendar = new GregorianCalendar(item.year, item.month, item.day + 1);
tmpCalendar.setTimeZone(TimeZone.getTimeZone("GMT"));
long itemMilliseconds = tmpCalendar.getTimeInMillis();
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/CalendarButtonSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/CalendarButtonSkin.java?rev=821375&r1=821374&r2=821375&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/CalendarButtonSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/CalendarButtonSkin.java Sat Oct 3 17:47:06 2009
@@ -287,8 +287,8 @@
calendar.setSelectedDate(date);
if (date != null) {
- calendar.setYear(date.getYear());
- calendar.setMonth(date.getMonth());
+ calendar.setYear(date.year);
+ calendar.setMonth(date.month);
}
}
}
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java?rev=821375&r1=821374&r2=821375&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java Sat Oct 3 17:47:06 2009
@@ -233,8 +233,7 @@
CalendarDate date = (CalendarDate)dateButton.getButtonData();
Calendar calendar = (Calendar)TerraCalendarSkin.this.getComponent();
- int cellIndex = getCellIndex(date.getYear(), date.getMonth(), date.getDay(),
- calendar.getLocale());
+ int cellIndex = getCellIndex(date.year, date.month, date.day, calendar.getLocale());
int rowIndex = cellIndex / 7;
int columnIndex = cellIndex % 7;
@@ -333,11 +332,10 @@
public void render(Object item, Spinner spinner) {
Calendar calendar = (Calendar)getComponent();
- CalendarDate date = new CalendarDate();
- date.set(date.getYear(), (Integer)item, 0);
+ // Since we're only rendering the month, the year and day do not matter here
+ CalendarDate date = new CalendarDate(2000, (Integer)item, 0);
- SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM",
- calendar.getLocale());
+ SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM", calendar.getLocale());
item = monthFormat.format(date.toCalendar().getTime());
super.render(item, spinner);
@@ -348,7 +346,7 @@
@Override
public void render(Object data, Button button, boolean highlighted) {
CalendarDate date = (CalendarDate)data;
- super.render(date.getDay() + 1, button, highlighted);
+ super.render(date.day + 1, button, highlighted);
if (button.isSelected()) {
label.getStyles().put("color", button.getStyles().get("selectionColor"));
@@ -483,8 +481,8 @@
// result of the user toggling the date button (as opposed
// to changing the month or year), clear the selection
if (selectedDate == null
- || (selectedDate.getYear() == yearSpinner.getSelectedIndex()
- && selectedDate.getMonth() == monthSpinner.getSelectedIndex())) {
+ || (selectedDate.year == yearSpinner.getSelectedIndex()
+ && selectedDate.month == monthSpinner.getSelectedIndex())) {
calendar.setSelectedDate((CalendarDate)null);
}
} else {
@@ -655,12 +653,12 @@
selection.setSelected(false);
}
} else {
- int year = selectedDate.getYear();
- int month = selectedDate.getMonth();
+ int year = selectedDate.year;
+ int month = selectedDate.month;
if (year == calendar.getYear()
&& month == calendar.getMonth()) {
- int day = selectedDate.getDay();
+ int day = selectedDate.day;
// Update the button group
int cellIndex = getCellIndex(year, month, day, calendar.getLocale());