Subversion Repositories eFlore/Archives.cel-v1

Rev

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