001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.main.web.demo;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019: import org.araneaframework.example.main.TemplateBaseWidget;
020: import org.araneaframework.http.PopupWindowContext;
021: import org.araneaframework.http.service.FileDownloaderService;
022: import org.araneaframework.http.support.PopupWindowProperties;
023: import org.araneaframework.uilib.event.OnChangeEventListener;
024: import org.araneaframework.uilib.event.OnClickEventListener;
025: import org.araneaframework.uilib.form.FormElement;
026: import org.araneaframework.uilib.form.FormWidget;
027: import org.araneaframework.uilib.form.control.ButtonControl;
028: import org.araneaframework.uilib.form.control.FileUploadControl;
029: import org.araneaframework.uilib.form.control.SelectControl;
030: import org.araneaframework.uilib.form.control.TextControl;
031: import org.araneaframework.uilib.form.data.FileInfoData;
032: import org.araneaframework.uilib.form.data.StringData;
033: import org.araneaframework.uilib.list.ListWidget;
034: import org.araneaframework.uilib.list.dataprovider.MemoryBasedListDataProvider;
035: import org.araneaframework.uilib.support.DisplayItem;
036: import org.araneaframework.uilib.support.FileInfo;
037:
038: /**
039: * Demonstrates usage of file upload control.
040: * @author Lauri Tulmin
041: * @author Taimo Peelo (taimo@araneaframework.org)
042: */
043: public class DemoFileUpload extends TemplateBaseWidget {
044: private static final long serialVersionUID = 1L;
045: private FormWidget form;
046: private ListWidget uploadList;
047:
048: private List files = new ArrayList();
049:
050: public void init() throws Exception {
051: setViewSelector("demo/demoFileUpload");
052:
053: buildList();
054:
055: form = buildForm();
056: addWidget("uploadForm", form);
057: }
058:
059: private void buildList() throws Exception {
060: uploadList = new ListWidget();
061: uploadList.setDataProvider(new FileListDataProvider());
062: uploadList.addField("originalFilename", "#Original filename");
063: uploadList.addField("size", "#File size");
064: uploadList.addField("contentType", "#Content Type");
065: uploadList.addField("dummy", null, false);
066: }
067:
068: private FormWidget buildForm() throws Exception {
069: final FormWidget result = new FormWidget();
070:
071: result.addElement("encodingTest", "#encodingTest",
072: new TextControl(), new StringData(), false);
073:
074: SelectControl selectControl = new SelectControl();
075: selectControl
076: .addOnChangeEventListener(new OnChangeEventListener() {
077: private static final long serialVersionUID = 1L;
078:
079: public void onChange() throws Exception {
080: }
081: });
082:
083: selectControl.addItem(new DisplayItem(null, "- choose -"));
084: selectControl.addItem(new DisplayItem("1", "one"));
085: selectControl.addItem(new DisplayItem("2", "two"));
086:
087: result.addElement("select", "#Select", selectControl,
088: new StringData(), true);
089: FormElement selectElement = (FormElement) result
090: .getElement("select");
091: selectElement.setValue("1");
092: selectElement.setDisabled(true);
093:
094: result.addElement("file", "#File", new FileUploadControl(),
095: new FileInfoData(), false);
096:
097: ButtonControl button = new ButtonControl();
098: button.addOnClickEventListener(new FileUploadButtonListener());
099: result
100: .addElement("upload", "#Upload file", button, null,
101: false);
102:
103: return result;
104: }
105:
106: public void handleEventSelectFile(String param) throws Exception {
107: FileInfo selectedFile = (FileInfo) uploadList
108: .getRowFromRequestId(param);
109:
110: getMessageCtx()
111: .showInfoMessage(
112: "Popup window with download content should have opened. If it did not, please relax your popup blocker settings.");
113: FileDownloaderService service = new FileDownloaderService(
114: selectedFile);
115:
116: PopupWindowContext popupContext = (PopupWindowContext) getEnvironment()
117: .getEntry(PopupWindowContext.class);
118: PopupWindowProperties p = new PopupWindowProperties();
119: popupContext.open(service, p, null);
120: }
121:
122: public void handleEventValidate() throws Exception {
123: form.convertAndValidate();
124: }
125:
126: // INNER CLASSES
127:
128: private class FileListDataProvider extends
129: MemoryBasedListDataProvider {
130: private static final long serialVersionUID = 1L;
131:
132: public FileListDataProvider() {
133: super (FileInfo.class);
134: }
135:
136: public List loadData() throws Exception {
137: return files;
138: }
139: }
140:
141: private class FileUploadButtonListener implements
142: OnClickEventListener {
143: private static final long serialVersionUID = 1L;
144:
145: public void onClick() throws Exception {
146: form.getElementByFullName("file").convertAndValidate();
147: FileInfo fileInfo = (FileInfo) form
148: .getValueByFullName("file");
149: if (fileInfo != null
150: && !fileInfo.getOriginalFilename().trim()
151: .equals("") && fileInfo.getSize() > 0) {
152: if (files.size() == 0) {
153: DemoFileUpload.this .addWidget("uploadList",
154: uploadList);
155: }
156: files.add(fileInfo);
157: form.setValueByFullName("file", null);
158: // refresh the list data
159: uploadList.getDataProvider().refreshData();
160: }
161: }
162: }
163:
164: }
|