eu.future.earth.gwt.client.date.week.BaseWeekPanel Maven / Gradle / Ivy
package eu.future.earth.gwt.client.date.week;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.RequiresResize;
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.DatePanel;
import eu.future.earth.gwt.client.date.DateRenderer;
import eu.future.earth.gwt.client.date.DateUtils;
import eu.future.earth.gwt.client.date.EventPanel;
import eu.future.earth.gwt.client.date.HasDateEventHandlers;
public abstract class BaseWeekPanel extends ResizeComposite implements HasDateEventHandlers, DatePanel, DateEventListener, RequiresResize {
private ArrayList> dayList = new ArrayList>();
private HashMap> cacheDayHeaders = new HashMap>();
private HashMap> cacheDaysEventPanels = new HashMap>();
public BaseWeekPanel(int newNumberOfDays, DateRenderer newRenderer, boolean newShowDayLabel) {
super();
showDayLabel = newShowDayLabel;
daysShown = newNumberOfDays;
renderer = newRenderer;
current.setFirstDayOfWeek(firstDayOfWeek);
current.setTime(new Date());
current.set(Calendar.HOUR_OF_DAY, 0);
current.set(Calendar.MINUTE, 0);
current.set(Calendar.SECOND, 0);
current.set(Calendar.MILLISECOND, 0);
}
protected int daysShown = 7;
protected int firstDayOfWeek = Calendar.MONDAY;
protected Date firstLogical = null;
protected Date lastLogical = null;
private boolean showDayLabel = true;
public boolean isShowDayLabel() {
return showDayLabel;
}
protected int getDailyIntervals() {
return (renderer.getEndHour() - renderer.getStartHour()) * renderer.getIntervalsPerHour();
}
protected Calendar current = new GregorianCalendar();
public int getFirstDayOfWeek() {
return firstDayOfWeek;
}
protected DateRenderer renderer = null;
public DateRenderer getRenderer() {
return renderer;
}
public void setDate(Date activeday) {
final Calendar test = new GregorianCalendar();
test.setTime(activeday);
test.set(Calendar.HOUR_OF_DAY, 0);
test.set(Calendar.MINUTE, 0);
test.set(Calendar.SECOND, 0);
test.set(Calendar.MILLISECOND, 0);
if (DateUtils.isSameDay(test, current)) {
return;
}
current.setTime(activeday);
current.set(Calendar.HOUR_OF_DAY, 0);
current.set(Calendar.MINUTE, 0);
current.set(Calendar.SECOND, 0);
current.set(Calendar.MILLISECOND, 0);
// buildPanel();
modifyDate();
}
public void setRenderer(DateRenderer renderer) {
this.renderer = renderer;
buildPanel();
}
public void setDaysShown(int newDaysShown) {
if (daysShown == newDaysShown) {
return;
}
this.daysShown = newDaysShown;
buildPanel();
}
public abstract void buildPanel();
public void setFirstDayOfWeek(int newFirstDayOfWeek) {
if (firstDayOfWeek == newFirstDayOfWeek) {
return;
}
this.firstDayOfWeek = newFirstDayOfWeek;
current.setFirstDayOfWeek(firstDayOfWeek);
buildPanel();
}
private void modifyDate() {
clearEvents();
Calendar helperCal = new GregorianCalendar();
helperCal.setFirstDayOfWeek(getFirstDayOfWeek());
helperCal.setTime(current.getTime());
if (daysShown > 1) {
helperCal.set(Calendar.DAY_OF_WEEK, helperCal.getFirstDayOfWeek());
}
firstLogical = helperCal.getTime();
for (int i = 0; i < daysShown; i++) {
int dayOfWeek = helperCal.get(Calendar.DAY_OF_WEEK);
if (renderer.showDay(dayOfWeek)) {
setDateForPanels(dayOfWeek, helperCal);
} else {
i--;
}
helperCal.add(Calendar.DAY_OF_WEEK, 1);
}
helperCal.add(Calendar.MINUTE, -1);
lastLogical = helperCal.getTime();
}
public int getDaysShown() {
return daysShown;
}
public void clearCache() {
cacheDayPanels.clear();
cacheDayHeaders.clear();
cacheDaysEventPanels.clear();
for (int i = 0; i < dayList.size(); i++) {
final DayEventElement result = dayList.get(i);
result.destroy();
result.clearEvents();
}
dayList.clear();
}
public void put(int dayOfWeek, AbstractDayPanel dayPanel) {
cacheDayPanels.put(dayOfWeek, dayPanel);
}
public void put(int dayOfWeek, DayHeader label) {
cacheDayHeaders.put(dayOfWeek, label);
}
private HashMap> cacheDayPanels = new HashMap>();
public void put(int dayOfWeek, DayEventpanel eventPanel) {
cacheDaysEventPanels.put(dayOfWeek, eventPanel);
dayList.add(eventPanel);
}
public void add(DayEventElement dayPanel) {
dayList.add(dayPanel);
}
public void setDateForPanels(int dayOfWeek, Calendar helperCal) {
final Calendar today = new GregorianCalendar();
if (cacheDayPanels.size() == 1) {
dayOfWeek = cacheDayPanels.keySet().iterator().next();
}
{
AbstractDayPanel dayPanel = (AbstractDayPanel) cacheDayPanels.get(dayOfWeek);
if (dayPanel != null) {
dayPanel.setDay(helperCal);
if (DateUtils.isSameDay(today, helperCal)) {
dayPanel.addStyleName(FtrGwtDateCss.DATE_DAY_TODAY);
} else {
dayPanel.removeStyleName(FtrGwtDateCss.DATE_DAY_TODAY);
}
}
}
{
final DayHeader label = (DayHeader) cacheDayHeaders.get(dayOfWeek);
if (label != null) {
label.setDate(helperCal.getTime());
if (DateUtils.isSameDay(today, helperCal)) {
label.addStyleName(FtrGwtDateCss.DATE_DAY_TODAY);
} else {
label.removeStyleName(FtrGwtDateCss.DATE_DAY_TODAY);
}
}
}
final DayEventpanel eventPanel = (DayEventpanel) cacheDaysEventPanels.get(dayOfWeek);
if (eventPanel != null) {
eventPanel.setDay(helperCal);
}
}
public void clearEvents() {
if (dayList != null) {
for (int i = 0; i < dayList.size(); i++) {
final DayEventElement result = dayList.get(i);
result.clearEvents();
}
}
}
public Date next() {
if (daysShown > 1) {
current.add(Calendar.DAY_OF_WEEK, 7);
} else {
current.add(Calendar.DAY_OF_WEEK, 1);
}
// current.add(Calendar.DAY_OF_WEEK, daysShown);
// buildPanel();
modifyDate();
return current.getTime();
}
public Date prev() {
if (daysShown > 1) {
current.add(Calendar.DAY_OF_WEEK, -7);
} else {
current.add(Calendar.DAY_OF_WEEK, -1);
}
// buildPanel();
modifyDate();
return current.getTime();
}
public Date nextMore() {
if (daysShown > 1) {
current.add(Calendar.MONTH, 1);
} else {
current.add(Calendar.DAY_OF_WEEK, 7);
}
// buildPanel();
modifyDate();
return current.getTime();
}
public Date prevMore() {
if (daysShown > 1) {
current.add(Calendar.MONTH, -1);
} else {
current.add(Calendar.DAY_OF_WEEK, -7);
}
// buildPanel();
modifyDate();
return current.getTime();
}
public void updateEventData(T newEvent) {
removeEventData(newEvent);
addEventData(newEvent);
}
public void removeEventData(T newEvent) {
for (DayEventElement panel : dayList) {
EventPanel extends T> removed = panel.removeEvent(newEvent);
if (removed != null) {
removed.clearParent();
return;
}
}
}
public void addEventsByList(List extends T> events) {
if (events != null) {
for (int i = 0; i < events.size(); i++) {
addEventData(events.get(i), true);
}
}
for (DayEventElement panel : dayList) {
panel.repaintEvents();
}
}
private void addEventData(T newEvent, boolean isPartOfBatch) {
for (DayEventElement panel : dayList) {
if (panel.isDay(newEvent)) {
createPanel(panel, newEvent, isPartOfBatch);
if (!isPartOfBatch) {
panel.repaintEvents();
}
}
}
}
public abstract void createPanel(DayEventElement panel, T newEvent, boolean isPartOfBatch);
public void addEventData(T newEvent) {
addEventData(newEvent, false);
}
@Override
public void onResize() {
super.onResize();
repaint();
}
public void repaint() {
for (DayEventElement panel : dayList) {
panel.repaintEvents();
}
}
public Date getFirstDateShow() {
return firstLogical;
}
public Date getLastDateShow() {
return lastLogical;
}
public Date getFirstDateLogical() {
return firstLogical;
}
public Date getLastDateLogical() {
return lastLogical;
}
public void clearData() {
if (dayList != null) {
for (DayEventElement panel : dayList) {
panel.clearEvents();
}
}
}
public void setEvents(List extends T> events) {
clearData();
addEventsByList(events);
}
public void notifyReady(DateEventActions action, T data, boolean reload) {
if (reload) {
loadData();
}
switch (action) {
case ADD_DONE: {
addEventData(data);
break;
}
case REMOVE_DONE: {
removeEventData(data);
break;
}
case UPDATE_DONE: {
updateEventData(data);
break;
}
default:
break;
}
DateEvent.fire(this, action, data);
}
public void loadData() {
clearData();
renderer.getEventsForRange(getFirstDateShow(), getLastDateShow(), this, true);
}
public void handleDateEvent(DateEvent newEvent) {
switch (newEvent.getCommand()) {
case DRAG_DROP: {
renderer.updateEvent(renderer.createUpdateHandler(this, newEvent.getData()));
break;
}
case UPDATE: {
renderer.updateEvent(renderer.createUpdateHandler(this, newEvent.getData()));
break;
}
case REMOVE: {
renderer.removeEvent(renderer.createRemoveHandler(this, newEvent.getData()));
break;
}
case ADD: {
renderer.addEvent(renderer.createAddHandler(this, newEvent.getData()));
break;
}
case ADD_DONE: {
addEventData(newEvent.getData());
DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
break;
}
case REMOVE_DONE: {
removeEventData(newEvent.getData());
DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
break;
}
case UPDATE_DONE: {
updateEventData(newEvent.getData());
DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
break;
}
case EDIT: {
renderer.editAfterClick(newEvent.getData(), this);
break;
}
case RELOAD: {
loadData();
DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
break;
}
default:
DateEvent.fire(this, newEvent.getDate(), newEvent.getCommand(), newEvent.getData());
break;
}
}
public HandlerRegistration addDateEventHandler(DateEventListener extends T> handler) {
return addHandler(handler, DateEvent.getType());
}
public HandlerRegistration addWeekScrollEventHandler(WeekScrollEventHandler handler) {
return super.addHandler(handler, WeekScrollEvent.getType());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy