root / src / gr / ebs / gss / client / FileUploadGearsDialog.java @ a73b1e75
History | View | Annotate | Download (13.7 kB)
1 | 5c6b4b2c | Panagiotis Astithas | /*
|
---|---|---|---|
2 | 5c6b4b2c | Panagiotis Astithas | * Copyright 2009 Electronic Business Systems Ltd.
|
3 | 5c6b4b2c | Panagiotis Astithas | *
|
4 | 5c6b4b2c | Panagiotis Astithas | * This file is part of GSS.
|
5 | 5c6b4b2c | Panagiotis Astithas | *
|
6 | 5c6b4b2c | Panagiotis Astithas | * GSS is free software: you can redistribute it and/or modify
|
7 | 5c6b4b2c | Panagiotis Astithas | * it under the terms of the GNU General Public License as published by
|
8 | 5c6b4b2c | Panagiotis Astithas | * the Free Software Foundation, either version 3 of the License, or
|
9 | 5c6b4b2c | Panagiotis Astithas | * (at your option) any later version.
|
10 | 5c6b4b2c | Panagiotis Astithas | *
|
11 | 5c6b4b2c | Panagiotis Astithas | * GSS is distributed in the hope that it will be useful,
|
12 | 5c6b4b2c | Panagiotis Astithas | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 5c6b4b2c | Panagiotis Astithas | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 5c6b4b2c | Panagiotis Astithas | * GNU General Public License for more details.
|
15 | 5c6b4b2c | Panagiotis Astithas | *
|
16 | 5c6b4b2c | Panagiotis Astithas | * You should have received a copy of the GNU General Public License
|
17 | 5c6b4b2c | Panagiotis Astithas | * along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 | 5c6b4b2c | Panagiotis Astithas | */
|
19 | 5c6b4b2c | Panagiotis Astithas | package gr.ebs.gss.client; |
20 | 5c6b4b2c | Panagiotis Astithas | |
21 | a8ede553 | Panagiotis Astithas | import gr.ebs.gss.client.rest.PostCommand; |
22 | 5c6b4b2c | Panagiotis Astithas | import gr.ebs.gss.client.rest.RestCommand; |
23 | a8ede553 | Panagiotis Astithas | import gr.ebs.gss.client.rest.RestException; |
24 | 5c6b4b2c | Panagiotis Astithas | import gr.ebs.gss.client.rest.resource.FileResource; |
25 | 5c6b4b2c | Panagiotis Astithas | import gr.ebs.gss.client.rest.resource.FolderResource; |
26 | 5c6b4b2c | Panagiotis Astithas | |
27 | 5c6b4b2c | Panagiotis Astithas | import java.util.ArrayList; |
28 | 5c6b4b2c | Panagiotis Astithas | import java.util.Arrays; |
29 | 22eb1f84 | Panagiotis Astithas | import java.util.HashMap; |
30 | 5c6b4b2c | Panagiotis Astithas | import java.util.List; |
31 | 22eb1f84 | Panagiotis Astithas | import java.util.Map; |
32 | 5c6b4b2c | Panagiotis Astithas | |
33 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.core.client.GWT; |
34 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.Factory; |
35 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.desktop.Desktop; |
36 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.desktop.File; |
37 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.desktop.OpenFilesHandler; |
38 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.HttpRequest; |
39 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.ProgressEvent; |
40 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.ProgressHandler; |
41 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.gears.client.httprequest.RequestCallback; |
42 | 9ad7d9ae | Panagiotis Astithas | import com.google.gwt.http.client.URL; |
43 | a8ede553 | Panagiotis Astithas | import com.google.gwt.json.client.JSONObject; |
44 | a8ede553 | Panagiotis Astithas | import com.google.gwt.json.client.JSONString; |
45 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.Command; |
46 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.DeferredCommand; |
47 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.ui.Button; |
48 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.ui.ClickListener; |
49 | 4fc9ea8b | Panagiotis Astithas | import com.google.gwt.user.client.ui.FlexTable; |
50 | 3de5aa1e | Panagiotis Astithas | import com.google.gwt.user.client.ui.HTML; |
51 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
52 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.ui.HorizontalPanel; |
53 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.ui.VerticalPanel; |
54 | 5c6b4b2c | Panagiotis Astithas | import com.google.gwt.user.client.ui.Widget; |
55 | 5c6b4b2c | Panagiotis Astithas | |
56 | 5c6b4b2c | Panagiotis Astithas | /**
|
57 | 5c6b4b2c | Panagiotis Astithas | * The 'File upload' dialog box implementation with Google Gears support.
|
58 | 5c6b4b2c | Panagiotis Astithas | */
|
59 | 5c6b4b2c | Panagiotis Astithas | public class FileUploadGearsDialog extends FileUploadDialog implements Updateable { |
60 | 5c6b4b2c | Panagiotis Astithas | |
61 | 9fd42cf4 | Panagiotis Astithas | protected final Factory factory = Factory.getInstance(); |
62 | 5c6b4b2c | Panagiotis Astithas | |
63 | 4fc9ea8b | Panagiotis Astithas | /**
|
64 | 4fc9ea8b | Panagiotis Astithas | * The array of files to upload.
|
65 | 4fc9ea8b | Panagiotis Astithas | */
|
66 | 4fc9ea8b | Panagiotis Astithas | private File[] fileObjects; |
67 | 4fc9ea8b | Panagiotis Astithas | |
68 | 4fc9ea8b | Panagiotis Astithas | /**
|
69 | 4fc9ea8b | Panagiotis Astithas | * A list of files to upload, created from files array. Used to signal
|
70 | 4fc9ea8b | Panagiotis Astithas | * finished state when empty.
|
71 | 4fc9ea8b | Panagiotis Astithas | */
|
72 | 9fd42cf4 | Panagiotis Astithas | protected List<File> selectedFiles = new ArrayList<File>(); |
73 | 5c6b4b2c | Panagiotis Astithas | |
74 | 4fc9ea8b | Panagiotis Astithas | /**
|
75 | 4fc9ea8b | Panagiotis Astithas | * The list of progress bars for individual files.
|
76 | 4fc9ea8b | Panagiotis Astithas | */
|
77 | 9fd42cf4 | Panagiotis Astithas | protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>(); |
78 | 5c6b4b2c | Panagiotis Astithas | |
79 | 204107fb | Panagiotis Astithas | private Button browse; |
80 | 204107fb | Panagiotis Astithas | |
81 | 204107fb | Panagiotis Astithas | private Button submit; |
82 | 204107fb | Panagiotis Astithas | |
83 | 4fc9ea8b | Panagiotis Astithas | private FlexTable generalTable;
|
84 | 4fc9ea8b | Panagiotis Astithas | |
85 | 22eb1f84 | Panagiotis Astithas | private Map<String, FileResource> toRename; |
86 | 22eb1f84 | Panagiotis Astithas | |
87 | 9fd42cf4 | Panagiotis Astithas | protected List<HttpRequest> requests = new ArrayList<HttpRequest>(); |
88 | c6f2b274 | Panagiotis Astithas | |
89 | 5c6b4b2c | Panagiotis Astithas | /**
|
90 | 5c6b4b2c | Panagiotis Astithas | * The widget's constructor.
|
91 | 5c6b4b2c | Panagiotis Astithas | */
|
92 | 5c6b4b2c | Panagiotis Astithas | public FileUploadGearsDialog() {
|
93 | 5c6b4b2c | Panagiotis Astithas | // Set the dialog's caption.
|
94 | 5c6b4b2c | Panagiotis Astithas | setText("File upload");
|
95 | 5c6b4b2c | Panagiotis Astithas | setAnimationEnabled(true);
|
96 | 5c6b4b2c | Panagiotis Astithas | // Create a panel to hold all of the dialog widgets.
|
97 | 5c6b4b2c | Panagiotis Astithas | VerticalPanel panel = new VerticalPanel();
|
98 | 22eb1f84 | Panagiotis Astithas | final HTML info = new HTML("You may select one or more files to upload."); |
99 | 3de5aa1e | Panagiotis Astithas | info.addStyleName("gss-uploadNote");
|
100 | 3de5aa1e | Panagiotis Astithas | panel.add(info); |
101 | 5c6b4b2c | Panagiotis Astithas | // Add an informative label with the folder name.
|
102 | 5c6b4b2c | Panagiotis Astithas | Object selection = GSS.get().getFolders().getCurrent().getUserObject();
|
103 | 5c6b4b2c | Panagiotis Astithas | folder = (FolderResource) selection; |
104 | 5c6b4b2c | Panagiotis Astithas | |
105 | 204107fb | Panagiotis Astithas | browse = new Button("Browse..."); |
106 | 5c6b4b2c | Panagiotis Astithas | |
107 | 5c6b4b2c | Panagiotis Astithas | HorizontalPanel fileUploadPanel = new HorizontalPanel();
|
108 | 5c6b4b2c | Panagiotis Astithas | fileUploadPanel.add(browse); |
109 | 5c6b4b2c | Panagiotis Astithas | |
110 | 4fc9ea8b | Panagiotis Astithas | generalTable = new FlexTable();
|
111 | 5c6b4b2c | Panagiotis Astithas | generalTable.setText(0, 0, "Folder"); |
112 | 5c6b4b2c | Panagiotis Astithas | generalTable.setText(1, 0, "File"); |
113 | 5c6b4b2c | Panagiotis Astithas | generalTable.setText(0, 1, folder.getName()); |
114 | 5c6b4b2c | Panagiotis Astithas | generalTable.setWidget(1, 1, fileUploadPanel); |
115 | 5c6b4b2c | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
116 | 5c6b4b2c | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(1, 0, "props-labels"); |
117 | 5c6b4b2c | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
118 | 5c6b4b2c | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(1, 1, "props-values"); |
119 | 5c6b4b2c | Panagiotis Astithas | generalTable.setCellSpacing(4);
|
120 | 5c6b4b2c | Panagiotis Astithas | |
121 | 5c6b4b2c | Panagiotis Astithas | panel.add(generalTable); |
122 | 5c6b4b2c | Panagiotis Astithas | |
123 | 5c6b4b2c | Panagiotis Astithas | // Create a panel to hold the buttons.
|
124 | 5c6b4b2c | Panagiotis Astithas | HorizontalPanel buttons = new HorizontalPanel();
|
125 | 5c6b4b2c | Panagiotis Astithas | |
126 | 204107fb | Panagiotis Astithas | submit = new Button("Upload"); |
127 | 5c6b4b2c | Panagiotis Astithas | submit.addClickListener(new ClickListener() {
|
128 | 5c6b4b2c | Panagiotis Astithas | |
129 | 5c6b4b2c | Panagiotis Astithas | public void onClick(Widget sender) { |
130 | 5c6b4b2c | Panagiotis Astithas | prepareAndSubmit(); |
131 | 5c6b4b2c | Panagiotis Astithas | } |
132 | 5c6b4b2c | Panagiotis Astithas | }); |
133 | 5c6b4b2c | Panagiotis Astithas | submit.setEnabled(false);
|
134 | 5c6b4b2c | Panagiotis Astithas | buttons.add(submit); |
135 | 5c6b4b2c | Panagiotis Astithas | buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER); |
136 | 5c6b4b2c | Panagiotis Astithas | // Create the 'Cancel' button, along with a listener that hides the
|
137 | 5c6b4b2c | Panagiotis Astithas | // dialog when the button is clicked.
|
138 | 5c6b4b2c | Panagiotis Astithas | Button cancel = new Button("Cancel", new ClickListener() { |
139 | 5c6b4b2c | Panagiotis Astithas | |
140 | 5c6b4b2c | Panagiotis Astithas | public void onClick(Widget sender) { |
141 | c6f2b274 | Panagiotis Astithas | cancelUpload(); |
142 | 5c6b4b2c | Panagiotis Astithas | } |
143 | 5c6b4b2c | Panagiotis Astithas | }); |
144 | 5c6b4b2c | Panagiotis Astithas | buttons.add(cancel); |
145 | 5c6b4b2c | Panagiotis Astithas | buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
146 | 5c6b4b2c | Panagiotis Astithas | buttons.setSpacing(8);
|
147 | 5c6b4b2c | Panagiotis Astithas | buttons.addStyleName("gss-DialogBox");
|
148 | 5c6b4b2c | Panagiotis Astithas | |
149 | 5c6b4b2c | Panagiotis Astithas | browse.addClickListener(new ClickListener() {
|
150 | 5c6b4b2c | Panagiotis Astithas | public void onClick(Widget sender) { |
151 | 5c6b4b2c | Panagiotis Astithas | Desktop desktop = factory.createDesktop(); |
152 | 5c6b4b2c | Panagiotis Astithas | desktop.openFiles(new OpenFilesHandler() {
|
153 | 5c6b4b2c | Panagiotis Astithas | |
154 | 5c6b4b2c | Panagiotis Astithas | public void onOpenFiles(OpenFilesEvent event) { |
155 | 4fc9ea8b | Panagiotis Astithas | fileObjects = event.getFiles(); |
156 | 4fc9ea8b | Panagiotis Astithas | selectedFiles.addAll(Arrays.asList(fileObjects));
|
157 | 4fc9ea8b | Panagiotis Astithas | for (int i = 0; i< selectedFiles.size(); i++) { |
158 | 4fc9ea8b | Panagiotis Astithas | generalTable.setText(i+1, 0, "File"); |
159 | 4fc9ea8b | Panagiotis Astithas | generalTable.setText(i+1, 1, selectedFiles.get(i).getName()); |
160 | 4fc9ea8b | Panagiotis Astithas | ProgressBar progress = new ProgressBar(20, 0); |
161 | 4fc9ea8b | Panagiotis Astithas | generalTable.setWidget(i+1, 2, progress); |
162 | 4fc9ea8b | Panagiotis Astithas | progressBars.add(progress); |
163 | 4fc9ea8b | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels"); |
164 | 4fc9ea8b | Panagiotis Astithas | generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values"); |
165 | 4fc9ea8b | Panagiotis Astithas | } |
166 | 5c6b4b2c | Panagiotis Astithas | submit.setEnabled(true);
|
167 | 5c6b4b2c | Panagiotis Astithas | } |
168 | 5c6b4b2c | Panagiotis Astithas | }); |
169 | 5c6b4b2c | Panagiotis Astithas | } |
170 | 5c6b4b2c | Panagiotis Astithas | }); |
171 | 5c6b4b2c | Panagiotis Astithas | |
172 | 5c6b4b2c | Panagiotis Astithas | panel.add(buttons); |
173 | 5c6b4b2c | Panagiotis Astithas | panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
174 | 5c6b4b2c | Panagiotis Astithas | panel.addStyleName("gss-DialogBox");
|
175 | 5c6b4b2c | Panagiotis Astithas | addStyleName("gss-DialogBox");
|
176 | 5c6b4b2c | Panagiotis Astithas | setWidget(panel); |
177 | 5c6b4b2c | Panagiotis Astithas | } |
178 | 5c6b4b2c | Panagiotis Astithas | |
179 | 5c6b4b2c | Panagiotis Astithas | /**
|
180 | c6f2b274 | Panagiotis Astithas | * Cancels the file upload.
|
181 | c6f2b274 | Panagiotis Astithas | */
|
182 | c6f2b274 | Panagiotis Astithas | private void cancelUpload() { |
183 | c6f2b274 | Panagiotis Astithas | for (HttpRequest request: requests)
|
184 | c6f2b274 | Panagiotis Astithas | request.abort(); |
185 | c6f2b274 | Panagiotis Astithas | hide(); |
186 | c6f2b274 | Panagiotis Astithas | } |
187 | c6f2b274 | Panagiotis Astithas | |
188 | c6f2b274 | Panagiotis Astithas | /**
|
189 | 5c6b4b2c | Panagiotis Astithas | * Check whether the specified file name exists in the selected folder.
|
190 | 5c6b4b2c | Panagiotis Astithas | */
|
191 | 5c6b4b2c | Panagiotis Astithas | private boolean canContinue(File file) { |
192 | 5c6b4b2c | Panagiotis Astithas | String fileName = getFilename(file.getName());
|
193 | 5c6b4b2c | Panagiotis Astithas | if (getFileForName(fileName) == null) |
194 | 5c6b4b2c | Panagiotis Astithas | // For file creation, check to see if the file already exists.
|
195 | 5c6b4b2c | Panagiotis Astithas | for (FileResource fileRes : files)
|
196 | 5c6b4b2c | Panagiotis Astithas | if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))
|
197 | 5c6b4b2c | Panagiotis Astithas | return false; |
198 | 5c6b4b2c | Panagiotis Astithas | return true; |
199 | 5c6b4b2c | Panagiotis Astithas | } |
200 | 5c6b4b2c | Panagiotis Astithas | |
201 | 5c6b4b2c | Panagiotis Astithas | @Override
|
202 | 204107fb | Panagiotis Astithas | public void prepareAndSubmit() { |
203 | 204107fb | Panagiotis Astithas | GSS app = GSS.get(); |
204 | 204107fb | Panagiotis Astithas | if (selectedFiles.size() == 0) { |
205 | 204107fb | Panagiotis Astithas | app.displayError("You must select a file!");
|
206 | 204107fb | Panagiotis Astithas | hide(); |
207 | 204107fb | Panagiotis Astithas | return;
|
208 | 204107fb | Panagiotis Astithas | } |
209 | 22eb1f84 | Panagiotis Astithas | for (File file: selectedFiles) |
210 | 204107fb | Panagiotis Astithas | if (!canContinue(file)) {
|
211 | 204107fb | Panagiotis Astithas | app.displayError("The file name " + file.getName() +
|
212 | 204107fb | Panagiotis Astithas | " already exists in this folder");
|
213 | 204107fb | Panagiotis Astithas | hide(); |
214 | 204107fb | Panagiotis Astithas | return;
|
215 | 204107fb | Panagiotis Astithas | } |
216 | 204107fb | Panagiotis Astithas | submit.setEnabled(false);
|
217 | 204107fb | Panagiotis Astithas | browse.setVisible(false);
|
218 | 22eb1f84 | Panagiotis Astithas | List<String> toUpdate = new ArrayList<String>(); |
219 | 22eb1f84 | Panagiotis Astithas | toRename = new HashMap<String, FileResource>(); |
220 | 22eb1f84 | Panagiotis Astithas | for (File file: selectedFiles) { |
221 | 22eb1f84 | Panagiotis Astithas | String fname = getFilename(file.getName());
|
222 | 22eb1f84 | Panagiotis Astithas | if (getFileForName(fname) == null) { |
223 | 22eb1f84 | Panagiotis Astithas | // We are going to create a file, so we check to see if there is a
|
224 | 22eb1f84 | Panagiotis Astithas | // trashed file with the same name.
|
225 | 22eb1f84 | Panagiotis Astithas | FileResource same = null;
|
226 | 22eb1f84 | Panagiotis Astithas | for (FileResource fres : folder.getFiles())
|
227 | 22eb1f84 | Panagiotis Astithas | if (fres.isDeleted() && fres.getName().equals(fname))
|
228 | 22eb1f84 | Panagiotis Astithas | same = fres; |
229 | 22eb1f84 | Panagiotis Astithas | // In that case add it to the list of files to rename.
|
230 | 22eb1f84 | Panagiotis Astithas | if (same != null) |
231 | 22eb1f84 | Panagiotis Astithas | toRename.put(getBackupFilename(fname), same); |
232 | 22eb1f84 | Panagiotis Astithas | } else
|
233 | 22eb1f84 | Panagiotis Astithas | // If we are updating a file add it to the list of files to update.
|
234 | 22eb1f84 | Panagiotis Astithas | toUpdate.add(fname); |
235 | 22eb1f84 | Panagiotis Astithas | } |
236 | 5c6b4b2c | Panagiotis Astithas | |
237 | 22eb1f84 | Panagiotis Astithas | if (!toUpdate.isEmpty()) {
|
238 | 22eb1f84 | Panagiotis Astithas | StringBuffer sb = new StringBuffer(); |
239 | 22eb1f84 | Panagiotis Astithas | for (String name: toUpdate) |
240 | 22eb1f84 | Panagiotis Astithas | sb.append(name).append("<br/>");
|
241 | 22eb1f84 | Panagiotis Astithas | // We are going to update existing files, so show a confirmation dialog.
|
242 | 5c6b4b2c | Panagiotis Astithas | ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " + |
243 | 22eb1f84 | Panagiotis Astithas | "you want to update the following files?<br/><i>" + sb +
|
244 | 22eb1f84 | Panagiotis Astithas | "</i>", "Update") { |
245 | 5c6b4b2c | Panagiotis Astithas | |
246 | 5c6b4b2c | Panagiotis Astithas | @Override
|
247 | 5c6b4b2c | Panagiotis Astithas | public void cancel() { |
248 | 204107fb | Panagiotis Astithas | hide(); |
249 | 5c6b4b2c | Panagiotis Astithas | } |
250 | 5c6b4b2c | Panagiotis Astithas | |
251 | 5c6b4b2c | Panagiotis Astithas | @Override
|
252 | 5c6b4b2c | Panagiotis Astithas | public void confirm() { |
253 | 22eb1f84 | Panagiotis Astithas | confirmRename(); |
254 | 5c6b4b2c | Panagiotis Astithas | } |
255 | 5c6b4b2c | Panagiotis Astithas | |
256 | 5c6b4b2c | Panagiotis Astithas | }; |
257 | 5c6b4b2c | Panagiotis Astithas | confirm.center(); |
258 | 22eb1f84 | Panagiotis Astithas | } else
|
259 | 22eb1f84 | Panagiotis Astithas | confirmRename(); |
260 | 5c6b4b2c | Panagiotis Astithas | } |
261 | 5c6b4b2c | Panagiotis Astithas | |
262 | 22eb1f84 | Panagiotis Astithas | /**
|
263 | 22eb1f84 | Panagiotis Astithas | * Confirm the renames of synonymous files already in the trash.
|
264 | 22eb1f84 | Panagiotis Astithas | */
|
265 | 22eb1f84 | Panagiotis Astithas | private void confirmRename() { |
266 | 22eb1f84 | Panagiotis Astithas | if (!toRename.isEmpty()) {
|
267 | 22eb1f84 | Panagiotis Astithas | StringBuffer sb = new StringBuffer(); |
268 | 22eb1f84 | Panagiotis Astithas | for (FileResource file: toRename.values())
|
269 | 22eb1f84 | Panagiotis Astithas | sb.append(file.getName()).append("<br/>");
|
270 | 22eb1f84 | Panagiotis Astithas | ConfirmationDialog confirm = new ConfirmationDialog("Files " + |
271 | 22eb1f84 | Panagiotis Astithas | "with the following names already exist in the trash. If" +
|
272 | 22eb1f84 | Panagiotis Astithas | " you continue,<br/>the trashed files will be renamed " +
|
273 | 22eb1f84 | Panagiotis Astithas | "automatically for you:<br/><i>" + sb + "</i>", "Continue") { |
274 | a8ede553 | Panagiotis Astithas | |
275 | 22eb1f84 | Panagiotis Astithas | @Override
|
276 | 22eb1f84 | Panagiotis Astithas | public void cancel() { |
277 | 22eb1f84 | Panagiotis Astithas | hide(); |
278 | 22eb1f84 | Panagiotis Astithas | } |
279 | a8ede553 | Panagiotis Astithas | |
280 | 22eb1f84 | Panagiotis Astithas | @Override
|
281 | 22eb1f84 | Panagiotis Astithas | public void confirm() { |
282 | 22eb1f84 | Panagiotis Astithas | updateTrashedFiles(); |
283 | 22eb1f84 | Panagiotis Astithas | } |
284 | a8ede553 | Panagiotis Astithas | |
285 | 22eb1f84 | Panagiotis Astithas | }; |
286 | 22eb1f84 | Panagiotis Astithas | confirm.center(); |
287 | 22eb1f84 | Panagiotis Astithas | } else
|
288 | 22eb1f84 | Panagiotis Astithas | uploadFiles(); |
289 | 22eb1f84 | Panagiotis Astithas | } |
290 | 22eb1f84 | Panagiotis Astithas | |
291 | 22eb1f84 | Panagiotis Astithas | /**
|
292 | 22eb1f84 | Panagiotis Astithas | * Rename the conflicting trashed files with the supplied new names.
|
293 | 22eb1f84 | Panagiotis Astithas | */
|
294 | 22eb1f84 | Panagiotis Astithas | private void updateTrashedFiles() { |
295 | 22eb1f84 | Panagiotis Astithas | for (final String name: toRename.keySet()) { |
296 | 22eb1f84 | Panagiotis Astithas | JSONObject json = new JSONObject();
|
297 | 22eb1f84 | Panagiotis Astithas | json.put("name", new JSONString(name)); |
298 | 22eb1f84 | Panagiotis Astithas | PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) { |
299 | 22eb1f84 | Panagiotis Astithas | |
300 | 22eb1f84 | Panagiotis Astithas | @Override
|
301 | 22eb1f84 | Panagiotis Astithas | public void onComplete() { |
302 | 22eb1f84 | Panagiotis Astithas | toRename.remove(name); |
303 | 22eb1f84 | Panagiotis Astithas | uploadFiles(); |
304 | 22eb1f84 | Panagiotis Astithas | } |
305 | 22eb1f84 | Panagiotis Astithas | |
306 | 22eb1f84 | Panagiotis Astithas | @Override
|
307 | 22eb1f84 | Panagiotis Astithas | public void onError(Throwable t) { |
308 | 22eb1f84 | Panagiotis Astithas | GSS app = GSS.get(); |
309 | 22eb1f84 | Panagiotis Astithas | GWT.log("", t);
|
310 | 22eb1f84 | Panagiotis Astithas | if (t instanceof RestException) { |
311 | 22eb1f84 | Panagiotis Astithas | int statusCode = ((RestException) t).getHttpStatusCode();
|
312 | 22eb1f84 | Panagiotis Astithas | if (statusCode == 405) |
313 | 22eb1f84 | Panagiotis Astithas | app.displayError("You don't have the necessary permissions");
|
314 | 22eb1f84 | Panagiotis Astithas | else if (statusCode == 404) |
315 | 22eb1f84 | Panagiotis Astithas | app.displayError("User in permissions does not exist");
|
316 | 22eb1f84 | Panagiotis Astithas | else if (statusCode == 409) |
317 | 22eb1f84 | Panagiotis Astithas | app.displayError("A file with the same name already exists");
|
318 | 22eb1f84 | Panagiotis Astithas | else if (statusCode == 413) |
319 | 22eb1f84 | Panagiotis Astithas | app.displayError("Your quota has been exceeded");
|
320 | 22eb1f84 | Panagiotis Astithas | else
|
321 | 22eb1f84 | Panagiotis Astithas | app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
|
322 | 22eb1f84 | Panagiotis Astithas | } else
|
323 | 22eb1f84 | Panagiotis Astithas | app.displayError("System error modifying file:" + t.getMessage());
|
324 | 22eb1f84 | Panagiotis Astithas | } |
325 | 22eb1f84 | Panagiotis Astithas | |
326 | 22eb1f84 | Panagiotis Astithas | }; |
327 | 22eb1f84 | Panagiotis Astithas | DeferredCommand.addCommand(cf); |
328 | 22eb1f84 | Panagiotis Astithas | } |
329 | a8ede553 | Panagiotis Astithas | } |
330 | a8ede553 | Panagiotis Astithas | |
331 | 5c6b4b2c | Panagiotis Astithas | /**
|
332 | 22eb1f84 | Panagiotis Astithas | * Checks if the renaming step for already trashed files is complete and
|
333 | 22eb1f84 | Panagiotis Astithas | * starts file uploads.
|
334 | 5c6b4b2c | Panagiotis Astithas | */
|
335 | 5c6b4b2c | Panagiotis Astithas | private void uploadFiles() { |
336 | 22eb1f84 | Panagiotis Astithas | if (!toRename.isEmpty()) return; |
337 | 4fc9ea8b | Panagiotis Astithas | for (int i = 0; i< fileObjects.length; i++) { |
338 | 4fc9ea8b | Panagiotis Astithas | final int index = i; |
339 | 5c6b4b2c | Panagiotis Astithas | DeferredCommand.addCommand(new Command() {
|
340 | 5c6b4b2c | Panagiotis Astithas | public void execute() { |
341 | 9fd42cf4 | Panagiotis Astithas | doSend(fileObjects[index], index); |
342 | 5c6b4b2c | Panagiotis Astithas | } |
343 | 5c6b4b2c | Panagiotis Astithas | }); |
344 | 4fc9ea8b | Panagiotis Astithas | } |
345 | 5c6b4b2c | Panagiotis Astithas | } |
346 | 5c6b4b2c | Panagiotis Astithas | |
347 | 5c6b4b2c | Panagiotis Astithas | /**
|
348 | 9fd42cf4 | Panagiotis Astithas | * Perform the HTTP request to upload the specified file.
|
349 | 5c6b4b2c | Panagiotis Astithas | */
|
350 | 9fd42cf4 | Panagiotis Astithas | protected void doSend(final File file, final int index) { |
351 | 22eb1f84 | Panagiotis Astithas | final GSS app = GSS.get();
|
352 | 5c6b4b2c | Panagiotis Astithas | HttpRequest request = factory.createHttpRequest(); |
353 | c6f2b274 | Panagiotis Astithas | requests.add(request); |
354 | 5c6b4b2c | Panagiotis Astithas | String method = "PUT"; |
355 | 5c6b4b2c | Panagiotis Astithas | |
356 | 5c6b4b2c | Panagiotis Astithas | String path;
|
357 | 22eb1f84 | Panagiotis Astithas | final String filename = getFilename(file.getName()); |
358 | 9ad7d9ae | Panagiotis Astithas | path = folder.getUri(); |
359 | 9ad7d9ae | Panagiotis Astithas | if (!path.endsWith("/")) |
360 | 9ad7d9ae | Panagiotis Astithas | path = path + "/";
|
361 | 9ad7d9ae | Panagiotis Astithas | path = path + encode(filename); |
362 | 5c6b4b2c | Panagiotis Astithas | |
363 | 5c6b4b2c | Panagiotis Astithas | String token = app.getToken();
|
364 | 5c6b4b2c | Panagiotis Astithas | String resource = path.substring(app.getApiPath().length()-1, path.length()); |
365 | 5c6b4b2c | Panagiotis Astithas | String date = RestCommand.getDate();
|
366 | 5c6b4b2c | Panagiotis Astithas | String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
|
367 | 5c6b4b2c | Panagiotis Astithas | request.open(method, path); |
368 | 5c6b4b2c | Panagiotis Astithas | request.setRequestHeader("X-GSS-Date", date);
|
369 | 5c6b4b2c | Panagiotis Astithas | request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig); |
370 | 5c6b4b2c | Panagiotis Astithas | request.setRequestHeader("Accept", "application/json; charset=utf-8"); |
371 | 5c6b4b2c | Panagiotis Astithas | request.setCallback(new RequestCallback() {
|
372 | 5c6b4b2c | Panagiotis Astithas | public void onResponseReceived(HttpRequest req) { |
373 | 9fd42cf4 | Panagiotis Astithas | int state = req.getReadyState();
|
374 | 9fd42cf4 | Panagiotis Astithas | if (state != 4) return; |
375 | 5c6b4b2c | Panagiotis Astithas | switch(req.getStatus()) {
|
376 | 5c6b4b2c | Panagiotis Astithas | case 201: // Created falls through to updated. |
377 | 5c6b4b2c | Panagiotis Astithas | case 204: |
378 | 5c6b4b2c | Panagiotis Astithas | selectedFiles.remove(file); |
379 | 5c6b4b2c | Panagiotis Astithas | finish(); |
380 | 5c6b4b2c | Panagiotis Astithas | break;
|
381 | 5c6b4b2c | Panagiotis Astithas | case 403: |
382 | 5c6b4b2c | Panagiotis Astithas | SessionExpiredDialog dlg = new SessionExpiredDialog();
|
383 | 5c6b4b2c | Panagiotis Astithas | dlg.center(); |
384 | 5c6b4b2c | Panagiotis Astithas | break;
|
385 | 22eb1f84 | Panagiotis Astithas | case 405: |
386 | 22eb1f84 | Panagiotis Astithas | app.displayError("You don't have permission to " +
|
387 | 22eb1f84 | Panagiotis Astithas | "upload file " + filename);
|
388 | 22eb1f84 | Panagiotis Astithas | break;
|
389 | 22eb1f84 | Panagiotis Astithas | case 409: |
390 | 22eb1f84 | Panagiotis Astithas | app.displayError("A folder with the name " + filename +
|
391 | 22eb1f84 | Panagiotis Astithas | " already exists at this level");
|
392 | 22eb1f84 | Panagiotis Astithas | break;
|
393 | 22eb1f84 | Panagiotis Astithas | case 413: |
394 | 22eb1f84 | Panagiotis Astithas | app.displayError("There is not enough free space " +
|
395 | 22eb1f84 | Panagiotis Astithas | "available for uploading " + filename);
|
396 | 22eb1f84 | Panagiotis Astithas | break;
|
397 | 5c6b4b2c | Panagiotis Astithas | default:
|
398 | 22eb1f84 | Panagiotis Astithas | app.displayError("Error uploading file " + filename +
|
399 | 22eb1f84 | Panagiotis Astithas | ": " + req.getStatus());
|
400 | 5c6b4b2c | Panagiotis Astithas | } |
401 | 5c6b4b2c | Panagiotis Astithas | } |
402 | 5c6b4b2c | Panagiotis Astithas | }); |
403 | 5c6b4b2c | Panagiotis Astithas | request.getUpload().setProgressHandler(new ProgressHandler() {
|
404 | 5c6b4b2c | Panagiotis Astithas | public void onProgress(ProgressEvent event) { |
405 | 5c6b4b2c | Panagiotis Astithas | double pcnt = (double) event.getLoaded() / event.getTotal(); |
406 | 4fc9ea8b | Panagiotis Astithas | progressBars.get(index).setProgress((int) Math.floor(pcnt * 100)); |
407 | 5c6b4b2c | Panagiotis Astithas | } |
408 | 5c6b4b2c | Panagiotis Astithas | }); |
409 | 5c6b4b2c | Panagiotis Astithas | request.send(file.getBlob()); |
410 | 5c6b4b2c | Panagiotis Astithas | } |
411 | 5c6b4b2c | Panagiotis Astithas | |
412 | 5c6b4b2c | Panagiotis Astithas | /**
|
413 | 5c6b4b2c | Panagiotis Astithas | * Perform the final actions after the files are uploaded.
|
414 | 5c6b4b2c | Panagiotis Astithas | */
|
415 | 9fd42cf4 | Panagiotis Astithas | protected void finish() { |
416 | 5c6b4b2c | Panagiotis Astithas | if (!selectedFiles.isEmpty()) return; |
417 | 5c6b4b2c | Panagiotis Astithas | hide(); |
418 | 5c6b4b2c | Panagiotis Astithas | GSS.get().showFileList(true);
|
419 | 5c6b4b2c | Panagiotis Astithas | GSS.get().getStatusPanel().updateStats(); |
420 | 5c6b4b2c | Panagiotis Astithas | } |
421 | 9ad7d9ae | Panagiotis Astithas | |
422 | 9ad7d9ae | Panagiotis Astithas | /**
|
423 | 9ad7d9ae | Panagiotis Astithas | * Same as URL.encode, but also encode apostrophe since browsers aren't
|
424 | 9ad7d9ae | Panagiotis Astithas | * consistent about it (FF encodes, IE does not).
|
425 | 9ad7d9ae | Panagiotis Astithas | */
|
426 | 9fd42cf4 | Panagiotis Astithas | protected String encode(String decodedURL) { |
427 | a73b1e75 | Dimitris Routsis | String retv = decodedURL.replaceAll("@", "_"); // Replace bad character |
428 | a73b1e75 | Dimitris Routsis | retv = URL.encodeComponent(retv);
|
429 | 9ad7d9ae | Panagiotis Astithas | retv = retv.replaceAll("'", "%27"); |
430 | 9ad7d9ae | Panagiotis Astithas | return retv;
|
431 | 9ad7d9ae | Panagiotis Astithas | } |
432 | 9ad7d9ae | Panagiotis Astithas | |
433 | 5c6b4b2c | Panagiotis Astithas | } |