AI2 Component  (Version nb184)
DatePicker.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2014 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
9 import android.app.DatePickerDialog;
10 import android.os.Handler;
11 
22 
23 import java.text.DateFormatSymbols;
24 import java.util.Calendar;
25 import java.util.GregorianCalendar;
26 
39 @DesignerComponent(version = YaVersion.DATEPICKER_COMPONENT_VERSION,
40  category = ComponentCategory.USERINTERFACE,
41  description = "<p>A button that, when clicked on, launches a popup" +
42  " dialog to allow the user to select a date.</p>")
43 @SimpleObject
44 public class DatePicker extends ButtonBase {
45 
46  private DatePickerDialog date;
47  //month is the property that AI devs see, and it's always javaMonth + 1; month is 0-based in Java
48  private int year, month, javaMonth, day;
49  private Calendar instant;
50  private String [] localizedMonths = new DateFormatSymbols().getMonths();
51  private boolean customDate = false;
52  private Form form;
53  private Handler androidUIHandler;
54 
60  super(container);
61  form = container.$form();
62  //Set the current date on creation
63  final Calendar c = Calendar.getInstance();
64  year = c.get(Calendar.YEAR);
65  javaMonth = c.get(Calendar.MONTH);
66  month = javaMonth + 1;
67  day = c.get(Calendar.DAY_OF_MONTH);
68  instant = Dates.DateInstant(year, month, day);
69  date = new DatePickerDialog(this.container.$context(), datePickerListener, year, javaMonth,
70  day);
71 
72  androidUIHandler = new Handler();
73 
74  }
75 
80  @SimpleProperty(description = "the Year that was last picked using the DatePicker",
81  category = PropertyCategory.APPEARANCE)
82  public int Year() {
83  return year;
84  }
85 
90  @SimpleProperty(description = "the number of the Month that was last picked using the " +
91  "DatePicker. Note that months start in 1 = January, 12 = December.",
92  category = PropertyCategory.APPEARANCE)
93  public int Month() {
94  return month;
95  }
96 
101  @SimpleProperty(description = "Returns the name of the Month that was last picked using the " +
102  "DatePicker, in textual format.",
103  category = PropertyCategory.APPEARANCE)
104  public String MonthInText() {
105  return localizedMonths[javaMonth];
106  }
107 
112  @SimpleProperty(description = "the Day of the month that was last picked using the DatePicker.",
113  category = PropertyCategory.APPEARANCE)
114  public int Day() {
115  return day;
116  }
117 
122  @SimpleProperty(description = "the instant of the date that was last picked using the DatePicker.",
123  category = PropertyCategory.APPEARANCE)
124  public Calendar Instant() {
125  return instant;
126  }
127 
128  @SimpleFunction(description = "Allows the user to set the date to be displayed when the date picker opens.\n" +
129  "Valid values for the month field are 1-12 and 1-31 for the day field.\n")
130  public void SetDateToDisplay(int year, int month, int day) {
131  int jMonth = month - 1;
132  try {
133  GregorianCalendar cal = new GregorianCalendar(year, jMonth, day);
134  cal.setLenient(false);
135  cal.getTime();
136  } catch (java.lang.IllegalArgumentException e) {
137  form.dispatchErrorOccurredEvent(this, "SetDateToDisplay", ErrorMessages.ERROR_ILLEGAL_DATE);
138  }
139  date.updateDate(year, jMonth, day);
140  instant = Dates.DateInstant(year, month, day);
141  customDate = true;
142  }
143 
144  @SimpleFunction(description = "Allows the user to set the date from the instant to be displayed when the date picker opens.")
145  public void SetDateToDisplayFromInstant(Calendar instant) {
146  int year = Dates.Year(instant);
147  int month = Dates.Month(instant);
148  int day = Dates.Day(instant);
149  date.updateDate(year, month, day);
150  instant = Dates.DateInstant(year, month, day);
151  customDate = true;
152  }
153 
158  @SimpleFunction(description="Launches the DatePicker dialog.")
159  public void LaunchPicker() {
160  click();
161  }
162 
166  @Override
167  public void click() {
168  if (!customDate) {
169  Calendar c = Calendar.getInstance();
170  int year = c.get(Calendar.YEAR);
171  int jMonth = c.get(Calendar.MONTH);
172  int day = c.get(Calendar.DAY_OF_MONTH);
173  date.updateDate(year, jMonth, day);
174  instant = Dates.DateInstant(year, jMonth+1, day);
175  } else {
176  customDate = false;
177  }
178  date.show();
179  }
180 
184  private DatePickerDialog.OnDateSetListener datePickerListener =
185  new DatePickerDialog.OnDateSetListener() {
186  @Override
187  public void onDateSet(android.widget.DatePicker datePicker, int selectedYear,
188  int selectedMonth, int selectedDay) {
189  if (datePicker.isShown()) {
190  year = selectedYear;
191  javaMonth = selectedMonth;
192  month = javaMonth + 1;
193  day = selectedDay;
194  date.updateDate(year, javaMonth, day);
195  instant = Dates.DateInstant(year, month, day);
196  // We post an event to the Android handler to do the App Inventor
197  // event dispatch. This way it gets called outside of the context
198  // of the datepicker's event. This permits the App Inventor dispatch
199  // handler to re-launch this date picker
200  androidUIHandler.post(new Runnable() {
201  public void run() {
202  AfterDateSet();
203  }
204  });
205  }
206  }
207  };
208 
212  @SimpleEvent(description = "Event that runs after the user chooses a Date in the dialog")
213  public void AfterDateSet() {
214  EventDispatcher.dispatchEvent(this, "AfterDateSet");
215  }
216 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.DatePicker.MonthInText
String MonthInText()
Definition: DatePicker.java:104
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components
com.google.appinventor.components.runtime.DatePicker.SetDateToDisplay
void SetDateToDisplay(int year, int month, int day)
Definition: DatePicker.java:130
com.google.appinventor.components.runtime.util.Dates.Year
static int Year(Calendar date)
Definition: Dates.java:399
com.google.appinventor.components.runtime.DatePicker
Definition: DatePicker.java:44
com.google.appinventor.components.runtime.util.Dates.Day
static int Day(Calendar date)
Definition: Dates.java:161
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.DatePicker.Instant
Calendar Instant()
Definition: DatePicker.java:124
com.google.appinventor.components.runtime.DatePicker.LaunchPicker
void LaunchPicker()
Definition: DatePicker.java:159
com.google.appinventor.components.runtime.DatePicker.Day
int Day()
Definition: DatePicker.java:114
com.google.appinventor.components.runtime.DatePicker.DatePicker
DatePicker(ComponentContainer container)
Definition: DatePicker.java:59
com.google.appinventor.components.runtime.DatePicker.AfterDateSet
void AfterDateSet()
Definition: DatePicker.java:213
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.DatePicker.Year
int Year()
Definition: DatePicker.java:82
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.Dates
Definition: Dates.java:24
com.google.appinventor.components.runtime.DatePicker.click
void click()
Definition: DatePicker.java:167
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.util.Dates.Month
static int Month(Calendar date)
Definition: Dates.java:321
com.google.appinventor.components.runtime.AndroidViewComponent.container
final ComponentContainer container
Definition: AndroidViewComponent.java:29
com.google
com.google.appinventor.components.runtime.ButtonBase
Definition: ButtonBase.java:47
com
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_ILLEGAL_DATE
static final int ERROR_ILLEGAL_DATE
Definition: ErrorMessages.java:193
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.DatePicker.Month
int Month()
Definition: DatePicker.java:93
com.google.appinventor.components.runtime.Form
Definition: Form.java:126
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.runtime.DatePicker.SetDateToDisplayFromInstant
void SetDateToDisplayFromInstant(Calendar instant)
Definition: DatePicker.java:145
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.util.Dates.DateInstant
static Calendar DateInstant(int year, int month, int day)
Definition: Dates.java:253