BrightSide Workbench Full Report + Source Code
CalendarComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.calendar;
20 
21 import java.util.Date;
22 import org.amic.util.date.CheckDate;
23 import org.turro.string.Strings;
24 import org.turro.auth.Authentication;
25 import org.turro.calendar.event.UserCalendarEvent;
26 import org.turro.command.Command;
27 import org.turro.command.Context;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.util.Components;
30 import org.turro.elephant.util.DateFormats;
31 import org.turro.entities.Entities;
32 import org.turro.entities.IElephantEntity;
33 import org.turro.i18n.I_;
34 import org.turro.plugin.calendar.ICalendarEvent;
35 import org.turro.plugin.command.CalendarEventPlugin;
36 import org.turro.util.Chars;
37 import org.zkoss.calendar.event.CalendarsEvent;
38 import org.zkoss.zk.ui.Component;
39 import org.zkoss.zk.ui.Executions;
40 import org.zkoss.zk.ui.event.Event;
41 import org.zkoss.zk.ui.event.Events;
42 import org.zkoss.zk.ui.event.ForwardEvent;
43 import org.zkoss.zk.ui.util.Clients;
44 import org.zkoss.zk.ui.util.GenericForwardComposer;
45 import org.zkoss.zul.Datebox;
46 import org.zkoss.zul.Label;
47 import org.zkoss.zul.Window;
48 
53 public class CalendarComposer extends GenericForwardComposer<Component> {
54 
55  private CalendarView calendar;
56  private Label currentDate;
57  private Datebox currentBox;
58  private ScheduleListbox schedule;
59  private CalendarEventCallback calendarEventCallback;
60 
61  public void onClick$butDay() {
62  calendar.setMode(CalendarMode.DAY_MODE);
63  calendar.refreshModel();
64  setCurrentDate();
65  }
66 
67  public void onClick$butWeek() {
68  calendar.setMode(CalendarMode.WEEK_MODE);
69  calendar.refreshModel();
70  setCurrentDate();
71  }
72 
73  public void onClick$butMonth() {
75  calendar.refreshModel();
76  setCurrentDate();
77  }
78 
79  public void onClick$butPrior() {
80  calendar.previousPage();
81  setCurrentDate();
82  }
83 
84  public void onClick$butNext() {
85  calendar.nextPage();
86  setCurrentDate();
87  }
88 
89  public void onClick$butToday() {
90  calendar.setCurrentDate(java.util.Calendar.getInstance().getTime());
91  calendar.refreshModel();
92  setCurrentDate();
93  }
94 
95  public void onClick$butRefresh() {
96  calendar.refreshModel();
97  schedule.refresh();
98  setCurrentDate();
99  }
100 
101  public void onClick$butCopy() {
102  Clients.showNotification(
103  I_.get("Copy to clipboard (Ctrl-C)") +
104  ":<br/><br/>" +
105  ElephantContext.getServerUrl("http") +
106  "/_reqxdc_/calendar?ses=" +
107  Strings.obfuscate(Authentication.getIContact().getId()),
108  true);
109  }
110 
111  public void onSelect$schedule() {
112  ICalendarEvent ce = schedule.getObjectValue();
113  if(ce != null) {
114  calendar.setCurrentDate(ce.getBeginDate());
115  calendar.refreshModel();
116  setCurrentDate();
117  }
118  }
119 
120  public void onDoubleClick$schedule() {
121  ICalendarEvent ce = schedule.getObjectValue();
122  if(ce != null) {
123  if(!Strings.isBlank(ce.getPath())) {
125  Object entity = iee.getEntity();
126  if(entity != null) {
127  iee.showEntity();
128  } else {
129  iee.showPath();
130  }
131  }
132  }
133  }
134 
135  public void onEventEdit$calendar(CalendarsEvent event) {
136  UserCalendarEvent calendarEvent = (UserCalendarEvent) event.getCalendarEvent();
137  if(!Strings.isBlank(calendarEvent.getPath())) {
138  IElephantEntity iee = Entities.getController(calendarEvent.getPath());
139  Object entity = iee.getEntity();
140  if(entity != null) {
141  iee.showEntity();
142  } else {
143  iee.showPath();
144  }
145  }
146  }
147 
148  public void onEventCreate$calendar(CalendarsEvent event) {
149  if(calendarEventCallback != null) {
150  calendarEventCallback.onCreate(event.getBeginDate(), event.getEndDate());
151  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(calendar).parent(Window.class)));
152  schedule.refresh();
153  } else {
154  new CalendarProcess(
157  null, null,
158  event.getBeginDate(), event.getEndDate(),
160  ).process(new Command() {
161  @Override
162  public Object execute(Context context) {
163  calendar.refreshModel();
164  setCurrentDate();
165  schedule.refresh();
166  return null;
167  }
168  });
169  }
170  }
171 
172  public void onEventUpdate$calendar(CalendarsEvent event) {
173  UserCalendarEvent calendarEvent = (UserCalendarEvent) event.getCalendarEvent();
174  if(!Strings.isBlank(calendarEvent.getPath())) {
175  if(calendarEventCallback != null && calendarEvent.getPath().equals(calendarEventCallback.getPath())) {
176  calendarEventCallback.onUpdate(event.getBeginDate(), event.getEndDate());
177  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(calendar).parent(Window.class)));
178  schedule.refresh();
179  setTooltips();
180  } else {
181  new CalendarProcess(
184  calendarEvent.getBeginDate(), calendarEvent.getEndDate(),
185  event.getBeginDate(), event.getEndDate(),
186  calendarEvent.getPath(), CalendarEventPlugin.UPDATE)
187  ).process(new Command() {
188  @Override
189  public Object execute(Context context) {
190  calendar.refreshModel();
191  schedule.refresh();
192  setTooltips();
193  return null;
194  }
195  });
196  }
197  }
198  }
199 
200  public void onDayClick$calendar(ForwardEvent event) {
201  calendar.setCurrentDate((Date) event.getOrigin().getData());
202  if(calendar.getMode() == CalendarMode.MONTH_MODE) {
203  calendar.setMode(CalendarMode.WEEK_MODE);
204  } else if(calendar.getMode() == CalendarMode.WEEK_MODE) {
205  calendar.setMode(CalendarMode.DAY_MODE);
206  }
207  calendar.refreshModel();
208  setCurrentDate();
209  }
210 
211  public void onChange$currentBox() {
212  calendar.setCurrentDate(currentBox.getValue());
213  calendar.refreshModel();
214  setCurrentDate();
215  }
216 
217  private void setCurrentDate() {
218  Date curr = calendar.getCurrentDate();
219  if(calendar.getMode() == CalendarMode.DAY_MODE) {
220  currentDate.setValue(DateFormats.format(curr, true));
221  currentBox.setValue(curr);
222  } else if(calendar.getMode() == CalendarMode.WEEK_MODE) {
223  currentDate.setValue(
224  DateFormats.format(calendar.getBeginDate(), true) +
225  Chars.forward().spaced().toString() +
226  DateFormats.format(calendar.getEndDate(), true));
227  currentBox.setValue(calendar.getBeginDate());
228  } else if(calendar.getMode() == CalendarMode.MONTH_MODE) {
229  currentDate.setValue(DateFormats.monthName(curr) +
230  Chars.forward().spaced().toString() +
231  new CheckDate(curr).getYear());
232  currentBox.setValue(calendar.getBeginDate());
233  }
234  setTooltips();
235  }
236 
237  @Override
238  public void doAfterCompose(Component comp) throws Exception {
239  super.doAfterCompose(comp);
240  calendarEventCallback = (CalendarEventCallback) Executions.getCurrent().getArg().get("eventCallback");
241  setCurrentDate();
242  schedule.refresh();
243  }
244 
245  private void setTooltips() {
246  Clients.evalJavaScript("setTimeout(addTooltips, 1000);");
247 // Clients.evalJavaScript("$(\".z-calevent\").each(function(){\n" +
248 //" var tooltip = $(\".z-calevent-header\", this).text() + \": \" + $(\".z-calevent-text\", this).text();\n" +
249 //" $(this).attr(\"title\", tooltip); \n" +
250 //" });");
251 // Clients.evalJavaScript("$(\".z-calevent-daylong-month\").each(function(){\n" +
252 //" var tooltip = $(\".z-calevent-text\", this).text();\n" +
253 //" $(this).attr(\"title\", tooltip); \n" +
254 //" });");
255 // Clients.evalJavaScript("$(\".z-calevent-month\").each(function(){\n" +
256 //" var tooltip = $(\".z-calevent-header\", this).text() + \": \" + $(\".z-calevent-cnt\", this).text();\n" +
257 //" $(this).attr(\"title\", tooltip); \n" +
258 //" });");
259  }
260 
261 }
void onEventEdit $calendar(CalendarsEvent event)
void onDayClick $calendar(ForwardEvent event)
void onEventUpdate $calendar(CalendarsEvent event)
void onEventCreate $calendar(CalendarsEvent event)
abstract void onUpdate(Date from, Date to)
abstract void onCreate(Date from, Date to)
void setMode(CalendarMode mode)
static String getServerUrl(String scheme)
static Components from(Object component)
Definition: Components.java:46
static final String format(Date d, boolean dateOnly)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
static Collection< Object > getCalendarEventFor(IContact contact, Date oldFrom, Date oldTo, Date from, Date to, String path, int mode)