PageRenderTime 66ms CodeModel.GetById 15ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ftr-gwt-library.googlecode.com/
Java | 313 lines | 238 code | 49 blank | 26 comment | 50 complexity | 1359fca0bacfe706ac8aaa6887eded2d MD5 | raw file
  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.weeklist;
 18
 19import java.util.ArrayList;
 20import java.util.Calendar;
 21import java.util.Collections;
 22import java.util.Date;
 23import java.util.GregorianCalendar;
 24import java.util.List;
 25
 26import org.cobogw.gwt.user.client.CSS;
 27
 28import com.google.gwt.core.client.GWT;
 29import com.google.gwt.event.dom.client.MouseDownEvent;
 30import com.google.gwt.event.dom.client.MouseDownHandler;
 31import com.google.gwt.event.dom.client.MouseMoveEvent;
 32import com.google.gwt.event.dom.client.MouseMoveHandler;
 33import com.google.gwt.event.dom.client.MouseOutEvent;
 34import com.google.gwt.event.dom.client.MouseOutHandler;
 35import com.google.gwt.event.dom.client.MouseUpEvent;
 36import com.google.gwt.event.dom.client.MouseUpHandler;
 37import com.google.gwt.event.shared.HandlerRegistration;
 38import com.google.gwt.user.client.ui.FlowPanel;
 39import com.google.gwt.user.client.ui.FocusPanel;
 40import com.google.gwt.user.client.ui.VerticalPanel;
 41import com.google.gwt.user.client.ui.Widget;
 42
 43import eu.future.earth.gwt.client.FtrGwtDateCss;
 44import eu.future.earth.gwt.client.date.DateEvent;
 45import eu.future.earth.gwt.client.date.DateEvent.DateEventActions;
 46import eu.future.earth.gwt.client.date.DateEventListener;
 47import eu.future.earth.gwt.client.date.DateRenderer;
 48import eu.future.earth.gwt.client.date.EventPanel;
 49import eu.future.earth.gwt.client.date.EventWildComparator;
 50import eu.future.earth.gwt.client.date.HasDateEventHandlers;
 51import eu.future.earth.gwt.client.date.week.DayEventElement;
 52import eu.future.earth.gwt.client.date.week.DayHelper;
 53
 54public class WeekListDayPanel<T> extends FocusPanel implements MouseDownHandler, MouseMoveHandler, MouseUpHandler, MouseOutHandler, DayEventElement<T>, HasDateEventHandlers<T>, DateEventListener<T> {
 55
 56	private Calendar helper = new GregorianCalendar();
 57
 58	private DayHelper<T> theDay = new DayHelper<T>();
 59
 60	private DateRenderer<T> renderer = null;
 61
 62	private String startTimeString = null;
 63
 64	private EventWildComparator<T> sorter = new EventWildComparator<T>();
 65
 66	private List<EventPanel<? extends T>> events = new ArrayList<EventPanel<? extends T>>();
 67
 68	public Date getDate() {
 69		return theDay.getTime();
 70	}
 71
 72	private VerticalPanel body = null;
 73
 74	private FlowPanel topper = new FlowPanel();
 75
 76	public WeekListDayPanel(DateRenderer<T> newRenderer) {
 77		super();
 78		this.renderer = newRenderer;
 79		body = new VerticalPanel();
 80		this.setWidget(topper);
 81		topper.add(body);
 82		super.setWidth(getPrefferedWitdh());
 83		CSS.setProperty(body, CSS.A.WIDTH, "100%");
 84		CSS.setProperty(this, CSS.A.HEIGHT, "100%");
 85		CSS.setProperty(this, CSS.A.BORDER_RIGHT_COLOR, "#d0e4f6");
 86		CSS.setProperty(this, CSS.A.BORDER_RIGHT_STYLE, CSS.V.BORDER_STYLE.SOLID);
 87		CSS.setProperty(this, CSS.A.BORDER_RIGHT_WIDTH, "2px");
 88		theDay.setRenderer(newRenderer);
 89		setStyleName(FtrGwtDateCss.DAY_PANEL);
 90		this.addMouseOutHandler(this);
 91		this.addMouseDownHandler(this);
 92		this.addMouseUpHandler(this);
 93	}
 94
 95	public void destroy() {
 96		if (getParent() != null) {
 97			removeFromParent();
 98		}
 99	}
100
101	public void clearEvents() {
102		for (EventPanel<? extends T> ev : events) {
103			body.remove(ev);
104		}
105		events.clear();
106	}
107
108	public int calculateIntervalSnapForY(int y) {
109		return (int) Math.round(((double) y) / renderer.getIntervalHeight());
110	}
111
112	public void addEventDrop(EventPanel<T> newEvent, boolean sameDay) {
113		GWT.log("Not same day", null);
114		events.add(newEvent);
115		repaintEvents();
116		GWT.log("Adding", null);
117		DateEvent.fire(this, DateEventActions.DRAG_DROP, newEvent.getValue());
118	}
119
120	public HandlerRegistration addDateEventHandler(DateEventListener<? extends T> handler) {
121		return addHandler(handler, DateEvent.getType());
122	}
123
124	public void addEvent(EventPanel<? extends T> newEvent, boolean partOfBatch) {
125		if (!partOfBatch) {
126			removeEvent(newEvent.getValue()); // Be safe
127		}
128		events.add(newEvent);
129		if (!partOfBatch) {
130			repaintEvents();
131		}
132	}
133
134	public void updateEvent(EventPanel<? extends T> newEvent) {
135		EventPanel<? extends T> found = null;
136		for (EventPanel<? extends T> test : events) {
137			if (renderer.equal(test.getValue(), newEvent.getValue())) {
138				found = test;
139			}
140		}
141		if (found != null) {
142			removeEvent(found.getValue(), false);
143		}
144		events.add(newEvent);
145		repaintEvents();
146	}
147
148	private void paintEvent(EventPanel<? extends T> newEvent) {
149		newEvent.repaintTime();
150		int wide = getOffsetWidth() - 6;
151		if (wide < 0) {
152			wide = 80;
153		}
154		body.add(newEvent);
155	}
156
157	public EventPanel<? extends T> removeEvent(T event) {
158		return removeEvent(event, true);
159	}
160
161	public EventPanel<? extends T> removeEvent(T event, boolean repaint) {
162		for (EventPanel<? extends T> ev : events) {
163			if (renderer.equal(ev.getValue(), event)) {
164				body.remove(ev);
165				events.remove(ev);
166				// Only repaint when there is something removed.
167				if (repaint) {
168					repaintEvents();
169				}
170				return ev;
171			}
172		}
173		return null;
174	}
175
176	public void repaintEvents() {
177		if (events != null) {
178			Collections.sort(events, sorter);
179			for (EventPanel<? extends T> ev : events) {
180				body.remove(ev);
181				paintEvent(ev);
182			}
183		}
184	}
185
186	public void repaint(EventPanel<T> notRepaint) {
187		if (events != null) {
188			Collections.sort(events, sorter);
189			for (EventPanel<? extends T> ev : events) {
190				if (!renderer.equal(ev.getValue(), notRepaint.getValue())) {
191					body.remove(ev);
192					paintEvent(ev);
193				}
194			}
195		}
196	}
197
198	public DateRenderer<T> getRenderer() {
199		return renderer;
200	}
201
202	public void setDay(Calendar newDay) {
203		theDay.setTime(newDay.getTime());
204		theDay.setFirstDayOfWeek(newDay.getFirstDayOfWeek());
205	}
206
207	public boolean isDay(T newEvent) {
208		if (renderer.isWholeDayEvent(newEvent)) {
209			return false;
210		}
211		return theDay.isDay(newEvent);
212	}
213
214	protected String getPrefferedWitdh() {
215		return "100%";
216	}
217
218	public void onMouseDown(MouseDownEvent event) {
219		if (event.getSource() instanceof Widget) {
220			Widget widget = (Widget) event.getSource();
221			if (widget != this) {
222				startTimeString = widget.getTitle();
223			}
224		}
225	}
226
227	public void onMouseOut(MouseOutEvent event) {
228		if (event.getSource() instanceof Widget) {
229			Widget widget = (Widget) event.getSource();
230			if (widget == this) {
231				startTimeString = null;
232			}
233		}
234	}
235
236	public void onMouseMove(MouseMoveEvent event) {
237		if (event.getSource() instanceof Widget) {
238			Widget widget = (Widget) event.getSource();
239			if (startTimeString != null && widget != this) {
240				widget.addStyleName(FtrGwtDateCss.HOURS_SELECTED);
241			}
242		}
243	}
244
245	public void onMouseUp(MouseUpEvent event) {
246		if (event.getSource() instanceof Widget) {
247			Widget widget = (Widget) event.getSource();
248			// startTime
249			Boolean add12 = false;
250			if (startTimeString != null && widget != this) {
251				if (startTimeString.indexOf("AM") > -1 || startTimeString.indexOf("PM") > -1) {
252					if (renderer.show24HourClock() == false) {
253						if (startTimeString.indexOf("PM") > -1) {
254							add12 = true;
255						}
256					}
257					startTimeString = startTimeString.substring(0, startTimeString.length() - 3);
258				}
259				helper.setTime(theDay.getTime());
260				final Calendar helperTime = new GregorianCalendar();
261
262				helperTime.setTime(theDay.getTime());
263				// Transplant the hour and minutes
264
265				if (add12 && helperTime.get(Calendar.HOUR_OF_DAY) != 12) {
266					helper.set(Calendar.HOUR_OF_DAY, helperTime.get(Calendar.HOUR_OF_DAY) + 12);
267				} else {
268					helper.set(Calendar.HOUR_OF_DAY, helperTime.get(Calendar.HOUR_OF_DAY));
269				}
270				// helper.set(Calendar.HOUR_OF_DAY,
271				// helperTime.get(Calendar.HOUR_OF_DAY));
272				helper.set(Calendar.MINUTE, helperTime.get(Calendar.MINUTE));
273				helper.set(Calendar.SECOND, 0);
274				helper.set(Calendar.MILLISECOND, 0);
275				final Date start = helper.getTime();
276
277				// add one interval's worth of time to get to the END of the
278				// selected interval
279
280				String endTimeString = widget.getTitle();
281				if (endTimeString != null) {
282					if (endTimeString.indexOf("AM") > -1 || endTimeString.indexOf("PM") > -1) {
283						endTimeString = endTimeString.substring(0, endTimeString.length() - 3);
284					}
285				}
286
287				renderer.createNewAfterClick(start, this);
288
289				startTimeString = null;
290			}
291
292		}
293	}
294
295	public void setElementWide(int newWide) {
296		// for (int i = 0; i < events.size(); i++) {
297		// final EventPanel ev = (EventPanel) events.get(i);
298		// ev.setWidth(newWide + "px");
299		// }
300
301	}
302
303	public void notifyParentOfUpdate(DateEventActions action, T data) {
304		DateEvent.fire(this, action, data);
305	}
306
307	public void handleDateEvent(DateEvent<T> newEvent) {
308		GWT.log("Here foor " + newEvent.getCommand(), null);
309		DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
310
311	}
312
313}