BrightSide Workbench Full Report + Source Code
Dropupload_6_5_2.java
Go to the documentation of this file.
1 /* Dropupload_6_5_2.java
2 
3  Purpose:
4 
5  Description:
6 
7  History:
8  Thu Jun 21 23:59:59 2012, Created by MontyPan
9 
10 Copyright (C) 2012 Potix Corporation. All Rights Reserved.
11  */
12 package org.zkoss.zkmax.zul;
13 
14 import java.util.Iterator;
15 import java.util.List;
16 import static org.zkoss.lang.Generics.cast;
17 import org.zkoss.lang.Objects;
18 import org.zkoss.util.media.Media;
19 import org.zkoss.zk.au.AuRequest;
20 import org.zkoss.zk.ui.Desktop;
21 import org.zkoss.zk.ui.event.Event;
22 import org.zkoss.zk.ui.event.Events;
23 import org.zkoss.zk.ui.event.UploadEvent;
24 import org.zkoss.zk.ui.sys.ContentRenderer;
25 import org.zkoss.zk.ui.util.Configuration;
26 import org.zkoss.zul.impl.XulElement;
27 
49 @SuppressWarnings("serial")
50 public class Dropupload_6_5_2 extends XulElement {
51  private static final String DEFAULT_DETECTION = "browser";
52 
53  static {
54  addClientEvent(Dropupload_6_5_2.class, Events.ON_UPLOAD, CE_IMPORTANT);
55  }
56 
57  private int _maxsize;
58  private String _viewerClass;
59  private String _detection = DEFAULT_DETECTION;
60  private String _content;
61  private boolean _native;
62 
63  public Dropupload_6_5_2() {}
64 
71  public void setMaxsize(int value) {
72  if (value < 0) {
73  value = -1;
74  }
75 
76  if (_maxsize != value) {
77  _maxsize = value;
78  smartUpdate("maxsize", _maxsize);
79  }
80  }
81 
86  public int getMaxsize() {
87  return _maxsize;
88  }
89 
103  public void setDetection(String value) {
104  if (!_detection.equals(value)) {
105  _detection = value;
106  smartUpdate("detection", _detection);
107  }
108  }
109 
113  public String getDetection() {
114  return _detection;
115  }
116 
121  public void setViewerClass(String value) {
122  if (value != null && value.length() == 0) {
123  value = null;
124  }
125 
126  if (!Objects.equals(_viewerClass, value)) {
127  _viewerClass = value;
128  smartUpdate("viewerClass", _viewerClass);
129  }
130  }
131 
135  public String getViewerClass() {
136  return _viewerClass;
137  }
138 
144  public void setContent(String value) {
145  if (value != null && !value.equals(_content)) {
146  _content = value;
147  smartUpdate("content", _content);
148  }
149  }
150 
154  public String getContent() {
155  return _content;
156  }
157 
158  public void setNative(boolean value) {
159  if (value != _native) {
160  _native = value;
161  smartUpdate("native", _native);
162  }
163  }
164 
165  public boolean isNative() {
166  return _native;
167  }
168 
169  @Override
170  protected void renderProperties(ContentRenderer renderer)
171  throws java.io.IOException {
172  if (_maxsize == 0) { // not assign maxsize, get the default setting.
173  _maxsize = this.getDesktop().getWebApp().getConfiguration().getMaxUploadSize();
174  }
175 
176  super.renderProperties(renderer);
177  if (!Objects.equals(_detection, DEFAULT_DETECTION)) {
178  render(renderer, "detection", _detection);
179  }
180  render(renderer, "maxsize", _maxsize);
181  render(renderer, "viewerClass", _viewerClass);
182  render(renderer, "content", _content);
183  render(renderer, "native", _native);
184  }
185 
190  @Override
191  public void service(AuRequest request, boolean everError) {
192  final String cmd = request.getCommand();
193  if (Events.ON_UPLOAD.equals(cmd)) {
194  Desktop desktop = getDesktop();
195  final List<Media> result = cast((List) desktop.getAttribute(this.getUuid()));
196  desktop.removeAttribute(this.getUuid());
197  Event event = new UploadEvent(
198  Events.ON_UPLOAD,
199  desktop.getComponentByUuid(this.getUuid()), parseResult(result)
200  );
201  Events.postEvent(event);
202  } else {
203  super.service(request, everError);
204  }
205  }
206 
207  // copy from UploadInfoService
208  private static Media[] parseResult(List<Media> result) {
209  if (result != null) {
210  // we have to filter items that user doesn't specify any file
211  for (Iterator<Media> it = result.iterator(); it.hasNext();) {
212  final Media media = it.next();
213  if (media != null && media.inMemory() && media.isBinary()) {
214  final String nm = media.getName();
215  if (nm == null || nm.length() == 0) {
216  final byte[] bs = media.getByteData();
217  if (bs == null || bs.length == 0)
218  it.remove(); // Upload is pressed without specifying
219  // a file
220  }
221  }
222  }
223 
224  if (!result.isEmpty())
225  return result.toArray(new Media[result.size()]);
226  }
227  return null;
228  }
229 }
void renderProperties(ContentRenderer renderer)
void service(AuRequest request, boolean everError)