001: package org.drools.brms.client.common;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Date;
020:
021: import com.google.gwt.user.client.DOM;
022: import com.google.gwt.user.client.Element;
023: import com.google.gwt.user.client.ui.Button;
024: import com.google.gwt.user.client.ui.ChangeListener;
025: import com.google.gwt.user.client.ui.ChangeListenerCollection;
026: import com.google.gwt.user.client.ui.ClickListener;
027: import com.google.gwt.user.client.ui.Composite;
028: import com.google.gwt.user.client.ui.DockPanel;
029: import com.google.gwt.user.client.ui.Grid;
030: import com.google.gwt.user.client.ui.HTML;
031: import com.google.gwt.user.client.ui.HasAlignment;
032: import com.google.gwt.user.client.ui.HorizontalPanel;
033: import com.google.gwt.user.client.ui.SourcesChangeEvents;
034: import com.google.gwt.user.client.ui.Widget;
035:
036: public class DatePicker extends Composite implements ClickListener,
037: SourcesChangeEvents {
038:
039: private class NavBar extends Composite implements ClickListener {
040:
041: public final DockPanel bar = new DockPanel();
042: public final Button prevMonth = new Button("<", this );
043: public final Button prevYear = new Button("<<", this );
044: public final Button nextYear = new Button(">>", this );
045: public final Button nextMonth = new Button(">", this );
046: public final HTML title = new HTML();
047:
048: private final DatePicker calendar;
049:
050: public NavBar(DatePicker calendar) {
051: this .calendar = calendar;
052:
053: initWidget(bar);
054: bar.setStyleName("navbar");
055: title.setStyleName("header");
056:
057: HorizontalPanel prevButtons = new HorizontalPanel();
058: prevButtons.add(prevMonth);
059: prevButtons.add(prevYear);
060:
061: HorizontalPanel nextButtons = new HorizontalPanel();
062: nextButtons.add(nextYear);
063: nextButtons.add(nextMonth);
064:
065: bar.add(prevButtons, DockPanel.WEST);
066: bar.setCellHorizontalAlignment(prevButtons,
067: DockPanel.ALIGN_LEFT);
068: bar.add(nextButtons, DockPanel.EAST);
069: bar.setCellHorizontalAlignment(nextButtons,
070: DockPanel.ALIGN_RIGHT);
071: bar.add(title, DockPanel.CENTER);
072: bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
073: bar.setCellHorizontalAlignment(title,
074: HasAlignment.ALIGN_CENTER);
075: bar.setCellVerticalAlignment(title,
076: HasAlignment.ALIGN_MIDDLE);
077: bar.setCellWidth(title, "100%");
078: }
079:
080: public void onClick(Widget sender) {
081: if (sender == prevMonth) {
082: calendar.prevMonth();
083: } else if (sender == prevYear) {
084: calendar.prevYear();
085: } else if (sender == nextYear) {
086: calendar.nextYear();
087: } else if (sender == nextMonth) {
088: calendar.nextMonth();
089: }
090: }
091: }
092:
093: private static class CellHTML extends HTML {
094: private int day;
095:
096: public CellHTML(String text, int day) {
097: super (text);
098: this .day = day;
099: }
100:
101: public int getDay() {
102: return day;
103: }
104: }
105:
106: private final NavBar navbar = new NavBar(this );
107: private final DockPanel outer = new DockPanel();
108:
109: private final Grid grid = new Grid(6, 7) {
110: public boolean clearCell(int row, int column) {
111: boolean retValue = super .clearCell(row, column);
112:
113: Element td = getCellFormatter().getElement(row, column);
114: DOM.setInnerHTML(td, "");
115: return retValue;
116: }
117: };
118:
119: private Date date = new Date();
120:
121: private ChangeListenerCollection changeListeners;
122:
123: private String[] days = new String[] { "Sunday", "Monday",
124: "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
125:
126: private String[] months = new String[] { "January", "February",
127: "March", "April", "May", "June", "July", "August",
128: "September", "October", "November", "December" };
129:
130: public DatePicker() {
131: initWidget(outer);
132: grid.setStyleName("table");
133: grid.setCellSpacing(0);
134: outer.add(navbar, DockPanel.NORTH);
135: outer.add(grid, DockPanel.CENTER);
136: drawCalendar();
137: setStyleName("DatePicker");
138: }
139:
140: private void drawCalendar() {
141: int year = getYear();
142: int month = getMonth();
143: int day = getDay();
144: setHeaderText(year, month);
145: grid.getRowFormatter().setStyleName(0, "weekheader");
146: for (int i = 0; i < days.length; i++) {
147: grid.getCellFormatter().setStyleName(0, i, "days");
148: grid.setText(0, i, days[i].substring(0, 3));
149: }
150:
151: Date now = new Date();
152: int sameDay = now.getDate();
153: int today = (now.getMonth() == month && now.getYear() + 1900 == year) ? sameDay
154: : 0;
155:
156: int firstDay = new Date(year - 1900, month, 1).getDay();
157: int numOfDays = getDaysInMonth(year, month);
158:
159: int j = 0;
160: for (int i = 1; i < 6; i++) {
161: for (int k = 0; k < 7; k++, j++) {
162: int displayNum = (j - firstDay + 1);
163: if (j < firstDay || displayNum > numOfDays) {
164: grid.getCellFormatter().setStyleName(i, k, "empty");
165: grid.setHTML(i, k, " ");
166: } else {
167: HTML html = new CellHTML("<span>"
168: + String.valueOf(displayNum) + "</span>",
169: displayNum);
170: html.addClickListener(this );
171: grid.getCellFormatter().setStyleName(i, k, "cell");
172: if (displayNum == today) {
173: grid.getCellFormatter().addStyleName(i, k,
174: "today");
175: } else if (displayNum == sameDay) {
176: grid.getCellFormatter().addStyleName(i, k,
177: "day");
178: }
179: grid.setWidget(i, k, html);
180: }
181: }
182: }
183: }
184:
185: protected void setHeaderText(int year, int month) {
186: navbar.title.setText(months[month] + ", " + year);
187: }
188:
189: private int getDaysInMonth(int year, int month) {
190: switch (month) {
191: case 1:
192: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
193: return 29; // leap year
194: else
195: return 28;
196: case 3:
197: return 30;
198: case 5:
199: return 30;
200: case 8:
201: return 30;
202: case 10:
203: return 30;
204: default:
205: return 31;
206: }
207: }
208:
209: public void prevMonth() {
210: int month = getMonth() - 1;
211: if (month < 0) {
212: setDate(getYear() - 1, 11, getDay());
213: } else {
214: setMonth(month);
215: }
216: drawCalendar();
217: }
218:
219: public void nextMonth() {
220: int month = getMonth() + 1;
221: if (month > 11) {
222: setDate(getYear() + 1, 0, getDay());
223: } else {
224: setMonth(month);
225: }
226: drawCalendar();
227: }
228:
229: public void prevYear() {
230: setYear(getYear() - 1);
231: drawCalendar();
232: }
233:
234: public void nextYear() {
235: setYear(getYear() + 1);
236: drawCalendar();
237: }
238:
239: private void setDate(int year, int month, int day) {
240: date = new Date(year - 1900, month, day);
241: }
242:
243: private void setYear(int year) {
244: date.setYear(year - 1900);
245: }
246:
247: private void setMonth(int month) {
248: date.setMonth(month);
249: }
250:
251: public int getYear() {
252: return 1900 + date.getYear();
253: }
254:
255: public int getMonth() {
256: return date.getMonth();
257: }
258:
259: public int getDay() {
260: return date.getDate();
261: }
262:
263: public Date getDate() {
264: return date;
265: }
266:
267: public void onClick(Widget sender) {
268: CellHTML cell = (CellHTML) sender;
269: setDate(getYear(), getMonth(), cell.getDay());
270: drawCalendar();
271: if (changeListeners != null) {
272: changeListeners.fireChange(this );
273: }
274: }
275:
276: public void addChangeListener(ChangeListener listener) {
277: if (changeListeners == null)
278: changeListeners = new ChangeListenerCollection();
279: changeListeners.add(listener);
280: }
281:
282: public void removeChangeListener(ChangeListener listener) {
283: if (changeListeners != null)
284: changeListeners.remove(listener);
285: }
286: }
|