BrightSide Workbench Full Report + Source Code
TaskEdit.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.scheduler.zul;
19 
20 import java.text.DateFormatSymbols;
21 import org.turro.string.Strings;
22 import org.turro.elephant.context.Application;
23 import org.turro.elephant.util.Components;
24 import org.turro.i18n.I_;
25 import org.turro.scheduler.constraints.Month;
26 import org.turro.scheduler.constraints.WeekDay;
27 import org.turro.scheduler.motor.DefaultTask;
28 import org.turro.scheduler.motor.Motor;
29 import org.turro.scheduler.motor.ScheduledSet;
30 import org.turro.scheduler.motor.ScheduledTask;
31 import org.turro.zkoss.dialog.SelectionDialog;
32 import org.turro.zkoss.input.DateboxShort;
33 import org.zkoss.zk.ui.event.Event;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zul.Button;
37 import org.zkoss.zul.Caption;
38 import org.zkoss.zul.Checkbox;
39 import org.zkoss.zul.Datebox;
40 import org.zkoss.zul.Groupbox;
41 import org.zkoss.zul.Hbox;
42 import org.zkoss.zul.Label;
43 import org.zkoss.zul.Panel;
44 import org.zkoss.zul.Panelchildren;
45 import org.zkoss.zul.Textbox;
46 import org.zkoss.zul.Vbox;
47 
52 @Deprecated
53 public class TaskEdit extends Panel {
54 
55  private ScheduledTask scheduledTask;
56 
57  private Panelchildren children;
58  private Textbox description, data;
59  private Checkbox months[], weekDays[], daysOfMonth[],
60  hours[], minutes[], active;
61  private Datebox startDate, endDate;
62 
63  public TaskEdit(ScheduledTask scheduledTask) {
64  this.scheduledTask = scheduledTask;
65  children = new Panelchildren();
66  children.setStyle("padding:10px");
67  appendChild(children);
68  addFields();
69  }
70 
71  private void addFields() {
72 
73  DateFormatSymbols dfs = DateFormatSymbols.getInstance(Application.getUsedLocale());
74 
75  children.getChildren().clear();
76 
77  Vbox parentBox = new Vbox();
78  parentBox.setSpacing("5px");
79  children.appendChild(parentBox);
80 
81  Hbox hbox = new Hbox();
82  hbox.setSpacing("5px");
83  parentBox.appendChild(hbox);
84 
85  hbox.appendChild(new Label(I_.get("Description")));
86 
87  description = new Textbox(scheduledTask.getDescription());
88  description.setCols(80);
89  hbox.appendChild(description);
90 
91  if(scheduledTask.getDataLabel() != null) {
92  hbox = new Hbox();
93  hbox.setSpacing("5px");
94  parentBox.appendChild(hbox);
95 
96  hbox.appendChild(new Label(scheduledTask.getDataLabel()));
97 
98  data = new Textbox(scheduledTask.getData());
99  data.setCols(80);
100  hbox.appendChild(data);
101  }
102 
103  hbox = new Hbox();
104  hbox.setSpacing("5px");
105  parentBox.appendChild(hbox);
106 
107  Groupbox gb = new Groupbox();
108  gb.setMold("3d");
109  hbox.appendChild(gb);
110 
111  Caption caption = new Caption(I_.get("Months"));
112  gb.appendChild(caption);
113 
114  Vbox vbox = new Vbox();
115  gb.appendChild(vbox);
116 
117  months = new Checkbox[12];
118  int i = 0;
119  for(Month m : Month.values()) {
120  months[i] = new Checkbox(Strings.capitalize(dfs.getMonths()[i]));
121  months[i].setChecked(((DefaultTask) scheduledTask).getConstraints().getMonths().contains(m));
122  vbox.appendChild(months[i]);
123  i++;
124  }
125 
126  gb = new Groupbox();
127  gb.setMold("3d");
128  hbox.appendChild(gb);
129 
130  caption = new Caption(I_.get("Days of month"));
131  gb.appendChild(caption);
132 
133  Hbox hb = new Hbox();
134  gb.appendChild(hb);
135 
136  daysOfMonth = new Checkbox[31];
137  for(int x = 0; x < 31; x++) {
138  if(x % 11 == 0) {
139  vbox = new Vbox();
140  hb.appendChild(vbox);
141  }
142  daysOfMonth[x] = new Checkbox((x + 1) + "");
143  daysOfMonth[x].setChecked(((DefaultTask) scheduledTask).getConstraints().getDaysOfMonth().contains(x + 1));
144  vbox.appendChild(daysOfMonth[x]);
145  }
146 
147  gb = new Groupbox();
148  gb.setMold("3d");
149  hbox.appendChild(gb);
150 
151  caption = new Caption(I_.get("Days of week"));
152  gb.appendChild(caption);
153 
154  vbox = new Vbox();
155  gb.appendChild(vbox);
156 
157  weekDays = new Checkbox[7];
158  i = 0;
159  for(WeekDay wd : WeekDay.values()) {
160  weekDays[i] = new Checkbox(Strings.capitalize(dfs.getWeekdays()[WeekDay.map(i)]));
161  weekDays[i].setChecked(((DefaultTask) scheduledTask).getConstraints().getWeekDays().contains(wd));
162  vbox.appendChild(weekDays[i]);
163  i++;
164  }
165 
166  gb = new Groupbox();
167  gb.setMold("3d");
168  hbox.appendChild(gb);
169 
170  caption = new Caption(I_.get("Hours"));
171  gb.appendChild(caption);
172 
173  hb = new Hbox();
174  gb.appendChild(hb);
175 
176  hours = new Checkbox[24];
177  for(int x = 0; x < 24; x++) {
178  if(x % 12 == 0) {
179  vbox = new Vbox();
180  hb.appendChild(vbox);
181  }
182  hours[x] = new Checkbox(x + "");
183  hours[x].setChecked(((DefaultTask) scheduledTask).getConstraints().getHours().contains(x));
184  vbox.appendChild(hours[x]);
185  }
186 
187  gb = new Groupbox();
188  gb.setMold("3d");
189  hbox.appendChild(gb);
190 
191  caption = new Caption(I_.get("Minutes"));
192  gb.appendChild(caption);
193 
194  vbox = new Vbox();
195  gb.appendChild(vbox);
196 
197  minutes = new Checkbox[12];
198  for(int x = 0; x < 12; x++) {
199  minutes[x] = new Checkbox((x * 5) + "");
200  minutes[x].setChecked(((DefaultTask) scheduledTask).getConstraints().getMinutes().contains((x * 5)));
201  vbox.appendChild(minutes[x]);
202  }
203 
204  gb = new Groupbox();
205  gb.setMold("3d");
206  hbox.appendChild(gb);
207 
208  vbox = new Vbox();
209  gb.appendChild(vbox);
210 
211  active = new Checkbox(I_.get("Active"));
212  active.setChecked(scheduledTask.isActive());
213  vbox.appendChild(active);
214 
215  hb = new Hbox();
216  vbox.appendChild(hb);
217 
218  Vbox vb = new Vbox();
219  hb.appendChild(vb);
220 
221  vb.appendChild(new Label(I_.get("Start date")));
222  vb.appendChild(new Label(I_.get("End date")));
223 
224  vb = new Vbox();
225  hb.appendChild(vb);
226 
227  startDate = new DateboxShort(scheduledTask.getStartDate());
228  vb.appendChild(startDate);
229 
230  endDate = new DateboxShort(scheduledTask.getEndDate());
231  vb.appendChild(endDate);
232 
233  hbox = new Hbox();
234  hbox.setSpacing("5px");
235  parentBox.appendChild(hbox);
236 
237  Button saveButton = new Button(
238  I_.get("Save"),
239  "/_zul/images/save.png"
240  );
241  saveButton.addEventListener(Events.ON_CLICK, new EventListener() {
242  @Override
243  public void onEvent(Event event) throws Exception {
244  doSave();
245  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(TaskEdit.this).parent(SelectionDialog.class)));
246  }
247 
248  });
249  hbox.appendChild(saveButton);
250 
251  Button saveRunButton = new Button(
252  I_.get("Save and run"),
253  "/_zul/images/service.png"
254  );
255  saveRunButton.addEventListener(Events.ON_CLICK, new EventListener() {
256  @Override
257  public void onEvent(Event event) throws Exception {
258  doSave();
259  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(TaskEdit.this).parent(SelectionDialog.class)));
260  if(scheduledTask.isDone()) {
261  scheduledTask.doRun(Motor.getInstance());
262  }
263  }
264 
265  });
266  hbox.appendChild(saveRunButton);
267 
268  Button cancelButton = new Button(
269  I_.get("Cancel"),
270  null
271  );
272  cancelButton.addEventListener(Events.ON_CLICK, new EventListener() {
273  @Override
274  public void onEvent(Event event) throws Exception {
275  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(TaskEdit.this).parent(SelectionDialog.class)));
276  }
277  });
278  hbox.appendChild(cancelButton);
279  }
280 
281  private void doSave() {
282  scheduledTask.setDescription(description.getText());
283  if(scheduledTask.getDataLabel() != null) {
284  scheduledTask.setData(data.getText());
285  }
286 
287  ((DefaultTask) scheduledTask).getConstraints().getMonths().clear();
288  int i = 0;
289  for(Month m : Month.values()) {
290  if(months[i].isChecked()) {
291  ((DefaultTask) scheduledTask).getConstraints().getMonths().add(m);
292  }
293  i++;
294  }
295 
296  ((DefaultTask) scheduledTask).getConstraints().getDaysOfMonth().clear();
297  for(int x = 0; x < 31; x++) {
298  if(daysOfMonth[x].isChecked()) {
299  ((DefaultTask) scheduledTask).getConstraints().getDaysOfMonth().add(x + 1);
300  }
301  }
302 
303  ((DefaultTask) scheduledTask).getConstraints().getWeekDays().clear();
304  i = 0;
305  for(WeekDay wd : WeekDay.values()) {
306  if(weekDays[i].isChecked()) {
307  ((DefaultTask) scheduledTask).getConstraints().getWeekDays().add(wd);
308  }
309  i++;
310  }
311 
312  ((DefaultTask) scheduledTask).getConstraints().getHours().clear();
313  for(int x = 0; x < 24; x++) {
314  if(hours[x].isChecked()) {
315  ((DefaultTask) scheduledTask).getConstraints().getHours().add(x);
316  }
317  }
318 
319  ((DefaultTask) scheduledTask).getConstraints().getMinutes().clear();
320  for(int x = 0; x < 12; x++) {
321  if(minutes[x].isChecked()) {
322  ((DefaultTask) scheduledTask).getConstraints().getMinutes().add(x * 5);
323  }
324  }
325 
326  scheduledTask.setStartDate(startDate.getValue());
327  scheduledTask.setEndDate(endDate.getValue());
328  scheduledTask.setActive(active.isChecked());
329 
330  ScheduledSet.saveSet(Application.getApplication().getConstructor(), Motor.getInstance().getTasks());
331  }
332 
333 }
static String get(String msg)
Definition: I_.java:41
TaskEdit(ScheduledTask scheduledTask)
Definition: TaskEdit.java:63