001    /*
002     * Copyright 2002-2007 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.table;
017    
018    import java.util.List;
019    
020    import org.springframework.beans.BeanWrapper;
021    import org.springframework.beans.BeanWrapperImpl;
022    import org.springframework.context.MessageSource;
023    import org.springframework.context.NoSuchMessageException;
024    import org.springframework.context.support.MessageSourceAccessor;
025    import org.springframework.util.Assert;
026    import org.springframework.util.ClassUtils;
027    
028    /**
029     * @author Keith Donald
030     */
031    public abstract class BeanTableModel extends BaseTableModel {
032        private BeanWrapper beanWrapper = new BeanWrapperImpl();
033    
034        private Class beanClass;
035    
036        private String[] columnPropertyNames;
037    
038        private MessageSourceAccessor messages;
039    
040        public BeanTableModel(Class beanClass) {
041            this(beanClass, (MessageSource)null);
042        }
043    
044        public BeanTableModel(Class beanClass, List rows) {
045            this(beanClass, rows, null);
046        }
047    
048        public BeanTableModel(Class beanClass, MessageSource messages) {
049            super();
050            setBeanClass(beanClass);
051            setMessageSource(messages);
052        }
053    
054        public BeanTableModel(Class beanClass, List rows, MessageSource messages) {
055            super(rows);
056            setBeanClass(beanClass);
057            setMessageSource(messages);
058        }
059    
060        public void setBeanClass(Class clazz) {
061            this.beanClass = clazz;
062        }
063    
064        public void setMessageSource(MessageSource messages) {
065            if (messages != null) {
066                this.messages = new MessageSourceAccessor(messages);
067                createColumnInfo();
068            }
069            else {
070                this.messages = null;
071            }
072        }
073    
074        protected void createColumnInfo() {
075            this.columnPropertyNames = createColumnPropertyNames();
076            super.createColumnInfo();
077        }
078    
079        protected abstract String[] createColumnPropertyNames();
080    
081        protected String[] createColumnNames() {
082            String[] columnPropertyNames = getColumnPropertyNames();
083            String[] columnNames = new String[columnPropertyNames.length];
084            Assert.state(this.messages != null, "First set the MessageSource.");
085            for (int i = 0; i < columnPropertyNames.length; i++) {
086                String className = ClassUtils.getShortNameAsProperty(beanClass);
087                String columnPropertyName = columnPropertyNames[i];
088                try {
089                    columnNames[i] = messages.getMessage(className + "." + columnPropertyName);
090                } catch(NoSuchMessageException e) {
091                    columnNames[i] = messages.getMessage(columnPropertyName, columnPropertyName);
092                }            
093            }
094            return columnNames;
095        }
096    
097        protected String[] getColumnPropertyNames() {
098            return columnPropertyNames;
099        }
100    
101        private String getColumnPropertyName(int index) {
102            return columnPropertyNames[index];
103        }
104    
105        protected Object getValueAtInternal(Object row, int columnIndex) {
106            beanWrapper.setWrappedInstance(row);
107            return beanWrapper.getPropertyValue(columnPropertyNames[columnIndex]);
108        }
109    
110        protected boolean isCellEditableInternal(Object row, int columnIndex) {
111            beanWrapper.setWrappedInstance(row);
112            return beanWrapper.isWritableProperty(getColumnPropertyName(columnIndex));
113        }
114    
115        protected void setValueAtInternal(Object value, Object bean, int columnIndex) {        
116            beanWrapper.setWrappedInstance(bean);
117            beanWrapper.setPropertyValue(getColumnPropertyName(columnIndex), value);        
118        }
119    }