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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2000-2013 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.client.ui.calendar.schedule;
  17. import java.util.Iterator;
  18. import com.google.gwt.dom.client.Style.Unit;
  19. import com.google.gwt.event.dom.client.ClickEvent;
  20. import com.google.gwt.event.dom.client.ClickHandler;
  21. import com.google.gwt.user.client.ui.Button;
  22. import com.google.gwt.user.client.ui.HorizontalPanel;
  23. import com.google.gwt.user.client.ui.Label;
  24. import com.google.gwt.user.client.ui.Widget;
  25. import com.vaadin.client.ui.VCalendar;
  26. /**
  27. *
  28. * @since 7.1
  29. * @author Vaadin Ltd.
  30. *
  31. */
  32. public class DayToolbar extends HorizontalPanel implements ClickHandler {
  33. private int width = 0;
  34. protected static final int MARGINLEFT = 50;
  35. protected static final int MARGINRIGHT = 20;
  36. protected Button backLabel;
  37. protected Button nextLabel;
  38. private boolean verticalSized;
  39. private boolean horizontalSized;
  40. private VCalendar calendar;
  41. public DayToolbar(VCalendar vcalendar) {
  42. calendar = vcalendar;
  43. setStylePrimaryName("v-calendar-header-week");
  44. backLabel = new Button();
  45. backLabel.setStylePrimaryName("v-calendar-back");
  46. nextLabel = new Button();
  47. nextLabel.addClickHandler(this);
  48. nextLabel.setStylePrimaryName("v-calendar-next");
  49. backLabel.addClickHandler(this);
  50. setBorderWidth(0);
  51. setSpacing(0);
  52. }
  53. public void setWidthPX(int width) {
  54. this.width = (width - MARGINLEFT) - MARGINRIGHT;
  55. // super.setWidth(this.width + "px");
  56. if (getWidgetCount() == 0) {
  57. return;
  58. }
  59. updateCellWidths();
  60. }
  61. public void updateCellWidths() {
  62. int count = getWidgetCount();
  63. if (count > 0) {
  64. setCellWidth(backLabel, MARGINLEFT + "px");
  65. setCellWidth(nextLabel, MARGINRIGHT + "px");
  66. setCellHorizontalAlignment(nextLabel, ALIGN_RIGHT);
  67. int cellw = width / (count - 2);
  68. if (cellw > 0) {
  69. int[] cellWidths = VCalendar
  70. .distributeSize(width, count - 2, 0);
  71. for (int i = 1; i < count - 1; i++) {
  72. Widget widget = getWidget(i);
  73. // if (remain > 0) {
  74. // setCellWidth(widget, cellw2 + "px");
  75. // remain--;
  76. // } else {
  77. // setCellWidth(widget, cellw + "px");
  78. // }
  79. setCellWidth(widget, cellWidths[i - 1] + "px");
  80. widget.setWidth(cellWidths[i - 1] + "px");
  81. }
  82. }
  83. }
  84. }
  85. public void add(String dayName, final String date,
  86. String localized_date_format, String extraClass) {
  87. Label l = new Label(dayName + " " + localized_date_format);
  88. l.setStylePrimaryName("v-calendar-header-day");
  89. if (extraClass != null) {
  90. l.addStyleDependentName(extraClass);
  91. }
  92. if (verticalSized) {
  93. l.addStyleDependentName("Vsized");
  94. }
  95. if (horizontalSized) {
  96. l.addStyleDependentName("Hsized");
  97. }
  98. l.addClickHandler(new ClickHandler() {
  99. @Override
  100. public void onClick(ClickEvent event) {
  101. if (calendar.getDateClickListener() != null) {
  102. calendar.getDateClickListener().dateClick(date);
  103. }
  104. }
  105. });
  106. add(l);
  107. }
  108. public void addBackButton() {
  109. if (!calendar.isBackwardNavigationEnabled()) {
  110. nextLabel.getElement().getStyle().setHeight(0, Unit.PX);
  111. }
  112. add(backLabel);
  113. }
  114. public void addNextButton() {
  115. if (!calendar.isForwardNavigationEnabled()) {
  116. backLabel.getElement().getStyle().setHeight(0, Unit.PX);
  117. }
  118. add(nextLabel);
  119. }
  120. @Override
  121. public void onClick(ClickEvent event) {
  122. if (!calendar.isDisabledOrReadOnly()) {
  123. if (event.getSource() == nextLabel) {
  124. if (calendar.getForwardListener() != null) {
  125. calendar.getForwardListener().forward();
  126. }
  127. } else if (event.getSource() == backLabel) {
  128. if (calendar.getBackwardListener() != null) {
  129. calendar.getBackwardListener().backward();
  130. }
  131. }
  132. }
  133. }
  134. public void setVerticalSized(boolean sized) {
  135. verticalSized = sized;
  136. updateDayLabelSizedStyleNames();
  137. }
  138. public void setHorizontalSized(boolean sized) {
  139. horizontalSized = sized;
  140. updateDayLabelSizedStyleNames();
  141. }
  142. private void updateDayLabelSizedStyleNames() {
  143. Iterator<Widget> it = iterator();
  144. while (it.hasNext()) {
  145. updateWidgetSizedStyleName(it.next());
  146. }
  147. }
  148. private void updateWidgetSizedStyleName(Widget w) {
  149. if (verticalSized) {
  150. w.addStyleDependentName("Vsized");
  151. } else {
  152. w.removeStyleDependentName("VSized");
  153. }
  154. if (horizontalSized) {
  155. w.addStyleDependentName("Hsized");
  156. } else {
  157. w.removeStyleDependentName("HSized");
  158. }
  159. }
  160. }