<commit_before><commit_msg>initial checkin<commit_after>/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * 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 org.springframework.richclient.table.support;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.util.Assert;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TableFormat;
import ca.odell.glazedlists.swing.WritableTableFormat;

/**
 * <code>TableModel</code> that accepts a <code>EventList</code>.
 * @author <NAME>
 */
public abstract class GlazedTableModel extends EventTableModel {

    private static final EventList EMPTY = new BasicEventList();

    private BeanWrapper beanWrapper = new BeanWrapperImpl();

    private String columnLabels[];

    private MessageSourceAccessor messages;

    private String propertyColumnNames[];

    public GlazedTableModel() {
        this((MessageSource) null);
    }

    public GlazedTableModel(Class beanClass, EventList rows) {
        this(rows, null);
    }

    public GlazedTableModel(EventList rows, MessageSource messageSource) {
        super(rows, null);
        setMessageSource(messageSource);
        setTableFormat(createTableFormat());
    }

    public GlazedTableModel(MessageSource messageSource) {
        this(EMPTY, messageSource);
    }

    public void setMessageSource(MessageSource messages) {
        if (messages != null) {
            this.messages = new MessageSourceAccessor(messages);
        } else {
            this.messages = null;
        }
    }

    protected abstract String[] createPropertyColumnNames();

    protected Object getColumnValue(Object row, int column) {
        beanWrapper.setWrappedInstance(row);
        return beanWrapper.getPropertyValue(propertyColumnNames[column]);
    }

    /**
     * May be overridden to achieve control over editable columns.
     * 
     * @param row
     *            the current row
     * @param column
     *            the column
     * @return editable
     */
    protected boolean isEditable(Object row, int column) {
        beanWrapper.setWrappedInstance(row);

        return beanWrapper.isWritableProperty(propertyColumnNames[column]);
    }

    protected Object setColumnValue(Object row, Object value, int column) {
        beanWrapper.setWrappedInstance(row);
        beanWrapper.setPropertyValue(propertyColumnNames[column], value);

        return row;
    }

    private String[] createColumnNames(String[] propertyColumnNames) {
        String[] columnNames = new String[propertyColumnNames.length];
        Assert.notNull(this.messages);
        for (int i = 0; i < propertyColumnNames.length; i++) {
            String columnPropertyName = propertyColumnNames[i];

            final String[] keys = { "label." + columnPropertyName, columnPropertyName };

            MessageSourceResolvable resolvable = new MessageSourceResolvable() {

                public String[] getCodes() {
                    return keys;
                }

                public Object[] getArguments() {
                    return null;
                }

                public String getDefaultMessage() {
                    if (keys.length > 0) {
                        return keys[0];
                    }
                    return "";
                }
            };

            columnNames[i] = messages.getMessage(resolvable);
        }

        return columnNames;
    }

    private TableFormat createTableFormat() {
        propertyColumnNames = createPropertyColumnNames();
        columnLabels = createColumnNames(propertyColumnNames);
        return new WritableTableFormat() {

            public int getColumnCount() {
                return columnLabels.length;
            }

            public String getColumnName(int column) {
                return columnLabels[column];
            }

            public Object getColumnValue(Object row, int column) {
                return GlazedTableModel.this.getColumnValue(row, column);
            }

            public boolean isEditable(Object row, int column) {
                return GlazedTableModel.this.isEditable(row, column);
            }

            public Object setColumnValue(Object row, Object value, int column) {
                return GlazedTableModel.this.setColumnValue(row, value, column);
            }
        };
    }
}
{'fraction_non_alphanumeric': 0.0821944177093359, 'fraction_numerical': 0.0028873917228103944, 'mean_word_length': 2.5785123966942147, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1796111', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1248, 'n_words': 341}