BrightSide Workbench Full Report + Source Code
entities/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.entities;
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.plugin.scheduler.ScheduledEdit;
26 import org.turro.scheduler.constraints.Month;
27 import org.turro.scheduler.constraints.WeekDay;
28 import org.turro.scheduler.entities.DefaultEntity;
29 import org.turro.scheduler.entities.ScheduledEntity;
30 import org.turro.scheduler.entities.ScheduledEntitySet;
31 import org.turro.zkoss.dialog.SelectionDialog;
32 import org.turro.zkoss.input.DateboxShort;
33 import org.zkoss.zk.ui.HtmlBasedComponent;
34 import org.zkoss.zk.ui.event.Event;
35 import org.zkoss.zk.ui.event.EventListener;
36 import org.zkoss.zk.ui.event.Events;
37 import org.zkoss.zul.Button;
38 import org.zkoss.zul.Caption;
39 import org.zkoss.zul.Checkbox;
40 import org.zkoss.zul.Datebox;
41 import org.zkoss.zul.Groupbox;
42 import org.zkoss.zul.Hbox;
43 import org.zkoss.zul.Label;
44 import org.zkoss.zul.Panel;
45 import org.zkoss.zul.Panelchildren;
46 import org.zkoss.zul.Textbox;
47 import org.zkoss.zul.Vbox;
48 
53 public class TaskEdit extends Panel {
54 
55  private ScheduledEntity scheduledEntity;
56  private ScheduledEdit scheduledEdit;
57 
58  private Panelchildren children;
59  private Textbox description, notify;
60  private Checkbox months[], weekDays[], daysOfMonth[], active;
61  private Datebox startDate, endDate;
62 
63  public TaskEdit(ScheduledEntity scheduledEntity, ScheduledEdit scheduledEdit) {
64  this.scheduledEntity = scheduledEntity;
65  this.scheduledEdit = scheduledEdit;
66  children = new Panelchildren();
67  children.setStyle("padding:10px");
68  appendChild(children);
69  addFields();
70  }
71 
72  private void addFields() {
73 
74  DateFormatSymbols dfs = DateFormatSymbols.getInstance(Application.getUsedLocale());
75 
76  children.getChildren().clear();
77 
78  Vbox parentBox = new Vbox();
79  parentBox.setSpacing("5px");
80  children.appendChild(parentBox);
81 
82  Hbox hbox = new Hbox();
83  hbox.setSpacing("5px");
84  parentBox.appendChild(hbox);
85 
86  hbox.appendChild(new Label(I_.get("Description")));
87 
88  description = new Textbox(scheduledEntity.getDescription());
89  description.setCols(80);
90  hbox.appendChild(description);
91 
92 // hbox = new Hbox();
93 // hbox.setSpacing("5px");
94 // parentBox.appendChild(hbox);
95 //
96 // hbox.appendChild(new Label(Application.getString("lNotify")));
97 //
98 // notify = new Textbox(scheduledEntity.getNotify());
99 // notify.setConstraint("/.+@.+\\.[a-z]+/: E-mail");
100 // notify.setCols(80);
101 // hbox.appendChild(notify);
102 
103  if(scheduledEdit != null) {
104  HtmlBasedComponent hbc = (HtmlBasedComponent) scheduledEdit.getDataEditor(scheduledEntity.getData());
105  if(hbc != null) {
106  parentBox.appendChild(hbc);
107  }
108  }
109 
110  hbox = new Hbox();
111  hbox.setSpacing("5px");
112  parentBox.appendChild(hbox);
113 
114  Groupbox gb = new Groupbox();
115  gb.setMold("3d");
116  hbox.appendChild(gb);
117 
118  Caption caption = new Caption(I_.get("Months"));
119  gb.appendChild(caption);
120 
121  Vbox vbox = new Vbox();
122  gb.appendChild(vbox);
123 
124  months = new Checkbox[12];
125  int i = 0;
126  for(Month m : Month.values()) {
127  months[i] = new Checkbox(Strings.capitalize(dfs.getMonths()[i]));
128  months[i].setChecked(((DefaultEntity) scheduledEntity).getConstraints().getMonths().contains(m));
129  vbox.appendChild(months[i]);
130  i++;
131  }
132 
133  gb = new Groupbox();
134  gb.setMold("3d");
135  hbox.appendChild(gb);
136 
137  caption = new Caption(I_.get("Days of month"));
138  gb.appendChild(caption);
139 
140  Hbox hb = new Hbox();
141  gb.appendChild(hb);
142 
143  daysOfMonth = new Checkbox[31];
144  for(int x = 0; x < 31; x++) {
145  if(x % 11 == 0) {
146  vbox = new Vbox();
147  hb.appendChild(vbox);
148  }
149  daysOfMonth[x] = new Checkbox((x + 1) + "");
150  daysOfMonth[x].setChecked(((DefaultEntity) scheduledEntity).getConstraints().getDaysOfMonth().contains(x + 1));
151  vbox.appendChild(daysOfMonth[x]);
152  }
153 
154  gb = new Groupbox();
155  gb.setMold("3d");
156  hbox.appendChild(gb);
157 
158  caption = new Caption(I_.get("Days of week"));
159  gb.appendChild(caption);
160 
161  vbox = new Vbox();
162  gb.appendChild(vbox);
163 
164  weekDays = new Checkbox[7];
165  i = 0;
166  for(WeekDay wd : WeekDay.values()) {
167  weekDays[i] = new Checkbox(Strings.capitalize(dfs.getWeekdays()[WeekDay.map(i)]));
168  weekDays[i].setChecked(((DefaultEntity) scheduledEntity).getConstraints().getWeekDays().contains(wd));
169  vbox.appendChild(weekDays[i]);
170  i++;
171  }
172 
173  gb = new Groupbox();
174  gb.setMold("3d");
175  hbox.appendChild(gb);
176 
177  vbox = new Vbox();
178  gb.appendChild(vbox);
179 
180  active = new Checkbox(I_.get("Active"));
181  active.setChecked(scheduledEntity.isActive());
182  vbox.appendChild(active);
183 
184  hb = new Hbox();
185  vbox.appendChild(hb);
186 
187  Vbox vb = new Vbox();
188  hb.appendChild(vb);
189 
190  vb.appendChild(new Label(I_.get("Start date")));
191  vb.appendChild(new Label(I_.get("End date")));
192 
193  vb = new Vbox();
194  hb.appendChild(vb);
195 
196  startDate = new DateboxShort(scheduledEntity.getStartDate());
197  vb.appendChild(startDate);
198 
199  endDate = new DateboxShort(scheduledEntity.getEndDate());
200  vb.appendChild(endDate);
201 
202  hbox = new Hbox();
203  hbox.setSpacing("5px");
204  parentBox.appendChild(hbox);
205 
206  Button saveButton = new Button(
207  I_.get("Save"),
208  "/_zul/images/save.png"
209  );
210  saveButton.addEventListener(Events.ON_CLICK, new EventListener() {
211 
212  @Override
213  public void onEvent(Event event) throws Exception {
214  doSave();
215  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(TaskEdit.this).parent(SelectionDialog.class)));
216  }
217 
218  });
219  hbox.appendChild(saveButton);
220 
221  Button cancelButton = new Button(
222  I_.get("Cancel"),
223  null
224  );
225  cancelButton.addEventListener(Events.ON_CLICK, new EventListener() {
226  @Override
227  public void onEvent(Event event) throws Exception {
228  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(TaskEdit.this).parent(SelectionDialog.class)));
229  }
230  });
231  hbox.appendChild(cancelButton);
232 
233  Button delButton = new Button(
234  I_.get("Delete"),
235  "/_zul/images/edit-delete.png"
236  );
237  delButton.addEventListener(Events.ON_CLICK, new EventListener() {
238 
239  @Override
240  public void onEvent(Event event) throws Exception {
241  ScheduledEntitySet.removeEntity(scheduledEntity);
242  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(TaskEdit.this).parent(SelectionDialog.class)));
243  }
244 
245  });
246  hbox.appendChild(delButton);
247  }
248 
249  private void doSave() {
250  scheduledEntity.setDescription(description.getText());
251  //scheduledEntity.setNotify(notify.getText());
252  if(scheduledEdit != null) {
253  scheduledEntity.setData(scheduledEdit.getDataEditorValue());
254  }
255 
256  ((DefaultEntity) scheduledEntity).getConstraints().getMonths().clear();
257  int i = 0;
258  for(Month m : Month.values()) {
259  if(months[i].isChecked()) {
260  ((DefaultEntity) scheduledEntity).getConstraints().getMonths().add(m);
261  }
262  i++;
263  }
264 
265  ((DefaultEntity) scheduledEntity).getConstraints().getDaysOfMonth().clear();
266  for(int x = 0; x < 31; x++) {
267  if(daysOfMonth[x].isChecked()) {
268  ((DefaultEntity) scheduledEntity).getConstraints().getDaysOfMonth().add(x + 1);
269  }
270  }
271 
272  ((DefaultEntity) scheduledEntity).getConstraints().getWeekDays().clear();
273  i = 0;
274  for(WeekDay wd : WeekDay.values()) {
275  if(weekDays[i].isChecked()) {
276  ((DefaultEntity) scheduledEntity).getConstraints().getWeekDays().add(wd);
277  }
278  i++;
279  }
280 
281  scheduledEntity.setStartDate(startDate.getValue());
282  scheduledEntity.setEndDate(endDate.getValue());
283  scheduledEntity.setActive(active.isChecked());
284 
285  ScheduledEntitySet.addEntity(scheduledEntity);
286  }
287 
288 }
static String get(String msg)
Definition: I_.java:41
TaskEdit(ScheduledEntity scheduledEntity, ScheduledEdit scheduledEdit)