BrightSide Workbench Full Report + Source Code
MyDataComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.contacts.mydata;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.string.Strings;
28 import org.apache.commons.io.input.ReaderInputStream;
29 import org.turro.auth.Authentication;
30 import org.turro.contacts.db.ContactsPU;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.util.Images;
33 import org.turro.i18n.I_;
34 import org.turro.zkoss.layout.GridLayout;
35 import org.zkoss.util.media.Media;
36 import org.zkoss.zk.ui.Component;
37 import org.zkoss.zk.ui.event.EventListener;
38 import org.zkoss.zk.ui.event.Events;
39 import org.zkoss.zk.ui.event.UploadEvent;
40 import org.zkoss.zk.ui.util.Clients;
41 import org.zkoss.zk.ui.util.GenericForwardComposer;
42 import org.zkoss.zul.Button;
43 import org.zkoss.zul.Hlayout;
44 import org.zkoss.zul.Image;
45 import org.zkoss.zul.Space;
46 import org.zkoss.zul.Textbox;
47 
52 @Deprecated
53 public class MyDataComposer extends GenericForwardComposer<Component> {
54 
55  private MyDataList myDataList;
56 
57  private GridLayout myDataForm;
58 
59  public void onClick$save() {
60  try {
61  myDataList.saveItems();
63  Clients.showNotification(I_.get("Saved"));
64  } catch (IOException ex) {
65  Logger.getLogger(MyDataComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
66  }
67  }
68 
69  @Override
70  public void doFinally() throws Exception {
71  super.doFinally();
72  myDataList = new MyDataList(Authentication.getIContact());
73  fillGrid();
74  }
75 
76  private void fillGrid() {
77  for(final MyDataItem item : myDataList) {
78  myDataForm.addRow();
79  if(!Strings.isBlank(item.getLabel())) {
80  myDataForm.addCaption(I_.get(item.getLabel()));
81  } else {
82  myDataForm.addCaption(item.getName());
83  }
84  if(item.getType().equals(MyDataItemType.MYDATA_IMAGE)) {
85  Hlayout hbox = new Hlayout();
86  Button upload = new Button(I_.get("Upload"));
87  upload.setUpload("true,maxsize=300");
88  String image = "/_internal/files" + ContactsPU.getObjectPath(
89  Authentication.getIContact().getContact()) + item.getValue();
90  File newFile = new File(ElephantContext.getRealPath(image));
91  upload.addEventListener(Events.ON_UPLOAD, new EventListener<UploadEvent>() {
92  @Override
93  public void onEvent(UploadEvent t) throws Exception {
94  if(!newFile.getParentFile().exists()) {
95  newFile.getParentFile().mkdirs();
96  }
97  try (FileOutputStream fos = new FileOutputStream(newFile)) {
98  Media media = t.getMedia();
99  if(media.inMemory()) {
100  fos.write(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
101  } else {
102  byte[] buffer = new byte[102400];
103  InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData());
104  int r;
105  while((r = is.read(buffer)) != -1) {
106  fos.write(buffer, 0, r);
107  }
108  is.close();
109  }
110  }
111  }
112  });
113  if(newFile.exists()) {
114  Image itmp = new Image(image);
115  itmp.setStyle("max-width:300px;max-heght:300px;");
116  hbox.appendChild(itmp);
117  }
118  hbox.appendChild(upload);
119  myDataForm.addComponent(hbox);
120  } else if(item.getType().equals(MyDataItemType.MYDATA_PASSWORD)) {
121  Textbox input = new Textbox();
122  if(!Strings.isBlank(item.getName())) {
123  input.setName(item.getName());
124  }
125  input.setType("password");
126  if(item.getCols() == 0) {
127  input.setHflex("1");
128  } else {
129  input.setCols(item.getCols());
130  }
131  input.setValue("");
132  item.setInput(input);
133  myDataForm.addComponent(input);
134  } else {
135  Textbox input = new Textbox();
136  if(!Strings.isBlank(item.getName())) {
137  input.setName(item.getName());
138  }
139  if(item.getCols() == 0) {
140  input.setHflex("1");
141  } else {
142  input.setCols(item.getCols());
143  }
144  input.setValue(item.getValue());
145  input.setReadonly(item.isReadonly());
146  item.setInput(input);
147  myDataForm.addComponent(input);
148  }
149  if(item.isRequired()) {
150  myDataForm.addComponent(new Image(Images.getImage("required")));
151  } else {
152  myDataForm.addComponent(new Space());
153  }
154  }
155  }
156 
157 }
static String get(String msg)
Definition: I_.java:41
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout addCaption(String label)