PageRenderTime 134ms CodeModel.GetById 17ms app.highlight 81ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://ftr-gwt-library.googlecode.com/
Java | 501 lines | 384 code | 95 blank | 22 comment | 71 complexity | d99e6e4f77c2b637c85fa759737688b7 MD5 | raw file
Possible License(s): Apache-2.0
  1/*
  2 * Copyright 2007 Future Earth, info@future-earth.eu
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package eu.future.earth.gwt.client.date;
 18
 19import java.util.ArrayList;
 20import java.util.Date;
 21import java.util.List;
 22
 23import org.cobogw.gwt.user.client.CSS;
 24import org.cobogw.gwt.user.client.ui.Button;
 25
 26import com.google.gwt.event.dom.client.ClickEvent;
 27import com.google.gwt.event.dom.client.ClickHandler;
 28import com.google.gwt.event.shared.HandlerRegistration;
 29import com.google.gwt.user.client.ui.HTML;
 30import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 31import com.google.gwt.user.client.ui.HorizontalPanel;
 32import com.google.gwt.user.client.ui.Image;
 33import com.google.gwt.user.client.ui.Label;
 34import com.google.gwt.user.client.ui.SimplePanel;
 35import com.google.gwt.user.client.ui.ToggleButton;
 36import com.google.gwt.user.client.ui.VerticalPanel;
 37import com.google.gwt.user.client.ui.Widget;
 38
 39import eu.future.earth.gwt.client.FtrGwtDateCss;
 40import eu.future.earth.gwt.client.date.list.EventListPanel;
 41import eu.future.earth.gwt.client.date.month.MonthPanel;
 42import eu.future.earth.gwt.client.date.week.WeekPanel;
 43import eu.future.earth.gwt.client.date.weeklist.WeekListPanel;
 44
 45public class MultiView<T> extends VerticalPanel implements ClickHandler, DateEventListener<T>, HasDateEventHandlers<T> {
 46
 47	private HorizontalPanel viewType = new HorizontalPanel();
 48
 49	private PanelType type = null;
 50
 51	private ToggleButton day = new ToggleButton(FtrGwtResources.TEXTS.day());
 52
 53	private WeekPanel<T> dayPanel = null;
 54
 55	private ToggleButton week = new ToggleButton(FtrGwtResources.TEXTS.week());
 56
 57	private WeekPanel<T> weekPanel = null;
 58
 59	private ToggleButton month = new ToggleButton(FtrGwtResources.TEXTS.month());
 60
 61	private MonthPanel<T> monthPanel = null;
 62
 63	private EventListPanel<T> listPanel = null;
 64
 65	private ToggleButton list = new ToggleButton(FtrGwtResources.TEXTS.list());
 66
 67	private WeekListPanel<T> weekListPanel = null;
 68
 69	private ToggleButton weekList = new ToggleButton(FtrGwtResources.TEXTS.weekList());
 70
 71	// private RoundedPanel body = new RoundedPanel(RoundedPanel.ALL, 3);
 72
 73	private SimplePanel body = new SimplePanel();
 74
 75	private DatePanel<T> currentView = null;
 76
 77	private HorizontalPanel topPanel = new HorizontalPanel();
 78
 79	private HorizontalPanel navi = new HorizontalPanel();
 80
 81	private List<ToggleButton> tabLabels = new ArrayList<ToggleButton>();
 82
 83	private Image prev = new Image(FtrGwtResources.IMAGES.btn_prev());
 84
 85	private Image next = new Image(FtrGwtResources.IMAGES.btn_next());
 86
 87	private Image prevMore = new Image(FtrGwtResources.IMAGES.btn_prev_2());
 88
 89	private Image nextMore = new Image(FtrGwtResources.IMAGES.btn_next_2());
 90
 91	private Button today = new Button(FtrGwtResources.TEXTS.today());
 92
 93	private Label dayInfo = new Label();
 94
 95	private Date centerDate = new Date();
 96
 97	// private EventController<T> controller = null;
 98
 99	private DateRenderer<T> renderer = null;
100
101	public MultiView(DateRenderer<T> newRenderer) {
102		this(newRenderer, PanelType.WEEK);
103	}
104
105	public MultiView(DateRenderer<T> newRenderer, PanelType defaultType) {
106		this(newRenderer, defaultType, false, true);
107	}
108
109	public HandlerRegistration addDateEventHandler(DateEventListener<? extends T> handler) {
110		return addHandler(handler, DateEvent.getType());
111	}
112
113	public void setNaviVisible(boolean newState) {
114		navi.setVisible(newState);
115	}
116
117	public MultiView(DateRenderer<T> newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton) {
118		super();
119		renderer = newRenderer;
120		topPanel.setSpacing(0);
121		today.addClickHandler(this);
122		// topPanel.setP
123		super.setSpacing(0);
124		super.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);
125		topPanel.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);
126		topPanel.setBorderWidth(0);
127		if (alignNaviCenter) {
128			topPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
129		}
130		this.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);
131		today.setSize(90);
132		next.addClickHandler(this);
133		CSS.setProperty(next, CSS.A.CURSOR, CSS.V.CURSOR.POINTER);
134
135		prev.addClickHandler(this);
136
137		CSS.setProperty(prev, CSS.A.CURSOR, CSS.V.CURSOR.POINTER);
138		nextMore.addClickHandler(this);
139
140		CSS.setProperty(nextMore, CSS.A.CURSOR, CSS.V.CURSOR.POINTER);
141		prevMore.addClickHandler(this);
142		CSS.setProperty(prevMore, CSS.A.CURSOR, CSS.V.CURSOR.POINTER);
143
144		// topPanel.setPixelSize();
145		if (alignNaviCenter) {
146			navi.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
147		}
148		navi.setVerticalAlignment(ALIGN_MIDDLE);
149		navi.setSpacing(0);
150
151		if (renderer.useShowMore()) {
152			navi.add(prevMore);
153			navi.setCellVerticalAlignment(prevMore, ALIGN_MIDDLE);
154			navi.add(new HTML("&nbsp;"));
155		}
156		navi.add(prev);
157		navi.setCellVerticalAlignment(prev, ALIGN_MIDDLE);
158		navi.add(new HTML("&nbsp;"));
159		navi.add(dayInfo);
160		navi.setCellVerticalAlignment(dayInfo, ALIGN_MIDDLE);
161		navi.add(new HTML("&nbsp;"));
162		navi.add(next);
163		navi.setCellVerticalAlignment(next, ALIGN_MIDDLE);
164		navi.add(new HTML("&nbsp;"));
165		if (renderer.useShowMore()) {
166			navi.add(nextMore);
167			navi.setCellVerticalAlignment(nextMore, ALIGN_MIDDLE);
168			navi.add(new HTML("&nbsp;"));
169		}
170		if (displayTodayButton) {
171			navi.add(today);
172			navi.setCellVerticalAlignment(today, ALIGN_MIDDLE);
173			navi.add(new HTML("&nbsp;"));
174		}
175		topPanel.add(navi);
176		super.setSpacing(0);
177		// super.setS
178		topPanel.add(viewType);
179		topPanel.setCellVerticalAlignment(viewType, HorizontalPanel.ALIGN_BOTTOM);
180		topPanel.setWidth("100%");
181		topPanel.setCellHorizontalAlignment(viewType, HorizontalPanel.ALIGN_RIGHT);
182		super.add(topPanel);
183		super.setCellHeight(topPanel, "15px");
184		viewType.setBorderWidth(0);
185		viewType.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);
186		// viewType.set
187		viewType.setSpacing(0);
188		int tabCount = 0;
189		if (renderer.supportDayView()) {
190			tabCount++;
191		}
192		if (renderer.supportWeekView()) {
193			tabCount++;
194		}
195
196		if (renderer.supportMonthView()) {
197			tabCount++;
198		}
199
200		if (renderer.supportListView()) {
201			tabCount++;
202		}
203
204		if (renderer.supportWeekListView()) {
205			tabCount++;
206		}
207
208		if (tabCount > 1) {
209			if (renderer.supportDayView()) {
210				addTab(day);
211			}
212			if (renderer.supportWeekView()) {
213				addTab(week);
214			}
215
216			if (renderer.supportMonthView()) {
217				addTab(month);
218			}
219
220			if (renderer.supportListView()) {
221				addTab(list);
222			}
223			if (renderer.supportWeekListView()) {
224				addTab(weekList);
225			}
226			viewType.add(new HTML("&nbsp;"));
227			viewType.add(new HTML("&nbsp;"));
228		}
229		super.add(body);
230		super.setCellVerticalAlignment(body, VerticalPanel.ALIGN_TOP);
231
232		super.add(spa);
233		spa.setHeight("10px");
234		setType(defaultType);
235	}
236
237	private HTML spa = new HTML("&nbsp;");
238
239	public DateRenderer<T> getRenderer() {
240		return renderer;
241	}
242
243	protected void onAttach() {
244		super.onAttach();
245		scrollToHour(renderer.getScrollHour());
246	}
247
248	public void addWidgetToNivigationBar(Widget newWidget) {
249		navi.add(newWidget);
250		navi.setCellVerticalAlignment(newWidget, ALIGN_MIDDLE);
251		navi.add(new HTML("&nbsp;"));
252	}
253
254	private void addTab(ToggleButton button) {
255		tabLabels.add(button);
256		button.addClickHandler(this);
257		viewType.add(button);
258	}
259
260	private int height = 300;
261
262	private int witdh = 400;
263
264	public void setPixelSize(int newWidth, int newHeight) {
265		super.setPixelSize(newWidth, newHeight);
266		height = newHeight;
267		witdh = newWidth;
268		if (currentView != null) {
269			currentView.setPixelSize(witdh, height - topPanel.getOffsetHeight() - spa.getOffsetHeight());
270		}
271	}
272
273	public int getHeight() {
274		return height;
275	}
276
277	public void rebuildPanel() {
278		if (weekPanel != null) {
279			weekPanel.buildPanel();
280		}
281		if (dayPanel != null) {
282			dayPanel.buildPanel();
283		}
284	}
285
286	public void scrollToHour(int hour) {
287		DatePanel<T> current = getCurrent();
288		if (current instanceof WeekPanel<?>) {
289			WeekPanel<T> real = (WeekPanel<T>) current;
290			real.scrollToHour(hour);
291		}
292	}
293
294	private void showCurrent(DatePanel<T> newShows) {
295		if (currentView != null) {
296			Widget real = (Widget) currentView;
297			body.remove(real);
298		}
299
300		Widget newReal = (Widget) newShows;
301
302		currentView = newShows;
303		body.setWidget(newReal);
304		newReal.setWidth("100%");
305		setPixelSize(witdh, height);
306		setDate(centerDate);
307	}
308
309	public void setSelected(ToggleButton button) {
310		for (int i = 0; i < tabLabels.size(); i++) {
311			final ToggleButton wi = tabLabels.get(i);
312			if (wi == button) {
313				wi.setDown(true);
314			} else {
315				wi.setDown(false);
316			}
317		}
318	}
319
320	public void setType(PanelType newType) {
321		if (!newType.equals(type)) {
322			type = newType;
323
324			switch (type) {
325			case MONTH: {
326				if (monthPanel == null) {
327					monthPanel = new MonthPanel<T>(renderer);
328					monthPanel.addDateEventHandler(this);
329				}
330				showCurrent(monthPanel);
331				setSelected(month);
332				break;
333			}
334			case WEEK: {
335				if (weekPanel == null) {
336					weekPanel = new WeekPanel<T>(DateUtils.countDays(renderer), renderer);
337					weekPanel.addDateEventHandler(this);
338				}
339				showCurrent(weekPanel);
340				setSelected(week);
341				break;
342			}
343			case DAY: {
344				if (dayPanel == null) {
345					dayPanel = new WeekPanel<T>(1, renderer);
346					dayPanel.addDateEventHandler(this);
347				}
348				showCurrent(dayPanel);
349				setSelected(day);
350				break;
351			}
352			case LIST: {
353				if (listPanel == null) {
354					listPanel = new EventListPanel<T>(renderer);
355					listPanel.addDateEventHandler(this);
356				}
357				showCurrent(listPanel);
358				setSelected(list);
359				break;
360			}
361			case WEEK_LIST: {
362				if (weekListPanel == null) {
363					weekListPanel = new WeekListPanel<T>(renderer);
364					weekListPanel.addDateEventHandler(this);
365				}
366				showCurrent(weekListPanel);
367				setSelected(weekList);
368				break;
369			}
370			}
371
372			scrollToHour(renderer.getScrollHour());
373		}
374	}
375
376	public void setDate(Date newCenterDate) {
377		setDate(newCenterDate, true);
378	}
379
380	public void setDate(Date newCenterDate, boolean setDatapanelDate) {
381		setDate(newCenterDate, setDatapanelDate, true);
382	}
383
384	// add a reloadData argument so that the controller can handle if it uses cached data or make a RPC call.
385	public void setDate(Date newCenterDate, boolean setDatapanelDate, boolean reloadData) {
386		centerDate = newCenterDate;
387		if (setDatapanelDate) {
388			currentView.setDate(centerDate);
389		}
390		currentView.loadData();
391		dayInfo.setText(renderer.getTitleDisplayText(currentView));
392	}
393
394	public void clearData() {
395		currentView.clearData();
396	}
397
398	public void setEvents(T[] events) {
399		currentView.clearData();
400		addEvents(events);
401	}
402
403	public void addEvents(T[] events) {
404		if (events != null) {
405			for (int i = 0; i < events.length; i++) {
406				currentView.addEventData(events[i]);
407			}
408		}
409
410	}
411
412	public void setEventsByList(List<? extends T> events) {
413		currentView.clearData();
414		addEventsList(events);
415	}
416
417	public void addEventsList(List<? extends T> events) {
418		if (events != null) {
419			currentView.addEventsByList(events);
420		}
421
422	}
423
424	public void setEventsByArrayList(List<T> events) {
425		currentView.clearData();
426		currentView.addEventsByList(events);
427	}
428
429	public void reloadData() {
430		setDate(centerDate, true, true);
431	}
432
433	public void repaintData() {
434		setDate(centerDate, true, false);
435	}
436
437	public DatePanel<T> getCurrent() {
438		return currentView;
439	}
440
441	public void handleDateEvent(DateEvent<T> newEvent) {
442		switch (newEvent.getCommand()) {
443		case GO_TO_DAY_VIEW:
444			if (renderer.supportDayView()) {
445				setType(PanelType.DAY);
446			} else {
447				if (renderer.supportWeekView()) {
448					setType(PanelType.WEEK);
449				}
450			}
451			break;
452
453		default:
454			break;
455		}
456		DateEvent.fire(this, newEvent);
457	}
458
459	public void onClick(ClickEvent event) {
460		if (event.getSource() == day) {
461			setType(PanelType.DAY);
462		}
463
464		if (event.getSource() == week) {
465			setType(PanelType.WEEK);
466		}
467
468		if (event.getSource() == month) {
469			setType(PanelType.MONTH);
470		}
471
472		if (event.getSource() == list) {
473			setType(PanelType.LIST);
474		}
475
476		if (event.getSource() == weekList) {
477			setType(PanelType.WEEK_LIST);
478		}
479
480		if (event.getSource() == prev) {
481			setDate(currentView.prev(), false);
482		}
483
484		if (event.getSource() == next) {
485			setDate(currentView.next(), false);
486		}
487
488		if (event.getSource() == prevMore) {
489			setDate(currentView.prevMore(), false);
490		}
491
492		if (event.getSource() == nextMore) {
493			setDate(currentView.nextMore(), false);
494		}
495
496		if (event.getSource() == today) {
497			setDate(new Date());
498		}
499
500	}
501}