All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.future.earth.gwt.client.date.picker.MonthNavigator Maven / Gradle / Ivy

The newest version!
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.Label;

import eu.future.earth.gwt.client.FtrGwtDatePickerCss;
import eu.future.earth.gwt.client.date.picker.DateSelectEvent.DateSelectEventActions;
import eu.future.earth.gwt.client.ui.button.FontAwesomeButton;
import eu.future.earth.gwt.client.ui.button.UiType;

public class MonthNavigator extends Grid implements ClickHandler, HasDateSelectHandlers {

	private FontAwesomeButton prev = new FontAwesomeButton("f0d9");

	private FontAwesomeButton next = new FontAwesomeButton("f0da");

	private Label label = new Label("");

	private int year = 2015;

	private int month = Calendar.JANUARY;

	public MonthNavigator(boolean setStyle) {
		this(setStyle, UiType.Regular);
	}

	public MonthNavigator(boolean setStyle, UiType uiType) {
		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%");
		if (UiType.Regular.equals(uiType)) {
			this.setHeight("16px");
		} else {
			this.setHeight("24px");
		}

		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;

	private Calendar forFormatOnly = new GregorianCalendar();

	public String getDisplayText() {
		forFormatOnly.set(Calendar.YEAR, year);
		forFormatOnly.set(Calendar.MONTH, month);
		forFormatOnly.set(Calendar.DAY_OF_MONTH, 1);
		return formatter.format(forFormatOnly.getTime());
	}

	public void setSelectedDate(Date newDate) {
		forFormatOnly.setTime(newDate);
		year = forFormatOnly.get(Calendar.YEAR);
		month = forFormatOnly.get(Calendar.MONTH);
	}

	@Override
	public HandlerRegistration addDateSelectEventHandler(DateSelectListener handler) {
		return addHandler(handler, DateSelectEvent.getType());
	}

	public int getYear() {
		return year;
	}

	public void setYear(int newYear) {
		year = newYear;
		setText();
	}

	private void setText() {
		label.setText(getDisplayText());
	}

	public int getPeriod() {
		return month;
	}

	public void setPeriod(int newYear, int newMonth) {
		year = newYear;
		month = newMonth;
		setText();
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource() == prev) {
			if (month == Calendar.JANUARY) {
				year--;
				month = Calendar.DECEMBER;
			} else {
				month--;
			}
			setText();
			DateSelectEvent.fire(this, getSelected(), DateSelectEventActions.NAVIGATE_MONTH);
		}

		if (event.getSource() == next) {
			if (month == Calendar.JANUARY) {
				year++;
				month = Calendar.DECEMBER;
			} else {
				month++;
			}
			setText();
			DateSelectEvent.fire(this, getSelected(), DateSelectEventActions.NAVIGATE_MONTH);
		}
		if (event.getSource() == label) {
			setSelectedDate(new Date());
			DateSelectEvent.fire(this, getSelected(), DateSelectEventActions.SELECT_NEW_MONTH);
		}

	}

	public Date getSelected() {
		return forFormatOnly.getTime();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy