01: /* FileuploadDlg.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Wed Aug 17 16:33:06 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.zul.impl;
20:
21: import java.util.List;
22: import java.util.Iterator;
23:
24: import org.zkoss.util.media.Media;
25: import org.zkoss.zk.ui.UiException;
26: import org.zkoss.zk.ui.ext.client.Updatable;
27: import org.zkoss.zul.Window;
28:
29: /**
30: * Used with {@link org.zkoss.zul.Fileupload} to implement
31: * the upload feature.
32: *
33: * @author tomyeh
34: */
35: public class FileuploadDlg extends Window {
36: private Media[] _result;
37:
38: /** Returns the result.
39: * @return an array of media (length >= 1), or null if nothing.
40: */
41: public Media[] getResult() {
42: return _result;
43: }
44:
45: public void onCancel() {
46: detach();
47: }
48:
49: /** Sets the result.
50: */
51: public void setResult(Media[] result) {
52: _result = result;
53: }
54:
55: public static Media[] parseResult(List result) {
56: if (result != null) {
57: //we have to filter items that user doesn't specify any file
58: for (Iterator it = result.iterator(); it.hasNext();) {
59: final Media media = (Media) it.next();
60: if (media != null && media.inMemory()
61: && media.isBinary()) {
62: final String nm = media.getName();
63: if (nm == null || nm.length() == 0) {
64: final byte[] bs = media.getByteData();
65: if (bs == null || bs.length == 0)
66: it.remove(); //Upload is pressed without specifying a file
67: }
68: }
69: }
70:
71: if (!result.isEmpty())
72: return (Media[]) result
73: .toArray(new Media[result.size()]);
74: }
75: return null;
76: }
77:
78: //-- ComponentCtrl --//
79: protected Object newExtraCtrl() {
80: return new ExtraCtrl();
81: }
82:
83: /** A utility class to implement {@link #getExtraCtrl}.
84: * It is used only by component developers.
85: */
86: protected class ExtraCtrl extends Window.ExtraCtrl implements
87: Updatable {
88: //-- Updatable --//
89: /** Updates the result from the client.
90: * Callback by the system only. Don't invoke it directly.
91: *
92: * @param result a list of media instances, or null
93: */
94: public void setResult(Object result) {
95: FileuploadDlg.this .setResult(parseResult((List) result));
96: }
97: }
98: }
|