eu.future.earth.gwt.client.date.picker.MonthNavigator Maven / Gradle / Ivy
package eu.future.earth.gwt.client.date.picker;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import eu.future.earth.gwt.client.FtrGwtDatePickerCss;
import eu.future.earth.gwt.client.date.FtrGwtResources;
import eu.future.earth.gwt.client.date.picker.DateSelectEvent.DateSelectEventActions;
public class MonthNavigator extends Grid implements ClickHandler, HasDateSelectHandlers {
private Image prev = new Image(FtrGwtResources.IMAGES.cal_prev());
private Image next = new Image(FtrGwtResources.IMAGES.cal_next());
private Label label = new Label("");
public MonthNavigator(boolean setStyle) {
super(1, 3);
this.setWidget(0, 0, prev);
this.setWidget(0, 1, label);
this.setWidget(0, 2, next);
this.getCellFormatter().setWidth(0, 0, "10%");
this.getCellFormatter().setWidth(0, 1, "80%");
this.getCellFormatter().setWidth(0, 2, "10%");
this.getCellFormatter().setHorizontalAlignment(0, 1, HorizontalPanel.ALIGN_CENTER);
this.getCellFormatter().setHorizontalAlignment(0, 2, HorizontalPanel.ALIGN_RIGHT);
this.setWidth("100%");
this.setHeight("18px");
next.addClickHandler(this);
prev.addClickHandler(this);
label.addClickHandler(this);
prev.setStyleName(FtrGwtDatePickerCss.IMAGE_BUTTON);
next.setStyleName(FtrGwtDatePickerCss.IMAGE_BUTTON);
if (setStyle) {
this.setStyleName(FtrGwtDatePickerCss.MONTH_NAV);
}
super.setStyleName(FtrGwtDatePickerCss.MONTH_SELECT);
label.setText(getDisplayText());
}
private DateTimeFormat formatter = DateTimeFormat.getFormat("yyyy MMMM"); // NOPMD;
public String getDisplayText() {
return formatter.format(current.getTime());
}
private Calendar current = new GregorianCalendar();
public void setSelectedDate(Date newDate) {
current.setTime(newDate);
label.setText(getDisplayText());
}
public HandlerRegistration addDateSelectEventHandler(DateSelectListener handler) {
return addHandler(handler, DateSelectEvent.getType());
}
public void onClick(ClickEvent event) {
if (event.getSource() == prev) {
current.add(Calendar.MONTH, -1);
label.setText(getDisplayText());
DateSelectEvent.fire(this, current.getTime(), DateSelectEventActions.NAVIGATE_MONTH);
}
if (event.getSource() == next) {
current.add(Calendar.MONTH, 1);
label.setText(getDisplayText());
DateSelectEvent.fire(this, current.getTime(), DateSelectEventActions.NAVIGATE_MONTH);
}
if (event.getSource() == label) {
current.setTime(new Date());
label.setText(getDisplayText());
DateSelectEvent.fire(this, current.getTime(), DateSelectEventActions.SELECT_NEW_MONTH);
}
}
public Date getSelected() {
return current.getTime();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy