Upload field support

This commit is contained in:
Yuriy Artamonov 2013-03-18 09:20:04 +00:00
parent c7e9349e14
commit 45eb4488fe
8 changed files with 142 additions and 68 deletions

View File

@ -2,20 +2,12 @@
* Copyright (c) 2009 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 29.10.2009 15:51:19
*
* $Id$
*/
package com.haulmont.cuba.gui.app.core.file;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.core.global.PersistenceHelper;
import com.haulmont.cuba.core.global.TimeProvider;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.upload.FileUploadingAPI;
@ -26,34 +18,37 @@ import javax.inject.Inject;
import java.io.File;
import java.util.Map;
public class FileEditor extends AbstractEditor {
/**
* @author krivopustov
* @version $Id$
*/
public class FileEditor extends AbstractEditor<FileDescriptor> {
@Inject
private Datasource<FileDescriptor> fileDs;
protected Datasource<FileDescriptor> fileDs;
@Resource(name = "windowActions.windowCommit")
private Button okBtn;
protected Button okBtn;
@Inject
private TextField nameField;
protected TextField nameField;
@Inject
private Label extLabel;
protected Label extLabel;
@Inject
private Label sizeLabel;
protected Label sizeLabel;
@Inject
private Label createDateLabel;
protected Label createDateLabel;
@Inject
private FileUploadField uploadField;
protected FileUploadField uploadField;
private boolean needSave;
protected boolean needSave;
public FileEditor(IFrame frame) {
super(frame);
}
@Inject
protected TimeSource timeSource;
@Override
public void init(Map<String, Object> params) {
@ -101,15 +96,16 @@ public class FileEditor extends AbstractEditor {
private class FileUploadListener extends FileUploadField.ListenerAdapter {
@Override
public void uploadSucceeded(Event event) {
nameField.setValue(uploadField.getFileName());
extLabel.setValue(getFileExt(uploadField.getFileName()));
getItem().setName(uploadField.getFileName());
getItem().setCreateDate(timeSource.currentTimestamp());
getItem().setExtension(getFileExt(uploadField.getFileName()));
FileUploadingAPI fileUploading = AppBeans.get(FileUploadingAPI.NAME);
File file = fileUploading.getFile(uploadField.getFileId());
Integer size = (int) file.length();
sizeLabel.setValue(size);
createDateLabel.setValue(TimeProvider.currentTimestamp());
getItem().setSize(size);
okBtn.setEnabled(true);
needSave = true;

View File

@ -19,6 +19,7 @@ import com.haulmont.cuba.web.sys.ActiveDirectoryHelper;
import com.haulmont.cuba.web.sys.LinkHandler;
import com.haulmont.cuba.web.sys.RequestContext;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -206,6 +207,9 @@ public abstract class App implements Serializable {
public void setLocale(Locale locale) {
this.locale = locale;
UI.getCurrent().setLocale(locale);
VaadinSession.getCurrent().setLocale(locale);
}
public String getClientAddress() {

View File

@ -342,6 +342,7 @@ public class LoginWindow extends UIView implements Action.Handler {
if (ActiveDirectoryHelper.useActiveDirectory() && StringUtils.containsAny(login, DOMAIN_SEPARATORS)) {
Locale locale = getUserLocale();
App.getInstance().setLocale(locale);
String password = passwordField.getValue();
if (loginByRememberMe && StringUtils.isNotEmpty(password))
password = decryptPassword(password);
@ -351,10 +352,12 @@ public class LoginWindow extends UIView implements Action.Handler {
((ActiveDirectoryConnection) connection).loginActiveDirectory(login, locale);
} else {
Locale locale = getUserLocale();
App.getInstance().setLocale(locale);
String value = passwordField.getValue() != null ? passwordField.getValue() : "";
String passwd = loginByRememberMe ? value : passwordEncryption.getPlainHash(value);
Locale locale = getUserLocale();
UI.getCurrent().setLocale(locale);
login(login, passwd, locale);
}
} catch (LoginException e) {

View File

@ -57,7 +57,7 @@ public class WebComponentsFactory implements ComponentsFactory {
classes.put(LookupPickerField.NAME, WebLookupPickerField.class);
// classes.put(SearchPickerField.NAME, WebSearchPickerField.class);
classes.put(OptionsGroup.NAME, WebOptionsGroup.class);
// classes.put(FileUploadField.NAME, WebFileUploadField.class);
classes.put(FileUploadField.NAME, WebFileUploadField.class);
// classes.put(FileMultiUploadField.NAME, WebFileMultiUploadField.class);
classes.put(SplitPanel.NAME, WebSplitPanel.class);
classes.put(Tree.NAME, WebTree.class);

View File

@ -6,6 +6,7 @@
package com.haulmont.cuba.web.gui.components;
import com.haulmont.chile.core.datatypes.Datatype;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.model.Instance;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
@ -17,6 +18,7 @@ import com.haulmont.cuba.gui.components.TextField;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.web.gui.data.ItemWrapper;
import com.haulmont.cuba.web.gui.data.PropertyWrapper;
import com.haulmont.cuba.web.toolkit.ui.converters.DatatypeToStringConverter;
import com.haulmont.cuba.web.toolkit.ui.converters.EntityToStringConverter;
import com.haulmont.cuba.web.toolkit.ui.converters.StringToStringConverter;
import com.vaadin.data.util.converter.Converter;
@ -56,9 +58,9 @@ public abstract class WebAbstractTextField<T extends com.haulmont.cuba.web.toolk
component.setConverter(new Converter<String, Object>() {
@Override
public Object convertToModel(String value, Locale locale) throws ConversionException {
if (datatype != null) {
if (getActualDatatype() != null) {
try {
return datatype.parse(value, locale);
return getActualDatatype().parse(value, locale);
} catch (ParseException e) {
log.warn("Unable to parse value of component " + getId() + "\n" + e.getMessage());
return null;
@ -70,8 +72,8 @@ public abstract class WebAbstractTextField<T extends com.haulmont.cuba.web.toolk
@Override
public String convertToPresentation(Object value, Locale locale) throws ConversionException {
if (datatype != null && value != null) {
return datatype.format(value, locale);
if (getActualDatatype() != null && value != null) {
return getActualDatatype().format(value, locale);
} else if (value != null) {
return value.toString();
} else {
@ -217,7 +219,13 @@ public abstract class WebAbstractTextField<T extends com.haulmont.cuba.web.toolk
if (metaProperty.getType() == MetaProperty.Type.ASSOCIATION)
component.setConverter(new EntityToStringConverter());
else
else if (metaProperty.getType() == MetaProperty.Type.DATATYPE) {
Datatype<?> datatype = Datatypes.get(metaProperty.getJavaType());
if (datatype != null)
component.setConverter(new DatatypeToStringConverter(datatype));
else
component.setConverter(new StringToStringConverter());
} else
component.setConverter(new StringToStringConverter());
}

View File

@ -30,8 +30,8 @@ import java.util.UUID;
* @author abramov
* @version $Id$
*/
public class WebFileUploadField /*extends WebAbstractComponent<Upload> implements FileUploadField*/ {
/*
public class WebFileUploadField extends WebAbstractComponent<com.vaadin.ui.Upload> implements FileUploadField {
private static final int BYTES_IN_MEGABYTE = 1048576;
protected FileUploadingAPI fileUploading;
@ -52,9 +52,9 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
fileUploading = AppBeans.get(FileUploadingAPI.NAME);
messages = AppBeans.get(Messages.class);
String caption = messages.getMessage(AppConfig.getMessagesPack(), "Upload");
component = new Upload(
*//* Fixes caption rendering.
* If caption == "", the VerticalLayout reserves an empty space *//*
component = new com.vaadin.ui.Upload(
/* Fixes caption rendering.
* If caption == "", the VerticalLayout reserves an empty space */
StringUtils.isEmpty(caption) ? null : caption,
new Upload.Receiver() {
@Override
@ -72,9 +72,8 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
});
// Set single click upload functional
component.setImmediate(true);
component.setAction("");
component.addListener(new Upload.StartedListener() {
component.addStartedListener(new Upload.StartedListener() {
@Override
public void uploadStarted(Upload.StartedEvent event) {
final Integer maxUploadSizeMb = AppBeans.get(Configuration.class).getConfig(ClientConfig.class).getMaxUploadSizeMb();
@ -91,7 +90,7 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
}
}
});
component.addListener(new Upload.FinishedListener() {
component.addFinishedListener(new Upload.FinishedListener() {
@Override
public void uploadFinished(Upload.FinishedEvent event) {
if (outputStream != null)
@ -106,7 +105,7 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
}
}
});
component.addListener(new Upload.SucceededListener() {
component.addSucceededListener(new Upload.SucceededListener() {
@Override
public void uploadSucceeded(Upload.SucceededEvent event) {
final Listener.Event e = new Listener.Event(event.getFilename());
@ -115,7 +114,7 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
}
}
});
component.addListener(new Upload.FailedListener() {
component.addFailedListener(new Upload.FailedListener() {
@Override
public void uploadFailed(Upload.FailedEvent event) {
try {
@ -156,12 +155,12 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
listeners.remove(listener);
}
*//**
/**
* Get content bytes for uploaded file
*
* @return Bytes for uploaded file
* @deprecated Please use {@link WebFileUploadField#getFileId()} method and {@link FileUploadingAPI}
*//*
*/
@Deprecated
public byte[] getBytes() {
byte[] bytes = null;
@ -200,17 +199,17 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
component.setDescription(description);
}
public String getButtonWidth() {
return component.getButtonWidth();
}
// public String getButtonWidth() {
// return component.getButtonWidth();
// }
//
// public void setButtonWidth(String buttonWidth) {
// component.setButtonWidth(buttonWidth);
// }
public void setButtonWidth(String buttonWidth) {
component.setButtonWidth(buttonWidth);
}
*//**
/**
* @return File id for uploaded file in {@link FileUploadingAPI}
*//*
*/
@Override
public UUID getFileId() {
return fileId;
@ -222,5 +221,5 @@ public class WebFileUploadField /*extends WebAbstractComponent<Upload> implement
return fileUploading.getFileDescriptor(fileId, fileName);
else
return null;
}*/
}
}

View File

@ -2,33 +2,36 @@
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Dmitry Abramov
* Created: 22.12.2008 18:16:37
* $Id$
*/
package com.haulmont.cuba.web.gui.components;
import com.haulmont.chile.core.datatypes.Datatype;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.model.Instance;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.Formatter;
import com.haulmont.cuba.gui.components.Label;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.ValueChangingListener;
import com.haulmont.cuba.gui.data.ValueListener;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.web.gui.data.ItemWrapper;
import com.haulmont.cuba.web.gui.data.PropertyWrapper;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.Instance;
import com.haulmont.cuba.web.toolkit.ui.converters.DatatypeToStringConverter;
import com.haulmont.cuba.web.toolkit.ui.converters.EntityToStringConverter;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* @author abramov
* @version $Id$
*/
public class WebLabel
extends
WebAbstractComponent<com.vaadin.ui.Label>
@ -71,7 +74,13 @@ public class WebLabel
// vaadin7 converters
if (metaProperty.getType() == MetaProperty.Type.ASSOCIATION)
component.setConverter(new EntityToStringConverter());
else
else if (metaProperty.getType() == MetaProperty.Type.DATATYPE) {
Datatype<?> datatype = Datatypes.get(metaProperty.getJavaType());
if (datatype != null)
component.setConverter(new DatatypeToStringConverter(datatype));
else
component.setConverter(null);
} else
component.setConverter(null);
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2013 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
*/
package com.haulmont.cuba.web.toolkit.ui.converters;
import com.haulmont.chile.core.datatypes.Datatype;
import com.vaadin.data.util.converter.Converter;
import java.text.ParseException;
import java.util.Locale;
/**
* @author artamonov
* @version $Id$
*/
public class DatatypeToStringConverter implements Converter<String, Object> {
private Datatype datatype;
public DatatypeToStringConverter(Datatype datatype) {
this.datatype = datatype;
}
@Override
public Object convertToModel(String value, Locale locale) throws ConversionException {
try {
if (locale != null)
return datatype.parse(value, locale);
else
return datatype.parse(value);
} catch (ParseException e) {
throw new ConversionException(e);
}
}
@Override
public String convertToPresentation(Object value, Locale locale) throws ConversionException {
if (locale != null)
return datatype.format(value, locale);
else
return datatype.format(value);
}
@Override
public Class<Object> getModelType() {
return Object.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
}