001    /*****************************************************************************
002     *
003     * jBudget.
004     * Copyright (C) 2006-2008  Dmitri Petrov
005     *
006     * This program is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation, either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     * 
019     */
020    
021    package org.jbudget.gui.month;
022    
023    import java.util.ArrayList;
024    import java.util.Enumeration;
025    import java.util.List;
026    import javax.swing.table.DefaultTableColumnModel;
027    import javax.swing.table.TableColumn;
028    import org.jbudget.Core.MoneyPit;
029    
030    /**
031     *
032     * @author petrov
033     */
034    public class TableColumnModel extends DefaultTableColumnModel
035    {
036        //============================= Data ===================================//
037        
038        /** Object used to sort the columns in the table. */
039        TableColumnSorter columnSorter;
040        
041        //============================= Methods ================================//
042        
043        /** Default constructor. Initializes the TableColumnModel to use the 
044         * DefaultTablesorter object to sort columns. */
045        public TableColumnModel()
046        {
047            columnSorter = new DefaultTableColumnSorter();
048        }
049        
050        /** Initializes the TableColumnModel with a given sorter object. */
051        public TableColumnModel(TableColumnSorter columnSorter)
052        {
053            this.columnSorter = columnSorter;
054        }
055        
056        /** Change the sorter used by the TableColumnModel. */
057        public void setSorter(TableColumnSorter columnSorter)
058        {
059            this.columnSorter = columnSorter;
060            
061            List<TableColumn> columns = new ArrayList<TableColumn>();
062            
063            for (Enumeration<TableColumn> c = getColumns(); c.hasMoreElements();)
064                columns.add(c.nextElement());
065            
066            for (TableColumn column : columns)
067                removeColumn(column);
068            
069            for (TableColumn column : columns)
070                addColumn(column);
071        }
072        
073        /** Adds a column. */
074        @Override
075        public void addColumn(TableColumn column)
076        {
077            boolean haveIdentifiers = true;
078            List<MoneyPit> identifiers = new ArrayList<MoneyPit>();
079            
080            for (Enumeration<TableColumn> c = getColumns(); 
081                 c.hasMoreElements() && haveIdentifiers;)
082            {
083                Object o = c.nextElement().getIdentifier();
084                if (o instanceof MoneyPit)
085                    identifiers.add((MoneyPit)o);
086                else
087                    haveIdentifiers = false;
088            }
089            
090            MoneyPit identifier = null;
091            Object o = column.getIdentifier();
092            if (o instanceof MoneyPit)
093                identifier = (MoneyPit)o;
094            else
095                haveIdentifiers = false;
096            
097            super.addColumn(column);
098            if (haveIdentifiers)
099            {
100                int index = columnSorter.getInsertionPoint(identifier, identifiers);
101                super.moveColumn(getColumnCount() - 1, index);
102            }
103        }
104    }