You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CalendarField.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.demo.reservation;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Collection;
  7. import java.util.Date;
  8. import java.util.Iterator;
  9. import com.vaadin.data.Container;
  10. import com.vaadin.data.Item;
  11. import com.vaadin.data.Property;
  12. import com.vaadin.demo.reservation.gwt.client.ui.VCalendarField;
  13. import com.vaadin.terminal.PaintException;
  14. import com.vaadin.terminal.PaintTarget;
  15. import com.vaadin.ui.ClientWidget;
  16. import com.vaadin.ui.DateField;
  17. // TODO send one month at a time, do lazyLoading
  18. // TODO check date limit when updating variables
  19. // TODO Allow item selection
  20. @SuppressWarnings("serial")
  21. @ClientWidget(VCalendarField.class)
  22. public class CalendarField extends DateField implements Container.Viewer {
  23. private static final String TAGNAME = "calendarfield";
  24. private Date minDate;
  25. private Date maxDate;
  26. private Container dataSource;
  27. private Object itemStyleNamePropertyId;
  28. private Object itemStartPropertyId;
  29. private Object itemEndPropertyId;
  30. private Object itemTitlePropertyId;
  31. private Object itemDescriptionPropertyId;
  32. private Object itemNotimePropertyId;
  33. public CalendarField() {
  34. super();
  35. init();
  36. }
  37. public CalendarField(Property dataSource) throws IllegalArgumentException {
  38. super(dataSource);
  39. init();
  40. }
  41. public CalendarField(String caption, Date value) {
  42. super(caption, value);
  43. init();
  44. }
  45. public CalendarField(String caption, Property dataSource) {
  46. super(caption, dataSource);
  47. init();
  48. }
  49. public CalendarField(String caption) {
  50. super(caption);
  51. init();
  52. }
  53. /*
  54. * Gets the components UIDL tag string. Don't add a JavaDoc comment here, we
  55. * use the default documentation from implemented interface.
  56. */
  57. @Override
  58. public String getTag() {
  59. return TAGNAME;
  60. }
  61. public void init() {
  62. super.setResolution(RESOLUTION_HOUR);
  63. }
  64. /**
  65. * Sets the resolution of the CalendarField. Only RESOLUTION_DAY and
  66. * RESOLUTION_HOUR are supported.
  67. *
  68. * @param resolution
  69. * the resolution to set.
  70. * @see com.vaadin.ui.DateField#setResolution(int)
  71. */
  72. @Override
  73. public void setResolution(int resolution) {
  74. if (resolution != RESOLUTION_DAY && resolution != RESOLUTION_HOUR) {
  75. throw new IllegalArgumentException();
  76. }
  77. super.setResolution(resolution);
  78. }
  79. public void setMinimumDate(Date date) {
  80. minDate = date;
  81. requestRepaint();
  82. }
  83. public Date getMinimumDate() {
  84. return minDate;
  85. }
  86. public void setMaximumDate(Date date) {
  87. maxDate = date;
  88. requestRepaint();
  89. }
  90. public Date getMaximumDate() {
  91. return maxDate;
  92. }
  93. public Container getContainerDataSource() {
  94. return dataSource;
  95. }
  96. public void setContainerDataSource(Container newDataSource) {
  97. if (newDataSource == null || checkDataSource(newDataSource)) {
  98. dataSource = newDataSource;
  99. } else {
  100. // TODO error message
  101. throw new IllegalArgumentException();
  102. }
  103. requestRepaint();
  104. }
  105. private boolean checkDataSource(Container dataSource) {
  106. // Check old propertyIds
  107. if (itemEndPropertyId != null) {
  108. final Class<?> c = dataSource.getType(itemEndPropertyId);
  109. if (!Date.class.isAssignableFrom(c)) {
  110. itemEndPropertyId = null;
  111. }
  112. }
  113. if (itemNotimePropertyId != null) {
  114. final Class<?> c = dataSource.getType(itemNotimePropertyId);
  115. if (!Boolean.class.isAssignableFrom(c)) {
  116. itemNotimePropertyId = null;
  117. }
  118. }
  119. if (itemStartPropertyId != null) {
  120. final Class<?> c = dataSource.getType(itemStartPropertyId);
  121. if (Date.class.isAssignableFrom(c)) {
  122. // All we _really_ need is one date
  123. return true;
  124. } else {
  125. itemStartPropertyId = null;
  126. }
  127. }
  128. // We need at least one Date
  129. final Collection<?> ids = dataSource.getContainerPropertyIds();
  130. for (final Iterator<?> it = ids.iterator(); it.hasNext();) {
  131. final Object id = it.next();
  132. final Class<?> c = dataSource.getType(id);
  133. if (Date.class.isAssignableFrom(c)) {
  134. itemStartPropertyId = id;
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. public Object getItemStyleNamePropertyId() {
  141. return itemStyleNamePropertyId;
  142. }
  143. public void setItemStyleNamePropertyId(Object propertyId) {
  144. itemStyleNamePropertyId = propertyId;
  145. }
  146. public Object getItemStartPropertyId() {
  147. return itemStartPropertyId;
  148. }
  149. public void setItemStartPropertyId(Object propertyId) {
  150. // TODO nullcheck for property id
  151. if (dataSource != null
  152. && !Date.class.isAssignableFrom(dataSource.getType(propertyId))) {
  153. // TODO error message
  154. throw new IllegalArgumentException();
  155. }
  156. itemStartPropertyId = propertyId;
  157. }
  158. public Object getItemEndPropertyId() {
  159. return itemEndPropertyId;
  160. }
  161. public void setItemEndPropertyId(Object propertyId) {
  162. // TODO nullcheck for property id
  163. if (dataSource != null
  164. && !Date.class.isAssignableFrom(dataSource.getType(propertyId))) {
  165. // TODO error message
  166. throw new IllegalArgumentException();
  167. }
  168. itemEndPropertyId = propertyId;
  169. }
  170. public Object getItemTitlePropertyId() {
  171. return itemTitlePropertyId;
  172. }
  173. public void setItemTitlePropertyId(Object propertyId) {
  174. itemTitlePropertyId = propertyId;
  175. }
  176. public Object getItemDescriptionPropertyId() {
  177. return itemDescriptionPropertyId;
  178. }
  179. public void setItemDescriptionPropertyId(Object propertyId) {
  180. itemDescriptionPropertyId = propertyId;
  181. }
  182. public Object getitemNotimePropertyId() {
  183. return itemNotimePropertyId;
  184. }
  185. public void setItemNotimePropertyId(Object propertyId) {
  186. // TODO nullcheck for property id
  187. if (dataSource != null
  188. && !Boolean.class.isAssignableFrom(dataSource
  189. .getType(propertyId))) {
  190. // TODO error message
  191. throw new IllegalArgumentException();
  192. }
  193. itemNotimePropertyId = propertyId;
  194. }
  195. /**
  196. * Paints the content of this component.
  197. *
  198. * @param target
  199. * the Paint Event.
  200. * @throws PaintException
  201. * if the paint operation failed.
  202. */
  203. @SuppressWarnings("deprecation")
  204. @Override
  205. public void paintContent(PaintTarget target) throws PaintException {
  206. super.paintContent(target);
  207. if (minDate != null) {
  208. target.addAttribute("min", String.valueOf(minDate.getTime()));
  209. }
  210. if (maxDate != null) {
  211. target.addAttribute("max", String.valueOf(maxDate.getTime()));
  212. }
  213. if (dataSource != null) {
  214. target.startTag("items");
  215. // TODO send one month now, the rest via lazyloading
  216. int month = new Date().getMonth();
  217. final Object value = getValue();
  218. if (value != null && value instanceof Date) {
  219. month = ((Date) value).getMonth();
  220. }
  221. for (final Iterator<?> it = dataSource.getItemIds().iterator(); it
  222. .hasNext();) {
  223. final Object itemId = it.next();
  224. final Item item = dataSource.getItem(itemId);
  225. Property p = item.getItemProperty(itemStartPropertyId);
  226. Date start = (Date) p.getValue();
  227. Date end = start; // assume same day
  228. if (itemEndPropertyId != null) {
  229. p = item.getItemProperty(itemEndPropertyId);
  230. end = (Date) p.getValue();
  231. if (end == null) {
  232. end = start;
  233. } else if (end.before(start)) {
  234. final Date tmp = start;
  235. start = end;
  236. end = tmp;
  237. }
  238. }
  239. // TODO half-done lazyloading logic (hence broken)
  240. if (start != null) {
  241. if ((start.getMonth() <= month || end.getMonth() >= month)) {
  242. target.startTag("item");
  243. // TODO different id?
  244. target.addAttribute("id", itemId.hashCode());
  245. if (itemStyleNamePropertyId != null) {
  246. p = item.getItemProperty(itemStyleNamePropertyId);
  247. final String styleName = (String) p.getValue();
  248. target.addAttribute("styleName", styleName);
  249. }
  250. SimpleDateFormat sdf = new SimpleDateFormat(
  251. "d MM yyyy HH:mm:ss Z");
  252. target.addAttribute("Z", start.getTimezoneOffset());
  253. target.addAttribute("start", "" + sdf.format(start));
  254. if (end != start) {
  255. target.addAttribute("end", "" + sdf.format(end));
  256. }
  257. if (itemTitlePropertyId != null) {
  258. p = item.getItemProperty(itemTitlePropertyId);
  259. final Object val = p.getValue();
  260. if (val != null) {
  261. target.addAttribute("title", val.toString());
  262. }
  263. }
  264. if (itemDescriptionPropertyId != null) {
  265. p = item.getItemProperty(itemDescriptionPropertyId);
  266. final Object val = p.getValue();
  267. if (val != null) {
  268. target.addAttribute("description", val
  269. .toString());
  270. }
  271. }
  272. if (itemNotimePropertyId != null) {
  273. p = item.getItemProperty(itemNotimePropertyId);
  274. final Object val = p.getValue();
  275. if (val != null) {
  276. target.addAttribute("notime", ((Boolean) val)
  277. .booleanValue());
  278. }
  279. }
  280. target.endTag("item");
  281. }
  282. }
  283. }
  284. target.endTag("items");
  285. }
  286. }
  287. }