@SuppressWarnings("unchecked") private <T> FormComponent<T> getField(String id) { return (FormComponent<T>) get(id); }
public void onUpdate(AjaxRequestTarget target) { Object dateFieldInput = getField("date").getModelObject(); Integer hours = (Integer) getField("hours").getModelObject(); Integer minutes = (Integer) getField("minutes").getModelObject(); updateDateTime(dateFieldInput, hours, minutes, target); }
@Override protected void onUpdate(AjaxRequestTarget target) { AjaxDateTimeField.this.onUpdate(target); } }
private void updateDateTime(Object datetime, Integer hours, Integer minutes, AjaxRequestTarget target) { MutableDateTime date = new MutableDateTime(datetime); try { TimeZone zone = getClientTimeZone(); if (zone != null) { date.setZone(DateTimeZone.forTimeZone(zone)); } if (hours != null) { date.set(DateTimeFieldType.hourOfDay(), hours.intValue() % 24); date.setMinuteOfHour((minutes != null) ? minutes.intValue() : 0); } // the date will be in the server's timezone setModelObject(date.toDate()); if (target != null) { target.add(this); } } catch (RuntimeException e) { error(e.getMessage()); invalid(); } }
public AjaxDateTimeField(String id, IModel<Date> model, boolean todayLinkVisible) { super(id, model); get("date").add(new ChangeBehaviour()); get("hours").add(new ChangeBehaviour()); get("minutes").add(new ChangeBehaviour()); get("amOrPmChoice").add(new ChangeBehaviour()); setOutputMarkupId(true); add(new AjaxLink<Date>("today") { private static final long serialVersionUID = 1L; @Override public void onClick(AjaxRequestTarget target) { updateDateTime(new Date(), null, null, target); } }.setVisible(todayLinkVisible)); }
public DateFieldWidget(String id, IModel<Date> model, boolean todayLinkVisible) { super(id, model); final JcrPropertyValueModel valueModel = (JcrPropertyValueModel) getDefaultModel(); Date date; try { date = valueModel.getValue().getDate().getTime(); } catch (RepositoryException ex) { // FIXME: log error date = null; } add(new AjaxDateTimeField("widget", new Model<Date>(date) { private static final long serialVersionUID = 1L; @Override public void setObject(Date object) { Calendar calendar = new GregorianCalendar(); calendar.setTime(object); try { valueModel.setValue(valueModel.getJcrPropertymodel().getProperty().getSession().getValueFactory() .createValue(calendar)); } catch (RepositoryException ex) { log.error(ex.getMessage(), ex); } super.setObject(object); } }, todayLinkVisible)); }