PageRenderTime 29ms CodeModel.GetById 1ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ftr-gwt-library.googlecode.com/
Java | 97 lines | 75 code | 22 blank | 0 comment | 7 complexity | 356f1290b71285bcb1028745e042a3a7 MD5 | raw file
 1package eu.future.earth.gwt.client.date.picker;
 2
 3import java.util.Calendar;
 4import java.util.Date;
 5import java.util.GregorianCalendar;
 6
 7import com.google.gwt.event.dom.client.ClickEvent;
 8import com.google.gwt.event.dom.client.ClickHandler;
 9import com.google.gwt.event.shared.HandlerRegistration;
10import com.google.gwt.i18n.client.DateTimeFormat;
11import com.google.gwt.user.client.ui.Grid;
12import com.google.gwt.user.client.ui.HorizontalPanel;
13import com.google.gwt.user.client.ui.Image;
14import com.google.gwt.user.client.ui.Label;
15
16import eu.future.earth.gwt.client.FtrGwtDatePickerCss;
17import eu.future.earth.gwt.client.date.FtrGwtResources;
18import eu.future.earth.gwt.client.date.picker.DateSelectEvent.DateSelectEventActions;
19
20public class MonthNavigator extends Grid implements ClickHandler, HasDateSelectHandlers {
21
22	private Image prev = new Image(FtrGwtResources.IMAGES.cal_prev());
23
24	private Image next = new Image(FtrGwtResources.IMAGES.cal_next());
25
26	private Label label = new Label("");
27
28	public MonthNavigator(boolean setStyle) {
29		super(1, 3);
30		this.setWidget(0, 0, prev);
31		this.setWidget(0, 1, label);
32		this.setWidget(0, 2, next);
33
34		this.getCellFormatter().setWidth(0, 0, "10%");
35		this.getCellFormatter().setWidth(0, 1, "80%");
36		this.getCellFormatter().setWidth(0, 2, "10%");
37
38		this.getCellFormatter().setHorizontalAlignment(0, 1, HorizontalPanel.ALIGN_CENTER);
39		this.getCellFormatter().setHorizontalAlignment(0, 2, HorizontalPanel.ALIGN_RIGHT);
40
41		this.setWidth("100%");
42		this.setHeight("18px");
43
44		next.addClickHandler(this);
45		prev.addClickHandler(this);
46		label.addClickHandler(this);
47		prev.setStyleName(FtrGwtDatePickerCss.IMAGE_BUTTON);
48		next.setStyleName(FtrGwtDatePickerCss.IMAGE_BUTTON);
49		if (setStyle) {
50			this.setStyleName(FtrGwtDatePickerCss.MONTH_NAV);
51		}
52		super.setStyleName(FtrGwtDatePickerCss.MONTH_SELECT);
53		label.setText(getDisplayText());
54	}
55
56	private DateTimeFormat formatter = DateTimeFormat.getFormat("yyyy MMMM"); // NOPMD;
57
58	public String getDisplayText() {
59		return formatter.format(current.getTime());
60	}
61
62	private Calendar current = new GregorianCalendar();
63
64	public void setSelectedDate(Date newDate) {
65		current.setTime(newDate);
66		label.setText(getDisplayText());
67	}
68
69	public HandlerRegistration addDateSelectEventHandler(DateSelectListener handler) {
70		return addHandler(handler, DateSelectEvent.getType());
71	}
72
73	public void onClick(ClickEvent event) {
74		if (event.getSource() == prev) {
75			current.add(Calendar.MONTH, -1);
76			label.setText(getDisplayText());
77			DateSelectEvent.fire(this, current.getTime(), DateSelectEventActions.NAVIGATE_MONTH);
78		}
79
80		if (event.getSource() == next) {
81			current.add(Calendar.MONTH, 1);
82			label.setText(getDisplayText());
83			DateSelectEvent.fire(this, current.getTime(), DateSelectEventActions.NAVIGATE_MONTH);
84		}
85		if (event.getSource() == label) {
86			current.setTime(new Date());
87			label.setText(getDisplayText());
88			DateSelectEvent.fire(this, current.getTime(), DateSelectEventActions.SELECT_NEW_MONTH);
89		}
90
91	}
92
93	public Date getSelected() {
94		return current.getTime();
95	}
96
97}