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.

DayToolbar.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.client.ui.calendar.schedule;
  17. import com.google.gwt.dom.client.Style.Unit;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.user.client.ui.Button;
  21. import com.google.gwt.user.client.ui.HorizontalPanel;
  22. import com.google.gwt.user.client.ui.Label;
  23. import com.google.gwt.user.client.ui.Widget;
  24. import com.vaadin.v7.client.ui.VCalendar;
  25. /**
  26. *
  27. * @since 7.1
  28. * @author Vaadin Ltd.
  29. *
  30. */
  31. public class DayToolbar extends HorizontalPanel implements ClickHandler {
  32. private int width = 0;
  33. protected static final int MARGINLEFT = 50;
  34. protected static final int MARGINRIGHT = 20;
  35. protected Button backLabel;
  36. protected Button nextLabel;
  37. private boolean verticalSized;
  38. private boolean horizontalSized;
  39. private VCalendar calendar;
  40. public DayToolbar(VCalendar vcalendar) {
  41. calendar = vcalendar;
  42. setStylePrimaryName("v-calendar-header-week");
  43. backLabel = new Button();
  44. backLabel.setStylePrimaryName("v-calendar-back");
  45. nextLabel = new Button();
  46. nextLabel.addClickHandler(this);
  47. nextLabel.setStylePrimaryName("v-calendar-next");
  48. backLabel.addClickHandler(this);
  49. setBorderWidth(0);
  50. setSpacing(0);
  51. }
  52. public void setWidthPX(int width) {
  53. this.width = (width - MARGINLEFT) - MARGINRIGHT;
  54. // super.setWidth(this.width + "px");
  55. if (getWidgetCount() == 0) {
  56. return;
  57. }
  58. updateCellWidths();
  59. }
  60. public void updateCellWidths() {
  61. int count = getWidgetCount();
  62. if (count > 0) {
  63. setCellWidth(backLabel, MARGINLEFT + "px");
  64. setCellWidth(nextLabel, MARGINRIGHT + "px");
  65. setCellHorizontalAlignment(nextLabel, ALIGN_RIGHT);
  66. int cellw = width / (count - 2);
  67. if (cellw > 0) {
  68. int[] cellWidths = VCalendar.distributeSize(width, count - 2,
  69. 0);
  70. for (int i = 1; i < count - 1; i++) {
  71. Widget widget = getWidget(i);
  72. // if (remain > 0) {
  73. // setCellWidth(widget, cellw2 + "px");
  74. // remain--;
  75. // } else {
  76. // setCellWidth(widget, cellw + "px");
  77. // }
  78. setCellWidth(widget, cellWidths[i - 1] + "px");
  79. widget.setWidth(cellWidths[i - 1] + "px");
  80. }
  81. }
  82. }
  83. }
  84. public void add(String dayName, final String date,
  85. String localizedDateFormat, String extraClass) {
  86. Label l = new Label(dayName + " " + localizedDateFormat);
  87. l.setStylePrimaryName("v-calendar-header-day");
  88. if (extraClass != null) {
  89. l.addStyleDependentName(extraClass);
  90. }
  91. if (verticalSized) {
  92. l.addStyleDependentName("Vsized");
  93. }
  94. if (horizontalSized) {
  95. l.addStyleDependentName("Hsized");
  96. }
  97. l.addClickHandler(new ClickHandler() {
  98. @Override
  99. public void onClick(ClickEvent event) {
  100. if (calendar.getDateClickListener() != null) {
  101. calendar.getDateClickListener().dateClick(date);
  102. }
  103. }
  104. });
  105. add(l);
  106. }
  107. public void addBackButton() {
  108. if (!calendar.isBackwardNavigationEnabled()) {
  109. nextLabel.getElement().getStyle().setHeight(0, Unit.PX);
  110. }
  111. add(backLabel);
  112. }
  113. public void addNextButton() {
  114. if (!calendar.isForwardNavigationEnabled()) {
  115. backLabel.getElement().getStyle().setHeight(0, Unit.PX);
  116. }
  117. add(nextLabel);
  118. }
  119. @Override
  120. public void onClick(ClickEvent event) {
  121. if (!calendar.isDisabled()) {
  122. if (event.getSource() == nextLabel) {
  123. if (calendar.getForwardListener() != null) {
  124. calendar.getForwardListener().forward();
  125. }
  126. } else if (event.getSource() == backLabel) {
  127. if (calendar.getBackwardListener() != null) {
  128. calendar.getBackwardListener().backward();
  129. }
  130. }
  131. }
  132. }
  133. public void setVerticalSized(boolean sized) {
  134. verticalSized = sized;
  135. updateDayLabelSizedStyleNames();
  136. }
  137. public void setHorizontalSized(boolean sized) {
  138. horizontalSized = sized;
  139. updateDayLabelSizedStyleNames();
  140. }
  141. private void updateDayLabelSizedStyleNames() {
  142. for (Widget w : this) {
  143. updateWidgetSizedStyleName(w);
  144. }
  145. }
  146. private void updateWidgetSizedStyleName(Widget w) {
  147. if (verticalSized) {
  148. w.addStyleDependentName("Vsized");
  149. } else {
  150. w.removeStyleDependentName("VSized");
  151. }
  152. if (horizontalSized) {
  153. w.addStyleDependentName("Hsized");
  154. } else {
  155. w.removeStyleDependentName("HSized");
  156. }
  157. }
  158. }