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

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