PageRenderTime 166ms CodeModel.GetById 93ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ftr-gwt-library.googlecode.com/
Java | 396 lines | 321 code | 56 blank | 19 comment | 38 complexity | 8baa3712723cb154a500b2d460cd38a1 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.month;
 18
 19import java.util.ArrayList;
 20import java.util.Calendar;
 21import java.util.Date;
 22import java.util.GregorianCalendar;
 23import java.util.List;
 24
 25import com.google.gwt.event.shared.HandlerRegistration;
 26import com.google.gwt.i18n.client.DateTimeFormat;
 27import com.google.gwt.user.client.DOM;
 28import com.google.gwt.user.client.ui.AbsolutePanel;
 29import com.google.gwt.user.client.ui.Grid;
 30import com.google.gwt.user.client.ui.HTML;
 31import com.google.gwt.user.client.ui.HorizontalPanel;
 32
 33import eu.future.earth.gwt.client.FtrGwtDateCss;
 34import eu.future.earth.gwt.client.date.DateEvent;
 35import eu.future.earth.gwt.client.date.DateEvent.DateEventActions;
 36import eu.future.earth.gwt.client.date.DateEventListener;
 37import eu.future.earth.gwt.client.date.DatePanel;
 38import eu.future.earth.gwt.client.date.DateRenderer;
 39import eu.future.earth.gwt.client.date.DateUtils;
 40import eu.future.earth.gwt.client.date.EventPanel;
 41import eu.future.earth.gwt.client.date.HasDateEventHandlers;
 42import eu.future.earth.gwt.client.date.PanelType;
 43
 44public class MonthPanel<T> extends AbsolutePanel implements DateEventListener<T>, DatePanel<T>, HasDateEventHandlers<T> {
 45
 46	private ArrayList<MonthPanelDropController<T>> dropControllers = new ArrayList<MonthPanelDropController<T>>();
 47
 48	private Calendar current = new GregorianCalendar();
 49
 50	private Calendar helperCal = new GregorianCalendar(); // NOPMD;
 51
 52	private Grid monthPanels = new Grid(7, 7);
 53
 54	private List<MonthPanelDayPanel<T>> panels = new ArrayList<MonthPanelDayPanel<T>>();
 55
 56	private List<HandlerRegistration> registrations = new ArrayList<HandlerRegistration>();
 57
 58	public int getFirstDayOfWeek() {
 59		return firstDayOfWeek;
 60	}
 61
 62	public void setFirstDayOfWeek(int newFirstDayOfWeek) {
 63		if (firstDayOfWeek == newFirstDayOfWeek) {
 64			return;
 65		}
 66		this.firstDayOfWeek = newFirstDayOfWeek;
 67		current.setFirstDayOfWeek(firstDayOfWeek);
 68		buildPanel();
 69	}
 70
 71	private int firstDayOfWeek = Calendar.MONDAY;
 72
 73	private MonthPanelDragController dragController = null;
 74
 75	private DateRenderer<T> renderer = null;
 76
 77	public MonthPanel(DateRenderer<T> newRenderer) {
 78		super();
 79		setRenderer(newRenderer);
 80		// firstDayOfWeek = newRenderer.
 81		setStyleName(FtrGwtDateCss.MONTH_PANEL);
 82		monthPanels.setStyleName(FtrGwtDateCss.MONTH_PANEL);
 83		monthPanels.setCellSpacing(0);
 84		monthPanels.setCellPadding(0);
 85		this.add(monthPanels, 2, 0);
 86	}
 87
 88	int maxWi = 70;
 89
 90	public void setPixelSize(int width, int newHeight) {
 91		if (newHeight > 0) {
 92			super.setHeight(newHeight + "px");
 93		}
 94		if (width > 8) {
 95			super.setWidth(width + "px");
 96			monthPanels.setWidth(width - 3 + "px");
 97			maxWi = ((width - 7) / 7);
 98			if (maxWi > 0) {
 99				for (MonthPanelDayPanel<T> result : panels) {
100					result.setWidth(maxWi);
101				}
102			}
103		}
104	}
105
106	public final void setRenderer(DateRenderer<T> newRenderer) {
107		renderer = newRenderer;
108		if (renderer.enableDragAndDrop()) {
109			if (dragController != null) {
110				for (int i = 0; i < dropControllers.size(); i++) {
111					final MonthPanelDropController<T> real = dropControllers.get(i);
112					dragController.unregisterDropController(real);
113				}
114			}
115			dragController = new MonthPanelDragController(this);
116		}
117		buildPanel();
118	}
119
120	public void setDate(Date newDate) {
121		if (DateUtils.isSameDay(current.getTime(), newDate)) {
122			return;
123		}
124		current.setTime(newDate);
125		buildPanel();
126	}
127
128	private int xPos = 0; // NOPMD;
129
130	private int yPos = 0; // NOPMD;
131
132	private Date firstShow = null;
133
134	private Date lastShow = null;
135
136	private Date firstLogical = null;
137
138	private Date lastLogical = null;
139
140	private void buildPanel() {
141		if (renderer.enableDragAndDrop()) {
142			for (int i = 0; i < dropControllers.size(); i++) {
143				final MonthPanelDropController<T> real = dropControllers.get(i);
144				dragController.unregisterDropController(real);
145			}
146			dropControllers.clear();
147		}
148
149		for (HandlerRegistration registration : registrations) {
150			registration.removeHandler();
151		}
152
153		registrations.clear();
154		monthPanels.clear();
155
156		DOM.setStyleAttribute(monthPanels.getElement(), "width", "100%");
157		DOM.setStyleAttribute(monthPanels.getElement(), "height", "100%");
158
159		helperCal.setFirstDayOfWeek(firstDayOfWeek);
160		xPos = 0;
161		yPos = 0;
162		final Calendar today = new GregorianCalendar();
163		final DateTimeFormat dayFormatter = DateTimeFormat.getFormat("E");
164		for (int i = firstDayOfWeek; i < 7 + firstDayOfWeek; i++) {
165			helperCal.set(Calendar.DAY_OF_WEEK, i);
166			monthPanels.setWidget(yPos, xPos, new HTML(dayFormatter.format(helperCal.getTime())));
167			monthPanels.getCellFormatter().setHorizontalAlignment(yPos, xPos, HorizontalPanel.ALIGN_CENTER);
168			monthPanels.getCellFormatter().setHeight(yPos, xPos, "9%");
169			monthPanels.getColumnFormatter().setWidth(xPos, "12%");
170			xPos++;
171		}
172		yPos++;
173
174		// set logical first day of the month
175		helperCal.setTime(current.getTime());
176		helperCal.set(Calendar.DAY_OF_MONTH, 1);
177		helperCal.set(Calendar.HOUR_OF_DAY, 0);
178		helperCal.set(Calendar.MINUTE, 0);
179		helperCal.set(Calendar.SECOND, 0);
180		helperCal.set(Calendar.MILLISECOND, 0);
181		firstLogical = helperCal.getTime();
182		// set logical last day of the month
183		helperCal.add(Calendar.MONTH, 1);
184		helperCal.add(Calendar.MILLISECOND, -1);
185		lastLogical = helperCal.getTime();
186		// now loop over full weeks for showing days
187		helperCal.setTime(current.getTime());
188		helperCal.add(Calendar.DAY_OF_MONTH, -(helperCal.get(Calendar.DAY_OF_MONTH)));
189		while (helperCal.get(Calendar.DAY_OF_WEEK) != helperCal.getFirstDayOfWeek()) {
190			helperCal.add(Calendar.DAY_OF_MONTH, -1);
191		}
192		panels.clear();
193		firstShow = helperCal.getTime();
194		for (int y = 1; y < 7; y++) {
195			for (int x = 0; x < 7; x++) {
196				monthPanels.getCellFormatter().setHeight(y, x, "13%");
197				final MonthPanelDayPanel<T> result = makeDayPanel(helperCal);
198				panels.add(result);
199				registrations.add(result.addDateEventHandler(this));
200				if (DateUtils.isSameDay(today, helperCal)) {
201					result.addStyleName(FtrGwtDateCss.DATE_DAY_TODAY);
202				}
203
204				monthPanels.setWidget(y, x, result);
205				if (renderer.enableDragAndDrop() && dragController != null) {
206					final MonthPanelDropController<T> dropController = new MonthPanelDropController<T>(result);
207					dropControllers.add(dropController);
208					dragController.registerDropController(dropController);
209				}
210				helperCal.add(Calendar.DAY_OF_MONTH, 1);
211			}
212		}
213		lastShow = helperCal.getTime();
214	}
215
216	private MonthPanelDayPanel<T> makeDayPanel(Calendar calender) {
217		return new MonthPanelDayPanel<T>(calender.get(Calendar.DAY_OF_MONTH), calender.getTime(), this, renderer);
218	}
219
220	@SuppressWarnings("unchecked")
221	public void addEventData(T newEvent) {
222		for (MonthPanelDayPanel<T> result : panels) {
223			if (result.isDay(newEvent)) {
224				final EventPanel<T> entry = (EventPanel<T>) renderer.createPanel(newEvent, getType());
225				if (dragController != null && entry.isDraggable()) {
226					dragController.makeDraggable(entry, entry.getDraggableItem());
227				}
228				entry.addParentDateEventHandler(this);
229				result.addEvent(entry);
230			}
231		}
232	}
233
234	public void notifyReady(DateEventActions action, T data, boolean reload) {
235		if (reload) {
236			loadData();
237		}
238		switch (action) {
239		case ADD_DONE: {
240			addEventData(data);
241			break;
242		}
243		case REMOVE_DONE: {
244			removeEventData(data);
245			break;
246		}
247		case UPDATE_DONE: {
248			updateEventData(data);
249			break;
250		}
251		default:
252			break;
253		}
254		DateEvent.fire(this, action, data);
255	}
256
257	public void loadData() {
258		clearData();
259		renderer.getEventsForRange(getFirstDateShow(), getLastDateShow(), this, true);
260	}
261
262	public void handleDateEvent(DateEvent<T> newEvent) {
263		switch (newEvent.getCommand()) {
264		case DRAG_DROP: {
265			renderer.updateEvent(renderer.createUpdateHandler(this, newEvent.getData()));
266			break;
267		}
268		case UPDATE: {
269			renderer.updateEvent(renderer.createUpdateHandler(this, newEvent.getData()));
270			break;
271		}
272		case REMOVE: {
273			renderer.removeEvent(renderer.createRemoveHandler(this, newEvent.getData()));
274			break;
275		}
276		case ADD: {
277			renderer.addEvent(renderer.createAddHandler(this, newEvent.getData()));
278			break;
279		}
280		case ADD_DONE: {
281			addEventData(newEvent.getData());
282			DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
283			break;
284		}
285		case REMOVE_DONE: {
286			removeEventData(newEvent.getData());
287			DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
288			break;
289		}
290		case UPDATE_DONE: {
291			updateEventData(newEvent.getData());
292			DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
293			break;
294		}
295		case EDIT: {
296			renderer.editAfterClick(newEvent.getData(), this);
297			break;
298		}
299		case RELOAD: {
300			loadData();
301			DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
302			break;
303		}
304		default:
305			DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
306			break;
307		}
308	}
309
310	public HandlerRegistration addDateEventHandler(DateEventListener<? extends T> handler) {
311		return addHandler(handler, DateEvent.getType());
312	}
313
314	public DateRenderer<T> getRenderer() {
315		return renderer;
316	}
317
318	public Date next() {
319		current.add(Calendar.MONTH, 1);
320		buildPanel();
321		return current.getTime();
322	}
323
324	public Date prev() {
325		current.add(Calendar.MONTH, -1);
326		buildPanel();
327		return current.getTime();
328	}
329
330	public Date nextMore() {
331		current.add(Calendar.YEAR, 1);
332		buildPanel();
333		return current.getTime();
334	}
335
336	public Date prevMore() {
337		current.add(Calendar.YEAR, -1);
338		buildPanel();
339		return current.getTime();
340	}
341
342	public void updateEventData(T newEvent) {
343		removeEventData(newEvent);
344		addEventData(newEvent);
345	}
346
347	public void removeEventData(T newEvent) {
348		for (MonthPanelDayPanel<T> result : panels) {
349			EventPanel<T> removed = result.removeEvent(newEvent);
350			if (removed != null) {
351				removed.clearParent();
352				return;
353			}
354		}
355	}
356
357	public Date getFirstDateShow() {
358		return firstShow;
359	}
360
361	public Date getLastDateShow() {
362		return lastShow;
363	}
364
365	public Date getFirstDateLogical() {
366		return firstLogical;
367	}
368
369	public Date getLastDateLogical() {
370		return lastLogical;
371	}
372
373	public void clearData() {
374		for (MonthPanelDayPanel<T> result : panels) {
375			result.clearEvents();
376		}
377	}
378
379	public PanelType getType() {
380		return PanelType.MONTH;
381	}
382
383	public void setEvents(List<? extends T> events) {
384		clearData();
385		addEventsByList(events);
386	}
387
388	public void addEventsByList(List<? extends T> events) {
389		if (events != null) {
390			for (int i = 0; i < events.size(); i++) {
391				addEventData(events.get(i));
392			}
393		}
394	}
395
396}