Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 12 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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