PageRenderTime 20ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ftr-gwt-library-file/src/main/java/eu/future/earth/gwt/client/file/EditFilePanel.java

http://ftr-gwt-library.googlecode.com/
Java | 198 lines | 149 code | 28 blank | 21 comment | 28 complexity | 39d9793a0e1a93e89dbac4809965f9af MD5 | raw file
  1/*
  2 * Copyright 2007 Future Earth, info@future-earth.eu
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package eu.future.earth.gwt.client.file;
 17
 18import com.google.gwt.event.shared.HandlerRegistration;
 19import com.google.gwt.json.client.JSONObject;
 20import com.google.gwt.json.client.JSONParser;
 21import com.google.gwt.json.client.JSONValue;
 22import com.google.gwt.user.client.ui.FormPanel;
 23import com.google.gwt.user.client.ui.SimplePanel;
 24import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
 25import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
 26
 27import eu.future.earth.gwt.client.events.FileEvent;
 28import eu.future.earth.gwt.client.events.FileEventListener;
 29import eu.future.earth.gwt.client.events.FileUploadAction;
 30import eu.future.earth.gwt.client.events.HasFileEventHandlers;
 31
 32public class EditFilePanel extends SimplePanel implements FileEventListener, HasFileEventHandlers {
 33	final FormPanel form = new FormPanel();
 34
 35	private FilePanel image = null;
 36
 37	private FileHolderHandler handler = null;
 38
 39	private FileHolder fileHolder = null;
 40
 41	public EditFilePanel(FileHolderHandler newHandler) {
 42		super();
 43		handler = newHandler;
 44		image = new FilePanel(newHandler);
 45		form.setAction(handler.getPostUrl());
 46		form.setEncoding(FormPanel.ENCODING_MULTIPART);
 47		form.setMethod(FormPanel.METHOD_POST);
 48		form.setWidget(image);
 49
 50		image.setFieldName("image");
 51		image.addFileEventHandler(this);
 52
 53		form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
 54
 55			public void onSubmitComplete(SubmitCompleteEvent event) {
 56				// When the form submission is successfully completed, this event is
 57				// fired. Assuming the service returned a response of type text/html,
 58				// we can get the result text here (see the FormPanel documentation for
 59				// further explanation).
 60				JSONValue jsonValue = JSONParser.parseLenient(event.getResults());
 61				JSONObject real = jsonValue.isObject();
 62				String stat = getValueFor("result", real);
 63
 64				if ("succes".equals(stat)) {
 65					String fileName = getValueFor("fileName", real);
 66					String fileType = getValueFor("fileType", real);
 67					String fileSize = getValueFor("fileSize", real);
 68					if (fileHolder == null) {
 69						fileHolder = new FileHolder();
 70					}
 71					fileHolder.setName(fileName);
 72					fileHolder.setUploaded(true);
 73					fileHolder.setSave(image.shouldSave());
 74					fileHolder.setSize(fileSize);
 75					fileHolder.setType(fileType);
 76					reportUploadComplete();
 77				} else {
 78					image.resetPanel();
 79					// info.setUploaded(false);
 80				}
 81			}
 82		});
 83
 84		super.setWidget(form);
 85
 86	}
 87
 88	private boolean edeting = true;
 89
 90	public void setEdeting(boolean newState) {
 91		if (edeting != newState) {
 92			edeting = newState;
 93			setUrlStates();
 94		}
 95	}
 96
 97	private static String getValueFor(String name, JSONObject real) {
 98		JSONValue data = real.get(name);
 99		String stat = null;
100		if (data != null) {
101			if (data.isString() == null) {
102				stat = data.isArray().get(0).isString().stringValue();
103			} else {
104				stat = data.isString().stringValue();
105			}
106		}
107		return stat;
108	}
109
110	private void reportUploadComplete() {
111		image.setInfo(fileHolder.getName(), createViewUrl(fileHolder));
112		setUrlStates();
113		image.setSelectedForSave(true);
114		FileEvent.fire(this, FileUploadAction.UPLOAD_DONE);
115
116	}
117
118	public FileHolder getFileInfo() {
119		return fileHolder;
120	}
121
122	public void setFile(FileHolder newInfo) {
123		fileHolder = newInfo;
124		image.setUploadState(!edeting);
125		if (fileHolder != null) {
126			image.setInfo(fileHolder.getName(), createViewUrl(newInfo));
127		} else {
128			image.resetPanel();
129		}
130		setUrlStates();
131	}
132
133	private void setUrlStates() {
134		if(edeting){
135			image.setUploadState(fileHolder != null);
136			image.setAddActionVisible(fileHolder == null && !image.isFileFieldVisible());
137			image.setSaveVisible(fileHolder != null);
138			image.setRemoveVisible(fileHolder != null && (fileHolder.isStored() || fileHolder.isUploaded()));
139		} else {
140			image.setUploadState(true);
141			image.setAddActionVisible(false);
142			image.setSaveVisible(false);
143			image.setRemoveVisible(false);
144		}
145	}
146
147	public boolean isSave() {
148		return fileHolder != null && fileHolder.isSave() && image.shouldSave();
149	}
150
151	private String createViewUrl(FileHolder holder) {
152		final StringBuffer result = new StringBuffer();
153		result.append(handler.getPostUrl());
154		result.append("?name=" + holder.getName());
155		if (holder.isStored()) {
156			result.append("&stored=true");
157		}
158		result.append(handler.getDownloadParameters(holder));
159		return result.toString();
160	}
161
162	public void setShowAddImage(boolean newState) {
163		image.setAddActionVisible(newState);
164	}
165
166	public void handleFileEvent(FileEvent newEvent) {
167		switch (newEvent.getCommand()) {
168		case FILE_SELECTED: {
169			form.submit();
170			break;
171		}
172		case UPLOAD_DONE: {
173			image.setSelectedForSave(true);
174			break;
175		}
176		case FILE_REMOVED: {
177			// Cancel the upload
178			fileHolder = null;
179			image.setSelectedForSave(false);
180			image.resetPanel();
181			
182			break;
183		}
184		case FILE_SELECTION_CHANGE: {
185			fileHolder.setSave(image.shouldSave());
186			break;
187		}
188		}
189		setUrlStates();
190		FileEvent.fire(this, newEvent.getCommand());
191	}
192
193	@Override
194	public HandlerRegistration addFileEventHandler(FileEventListener handler) {
195		return addHandler(handler, FileEvent.getType());
196	}
197
198}