Subversion Repositories eFlore/Archives.cel-v1

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 ddelon 1
package org.tela_botanica.client;
2
import java.util.Date;
3
 
4
import com.google.gwt.user.client.DOM;
5
import com.google.gwt.user.client.Element;
6
import com.google.gwt.user.client.ui.Button;
7
import com.google.gwt.user.client.ui.ChangeListener;
8
import com.google.gwt.user.client.ui.ChangeListenerCollection;
9
import com.google.gwt.user.client.ui.ClickListener;
10
import com.google.gwt.user.client.ui.Composite;
11
import com.google.gwt.user.client.ui.DockPanel;
12
import com.google.gwt.user.client.ui.Grid;
13
import com.google.gwt.user.client.ui.HTML;
14
import com.google.gwt.user.client.ui.HasAlignment;
15
import com.google.gwt.user.client.ui.HorizontalPanel;
16
import com.google.gwt.user.client.ui.SourcesChangeEvents;
17
import com.google.gwt.user.client.ui.Widget;
18
 
19
public class CalendarWidget extends Composite
20
    implements ClickListener, SourcesChangeEvents {
21
 
22
  private class NavBar extends Composite implements ClickListener {
23
 
24
    public final DockPanel bar = new DockPanel();
25
    public final Button prevMonth = new Button("<", this);
26
    public final Button prevYear = new Button("<<", this);
27
    public final Button nextYear = new Button(">>", this);
28
    public final Button nextMonth = new Button(">", this);
29
    public final HTML title = new HTML();
30
 
31
    private final CalendarWidget calendar;
32
 
33
    public NavBar(CalendarWidget calendar) {
34
      this.calendar = calendar;
35
 
36
      setWidget(bar);
37
      bar.setStyleName("navbar");
38
      title.setStyleName("header");
39
 
40
      HorizontalPanel prevButtons = new HorizontalPanel();
41
      prevButtons.add(prevYear);
42
      prevButtons.add(prevMonth);
43
 
44
      HorizontalPanel nextButtons = new HorizontalPanel();
45
      nextButtons.add(nextMonth);
46
      nextButtons.add(nextYear);
47
 
48
      bar.add(prevButtons, DockPanel.WEST);
49
      bar.setCellHorizontalAlignment(prevButtons, DockPanel.ALIGN_LEFT);
50
      bar.add(nextButtons, DockPanel.EAST);
51
      bar.setCellHorizontalAlignment(nextButtons, DockPanel.ALIGN_RIGHT);
52
      bar.add(title, DockPanel.CENTER);
53
      bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
54
      bar.setCellHorizontalAlignment(title, HasAlignment.ALIGN_CENTER);
55
      bar.setCellVerticalAlignment(title, HasAlignment.ALIGN_MIDDLE);
56
      bar.setCellWidth(title, "100%");
57
    }
58
 
59
    public void onClick(Widget sender) {
60
      if (sender == prevMonth) {
61
        calendar.prevMonth();
62
      } else if (sender == prevYear) {
63
        calendar.prevYear();
64
      } else if (sender == nextYear) {
65
        calendar.nextYear();
66
      } else if (sender == nextMonth) {
67
        calendar.nextMonth();
68
      }
69
    }
70
  }
71
 
72
  private static class CellHTML extends HTML {
73
    private int day;
74
 
75
    public CellHTML(String text, int day) {
76
      super(text);
77
      this.day = day;
78
    }
79
 
80
    public int getDay() {
81
      return day;
82
    }
83
  }
84
 
85
  private final NavBar navbar = new NavBar(this);
86
  private final DockPanel outer = new DockPanel();
87
 
88
  private final Grid grid = new Grid(6, 7) {
89
    public boolean clearCell(int row, int column) {
90
      boolean retValue = super.clearCell(row, column);
91
 
92
      Element td = getCellFormatter().getElement(row, column);
93
      DOM.setInnerHTML(td, "");
94
      return retValue;
95
    }
96
  };
97
 
98
  private Date date = new Date();
99
 
100
  private ChangeListenerCollection changeListeners;
101
 
102
  private String[] days = new String[] { "Dimanche", "Lundi", "Mardi",
103
      "Mercredi", "Jeudi", "Vendredi", "Samedi" };
104
 
105
  private String[] months = new String[] { "Janvier", "Fevrier", "Mars",
106
      "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre",
107
      "Novembre", "Decembre" };
108
 
109
  public CalendarWidget() {
110
    setWidget(outer);
111
    grid.setStyleName("table");
112
    grid.setCellSpacing(0);
113
    outer.add(navbar, DockPanel.NORTH);
114
    outer.add(grid, DockPanel.CENTER);
115
    drawCalendar();
116
    setStyleName("CalendarWidget");
117
  }
118
 
119
  private void drawCalendar() {
120
    int year = getYear();
121
    int month = getMonth();
122
    int day = getDay();
123
    setHeaderText(year, month);
124
    grid.getRowFormatter().setStyleName(0, "weekheader");
125
    for (int i = 0; i < days.length; i++) {
126
      grid.getCellFormatter().setStyleName(0, i, "days");
127
      grid.setText(0, i, days[i].substring(0, 3));
128
    }
129
 
130
    Date now = new Date();
131
    int sameDay = now.getDate();
132
    int today = (now.getMonth() == month && now.getYear()+1900 == year) ? sameDay : 0;
133
 
134
    int firstDay = new Date(year - 1900, month, 1).getDay();
135
    int numOfDays = getDaysInMonth(year, month);
136
 
137
    int j = 0;
138
    for (int i = 1; i < 6; i++) {
139
      for (int k = 0; k < 7; k++, j++) {
140
        int displayNum = (j - firstDay + 1);
141
        if (j < firstDay || displayNum > numOfDays) {
142
          grid.getCellFormatter().setStyleName(i, k, "empty");
143
          grid.setHTML(i, k, "&nbsp;");
144
        } else {
145
          HTML html = new CellHTML(
146
            "<span>" + String.valueOf(displayNum) + "</span>",
147
            displayNum);
148
          html.addClickListener(this);
149
          grid.getCellFormatter().setStyleName(i, k, "cell");
150
          if (displayNum == today) {
151
            grid.getCellFormatter().addStyleName(i, k, "today");
152
          } else if (displayNum == sameDay) {
153
            grid.getCellFormatter().addStyleName(i, k, "day");
154
          }
155
          grid.setWidget(i, k, html);
156
        }
157
      }
158
    }
159
  }
160
 
161
  protected void setHeaderText(int year, int month) {
162
    navbar.title.setText(months[month] + ", " + year);
163
  }
164
 
165
  private int getDaysInMonth(int year, int month) {
166
    switch (month) {
167
      case 1:
168
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
169
          return 29; // leap year
170
        else
171
          return 28;
172
      case 3:
173
        return 30;
174
      case 5:
175
        return 30;
176
      case 8:
177
        return 30;
178
      case 10:
179
        return 30;
180
      default:
181
        return 31;
182
    }
183
  }
184
 
185
  public void prevMonth() {
186
    int month = getMonth() - 1;
187
    if (month < 0) {
188
      setDate(getYear() - 1, 11, getDay());
189
    } else {
190
      setMonth(month);
191
    }
192
    drawCalendar();
193
  }
194
 
195
  public void nextMonth() {
196
    int month = getMonth() + 1;
197
    if (month > 11) {
198
      setDate(getYear() + 1, 0, getDay());
199
    } else {
200
      setMonth(month);
201
    }
202
    drawCalendar();
203
  }
204
 
205
  public void prevYear() {
206
    setYear(getYear() - 1);
207
    drawCalendar();
208
  }
209
 
210
  public void nextYear() {
211
    setYear(getYear() + 1);
212
    drawCalendar();
213
  }
214
 
215
  private void setDate(int year, int month, int day) {
216
    date = new Date(year - 1900, month, day);
217
  }
218
 
219
  private void setYear(int year) {
220
    date.setYear(year - 1900);
221
  }
222
 
223
  private void setMonth(int month) {
224
    date.setMonth(month);
225
  }
226
 
227
  public int getYear() {
228
    return 1900 + date.getYear();
229
  }
230
 
231
  public int getMonth() {
232
    return date.getMonth();
233
  }
234
 
235
  public int getDay() {
236
    return date.getDate();
237
  }
238
 
239
  public Date getDate() {
240
    return date;
241
  }
242
 
243
  public void onClick(Widget sender) {
244
    CellHTML cell = (CellHTML)sender;
245
    setDate(getYear(), getMonth(), cell.getDay());
246
    drawCalendar();
247
    if (changeListeners != null) {
248
      changeListeners.fireChange(this);
249
    }
250
  }
251
 
252
  public void addChangeListener(ChangeListener listener) {
253
    if (changeListeners == null)
254
      changeListeners = new ChangeListenerCollection();
255
    changeListeners.add(listener);
256
  }
257
 
258
  public void removeChangeListener(ChangeListener listener) {
259
    if (changeListeners != null)
260
      changeListeners.remove(listener);
261
  }
262
}