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

eu.future.earth.gwt.client.date.MultiView Maven / Gradle / Ivy

There is a newer version: 3.3
Show newest version
/*
 * Copyright 2007 Future Earth, [email protected]
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package eu.future.earth.gwt.client.date;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
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.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
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 com.google.gwt.user.client.ui.ProvidesResize;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import eu.future.earth.gwt.client.FtrGwtDateCss;
import eu.future.earth.gwt.client.date.list.EventListPanel;
import eu.future.earth.gwt.client.date.month.MonthPanel;
import eu.future.earth.gwt.client.date.week.BaseWeekPanel;
import eu.future.earth.gwt.client.date.week.WeekPanel;
import eu.future.earth.gwt.client.date.week.list.WeekListPanel;
import eu.future.earth.gwt.client.ui.ImageResources;
import eu.future.earth.gwt.client.ui.button.TouchButton;
import eu.future.earth.gwt.client.ui.button.TouchSelectButton;
import eu.future.earth.gwt.client.ui.button.TouchToggleButton;
import eu.future.earth.gwt.client.ui.button.UiType;
import eu.future.earth.gwt.client.ui.popup.PopupRenderer;

public class MultiView extends ResizeComposite implements RequiresResize, ProvidesResize, ClickHandler, DateEventListener, HasDateEventHandlers {

	private FlowPanel viewType = new FlowPanel();

	private PanelFactory panelFactory = null;

	private Map, DatePanel> panelCache = new HashMap, DatePanel>();

	private Map> buttonFactoryMap = new HashMap>();

	private Map typeMap = new HashMap();

	private List tabLabels = new ArrayList();

	private SimpleLayoutPanel body = new SimpleLayoutPanel();

	private TouchSelectButton popup = null;

	private DatePanel currentView = null;

	private HorizontalPanel topPanel = new HorizontalPanel();

	public HorizontalPanel getTopPanel() {
		return topPanel;
	}

	public enum PanelSelectType {
		Buttons, Popup
	}

	private HorizontalPanel navi = new HorizontalPanel();

	private TouchButton prev = null;

	private TouchButton next = null;

	private TouchButton prevMore = null;

	private TouchButton nextMore = null;

	private TouchButton today = null;

	private Label dayLabel = new Label();

	private Date centerDate = new Date();

	// private EventController controller = null;

	private DateRenderer renderer = null;

	private final PanelSelectType selectType;

	public HandlerRegistration addDateEventHandler(DateEventListener handler) {
		return addHandler(handler, DateEvent.getType());
	}

	public void setNaviVisible(boolean newState) {
		navi.setVisible(newState);
	}

	public MultiView(DateRenderer newRenderer) {
		this(newRenderer, PanelType.WEEK, UiType.Regular);
	}

	public MultiView(DateRenderer newRenderer, UiType type) {
		this(newRenderer, PanelType.WEEK, type);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType) {
		this(newRenderer, defaultType, false, true, UiType.Regular);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, UiType type) {
		this(newRenderer, defaultType, false, true, type);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, true, UiType.Regular);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, UiType type) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, true, type);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, boolean showNavigation) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, showNavigation, UiType.Regular);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, boolean showNavigation, UiType type) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, showNavigation, type, true);
	}

	// Wiht popop type
	public MultiView(DateRenderer newRenderer, PanelSelectType newSelectType) {
		this(newRenderer, PanelType.WEEK, UiType.Regular, newSelectType);
	}

	public MultiView(DateRenderer newRenderer, UiType type, PanelSelectType newSelectType) {
		this(newRenderer, PanelType.WEEK, type, newSelectType);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, PanelSelectType newSelectType) {
		this(newRenderer, defaultType, false, true, UiType.Regular, newSelectType);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, UiType type, PanelSelectType newSelectType) {
		this(newRenderer, defaultType, false, true, type, newSelectType);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, PanelSelectType newSelectType) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, true, UiType.Regular, newSelectType);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, UiType type, PanelSelectType newSelectType) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, true, type, newSelectType);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, boolean showNavigation, PanelSelectType newSelectType) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, showNavigation, UiType.Regular, newSelectType);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, boolean showNavigation, UiType type, PanelSelectType newSelectType) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, showNavigation, type, true, newSelectType);
	}

	private final boolean showTopBar;

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, boolean showNavigation, UiType type, boolean newShowTopBar) {
		this(newRenderer, defaultType, alignNaviCenter, displayTodayButton, showNavigation, type, newShowTopBar, PanelSelectType.Buttons);
	}

	public MultiView(DateRenderer newRenderer, PanelType defaultType, boolean alignNaviCenter, boolean displayTodayButton, boolean showNavigation, UiType type, boolean newShowTopBar, PanelSelectType newSelectType) {
		super();
		selectType = newSelectType;
		showTopBar = newShowTopBar;
		if (PanelSelectType.Popup.equals(selectType)) {
			popup = new TouchSelectButton("----", new PopupRenderer() {

				@Override
				public String getId(PanelType newRow) {
					return String.valueOf(newRow);
				}

				@Override
				public String getLabel(PanelType item) {
					return getText(item);
				}
			}, type);

			popup.addValueChangeHandler(new ValueChangeHandler() {

				@Override
				public void onValueChange(ValueChangeEvent event) {
					setPanelToType(event.getValue());

				}
			});
		}

		if (showTopBar) {
			DockLayoutPanel panel = new DockLayoutPanel(Unit.PX);
			initWidget(panel);
			topPanel.setWidth("100%");
			panel.addNorth(topPanel, 32);
			panel.add(body);
			// topPanel.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);
			topPanel.setBorderWidth(0);
			if (alignNaviCenter) {
				topPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			}
			viewType.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);
		} else {
			initWidget(body);
		}

		viewType.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
		prev = new TouchButton(new Image(ImageResources.getImages(type).back()), type);
		next = new TouchButton(new Image(ImageResources.getImages(type).next()), type);
		prevMore = new TouchButton(new Image(ImageResources.getImages(type).first()), type);
		nextMore = new TouchButton(new Image(ImageResources.getImages(type).last()), type);
		today = new TouchButton(FtrGwtResources.TEXTS.today(), type);

		renderer = newRenderer;
		topPanel.setSpacing(0);
		today.addClickHandler(this);
		// topPanel.setP

		super.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);

		this.setStyleName(FtrGwtDateCss.TAB_SELECTED_BODY);
		next.addClickHandler(this);
		prev.addClickHandler(this);
		nextMore.addClickHandler(this);
		prevMore.addClickHandler(this);
		// topPanel.setPixelSize();
		if (alignNaviCenter) {
			navi.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		}
		navi.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		navi.setSpacing(0);

		if (showNavigation) {

			if (displayTodayButton) {
				navi.add(today);
				navi.setCellVerticalAlignment(today, HorizontalPanel.ALIGN_MIDDLE);
				navi.add(new HTML(" "));
			}

			if (renderer.useShowMore()) {
				navi.add(prevMore);
				navi.setCellVerticalAlignment(prevMore, HorizontalPanel.ALIGN_MIDDLE);
			}
			navi.add(prev);
			navi.setCellVerticalAlignment(prev, HorizontalPanel.ALIGN_MIDDLE);
			navi.add(next);
			navi.setCellVerticalAlignment(next, HorizontalPanel.ALIGN_MIDDLE);
			if (renderer.useShowMore()) {
				navi.add(nextMore);
				navi.setCellVerticalAlignment(nextMore, HorizontalPanel.ALIGN_MIDDLE);
			}

			navi.add(new HTML(" "));
			navi.add(dayLabel);
			dayLabel.getElement().getStyle().setFontWeight(FontWeight.BOLD);
			navi.setCellVerticalAlignment(dayLabel, HorizontalPanel.ALIGN_MIDDLE);
		}

		topPanel.add(navi);

		// super.setS
		topPanel.add(viewType);
		topPanel.setCellVerticalAlignment(viewType, HorizontalPanel.ALIGN_BOTTOM);

		topPanel.setCellHorizontalAlignment(viewType, HorizontalPanel.ALIGN_RIGHT);

		// viewType.setBorderWidth(0);

		// viewType.set
		// viewType.setSpacing(0);
		int tabCount = 0;
		if (renderer.supportDayView()) {
			tabCount++;
		}
		if (renderer.supportDayListView()) {
			tabCount++;
		}
		if (renderer.supportWeekView()) {
			tabCount++;
		}

		if (renderer.supportMonthView()) {
			tabCount++;
		}

		if (renderer.supportListView()) {
			tabCount++;
		}

		if (renderer.supportWeekListView()) {
			tabCount++;
		}

		if (tabCount > 1) {
			if (popup != null) {
				viewType.add(popup);
			}
			if (renderer.supportDayView()) {
				addTab(PanelType.DAY, type, new WeekPanel.FactoryDayPanel());
			}
			if (renderer.supportDayListView()) {
				addTab(PanelType.DAY_LIST, type, new WeekListPanel.FactoryDayList());
			}
			if (renderer.supportWeekView()) {
				addTab(PanelType.WEEK, type, new WeekPanel.FactoryWeekPanel());
			}
			if (renderer.supportWeekListView()) {
				addTab(PanelType.WEEK_LIST, type, new WeekListPanel.FactoryWeekList());
			}
			if (renderer.supportMonthView()) {
				addTab(PanelType.MONTH, type, new MonthPanel.Factory());
			}
			if (renderer.supportListView()) {
				addTab(PanelType.LIST, type, new EventListPanel.Factory());
			}

		}
		TouchToggleButton def = typeMap.get(defaultType);
		if (def != null) {
			def.setSelected(true);
		}
		setType(defaultType);
		if (popup != null) {
			popup.setValue(defaultType);
		}
	}

	protected void setPanelToType(PanelType value) {
		TouchToggleButton button = typeMap.get(value);
		PanelFactory selectedPanel = buttonFactoryMap.get(button);
		if (selectedPanel != null) {
			setPanelType(selectedPanel);

		}
	}

	public DateRenderer getRenderer() {
		return renderer;
	}

	public FlowPanel getViewType() {
		return viewType;
	}

	protected void onAttach() {
		super.onAttach();
		scrollToHour(renderer.getScrollHour());
	}

	public void addWidgetToNivigationBar(Widget newWidget) {
		navi.add(new HTML(" "));
		navi.add(newWidget);
		navi.setCellVerticalAlignment(newWidget, HorizontalPanel.ALIGN_MIDDLE);

	}

	private void addTab(PanelType type, UiType type2, PanelFactory factory) {
		TouchToggleButton button = new TouchToggleButton(getText(type), type2);
		tabLabels.add(button);
		if (popup != null) {
			popup.addValue(type);
		}
		button.addClickHandler(this);
		if (popup == null) {
			viewType.add(button);
		}
		typeMap.put(type, button);
		buttonFactoryMap.put(button, factory);
	}

	public static String getText(PanelType panel) {
		switch (panel) {
		case DAY:
			return FtrGwtResources.TEXTS.day();
		case WEEK:
			return FtrGwtResources.TEXTS.week();
		case DAY_LIST:
			return FtrGwtResources.TEXTS.dayList();
		case WEEK_LIST:
			return FtrGwtResources.TEXTS.weekList();
		case MONTH:
			return FtrGwtResources.TEXTS.month();
		case LIST:
			return FtrGwtResources.TEXTS.list();
		default:
			break;
		}
		return "----";
	}

	public void setFactory(PanelType newType, PanelFactory factory) {
		TouchToggleButton def = typeMap.get(newType);
		if (def != null) {
			buttonFactoryMap.put(def, factory);
		}
	}

	public void rebuildPanel() {
		for (DatePanel datePanel : panelCache.values()) {
			if (datePanel instanceof BaseWeekPanel) {
				BaseWeekPanel real = (BaseWeekPanel) datePanel;
				if (real.getDaysShown() > 1) {
					real.setDaysShown(DateUtils.countDays(renderer));
				}
				real.buildPanel();
			} else {
				// if (datePanel instanceof MonthPanel) {
				// MonthPanel real = (MonthPanel) datePanel;
				// if (real.getDaysShown() > 1) {
				// real.setDaysShown(DateUtils.countDays(renderer));
				// }
				// real.buildPanel();
				// } else {
				datePanel.buildPanel();
				// }
			}
		}

	}

	public void scrollToHour(int hour) {
		DatePanel current = getCurrent();
		if (current instanceof WeekPanel) {
			WeekPanel real = (WeekPanel) current;
			real.scrollToHour(hour);
		}
	}

	private void showCurrent(DatePanel newShows) {
		if (currentView != null) {
			Widget real = (Widget) currentView;
			body.remove(real);
		}

		Widget newReal = (Widget) newShows;

		currentView = newShows;
		body.setWidget(newReal);
		// newReal.setWidth("100%");
		// setPixelSize(witdh, height);
		setDate(centerDate);
	}

	public void setSelected(TouchToggleButton button) {
		for (TouchToggleButton wi : tabLabels) {
			if (wi == button) {
				wi.setSelected(true);
			} else {
				wi.setSelected(false);
			}
		}
	}

	public void setPanelType(PanelFactory panelFactory) {
		if (!panelFactory.equals(this.panelFactory)) {
			this.panelFactory = panelFactory;
			DatePanel datePanel = panelCache.get(panelFactory);
			if (datePanel == null) {
				datePanel = panelFactory.createDatePanel(renderer);
				datePanel.addDateEventHandler(this);
				panelCache.put(panelFactory, datePanel);
			}
			showCurrent(datePanel);
			scrollToHour(renderer.getScrollHour());
		}
	}

	public void setType(PanelType type) {
		switch (type) {
		case MONTH: {
			setPanelType(new MonthPanel.Factory());
			break;
		}
		case WEEK: {
			setPanelType(new WeekPanel.FactoryWeekPanel());
			break;
		}
		case DAY: {
			setPanelType(new WeekPanel.FactoryDayPanel());
			break;
		}
		case DAY_LIST: {
			setPanelType(new WeekListPanel.FactoryDayList());
			break;
		}
		case LIST: {
			setPanelType(new EventListPanel.Factory());
			break;
		}
		case WEEK_LIST: {
			setPanelType(new WeekListPanel.FactoryWeekList());
			break;
		}
		}
	}

	public void setDate(Date newCenterDate) {
		setDate(newCenterDate, true);
	}

	public void setDate(Date newCenterDate, boolean setDatapanelDate) {
		setDate(newCenterDate, setDatapanelDate, true);
	}

	// add a reloadData argument so that the controller can handle if it uses
	// cached data or make a RPC call.
	public void setDate(Date newCenterDate, boolean setDatapanelDate, boolean reloadData) {
		centerDate = newCenterDate;
		if (setDatapanelDate) {
			currentView.setDate(centerDate);
		}
		currentView.loadData();
		dayLabel.setText(renderer.getTitleDisplayText(currentView));
	}

	public void clearData() {
		currentView.clearData();
	}

	public void setEvents(T[] events) {
		currentView.clearData();
		addEvents(events);
	}

	public void addEvents(T[] events) {
		if (events != null) {
			for (int i = 0; i < events.length; i++) {
				currentView.addEventData(events[i]);
			}
		}

	}

	public void setEventsByList(List events) {
		currentView.clearData();
		addEventsList(events);
	}

	public void addEventsList(List events) {
		if (events != null) {
			currentView.addEventsByList(events);
		}

	}

	public void setEventsByArrayList(List events) {
		currentView.clearData();
		currentView.addEventsByList(events);
	}

	public void reloadData() {
		setDate(centerDate, true, true);
	}

	public void repaintData() {
		setDate(centerDate, true, false);
	}

	public DatePanel getCurrent() {
		return currentView;
	}

	public void handleDateEvent(DateEvent newEvent) {
		switch (newEvent.getCommand()) {
		case GO_TO_DAY_VIEW:
			if (renderer.supportDayView()) {
				setType(PanelType.DAY);
				TouchToggleButton def = typeMap.get(PanelType.DAY);
				if (def != null) {
					for (Iterator iterator = typeMap.values().iterator(); iterator.hasNext();) {
						iterator.next().setSelected(false);
					}
					def.setSelected(true);
					if (popup != null) {
						popup.setValue(PanelType.DAY);
					}
				}
			} else {
				if (renderer.supportWeekView()) {
					setType(PanelType.WEEK);
					if (popup != null) {
						popup.setValue(PanelType.WEEK);
					}
					TouchToggleButton def = typeMap.get(PanelType.WEEK);
					if (def != null) {
						for (Iterator iterator = typeMap.values().iterator(); iterator.hasNext();) {
							iterator.next().setSelected(false);
						}
						def.setSelected(true);
					}
				}
			}
			break;

		default:
			break;
		}
		DateEvent.fire(this, newEvent);
	}

	public void onClick(ClickEvent event) {
		if (event.getSource() instanceof TouchToggleButton) {
			TouchToggleButton button = (TouchToggleButton) event.getSource();
			PanelFactory selectedPanel = buttonFactoryMap.get(button);
			if (selectedPanel != null) {
				setPanelType(selectedPanel);
				setSelected(button);
			}
		}

		if (event.getSource() == prev) {
			setDate(currentView.prev(), false);
		}

		if (event.getSource() == next) {
			setDate(currentView.next(), false);
		}

		if (event.getSource() == prevMore) {
			setDate(currentView.prevMore(), false);
		}

		if (event.getSource() == nextMore) {
			setDate(currentView.nextMore(), false);
		}

		if (event.getSource() == today) {
			setDate(new Date());
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy