PageRenderTime 41ms CodeModel.GetById 11ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/ftr-gwt-library-date/src/main/java/eu/future/earth/gwt/client/date/picker/DatePickerMonthPanel.java

http://ftr-gwt-library.googlecode.com/
Java | 221 lines | 165 code | 40 blank | 16 comment | 21 complexity | 33fcea2f3d7175fb4bcb73ac526217f0 MD5 | raw file
  1/*
  2 * Copyright 2007 Future Earth, info@future-earth.eu
  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
 17package eu.future.earth.gwt.client.date.picker;
 18
 19import java.util.Calendar;
 20import java.util.Date;
 21import java.util.GregorianCalendar;
 22
 23import com.google.gwt.event.shared.HandlerRegistration;
 24import com.google.gwt.i18n.client.DateTimeFormat;
 25import com.google.gwt.user.client.DOM;
 26import com.google.gwt.user.client.ui.Composite;
 27import com.google.gwt.user.client.ui.Grid;
 28import com.google.gwt.user.client.ui.HTML;
 29import com.google.gwt.user.client.ui.HorizontalPanel;
 30import com.google.gwt.user.client.ui.Widget;
 31
 32import eu.future.earth.gwt.client.FtrGwtDatePickerCss;
 33import eu.future.earth.gwt.client.date.DateUtils;
 34
 35public class DatePickerMonthPanel extends Composite implements DateSelectListener, HasDateSelectHandlers {
 36
 37	private Calendar current = new GregorianCalendar();
 38
 39	private Calendar helperCal = new GregorianCalendar(); // NOPMD;
 40
 41	private Grid monthPanel = new Grid(8, 7);
 42
 43	private int firstDayOfWeek = Calendar.MONDAY; // NOPMD;
 44
 45	private DatePickerRenderer renderer = null;
 46
 47	public DatePickerMonthPanel(DatePickerRenderer newRenderer) {
 48		super();
 49		this.initWidget(monthPanel);
 50		setRenderer(newRenderer);
 51		setStyleName(FtrGwtDatePickerCss.MONTH);
 52		monthPanel.setStyleName(FtrGwtDatePickerCss.MONTH);
 53	}
 54
 55	public void setRenderer(DatePickerRenderer newRenderer) {
 56		renderer = newRenderer;
 57		buildPanel();
 58	}
 59
 60	public void setSelectedDate(Date newDate) {
 61		setDate(newDate);
 62		for (int y = 1; y < 8; y++) {
 63			for (int x = 0; x < 7; x++) {
 64				Widget got = monthPanel.getWidget(y, x);
 65				if (got instanceof DatePickerMonthPanelDayPanel) {
 66					final DatePickerMonthPanelDayPanel result = (DatePickerMonthPanelDayPanel) got;
 67					if (result.isDay(newDate)) {
 68						result.setSelected(true);
 69					} else {
 70						result.setSelected(false);
 71					}
 72				}
 73			}
 74		}
 75
 76	}
 77
 78	public void setDate(Date newDate) {
 79		final Calendar test = new GregorianCalendar();
 80		test.setTime(newDate);
 81		if (DateUtils.isSameDay(test, current)) {
 82			return;
 83		}
 84
 85		current.setTime(newDate);
 86		buildPanel();
 87	}
 88
 89	public Date getCurrent() {
 90		return current.getTime();
 91	}
 92
 93	private int xPos = 0; // NOPMD;
 94
 95	private int yPos = 0; // NOPMD;
 96
 97	private Date first = null;
 98
 99	private Date latest = null;
100
101	private void buildPanel() {
102		monthPanel.clear();
103		DOM.setStyleAttribute(monthPanel.getElement(), "width", "100%");
104		DOM.setStyleAttribute(monthPanel.getElement(), "height", "100%");
105		monthPanel.setCellPadding(1);
106		monthPanel.setCellSpacing(1);
107		helperCal.setFirstDayOfWeek(firstDayOfWeek);
108		xPos = 0;
109		yPos = 0;
110		final Calendar today = new GregorianCalendar();
111		final DateTimeFormat dayFormatter = DateTimeFormat.getFormat("E");
112		for (int i = firstDayOfWeek; i < 7 + firstDayOfWeek; i++) {
113			helperCal.set(Calendar.DAY_OF_WEEK, i);
114			monthPanel.setWidget(yPos, xPos, new HTML(dayFormatter.format(helperCal.getTime()).substring(0, 1)));
115			monthPanel.getCellFormatter().setHorizontalAlignment(yPos, xPos, HorizontalPanel.ALIGN_CENTER);
116			monthPanel.getCellFormatter().setHeight(yPos, xPos, "9%");
117			monthPanel.getColumnFormatter().setWidth(xPos, "12%");
118			xPos++;
119		}
120		yPos++;
121
122		helperCal.setTime(current.getTime());
123		final Calendar prevCalendar = (Calendar) helperCal.clone();
124		prevCalendar.set(Calendar.DAY_OF_MONTH, 1);
125		prevCalendar.setFirstDayOfWeek(firstDayOfWeek);
126		final Calendar nextCalendar = (Calendar) helperCal.clone();
127		nextCalendar.setFirstDayOfWeek(firstDayOfWeek);
128		nextCalendar.add(Calendar.MONTH, 1);
129		nextCalendar.set(Calendar.DAY_OF_MONTH, 1);
130		helperCal.add(Calendar.DAY_OF_MONTH, -(helperCal.get(Calendar.DAY_OF_MONTH)));
131		while (helperCal.get(Calendar.DAY_OF_WEEK) != helperCal.getFirstDayOfWeek()) {
132			helperCal.add(Calendar.DAY_OF_MONTH, -1);
133		}
134		first = helperCal.getTime();
135		final int thisMonth = current.get(Calendar.MONTH);
136		for (int y = 1; y < 8; y++) {
137			for (int x = 0; x < 7; x++) {
138				monthPanel.getCellFormatter().setHeight(y, x, "13%");
139				monthPanel.getCellFormatter().setVerticalAlignment(y, x, HorizontalPanel.ALIGN_MIDDLE);
140				final DatePickerMonthPanelDayPanel result = makeJButton(prevCalendar, helperCal, nextCalendar);
141				if (DateUtils.isSameDay(today, helperCal)) {
142					result.setToday(true);
143				}
144				if (thisMonth != helperCal.get(Calendar.MONTH)) {
145					result.setOtherMonth(true);
146				}
147				if (helperCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || helperCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
148					result.setWeekend(true);
149				}
150				if (DateUtils.isSameDay(helperCal, current)) {
151					result.setSelected(true);
152				} else {
153					result.setSelected(false);
154				}
155
156				monthPanel.setWidget(y, x, result);
157				helperCal.add(Calendar.DAY_OF_MONTH, 1);
158			}
159		}
160		latest = helperCal.getTime();
161	}
162
163	private DatePickerMonthPanelDayPanel makeJButton(Calendar prevCalender, Calendar calender, Calendar nextCalender) {
164		final DatePickerMonthPanelDayPanel result = new DatePickerMonthPanelDayPanel(calender.get(Calendar.DAY_OF_MONTH), calender.getTime(), renderer);
165		result.addDateSelectEventHandler(this);
166		return result;
167	}
168
169	public void handleDateSelectEvent(DateSelectEvent newEvent) {
170		switch (newEvent.getCommand()) {
171
172		case SELECT_DAY: {
173			setSelectedDate(newEvent.getDate());
174			break;
175		}
176		}
177
178		DateSelectEvent.fire(this, newEvent.getDate(), newEvent.getCommand());
179
180	}
181
182	public HandlerRegistration addDateSelectEventHandler(DateSelectListener handler) {
183		return addHandler(handler, DateSelectEvent.getType());
184	}
185
186	public DatePickerRenderer getRenderer() {
187		return renderer;
188	}
189
190	public Date next() {
191		current.add(Calendar.MONTH, 1);
192		buildPanel();
193		return current.getTime();
194	}
195
196	public Date prev() {
197		current.add(Calendar.MONTH, -1);
198		buildPanel();
199		return current.getTime();
200	}
201
202	private DateTimeFormat formatter = DateTimeFormat.getFormat("MMMM yyyy"); // NOPMD;
203
204	public String getDisplayText() {
205		return formatter.format(current.getTime());
206	}
207
208	public Date getFirstDateShow() {
209		return first;
210	}
211
212	public Date getLastDateShow() {
213		return latest;
214	}
215
216	public void setHeight(int newHeight) {
217		// TODO Auto-generated method stub
218
219	}
220
221}