Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
nz.co.senanque.vaadin.MaduraForm Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c)2014 Prometheus Consulting
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package nz.co.senanque.vaadin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import nz.co.senanque.validationengine.SetterListener;
import nz.co.senanque.validationengine.ValidationObject;
import nz.co.senanque.validationengine.ValidationSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
/**
* Extend this abstract class to implement a specific form.
* Site specific form will include buttons (and their action code) and
* specify what it is to be mapped to etc.
*
* @author Roger Parkinson
*
*/
@Deprecated
public class MaduraForm extends Form implements PropertiesSource {
private static final long serialVersionUID = 1L;
Logger logger = LoggerFactory.getLogger(MaduraForm.class);
List m_errors = new ArrayList();
List m_myButtons = new ArrayList();
private List m_itemDataSourceProperties = new ArrayList();
private List m_fieldList;
private final AbstractLayout m_layout;
private FieldFactory m_maduraFieldFactory;
private MaduraSessionManager m_maduraSessionManager;
public MaduraForm(AbstractLayout layout,MaduraSessionManager maduraSessionManager)
{
m_layout = layout;
m_maduraSessionManager = maduraSessionManager;
m_maduraFieldFactory = maduraSessionManager.getFieldFactory();
this.setFormFieldFactory(m_maduraFieldFactory);
setLayout(m_layout);
setLocale(LocaleContextHolder.getLocale());
setBuffered(false);
setImmediate(true);
}
public MaduraForm(MaduraSessionManager maduraSessionManager)
{
this(new VerticalLayout(),maduraSessionManager);
}
public void setFooter(HorizontalLayout layout) {
HorizontalLayout l = new HorizontalLayout();
l.addComponent(layout);
l.setComponentAlignment(layout, Alignment.MIDDLE_CENTER);
super.setFooter(l);
}
public List getItemDataSourceProperties()
{
return m_itemDataSourceProperties;
}
@SuppressWarnings("rawtypes")
public void setItemDataSource(Item dataSource) {
MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
if (dataSource instanceof BeanItem)
{
Object source = ((BeanItem)dataSource).getBean();
if (source instanceof ValidationObject)
{
m_maduraSessionManager.getValidationSession().bind((ValidationObject)source);
List allFields = maduraSessionManager.getFieldList((ValidationObject)source,dataSource);
List fields = allFields;
if (getFieldList() != null)
{
fields = getFieldList();
}
super.setItemDataSource(dataSource, fields);
getFooter().setVisible(true);
List properties = new ArrayList();
for (String fieldName: allFields)
{
Property p = getItemProperty(fieldName);
if (p == null)
{
p = maduraSessionManager.getMaduraPropertyWrapper((ValidationObject)source, fieldName);
}
if (p instanceof MaduraPropertyWrapper)
{
properties.add((MaduraPropertyWrapper)p);
}
}
m_itemDataSourceProperties = properties;
for (Button button : m_myButtons)
{
ButtonProperty buttonProperty = (ButtonProperty)button.getData();
buttonProperty.getPainter().setPropertiesSource(this);
maduraSessionManager.register(button, buttonProperty.getPainter());
buttonProperty.getPainter().paint(button);
MaduraPropertyWrapper wrapper = buttonProperty.getPainter().getProperty();
if (wrapper != null) {
final Button finalButton = button;
m_maduraSessionManager.getValidationSession().addListener(wrapper.getOwner(),wrapper.getName(), new SetterListener(){
@Override
public void run(ValidationObject object, String name,
Object newValue, ValidationSession session) {
finalButton.markAsDirty();
}});
}
button.markAsDirty();
}
}
else
{
// some other kind of bean item
super.setItemDataSource(dataSource,getFieldList());
getFooter().setVisible(false);
}
}
else
{
// Some other kind of item
super.setItemDataSource(dataSource,getFieldList());
getFooter().setVisible(false);
}
this.markAsDirtyRecursive();
}
/**
* Create a button that only enables when all the required fields are completed without error.
* @param name
* @param listener
* @return button
*/
public Button createSubmitButton(String name, ClickListener listener) {
return createButton(name,new SubmitButtonPainter(m_maduraSessionManager),listener);
}
/**
* Create a simple button.
* @param name
* @param listener
* @return button
*/
public Button createButton(String name, ClickListener listener) {
return createButton(name,new SimpleButtonPainter(m_maduraSessionManager),listener);
}
/**
* Create a button that enables and disables depending on the value of the boolean propertyId.
* @param name
* @param propertyId
* @param listener
* @return button
*/
public Button createFieldButton(String name, String propertyId, ClickListener listener) {
return createButton(name,new FieldButtonPainter(propertyId, m_maduraSessionManager),listener);
}
/**
* Create a button that only enables when all the required fields are completed without error.
* @param name
* @param permission
* @param listener
* @return button
*/
public Button createSubmitButton(String name, String permission, ClickListener listener) {
return createButton(name,new SubmitButtonPainter(permission,m_maduraSessionManager),listener);
}
/**
* Create a simple button.
* @param name
* @param permission
* @param listener
* @return button
*/
public Button createButton(String name, String permission, ClickListener listener) {
return createButton(name,new SimpleButtonPainter(permission, m_maduraSessionManager),listener);
}
/**
* Create a button that enables and disables depending on the value of the boolean propertyId.
* @param name
* @param permission
* @param propertyId
* @param listener
* @return
*/
public Button createFieldButton(String name, String propertyId, String permission, ClickListener listener) {
return createButton(name,new FieldButtonPainter(propertyId, permission, m_maduraSessionManager),listener);
}
private Button createButton(String name,
ButtonPainter painter, ClickListener listener)
{
return createButton(name,painter,listener,null);
}
private Button createButton(String name,
ButtonPainter painter, ClickListener listener, Object data)
{
Button ret = m_maduraFieldFactory.createButton(name, listener, painter);
painter.setPropertiesSource(this);
m_myButtons.add(ret);
return ret;
}
public void addExtraField(Item item, Object id)
{
final Property> property = item.getItemProperty(id);
final Field> f = getFormFieldFactory().createField(item, id, this);
if (f != null) {
f.setPropertyDataSource(property);
addField(id, f);
}
}
public void changeVariables(Object source, Map variables) {
super.changeVariables(source, variables);
logger.debug(" {} {}",source,variables);
}
public void setErrors(List errors) {
m_errors = errors;
}
public List getErrors() {
return m_errors;
}
protected List getFieldList()
{
return m_fieldList;
}
public void setFieldList(List fieldList) {
m_fieldList = fieldList;
}
public void setFieldList(String[] fieldList) {
m_fieldList = Arrays.asList(fieldList);
}
public void destroy()
{
MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
for (MaduraPropertyWrapper property: m_itemDataSourceProperties)
{
AbstractComponent field = (AbstractComponent)getField(property.getName());
maduraSessionManager.deregister(field);
}
for (Button button : m_myButtons)
{
maduraSessionManager.deregister(button);
}
}
public MaduraSessionManager getMaduraSessionManager() {
return m_maduraSessionManager;
}
public void dumpFields() {
if (logger.isDebugEnabled()) {
for (String propertyId: m_fieldList) {
Field> field = getField(propertyId);
logger.debug("Field {} Enabled: {} ReadOnly: {}",propertyId,field.isEnabled(),field.isReadOnly());
}
}
}
@Override
public List getProperties() {
return m_itemDataSourceProperties;
}
@Override
public MaduraPropertyWrapper findProperty(String propertyName) {
return m_maduraSessionManager.findProperty(propertyName, m_itemDataSourceProperties);
}
}