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.

IPopupCalendar.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import com.google.gwt.user.client.DOM;
  6. import com.google.gwt.user.client.Timer;
  7. import com.google.gwt.user.client.Window;
  8. import com.google.gwt.user.client.ui.Button;
  9. import com.google.gwt.user.client.ui.ClickListener;
  10. import com.google.gwt.user.client.ui.PopupListener;
  11. import com.google.gwt.user.client.ui.PopupPanel;
  12. import com.google.gwt.user.client.ui.Widget;
  13. import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
  14. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  15. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  16. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  17. public class IPopupCalendar extends ITextualDate implements Paintable, Field,
  18. ClickListener, PopupListener {
  19. private final Button calendarToggle;
  20. private final ICalendarPanel calendar;
  21. private final IToolkitOverlay popup;
  22. private boolean open = false;
  23. public IPopupCalendar() {
  24. super();
  25. calendarToggle = new Button();
  26. calendarToggle.setStyleName(CLASSNAME + "-button");
  27. calendarToggle.setText("...");
  28. calendarToggle.addClickListener(this);
  29. add(calendarToggle);
  30. calendar = new ICalendarPanel(this);
  31. popup = new IToolkitOverlay(true, true, true);
  32. popup.setStyleName(IDateField.CLASSNAME + "-popup");
  33. popup.setWidget(calendar);
  34. popup.addPopupListener(this);
  35. DOM.setElementProperty(calendar.getElement(), "id",
  36. "PID_TOOLKIT_POPUPCAL");
  37. }
  38. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  39. super.updateFromUIDL(uidl, client);
  40. if (date != null) {
  41. calendar.updateCalendar();
  42. }
  43. calendarToggle.setEnabled(enabled);
  44. }
  45. public void onClick(Widget sender) {
  46. if (sender == calendarToggle && !open) {
  47. open = true;
  48. calendar.updateCalendar();
  49. // clear previous values
  50. popup.setWidth("");
  51. popup.setHeight("");
  52. popup.setPopupPositionAndShow(new PositionCallback() {
  53. public void setPosition(int offsetWidth, int offsetHeight) {
  54. final int w = offsetWidth;
  55. final int h = offsetHeight;
  56. int t = calendarToggle.getAbsoluteTop();
  57. int l = calendarToggle.getAbsoluteLeft();
  58. if (l + w > Window.getClientWidth()
  59. + Window.getScrollLeft()) {
  60. l = Window.getClientWidth() + Window.getScrollLeft()
  61. - w;
  62. }
  63. if (t + h + calendarToggle.getOffsetHeight() + 30 > Window
  64. .getClientHeight()
  65. + Window.getScrollTop()) {
  66. t = Window.getClientHeight() + Window.getScrollTop()
  67. - h - calendarToggle.getOffsetHeight() - 30;
  68. l += calendarToggle.getOffsetWidth();
  69. }
  70. // fix size
  71. popup.setWidth(w + "px");
  72. popup.setHeight(h + "px");
  73. popup.setPopupPosition(l, t
  74. + calendarToggle.getOffsetHeight() + 2);
  75. setFocus(true);
  76. }
  77. });
  78. }
  79. }
  80. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  81. if (sender == popup) {
  82. buildDate();
  83. // Sigh.
  84. Timer t = new Timer() {
  85. public void run() {
  86. open = false;
  87. }
  88. };
  89. t.schedule(100);
  90. }
  91. }
  92. /**
  93. * Sets focus to Calendar panel.
  94. *
  95. * @param focus
  96. */
  97. public void setFocus(boolean focus) {
  98. calendar.setFocus(focus);
  99. }
  100. protected int getFieldExtraWidth() {
  101. if (fieldExtraWidth < 0) {
  102. fieldExtraWidth = super.getFieldExtraWidth();
  103. fieldExtraWidth += calendarToggle.getOffsetWidth();
  104. }
  105. return fieldExtraWidth;
  106. }
  107. }