add FileUploadField

This commit is contained in:
Dmitry Abramov 2009-03-11 14:21:40 +00:00
parent 7640fdf78b
commit f93096fa47
6 changed files with 195 additions and 1 deletions

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Dmitry Abramov
* Created: 11.03.2009 17:42:22
* $Id$
*/
package com.haulmont.cuba.gui.components;
import java.io.File;
public interface FileUploadField extends Component {
interface Listener {
class Event {
String filename;
public Event(String filename) {
this.filename = filename;
}
public String getFilename() {
return filename;
}
}
void uploadStarted(Event event);
void uploadFinished(Event event);
void uploadSucceeded(Event event);
void uploadFailed(Event event);
void updateProgress(long readBytes, long contentLength);
}
boolean isUploading();
File getFile();
long getBytesRead();
void addListener(Listener listener);
void removeListener(Listener listener);
}

View File

@ -324,6 +324,14 @@
<xs:attribute name="optionsDatasource" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="upload">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="optional"/>
<xs:attribute name="width" type="xs:string" use="optional"/>
<xs:attribute name="height" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="tree">

View File

@ -33,7 +33,7 @@ public class LayoutLoaderConfig {
lookupLoaders.registerLoader("window", WindowLoader.Lookup.class);
registerComponents(lookupLoaders);
frameLoaders.registerLoader("frame", FrameLoader.class);
frameLoaders.registerLoader("window", FrameLoader.class);
registerComponents(frameLoaders);
}
@ -51,6 +51,7 @@ public class LayoutLoaderConfig {
config.registerLoader("lookupField", LookupFieldLoader.class);
config.registerLoader("pickerField", PickerFieldLoader.class);
config.registerLoader("optionsGroup", OptionsGroupLoader.class);
config.registerLoader("upload", FileUploadFieldLoader.class);
config.registerLoader("table", TableLoader.class);
config.registerLoader("iframe", IFrameLoader.class);
config.registerLoader("split", SplitPanelLoader.class);

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Dmitry Abramov
* Created: 11.03.2009 18:14:23
* $Id$
*/
package com.haulmont.cuba.gui.xml.layout.loaders;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.FileUploadField;
import com.haulmont.cuba.gui.xml.layout.*;
import org.dom4j.Element;
public class FileUploadFieldLoader extends ComponentLoader{
protected FileUploadFieldLoader(Context context) {
super(context);
}
public Component loadComponent(ComponentsFactory factory, Element element) throws InstantiationException, IllegalAccessException {
FileUploadField component = factory.createComponent("upload");
loadId(component, element);
loadHeight(component, element);
loadWidth(component, element);
return component;
}
}

View File

@ -0,0 +1,109 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Dmitry Abramov
* Created: 11.03.2009 17:48:51
* $Id$
*/
package com.haulmont.cuba.web.gui.components;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.web.gui.Window;
import com.itmill.toolkit.ui.Upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileUploadField
extends
AbstractComponent<Upload>
implements
com.haulmont.cuba.gui.components.FileUploadField
{
protected File file;
private List<Listener> listeners = new ArrayList<Listener>();
public FileUploadField() {
component = new Upload(MessageProvider.getMessage(Window.class, "msg://Upload"), new Upload.Receiver() {
public OutputStream receiveUpload(String filename, String MIMEType) {
FileOutputStream fos;
file = new File("/tmp/uploads/" + filename);
try {
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
return null;
}
return fos;
}
});
component.addListener(new Upload.StartedListener() {
public void uploadStarted(Upload.StartedEvent event) {
final Listener.Event e = new Listener.Event(event.getFilename());
for (Listener listener : listeners) {
listener.uploadStarted(e);
}
}
});
component.addListener(new Upload.FinishedListener() {
public void uploadFinished(Upload.FinishedEvent event) {
final Listener.Event e = new Listener.Event(event.getFilename());
for (Listener listener : listeners) {
listener.uploadFinished(e);
}
}
});
component.addListener(new Upload.SucceededListener() {
public void uploadSucceeded(Upload.SucceededEvent event) {
final Listener.Event e = new Listener.Event(event.getFilename());
for (Listener listener : listeners) {
listener.uploadSucceeded(e);
}
}
});
component.addListener(new Upload.FailedListener() {
public void uploadFailed(Upload.FailedEvent event) {
final Listener.Event e = new Listener.Event(event.getFilename());
for (Listener listener : listeners) {
listener.uploadFailed(e);
}
}
});
component.addListener(new Upload.ProgressListener() {
public void updateProgress(long readBytes, long contentLength) {
for (Listener listener : listeners) {
listener.updateProgress(readBytes, contentLength);
}
}
});
}
public <T> T getComponent() {
return (T) component;
}
public boolean isUploading() {
return component.isUploading();
}
public long getBytesRead() {
return component.getBytesRead();
}
public void addListener(Listener listener) {
if (!listeners.contains(listener)) listeners.add(listener);
}
public void removeListener(Listener listener) {
listeners.remove(listener);
}
public File getFile() {
return file;
}
}

View File

@ -42,6 +42,7 @@ public class WebComponentsFactory implements ComponentsFactory {
classes.put("lookupField", LookupField.class);
classes.put("pickerField", PickerField.class);
classes.put("optionsGroup", OptionsGroup.class);
classes.put("upload", FileUploadField.class);
classes.put("split", SplitPanel.class);
classes.put("tree", Tree.class);
classes.put("tabsheet", Tabsheet.class);