BrightSide Workbench Full Report + Source Code
XMLTimeControlComposer.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.erp.time;
20 
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.InputStream;
25 import java.util.Date;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.amic.util.date.CheckDate;
29 import org.apache.commons.io.input.ReaderInputStream;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.impl.util.FileUtil;
33 import org.turro.elephant.util.ZkossUtils;
34 import org.zkoss.text.DateFormats;
35 import org.zkoss.util.media.Media;
36 import org.zkoss.zk.ui.Component;
37 import org.zkoss.zk.ui.event.Event;
38 import org.zkoss.zk.ui.select.SelectorComposer;
39 import org.zkoss.zk.ui.select.annotation.Listen;
40 import org.zkoss.zk.ui.select.annotation.Wire;
41 import org.zkoss.zul.Button;
42 import org.zkoss.zul.Filedownload;
43 import org.zkoss.zul.Fileupload;
44 import org.zkoss.zul.Hlayout;
45 import org.zkoss.zul.Image;
46 import org.zkoss.zul.Label;
47 import org.zkoss.zul.Tab;
48 import org.zkoss.zul.Vbox;
49 
54 public class XMLTimeControlComposer extends SelectorComposer<Component> {
55 
56  @Wire private Image logo;
57 
58  @Wire private Tab tabHr;
59  @Wire private Tab tabTask;
60  @Wire private Tab tabReport;
61  @Wire private Tab tabKey;
62  @Wire private Hlayout info;
63 
64  @Wire private XMLHumanResourceSelector hrList;
65  @Wire private XMLTaskSelector taskList;
66  @Wire private XMLTimeList timeList;
67 
68  @Wire private Label currDay;
69 
70  @Wire private Vbox numberBox;
71 
72  private XMLHumanResource selected;
73  private Date currDate = new Date();
74  private String key, secret;
75 
76  @Listen("onChange = #hrList")
77  public void onHumanResourceSelect(Event evt) {
78  selected = (XMLHumanResource) evt.getData();
79  if(selected != null) {
80  taskList.setHumanResource(selected);
81  info.getChildren().clear();
82  info.appendChild(new Label(selected.getName()));
83  tabTask.setSelected(true);
84  }
85  }
86 
87  @Listen("onChange = #taskList")
88  public void onChange(Event evt) {
89  Object obj = evt.getData();
90  if(obj instanceof XMLTimeControl) {
91  XMLTimeControl tc = (XMLTimeControl) obj;
93  tcu.end(tc);
94  hrList.setStartsWith(null);
95  hrList.afterCompose();
96  tabHr.setSelected(true);
97  }
98  }
99 
100  @Listen("onUser = #taskList")
101  public void onStart() {
102  if(selected != null) {
104  tcu.setHumanResource(selected);
105  tcu.start();
106  hrList.setStartsWith(null);
107  hrList.afterCompose();
108  tabHr.setSelected(true);
109  }
110  }
111 
112  @Listen("onCancel = #taskList")
113  public void onCancelSelect() {
114  tabHr.setSelected(true);
115  }
116 
117  @Listen("onClick = #import")
118  public void onImport() {
120  FileUtil.getFolderFile(file).mkdirs();
121  try {
122  Media media = Fileupload.get(true);
123  if(media != null) {
124  FileOutputStream fos = new FileOutputStream(file);
125  if(media.inMemory()) {
126  fos.write(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
127  } else {
128  byte[] buffer = new byte[102400];
129  InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData());
130  int r;
131  while((r = is.read(buffer)) != -1) {
132  fos.write(buffer, 0, r);
133  }
134  is.close();
135  }
136  fos.close();
137  hrList.setStartsWith(null);
138  hrList.afterCompose();
139  tabHr.setSelected(true);
140  }
141  } catch (Exception ex) {
142  Logger.getLogger(XMLTimeControlComposer.class.getName()).log(Level.SEVERE, null, ex);
143  }
144  }
145 
146  @Listen("onClick = #export")
147  public void onExport() {
149  FileUtil.getFolderFile(file).mkdirs();
150  if(file.exists()) {
151  try {
152  Filedownload.save(file, "text/xml");
153  //if(ZkossUtils.confirmDeletion(XMLTimeControlSet.XML_TIMECONTROL)) {
155  tcu.deleteCompleted();
156  //}
157  } catch (FileNotFoundException ex) {
158  Logger.getLogger(XMLTimeControlComposer.class.getName()).log(Level.SEVERE, null, ex);
159  }
160  hrList.setStartsWith(null);
161  hrList.afterCompose();
162  tabHr.setSelected(true);
163  }
164  }
165 
166  @Listen("onClick = #logo")
167  public void onReport() {
168  secret = "";
169  tabKey.setSelected(true);
170  }
171 
172  @Listen("onClick = button.kbabutton")
173  public void onLetter(Event evt) {
174  hrList.setFocus(true);
175  hrList.setStartsWith(((Button) evt.getTarget()).getLabel());
176  }
177 
178  @Listen("onClick = #prevDay")
179  public void onPrevious() {
180  currDate = new CheckDate(currDate).addDays(-1).getDate();
181  currDay.setValue(DateFormats.format(currDate, true));
182  timeList.setDate(currDate);
183  }
184 
185  @Listen("onClick = #nextDay")
186  public void onNext() {
187  currDate = new CheckDate(currDate).addDays(1).getDate();
188  currDay.setValue(DateFormats.format(currDate, true));
189  timeList.setDate(currDate);
190  }
191 
192  @Listen("onClick = #back")
193  public void onBack() {
194  hrList.setStartsWith(null);
195  tabHr.setSelected(true);
196  }
197 
198  @Listen("onClick = button.kbnbutton")
199  public void onNumber(Event evt) {
200  secret += ((Button) evt.getTarget()).getLabel();
201  if(secret.equals(key)) {
202  tabReport.setSelected(true);
203  currDay.setValue(DateFormats.format(currDate, true));
204  timeList.setDate(currDate);
205  } else if(key.length() <= secret.length()) {
206  tabHr.setSelected(true);
207  }
208  }
209 
210  @Override
211  public void doAfterCompose(Component comp) throws Exception {
212  super.doAfterCompose(comp);
213  key = numberBox.getStyle().substring(numberBox.getStyle().indexOf(";key:") + 5);
214  hrList.setFocus(true);
215  }
216 
217 }
static File getFolderFile(File file)
Definition: FileUtil.java:290
XMLTimeControl end(XMLTimeControl tc)
void setHumanResource(XMLHumanResource humanResource)