1 package org.whatsitcalled.webflange.webapp;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.apache.commons.io.FileUtils;
7 import org.apache.log4j.Logger;
8 import org.apache.wicket.AttributeModifier;
9 import org.apache.wicket.Page;
10 import org.apache.wicket.ajax.AjaxRequestTarget;
11 import org.apache.wicket.ajax.markup.html.AjaxLink;
12 import org.apache.wicket.behavior.SimpleAttributeModifier;
13 import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
14 import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
15 import org.apache.wicket.markup.html.WebPage;
16 import org.apache.wicket.markup.html.basic.Label;
17 import org.apache.wicket.markup.html.form.Form;
18 import org.apache.wicket.markup.html.form.upload.FileUpload;
19 import org.apache.wicket.markup.html.form.upload.FileUploadField;
20 import org.apache.wicket.markup.html.link.Link;
21 import org.apache.wicket.markup.html.panel.Panel;
22 import org.apache.wicket.markup.repeater.Item;
23 import org.apache.wicket.markup.repeater.data.DataView;
24 import org.apache.wicket.model.AbstractReadOnlyModel;
25 import org.apache.wicket.util.lang.Bytes;
26 import org.whatsitcalled.webflange.ResourceFactory;
27 import org.whatsitcalled.webflange.file.FileManagerException;
28 import org.whatsitcalled.webflange.webapp.model.DataFileDataProvider;
29
30 public class FilePanel extends Panel {
31 private static final Logger LOGGER = Logger.getLogger(FilePanel.class);
32
33 public FilePanel(String id) {
34 super(id);
35 add(new Label("selectHdr", "Action"));
36 add(new Label("dataFileHdr", "File Name"));
37
38 add(new DataView("dataFiles", new DataFileDataProvider()) {
39 protected void populateItem(final Item item) {
40 final File dataFile = (File) item.getModelObject();
41
42 final ModalWindow viewDataFile = new ModalWindow("viewFile");
43 viewDataFile.setTitle(dataFile.getName());
44 viewDataFile.setCookieName("viewFile");
45 viewDataFile.setPageMapName("viewFile");
46
47 viewDataFile.setPageCreator(new ModalWindow.PageCreator()
48 {
49 public Page createPage()
50 {
51 return new ViewPage(dataFile, viewDataFile);
52 }
53 });
54
55 item.add(viewDataFile);
56
57 item.add(new AjaxLink("view") {
58 public void onClick(AjaxRequestTarget target) {
59 viewDataFile.show(target);
60 }
61 });
62
63
64 Link deleteLink = new Link("delete") {
65 public void onClick() {
66 File dataFile = (File) getParent().getModelObject();
67 dataFile.delete();
68 }
69 };
70 deleteLink.add(new SimpleAttributeModifier(
71 "onclick", "return confirm('Delete File " + dataFile.getName() + "?');"));
72 item.add(deleteLink);
73
74 item.add(new Label("name", dataFile.getName()));
75 item.add(new AttributeModifier("class", true,
76 new AbstractReadOnlyModel() {
77 public Object getObject() {
78 return (item.getIndex() % 2 == 1) ? "even"
79 : "odd";
80 }
81 }));
82 }
83 });
84
85
86 final FileUploadForm ajaxSimpleUploadForm = new FileUploadForm("ajax-simpleUpload");
87 ajaxSimpleUploadForm.add(new UploadProgressBar("progress", ajaxSimpleUploadForm));
88 add(ajaxSimpleUploadForm);
89 }
90
91
92
93 private class FileUploadForm extends Form
94 {
95 private FileUploadField fileUploadField;
96
97
98
99
100
101
102
103 public FileUploadForm(String name)
104 {
105 super(name);
106
107
108 setMultiPart(true);
109
110
111 add(fileUploadField = new FileUploadField("fileInput"));
112
113
114 setMaxSize(Bytes.megabytes(10));
115 }
116
117
118
119
120 protected void onSubmit()
121 {
122 final FileUpload upload = fileUploadField.getFileUpload();
123 if (upload != null)
124 {
125
126 try {
127 ResourceFactory.getFileManager().saveUploadFile(upload.getClientFileName(), upload.getInputStream());
128 } catch (FileManagerException e) {
129
130 LOGGER.error("Unable to save uploaded file.",e);
131 } catch (IOException e) {
132 LOGGER.error("Unable to upload file.",e);
133
134 }
135 }
136 }
137 }
138
139 protected class ViewPage extends WebPage {
140 public ViewPage(File file, ModalWindow window) {
141 String content = "";
142 try {
143 content = FileUtils.readFileToString(file);
144 } catch (IOException e) {
145 content = "Can't load file: " + file.getAbsolutePath();
146 LOGGER.error(content, e);
147
148 }
149 add(new Label("file", content));
150 }
151 }
152
153 }