Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VPopupCalendar.java 4.2KB

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