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

eu.future.earth.gwt.client.date.month.MonthPanelDayPanel Maven / Gradle / Ivy

The 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.month;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;

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.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
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.FocusPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ResizeComposite;

import eu.future.earth.gwt.client.FtrGwtDateCss;
import eu.future.earth.gwt.client.date.DateEvent;
import eu.future.earth.gwt.client.date.DateEvent.DateEventActions;
import eu.future.earth.gwt.client.date.DateEventListener;
import eu.future.earth.gwt.client.date.DateRenderer;
import eu.future.earth.gwt.client.date.EventComparator;
import eu.future.earth.gwt.client.date.EventPanel;
import eu.future.earth.gwt.client.date.HasDateEventHandlers;
import eu.future.earth.gwt.client.date.list.DateAnchor;
import eu.future.earth.gwt.client.date.week.DayHelper;

public class MonthPanelDayPanel extends ResizeComposite implements MouseDownHandler, HasDateEventHandlers, ClickHandler {

	private DateAnchor dayLabel = null; // NOPMD;

	private FocusPanel clickPanel = new FocusPanel(); // NOPMD;

	private FlowPanel entryPanel = new FlowPanel();

	private Calendar helper = new GregorianCalendar(); // NOPMD;

	private DayHelper theDay = new DayHelper();

	private MonthPanel parent = null;

	private DateRenderer renderer = null;

	private EventComparator sorter = null;

	private Date date = null;

	public MonthPanelDayPanel(int day, Date newDate, MonthPanel newParent, DateRenderer newRenderer) {
		super();
		DockLayoutPanel panel = new DockLayoutPanel(Unit.PX);
		initWidget(panel);

		theDay.setRenderer(newRenderer);
		theDay.setFirstDayOfWeek(Calendar.MONDAY);
		date = newDate;
		renderer = newRenderer;
		sorter = new EventComparator();
		parent = newParent;
		theDay.setTime(newDate);
		dayLabel = new DateAnchor(String.valueOf(day), newDate);
		dayLabel.addClickHandler(this);
		dayLabel.setHorizontalAlignment(Label.ALIGN_LEFT);
		dayLabel.setStyleName(FtrGwtDateCss.DATE_DAY_HEADER);
		setStyleName(FtrGwtDateCss.DATE_DAY);
		clickPanel.addMouseDownHandler(this);
		panel.addNorth(dayLabel, renderer.getMonthDayHeaderHeight());
		clickPanel.setStyleName(FtrGwtDateCss.DATE_MONTH_DAY_DATA);
		entryPanel.setStyleName(FtrGwtDateCss.DATE_DAY_DATA);
		panel.add(entryPanel);
		entryPanel.add(clickPanel);
	}

	public boolean isDay(T newEvent) {
		return theDay.isDay(newEvent);
	}

	private ArrayList> events = new ArrayList>();

	public void addEvent(EventPanel newEvent) {
		if (renderer.enableDragAndDrop()) {
			newEvent.addStyleName(FtrGwtDateCss.DND_GETTING_STARTED_LABEL);
		}
		// newEvent.setWidth(width);
		events.add(newEvent);
		repaintPanel();
	}

	private void repaintPanel() {
		entryPanel.clear();
		Collections.sort(events, sorter);
		for (EventPanel event : events) {
			entryPanel.add(event);
			event.repaintTime();
		}
		entryPanel.add(clickPanel);
	}

	public EventPanel removeEvent(T newEvent) {
		for (EventPanel ev : events) {
			if (renderer.equal(ev.getValue(), newEvent)) {
				entryPanel.remove(ev);
				events.remove(ev);
				repaintPanel();
				return ev;
			}
		}
		return null;

	}

	public void clearEvents() {
		for (EventPanel ev : events) {
			entryPanel.remove(ev);
			ev.clearParent();
		}
		events.clear();
	}

	public void addEventByDrop(EventPanel newEvent) {
		if (newEvent != null) {

			events.add(newEvent);

			newEvent.addStyleName(FtrGwtDateCss.DND_GETTING_STARTED_LABEL);
			{
				helper.setTime(newEvent.getStart());
				helper.set(Calendar.YEAR, theDay.get(Calendar.YEAR));
				helper.set(Calendar.MONTH, theDay.get(Calendar.MONTH));
				helper.set(Calendar.DAY_OF_MONTH, theDay.get(Calendar.DAY_OF_MONTH));
				newEvent.setStart(helper.getTime());
			}

			{
				if (newEvent.getEnd() != null) {
					helper.setTime(newEvent.getEnd());
					helper.set(Calendar.YEAR, theDay.get(Calendar.YEAR));
					helper.set(Calendar.MONTH, theDay.get(Calendar.MONTH));
					helper.set(Calendar.DAY_OF_MONTH, theDay.get(Calendar.DAY_OF_MONTH));
					newEvent.setEndTime(helper.getTime());
				}
			}

			repaintPanel();
			DateEvent.fire(this, DateEventActions.DRAG_DROP, newEvent.getValue());
		}
	}

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

	public void onClick(ClickEvent event) {
		if (event.getSource() instanceof DateAnchor) {
			DateEvent.fire(this, date, DateEventActions.GO_TO_DAY_VIEW);
		}

	}

	public void onMouseDown(MouseDownEvent event) {
		if (event.getSource() == clickPanel) {
			final Calendar helper = new GregorianCalendar();
			helper.setTime(theDay.getTime());
			// Transplant the hour and minutes
			helper.set(Calendar.HOUR_OF_DAY, 0);
			helper.set(Calendar.MINUTE, 0);
			helper.set(Calendar.SECOND, 0);
			helper.set(Calendar.MILLISECOND, 0);
			final Date start = helper.getTime();
			parent.getRenderer().createNewAfterClick(start, parent);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy