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

eu.future.earth.gwt.client.DateEditFieldWithPicker Maven / Gradle / Ivy

The newest version!
package eu.future.earth.gwt.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

import eu.future.earth.client.date.DateNoTimeZone;
import eu.future.earth.gwt.client.date.FtrGwtResources;
import eu.future.earth.gwt.client.date.picker.DatePickerDialog;
import eu.future.earth.gwt.client.date.picker.DateSelectEvent;
import eu.future.earth.gwt.client.date.picker.DateSelectListener;
import eu.future.earth.gwt.client.date.picker.NoneContraintAndEntryRenderer;
import eu.future.earth.gwt.client.ui.button.TouchButton;
import eu.future.earth.gwt.client.ui.button.UiType;
import eu.future.earth.gwt.client.ui.general.IdUiObject;

public class DateEditFieldWithPicker extends Composite implements ClickHandler, DateSelectListener, HasValueChangeHandlers, ValueChangeHandler, IdUiObject {

	private List toAdd = new ArrayList();

	private DateBox field = null;

	private TouchButton dateButton = new TouchButton(new Image(FtrGwtResources.IMAGES.cal()), UiType.Regular);

	private DatePickerDialog datePicker = null;

	private NoneContraintAndEntryRenderer renderer = new NoneContraintAndEntryRenderer();

	private final UiType type;

	private boolean state = true;

	private boolean stateField = true;

	public DateEditFieldWithPicker() {
		this(null, UiType.Regular);
	}

	public DateEditFieldWithPicker(String newFormat) {
		this(newFormat, UiType.Regular);
	}

	public DateEditFieldWithPicker(String format, UiType newType) {
		super();
		type = newType;
		FlowPanel main = new FlowPanel();
		main.setStyleName("ftr-date-edit-picker");
		initWidget(main);
		field = new DateBox(format);
		main.add(field);
		// super.setCellVerticalAlignment(field, HorizontalPanel.ALIGN_MIDDLE);
		// add(new HTML(" "));
		main.add(dateButton);
		// super.setCellVerticalAlignment(dateButton, HorizontalPanel.ALIGN_MIDDLE);
		dateButton.addClickHandler(this);
		field.setEnabled(true);
		field.addValueChangeHandler(this);
	}

	public DateEditFieldWithPicker(UiType newType) {
		this(null, newType);
	}

	@Override
	public void addStyleName(String style) {
		field.addStyleName(style);
	}

	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler handler) {
		return addHandler(handler, ValueChangeEvent.getType());
	}

	public void addWidgetToPicker(Widget newWidget) {
		toAdd.add(newWidget);
	}

	@Override
	public String getStyleName() {
		return field.getStyleName();
	}

	public Date getValue() {
		return field.getValue();
	}

	public DateNoTimeZone getValueNoTimeZone() {
		return DateNoTimeZone.convert(field.getValue());
	}

	@Override
	public void handleDateSelectEvent(DateSelectEvent newEvent) {
		field.setValue(newEvent.getDate());
		ValueChangeEvent.fire(this, newEvent.getDate());
	}

	@Override
	public void onClick(ClickEvent event) {
		if (datePicker == null) {
			datePicker = new DatePickerDialog(renderer, type);
			datePicker.addDateSelectEventHandler(this);
			for (Widget add : toAdd) {
				datePicker.addWidgetToPicker(add);
			}
		}

		Date theDate = field.getValue();
		if (theDate == null) {
			theDate = new Date();
		}
		datePicker.setSelectedDate(theDate);
		datePicker.showRelativeTo(dateButton);

	}

	@Override
	public void onValueChange(ValueChangeEvent event) {
		ValueChangeEvent.fire(this, event.getValue());
	}

	@Override
	public void removeStyleName(String style) {
		field.removeStyleName(style);
	}

	public void setButtonEnabled(boolean newState) {
		dateButton.setEnabled(newState);
	}

	public void setDate(Date newValue) {
		if (newValue != null) {
			field.setValue(newValue);
		} else {
			field.setValue(null);
		}
	}

	public void setDateNoTimeZone(DateNoTimeZone newValue) {
		if (newValue != null) {
			field.setValueNoTimeZone(newValue);
		} else {
			field.setValue(null);
		}
	}

	public void setEditable(boolean newState) {
		state = newState;
		if (state) {
			dateButton.setVisible(true);
		} else {
			dateButton.setVisible(false);
		}
		if (stateField) {
			field.setEnabled(newState);
		} else {
			field.setEnabled(false);
		}
	}

	public void setFieldEditable(boolean newState) {
		stateField = newState;
		field.setEnabled(stateField);
	}

	private String id = null;

	@Override
	public void setId(String newId) {
		id = newId;
		field.getElement().setId(id + ":field");
		dateButton.getElement().setId(id + ":button");
	}

	@Override
	public void setStyleName(String style) {
		field.setStyleName(style);
	}

	@Override
	public void setStylePrimaryName(String style) {
		field.setStylePrimaryName(style);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy