266 lines
9.6 KiB
Java
266 lines
9.6 KiB
Java
/*
|
|
* Copyright 2004-2007 IDCA. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
|
|
* following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
|
|
* the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
|
* and the following disclaimer in the documentation and/or other materials provided with the
|
|
* distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
* The views and conclusions contained in the software and documentation are those of the authors and
|
|
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
|
|
*/
|
|
|
|
package com.idcanet.vasc.impl;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import org.hibernate.validator.ClassValidator;
|
|
import org.hibernate.validator.InvalidValue;
|
|
|
|
import com.idcanet.vasc.annotations.VascAnnotationParser;
|
|
import com.idcanet.vasc.core.VascEventListener;
|
|
import com.idcanet.vasc.core.VascExceptionListener;
|
|
import com.idcanet.vasc.core.VascTable;
|
|
import com.idcanet.vasc.core.VascTableController;
|
|
import com.idcanet.vasc.core.column.VascAnnotationTableColumn;
|
|
import com.idcanet.vasc.core.column.VascTableColumn;
|
|
import com.idcanet.vasc.core.ui.VascDate;
|
|
import com.idcanet.vasc.core.ui.VascTextField;
|
|
import com.idcanet.vasc.core.ui.VascToggle;
|
|
import com.idcanet.vasc.impl.column.BeanPropertyVascColumnValue;
|
|
|
|
/**
|
|
*
|
|
* @author Willem Cazander
|
|
* @version 1.0 Apr 28, 2007
|
|
*/
|
|
public class DefaultVascTableController implements VascTableController {
|
|
|
|
private Logger logger = null;
|
|
private List<VascEventListener> eventListeners = null;
|
|
private List<VascExceptionListener> exceptionListeners = null;
|
|
|
|
public DefaultVascTableController() {
|
|
logger = Logger.getLogger(DefaultVascTableController.class.getName());
|
|
eventListeners = new ArrayList<VascEventListener>(2);
|
|
exceptionListeners = new ArrayList<VascExceptionListener>(2);
|
|
}
|
|
|
|
/**
|
|
* @see com.idcanet.vasc.core.VascTableController#finalizeVascColumns(com.idcanet.vasc.core.VascTable)
|
|
*/
|
|
public void finalizeVascColumns(VascTable table) throws Exception {
|
|
VascAnnotationParser vap = new VascAnnotationParser();
|
|
|
|
for(VascTableColumn c:table.getTableColumns()) {
|
|
if (c instanceof VascAnnotationTableColumn) {
|
|
VascAnnotationTableColumn column = (VascAnnotationTableColumn)c;
|
|
|
|
if (c.getName()==null) {
|
|
c.setName(vap.getVascNameKey(table.getVascRecordCreator().getObjectClass(), column.getBeanProperty()));
|
|
}
|
|
if (c.getToolTip()==null) {
|
|
c.setToolTip(vap.getVascToolTipKey(table.getVascRecordCreator().getObjectClass(), column.getBeanProperty()));
|
|
}
|
|
if (c.getDefaultValue()==null) {
|
|
c.setDefaultValue(vap.getVascDefaultValue(table.getVascRecordCreator().getObjectClass(), column.getBeanProperty()));
|
|
}
|
|
if (c.getWidth()==null) {
|
|
Object obj = vap.getVascColumnWidth(table.getVascRecordCreator().getObjectClass(), column.getBeanProperty());
|
|
if (obj instanceof Integer) {
|
|
c.setWidth((Integer)obj);
|
|
}
|
|
c.setWidth(100);
|
|
// get KEY
|
|
}
|
|
if (c.getHelpId()==null) {
|
|
c.setHelpId(vap.getVascHelpId(table.getVascRecordCreator().getObjectClass(), column.getBeanProperty()));
|
|
}
|
|
if (c.getVascColumnValue()==null) {
|
|
c.setVascColumnValue(new BeanPropertyVascColumnValue(column.getBeanProperty()));
|
|
}
|
|
if (c.getImage()==null) {
|
|
c.setImage(vap.getVascImage(table.getVascRecordCreator().getObjectClass(),column.getBeanProperty()));
|
|
}
|
|
}
|
|
if (c.getVascUIComponent()==null) {
|
|
if (c.getDefaultValue() instanceof Boolean) {
|
|
c.setVascUIComponent(new VascToggle());
|
|
} else if (c.getDefaultValue() instanceof Date) {
|
|
c.setVascUIComponent(new VascDate());
|
|
} else {
|
|
c.setVascUIComponent(new VascTextField());
|
|
}
|
|
}
|
|
if (c.getVascColumnRenderer()==null) {
|
|
//c.setVascColumnRenderer(new DefaultVascColumnRenderer());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see com.idcanet.vasc.core.VascTableController#finalizeVascTable(com.idcanet.vasc.core.VascTable)
|
|
*/
|
|
public void finalizeVascTable(VascTable table) throws Exception {
|
|
}
|
|
|
|
/**
|
|
* @see com.idcanet.vasc.core.VascTableController#getTotalColumnsWidth(com.idcanet.vasc.core.VascTable)
|
|
*/
|
|
public Integer getTotalColumnsWidth(VascTable table) {
|
|
int result = 0;
|
|
for(VascTableColumn c:table.getTableColumns()) {
|
|
if(c.getWidth()==null) {
|
|
Logger.getLogger(VascTable.class.getName()).finer("Column no size: "+c.getName());
|
|
} else {
|
|
result+=c.getWidth();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @see com.idcanet.vasc.core.VascTableController#initEditObject(com.idcanet.vasc.core.VascTable, java.lang.Object)
|
|
*/
|
|
public Object initEditObject(VascTable table, Object object) throws Exception {
|
|
if (object!=null) {
|
|
return object;
|
|
}
|
|
object = table.getVascRecordCreator().newRecord(table);
|
|
fireVascEvent(VascEventListener.VascEventType.BEAN_INIT, object);
|
|
return object;
|
|
}
|
|
|
|
public void initEditObjectColumn(VascTableColumn c,Object bean) throws Exception {
|
|
Object value = c.getVascColumnValue().getValue(c, bean);
|
|
if(value==null & c.getDefaultValue()!=null) {
|
|
try {
|
|
logger.finer("Setting default value for: "+c.getName()+" def: "+c.getDefaultValue());
|
|
c.getVascColumnValue().setValue(c, bean, c.getDefaultValue());
|
|
} catch (Exception e) {
|
|
logger.log(Level.WARNING,"Error in setting default value: '"+c.getDefaultValue()+"' error: "+e.getMessage(),e);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see com.idcanet.vasc.core.VascTableController#refreshData()
|
|
*/
|
|
public void refreshData(VascTable table) throws Exception {
|
|
table.setSelectedObject(null);
|
|
table.setTableData(table.getVascDataSource().execute());
|
|
fireVascEvent(VascEventListener.VascEventType.DATA_UPDATE, null);
|
|
}
|
|
|
|
public void handleException(Exception e,VascTable table) {
|
|
if (exceptionListeners.isEmpty()) {
|
|
Logger.getLogger(DefaultVascTableController.class.getName()).log(Level.WARNING,e.getMessage(),e);
|
|
return;
|
|
}
|
|
for(VascExceptionListener ee:exceptionListeners) {
|
|
try {
|
|
ee.handleException(e, table);
|
|
} catch (Exception eee) {
|
|
Logger.getLogger(DefaultVascTableController.class.getName()).log(Level.WARNING,"Error in ExceptionListener: "+eee.getMessage(),eee);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void addEventListener(VascEventListener e) {
|
|
eventListeners.add(e);
|
|
}
|
|
public void removeEventListener(VascEventListener e) {
|
|
eventListeners.remove(e);
|
|
}
|
|
|
|
public void fireVascEvent(VascEventListener.VascEventType type,Object data) {
|
|
for(VascEventListener e:eventListeners) {
|
|
e.vascEvent(type, data);
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public boolean setUIComponentsBeanErrors(VascTable table,Object bean) {
|
|
boolean error = false;
|
|
if(bean==null) {
|
|
logger.finest("No bean to check.");
|
|
return true; // nothing to check
|
|
}
|
|
|
|
ClassValidator val = new ClassValidator(bean.getClass());
|
|
InvalidValue[] ival = val.getInvalidValues(bean);
|
|
logger.fine("Got invaliled value: "+ival.length);
|
|
|
|
for(VascTableColumn col:table.getTableColumns()) {
|
|
if(col.getVascUIComponent()==null) {
|
|
continue; // we only DISPLAY user input errors !!
|
|
}
|
|
if (col instanceof VascAnnotationTableColumn) {
|
|
VascAnnotationTableColumn column = (VascAnnotationTableColumn)col;
|
|
|
|
InvalidValue iv = findInvalidValueByProperty(ival,column.getBeanProperty());
|
|
if(iv==null) {
|
|
column.getVascUIComponent().setErrorText(null);
|
|
continue; // no error on this property
|
|
}
|
|
error = true;
|
|
column.getVascUIComponent().setErrorText(iv.getMessage());
|
|
}
|
|
}
|
|
logger.finest("Checked for errors: "+error);
|
|
return error;
|
|
}
|
|
private InvalidValue findInvalidValueByProperty(InvalidValue[] ival,String property) {
|
|
for(InvalidValue iv:ival) {
|
|
if(iv.getPropertyName().equals(property)) {
|
|
return iv;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void addExceptionListener(VascExceptionListener listener) {
|
|
exceptionListeners.add(listener);
|
|
}
|
|
|
|
public void removeExceptionListener(VascExceptionListener listener) {
|
|
exceptionListeners.remove(listener);
|
|
}
|
|
|
|
public Object mergeObject(VascTable table,Object object) {
|
|
Object result = null;
|
|
try {
|
|
object = table.getVascDataSource().merge(object);
|
|
fireVascEvent(VascEventListener.VascEventType.BEAN_MERGE,object);
|
|
// todo: make faster
|
|
// add to table at position old old object
|
|
// then remove old object
|
|
// send refresh
|
|
|
|
table.getVascTableController().refreshData(table);
|
|
} catch (Exception e) {
|
|
handleException(e, table);
|
|
}
|
|
return result;
|
|
}
|
|
} |