BrightSide Workbench Full Report + Source Code
ConstrainsLayout.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.scheduler.model;
20 
21 import java.text.DateFormatSymbols;
22 import org.turro.elephant.context.Application;
23 import org.turro.i18n.I_;
24 import org.turro.scheduler.task.AbstractTask;
25 import org.turro.scheduler.task.constraints.Month;
26 import org.turro.scheduler.task.constraints.WeekDay;
27 import org.turro.string.Strings;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.EventListener;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zul.Caption;
32 import org.zkoss.zul.Checkbox;
33 import org.zkoss.zul.Groupbox;
34 import org.zkoss.zul.Hlayout;
35 import org.zkoss.zul.Vlayout;
36 
41 public class ConstrainsLayout extends Hlayout {
42 
43  private final DateFormatSymbols dfs = DateFormatSymbols.getInstance(Application.getUsedLocale());
44  private final Checkbox[] months = new Checkbox[12];
45  private final Checkbox[] daysOfMonth = new Checkbox[31];
46  private final Checkbox[] weekDays = new Checkbox[7];
47  private final Checkbox[] hours = new Checkbox[24];
48  private final Checkbox[] minutes = new Checkbox[12];
49  private AbstractTask task;
50 
51  public ConstrainsLayout() {
52  initMonth();
53  initDayOfMonth();
54  initWeekDays();
55  initHours();
56  initMinutes();
57  }
58 
59  public void setTask(AbstractTask task) {
60  this.task = task;
61  if(task != null) {
62  for(Month month : Month.values()) {
63  months[month.ordinal()].setChecked(task.getSettings().getConstraints().getMonths().contains(month));
64  months[month.ordinal()].setDisabled(task.isSystem());
65  }
66  for(int x = 0; x < 31; x++) {
67  daysOfMonth[x].setChecked(task.getSettings().getConstraints().getDaysOfMonth().contains(x + 1));
68  daysOfMonth[x].setDisabled(task.isSystem());
69  }
70  for(WeekDay weekDay : WeekDay.values()) {
71  weekDays[weekDay.ordinal()].setChecked(task.getSettings().getConstraints().getWeekDays().contains(weekDay));
72  weekDays[weekDay.ordinal()].setDisabled(task.isSystem());
73  }
74  for(int x = 0; x < 24; x++) {
75  hours[x].setChecked(task.getSettings().getConstraints().getHours().contains(x));
76  hours[x].setDisabled(task.isSystem());
77  }
78  for(int x = 0; x < 12; x++) {
79  minutes[x].setChecked(task.getSettings().getConstraints().getMinutes().contains(x * 5));
80  minutes[x].setDisabled(task.isSystem());
81  }
82  }
83  }
84 
85  private void initMonth() {
86  EventListener<? extends Event> listener = (Event event) -> {
87  Checkbox ck = (Checkbox) event.getTarget();
88  if(ck.isChecked()) {
89  task.getSettings().getConstraints().getMonths().add(ck.getValue());
90  } else {
91  task.getSettings().getConstraints().getMonths().remove(ck.getValue());
92  }
93  };
94  Groupbox box = new Groupbox();
95  appendChild(box);
96  box.setMold("3d");
97  box.appendChild(new Caption(I_.get("Months")));
98  Vlayout vbox = new Vlayout();
99  box.appendChild(vbox);
100  for(Month m : Month.values()) {
101  months[m.ordinal()] = new Checkbox(Strings.capitalize(dfs.getMonths()[m.ordinal()]));
102  months[m.ordinal()].setValue(m);
103  months[m.ordinal()].addEventListener(Events.ON_CHECK, listener);
104  vbox.appendChild(months[m.ordinal()]);
105  }
106  }
107 
108  private void initDayOfMonth() {
109  EventListener<? extends Event> listener = (Event event) -> {
110  Checkbox ck = (Checkbox) event.getTarget();
111  if(ck.isChecked()) {
112  task.getSettings().getConstraints().getDaysOfMonth().add(ck.getValue());
113  } else {
114  task.getSettings().getConstraints().getDaysOfMonth().remove(ck.getValue());
115  }
116  };
117  Groupbox box = new Groupbox();
118  appendChild(box);
119  box.setMold("3d");
120  box.appendChild(new Caption(I_.get("Days of month")));
121  Hlayout hbox = new Hlayout();
122  box.appendChild(hbox);
123  Vlayout vbox = null;
124  for(int x = 0; x < 31; x++) {
125  if(x % 11 == 0) {
126  vbox = new Vlayout();
127  hbox.appendChild(vbox);
128  }
129  daysOfMonth[x] = new Checkbox((x + 1) + "");
130  daysOfMonth[x].setValue(x + 1);
131  daysOfMonth[x].addEventListener(Events.ON_CHECK, listener);
132  vbox.appendChild(daysOfMonth[x]);
133  }
134  }
135 
136  private void initWeekDays() {
137  EventListener<? extends Event> listener = (Event event) -> {
138  Checkbox ck = (Checkbox) event.getTarget();
139  if(ck.isChecked()) {
140  task.getSettings().getConstraints().getWeekDays().add(ck.getValue());
141  } else {
142  task.getSettings().getConstraints().getWeekDays().remove(ck.getValue());
143  }
144  };
145  Groupbox box = new Groupbox();
146  appendChild(box);
147  box.setMold("3d");
148  box.appendChild(new Caption(I_.get("Days of week")));
149  Vlayout vbox = new Vlayout();
150  box.appendChild(vbox);
151  for(WeekDay wd : WeekDay.values()) {
152  weekDays[wd.ordinal()] = new Checkbox(Strings.capitalize(dfs.getWeekdays()[WeekDay.map(wd.ordinal())]));
153  weekDays[wd.ordinal()].setValue(wd);
154  weekDays[wd.ordinal()].addEventListener(Events.ON_CHECK, listener);
155  vbox.appendChild(weekDays[wd.ordinal()]);
156  }
157  }
158 
159  private void initHours() {
160  EventListener<? extends Event> listener = (Event event) -> {
161  Checkbox ck = (Checkbox) event.getTarget();
162  if(ck.isChecked()) {
163  task.getSettings().getConstraints().getHours().add(ck.getValue());
164  } else {
165  task.getSettings().getConstraints().getHours().remove(ck.getValue());
166  }
167  };
168  Groupbox box = new Groupbox();
169  appendChild(box);
170  box.setMold("3d");
171  box.appendChild(new Caption(I_.get("Hours")));
172  Hlayout hbox = new Hlayout();
173  box.appendChild(hbox);
174  Vlayout vbox = null;
175  for(int x = 0; x < 24; x++) {
176  if(x % 12 == 0) {
177  vbox = new Vlayout();
178  hbox.appendChild(vbox);
179  }
180  hours[x] = new Checkbox(x + "");
181  hours[x].setValue(x);
182  hours[x].addEventListener(Events.ON_CHECK, listener);
183  vbox.appendChild(hours[x]);
184  }
185  }
186 
187  private void initMinutes() {
188  EventListener<? extends Event> listener = (Event event) -> {
189  Checkbox ck = (Checkbox) event.getTarget();
190  if(ck.isChecked()) {
191  task.getSettings().getConstraints().getMinutes().add(ck.getValue());
192  } else {
193  task.getSettings().getConstraints().getMinutes().remove(ck.getValue());
194  }
195  };
196  Groupbox box = new Groupbox();
197  appendChild(box);
198  box.setMold("3d");
199  box.appendChild(new Caption(I_.get("Minutes")));
200  Vlayout vbox = new Vlayout();
201  box.appendChild(vbox);
202  for(int x = 0; x < 12; x++) {
203  minutes[x] = new Checkbox((x * 5) + "");
204  minutes[x].setValue(x * 5);
205  minutes[x].addEventListener(Events.ON_CHECK, listener);
206  vbox.appendChild(minutes[x]);
207  }
208  }
209 
210 }