eu.future.earth.gwt.client.date.picker.DatePickerMonthPanel Maven / Gradle / Ivy
/*
* Copyright 2007 Future Earth, [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package eu.future.earth.gwt.client.date.picker;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import eu.future.earth.gwt.client.FtrGwtDatePickerCss;
import eu.future.earth.gwt.client.date.DateUtils;
public class DatePickerMonthPanel extends Composite implements DateSelectListener, HasDateSelectHandlers {
private Calendar current = new GregorianCalendar();
private Calendar helperCal = new GregorianCalendar(); // NOPMD;
private final static int ROWS = 7;
private Grid monthPanel = new Grid(ROWS, 7);
private int firstDayOfWeek = Calendar.MONDAY; // NOPMD;
private DatePickerRenderer renderer = null;
public DatePickerMonthPanel(DatePickerRenderer newRenderer) {
super();
this.initWidget(monthPanel);
setRenderer(newRenderer);
setStyleName(FtrGwtDatePickerCss.MONTH);
monthPanel.setStyleName(FtrGwtDatePickerCss.MONTH);
}
public void setRenderer(DatePickerRenderer newRenderer) {
renderer = newRenderer;
buildPanel();
}
public void setSelectedDate(Date newDate) {
setDate(newDate);
for (int y = 1; y < ROWS; y++) {
for (int x = 0; x < 7; x++) {
Widget got = monthPanel.getWidget(y, x);
if (got instanceof DatePickerMonthPanelDayPanel) {
final DatePickerMonthPanelDayPanel result = (DatePickerMonthPanelDayPanel) got;
if (result.isDay(newDate)) {
result.setSelected(true);
} else {
result.setSelected(false);
}
}
}
}
}
public void setDate(Date newDate) {
final Calendar test = new GregorianCalendar();
test.setTime(newDate);
if (DateUtils.isSameDay(test, current)) {
return;
}
current.setTime(newDate);
buildPanel();
}
public Date getCurrent() {
return current.getTime();
}
private int xPos = 0; // NOPMD;
private int yPos = 0; // NOPMD;
private Date first = null;
private Date latest = null;
public void buildPanel() {
monthPanel.clear();
monthPanel.getElement().getStyle().setWidth(100, Unit.PCT);
monthPanel.setCellPadding(1);
monthPanel.setCellSpacing(1);
helperCal.setFirstDayOfWeek(firstDayOfWeek);
xPos = 0;
yPos = 0;
final Calendar today = new GregorianCalendar();
final DateTimeFormat dayFormatter = DateTimeFormat.getFormat("E");
for (int i = firstDayOfWeek; i < 7 + firstDayOfWeek; i++) {
helperCal.set(Calendar.DAY_OF_WEEK, i);
monthPanel.setWidget(yPos, xPos, new HTML(dayFormatter.format(helperCal.getTime()).substring(0, 1)));
monthPanel.getCellFormatter().setHorizontalAlignment(yPos, xPos, HorizontalPanel.ALIGN_CENTER);
monthPanel.getCellFormatter().setHeight(yPos, xPos, "9%");
monthPanel.getColumnFormatter().setWidth(xPos, "12%");
xPos++;
}
yPos++;
helperCal.setTime(current.getTime());
final Calendar prevCalendar = (Calendar) helperCal.clone();
prevCalendar.set(Calendar.DAY_OF_MONTH, 1);
prevCalendar.setFirstDayOfWeek(firstDayOfWeek);
final Calendar nextCalendar = (Calendar) helperCal.clone();
nextCalendar.setFirstDayOfWeek(firstDayOfWeek);
nextCalendar.add(Calendar.MONTH, 1);
nextCalendar.set(Calendar.DAY_OF_MONTH, 1);
helperCal.add(Calendar.DAY_OF_MONTH, -(helperCal.get(Calendar.DAY_OF_MONTH)));
while (helperCal.get(Calendar.DAY_OF_WEEK) != helperCal.getFirstDayOfWeek()) {
helperCal.add(Calendar.DAY_OF_MONTH, -1);
}
first = helperCal.getTime();
final int thisMonth = current.get(Calendar.MONTH);
for (int y = 1; y < ROWS; y++) {
for (int x = 0; x < 7; x++) {
monthPanel.getCellFormatter().setHeight(y, x, "13%");
monthPanel.getCellFormatter().setVerticalAlignment(y, x, HorizontalPanel.ALIGN_MIDDLE);
final DatePickerMonthPanelDayPanel result = makeJButton(prevCalendar, helperCal, nextCalendar);
if (DateUtils.isSameDay(today, helperCal)) {
result.setToday(true);
}
if (thisMonth != helperCal.get(Calendar.MONTH)) {
result.setOtherMonth(true);
}
if (helperCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || helperCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
result.setWeekend(true);
}
if (DateUtils.isSameDay(helperCal, current)) {
result.setSelected(true);
} else {
result.setSelected(false);
}
monthPanel.setWidget(y, x, result);
helperCal.add(Calendar.DAY_OF_MONTH, 1);
}
}
latest = helperCal.getTime();
}
private DatePickerMonthPanelDayPanel makeJButton(Calendar prevCalender, Calendar calender, Calendar nextCalender) {
final DatePickerMonthPanelDayPanel result = new DatePickerMonthPanelDayPanel(calender.get(Calendar.DAY_OF_MONTH), calender.getTime(), renderer);
result.addDateSelectEventHandler(this);
return result;
}
public void handleDateSelectEvent(DateSelectEvent newEvent) {
switch (newEvent.getCommand()) {
case SELECT_DAY: {
setSelectedDate(newEvent.getDate());
break;
}
}
DateSelectEvent.fire(this, newEvent.getDate(), newEvent.getCommand());
}
public HandlerRegistration addDateSelectEventHandler(DateSelectListener handler) {
return addHandler(handler, DateSelectEvent.getType());
}
public DatePickerRenderer getRenderer() {
return renderer;
}
public Date next() {
current.add(Calendar.MONTH, 1);
buildPanel();
return current.getTime();
}
public Date prev() {
current.add(Calendar.MONTH, -1);
buildPanel();
return current.getTime();
}
private DateTimeFormat formatter = DateTimeFormat.getFormat("MMMM yyyy"); // NOPMD;
public String getDisplayText() {
return formatter.format(current.getTime());
}
public Date getFirstDateShow() {
return first;
}
public Date getLastDateShow() {
return latest;
}
public void setHeight(int newHeight) {
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy