PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://ftr-gwt-library.googlecode.com/
Java | 264 lines | 177 code | 43 blank | 44 comment | 26 complexity | aa85bddc7599ba922f8c74e4fd9d7c03 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 org.cobogw.gwt.user.client.CSS;
 19
 20import com.google.gwt.core.client.GWT;
 21import com.google.gwt.event.dom.client.ClickEvent;
 22import com.google.gwt.event.dom.client.ClickHandler;
 23import com.google.gwt.event.shared.HandlerRegistration;
 24import com.google.gwt.user.client.Element;
 25import com.google.gwt.user.client.Timer;
 26import com.google.gwt.user.client.ui.Anchor;
 27import com.google.gwt.user.client.ui.CheckBox;
 28import com.google.gwt.user.client.ui.FileUpload;
 29import com.google.gwt.user.client.ui.HTML;
 30import com.google.gwt.user.client.ui.HorizontalPanel;
 31import com.google.gwt.user.client.ui.SimplePanel;
 32
 33import eu.future.earth.gwt.client.events.FileEvent;
 34import eu.future.earth.gwt.client.events.FileEventListener;
 35import eu.future.earth.gwt.client.events.FileUploadAction;
 36import eu.future.earth.gwt.client.events.HasFileEventHandlers;
 37
 38public class FilePanel extends HorizontalPanel implements ClickHandler, HasFileEventHandlers {
 39
 40	private FileText texts = (FileText) GWT.create(FileText.class);
 41
 42	private SimplePanel holder = new SimplePanel();
 43
 44	private FileUpload fileField = new FileUpload();
 45
 46	private int maxDetectionDelay = 3000;
 47
 48	private Anchor addAction = new Anchor(texts.add());
 49
 50	private Anchor remove = new Anchor(texts.remove());
 51
 52	private CheckBox checkBoxForSave = new CheckBox();
 53
 54	private HTML info = new HTML();
 55
 56	private FileHolderHandler handler = null;
 57
 58	private HandlerRegistration addListener = null;
 59
 60	public FilePanel(FileHolderHandler newhandler) {
 61		super();
 62		handler = newhandler;
 63		super.setSpacing(0);
 64
 65		super.add(checkBoxForSave);
 66		super.add(holder);
 67		holder.add(fileField);
 68		super.add(info);
 69		super.add(addAction);
 70		super.add(remove);
 71
 72		addListener = addAction.addClickHandler(this);
 73		remove.addClickHandler(this);
 74		checkBoxForSave.addClickHandler(this);
 75
 76		checkBoxForSave.setVisible(false);
 77		remove.setVisible(false);
 78		info.setVisible(false);
 79		fileField.setVisible(false);
 80		checkBoxForSave.setValue(true);
 81		CSS.setProperty(checkBoxForSave, CSS.A.MARGIN_RIGHT, "3px");
 82		CSS.setProperty(holder, CSS.A.MARGIN_RIGHT, "3px");
 83		CSS.setProperty(info, CSS.A.MARGIN_RIGHT, "3px");
 84		CSS.setProperty(info, CSS.A.MARGIN_RIGHT, "3px");
 85	}
 86
 87	private String fieldname = null;
 88
 89	public void setFieldName(String newName) {
 90		fieldname = newName;
 91		fileField.setName(fieldname);
 92	}
 93
 94	private String url = null;
 95
 96	private String fileName = null;
 97
 98	public void setInfo(String newName, String newUrl) {
 99		fileName = newName;
100		url = newUrl;
101		setUploadState(uploadState);
102	}
103
104	private boolean uploadState = false;
105
106	public boolean hasImageSelected() {
107		return fileField.getFilename() != null && fileField.getFilename().length() > 0;
108	}
109
110	public String getFileName() {
111		if (fileName == null) {
112			return fileField.getFilename();
113		} else {
114			return fileName;
115		}
116	}
117
118	public void resetPanel() {
119		if (hasImageSelected()) {
120			holder.remove(fileField);
121			fileField = new FileUpload();
122			fileField.setName(fieldname);
123			holder.setWidget(fileField);
124			fileField.setVisible(false);
125		}
126	}
127
128	public void setAddActionVisible(boolean newState) {
129		addAction.setVisible(newState);
130	}
131
132	public void setSaveVisible(boolean newState) {
133		if (handler.showViewLink()) {
134			checkBoxForSave.setVisible(newState);
135		} else {
136			checkBoxForSave.setVisible(false);
137		}
138	}
139
140	public void setRemoveVisible(boolean newState) {
141		remove.setVisible(newState);
142	}
143
144	public void setUploadState(boolean newState) {
145		uploadState = newState;
146		remove.setVisible(!newState);
147		if (handler.showViewLink()) {
148			checkBoxForSave.setVisible(newState);
149		} else {
150			checkBoxForSave.setVisible(false);
151		}
152		if (newState) {
153			info.setHTML("<a href='" + url + " '  >" + getFileName() + "</a>");
154		} else {
155			info.setText(getFileName());
156		}
157		if (handler.showViewLink()) {
158			if (uploadState) {
159				info.setVisible(true);
160			} else {
161				info.setVisible(false);
162			}
163		} else {
164			info.setVisible(false);
165		}
166	}
167
168	public void setSelectedForSave(boolean newState) {
169		checkBoxForSave.setValue(newState);
170	}
171
172	public boolean isFileFieldVisible() {
173		// Window.alert("Visible " + fileField.isVisible());
174		return fileField.isVisible();
175	}
176
177	/**
178	 * Timer for that start after the click on determining if a file is present.
179	 */
180	private Timer filePresentTimer = null;
181
182	private void selectFileAction() {
183		// image.se
184		if (getBrowserType().indexOf("ie") > -1) {
185			jsClickUpload(fileField.getElement());
186		} else {
187			addAction.setVisible(false);
188			fileField.setVisible(true);
189		}
190
191		filePresentTimer = new Timer() {
192			public void run() {
193				if (hasImageSelected()) {
194					fireFileSelectedEvent();
195				}
196			}
197		};
198		filePresentTimer.scheduleRepeating(maxDetectionDelay);
199	}
200
201	private void fireFileSelectedEvent() {
202		fileField.setVisible(false);
203		addAction.setVisible(false);
204		// addListener.removeHandler();
205		remove.setVisible(true);
206		info.setText(getFileName());
207		if (handler.showViewLink()) {
208			info.setVisible(true);
209		}
210		filePresentTimer.cancel();
211		FileEvent.fire(this, FileUploadAction.FILE_SELECTED);
212	}
213
214	public void onClick(ClickEvent widget) {
215		if (widget.getSource() == addAction) {
216			selectFileAction();
217		}
218		if (widget.getSource() == remove) {
219			FileEvent.fire(this, FileUploadAction.FILE_REMOVED);
220		}
221		if (widget.getSource() == checkBoxForSave) {
222			FileEvent.fire(this, FileUploadAction.FILE_SELECTION_CHANGE);
223		}
224	}
225
226	public boolean shouldSave() {
227		return checkBoxForSave.getValue();
228	}
229
230	private native String getBrowserType() /*-{ 
231											var ua = navigator.userAgent.toLowerCase(); 
232											if (ua.indexOf("opera") != -1) { 
233											return "opera"; 
234											} 
235											else if (ua.indexOf("webkit") != -1) { 
236											return "safari"; 
237											} 
238											else if ((ua.indexOf("msie 6.0") != -1) || 
239											(ua.indexOf("msie 7.0") != -1)) { 
240											return "ie6"; 
241											} 
242											else if (ua.indexOf("gecko") != -1) { 
243											var result = /rv:([0-9]+)\.([0-9]+)/.exec(ua); 
244											if (result && result.length == 3) { 
245											var version = (parseInt(result[1]) * 10) + 
246											parseInt(result[2]); 
247											if (version >= 18) 
248											return "gecko1_8"; 
249											} 
250											return "gecko"; 
251											} 
252											return "unknown"; 
253											}-*/;
254
255	native void jsClickUpload(Element pElement) /*-{ 
256												pElement.click(); 
257												}-*/;
258
259	@Override
260	public HandlerRegistration addFileEventHandler(FileEventListener handler) {
261		return addHandler(handler, FileEvent.getType());
262	}
263
264}