001     /*****************************************************************************
002    
003        jBudget.
004        Copyright (C) 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.List;
024    import org.jbudget.Core.MoneyPit;
025    import org.jdom.Attribute;
026    import org.jdom.Element;
027    import org.jdom.JDOMException;
028    
029    /**
030     * Interface that is used by classes that can be used to sort the columns
031     * in the <code>DatesInfoTable</code>. Each column in this table is labeled by a
032     * <code>MoneyPit</code> object, so the TableColumSorter operates on these
033     * objects.
034     * 
035     * @author Dmitri Petrov
036     */
037    public abstract class TableColumnSorter 
038    {
039        //=============================== Data ===================================//
040        
041        /** Name of the XML element that represents instances of TableColumnSorter. */
042        public static String xmlNodeName = "TableColumnSorter";
043        
044        //=============================== Methods ================================//
045            
046        /** Returns the index of the a given <code>MoneyPit</code> that should be 
047         * inserted in a given list of items.
048         * 
049         * @param item MoneyPit that corresponds to the colum that is inserted in 
050         * table.
051         * @param otherItems The list of <code>MoneyPit</code> objects that 
052         * correspond to the columns that already exist in the table.
053         * @return the index of the new column.
054         */
055        public abstract int getInsertionPoint(MoneyPit item,
056                                              List<MoneyPit> otherItems);
057        
058        //=========================== XML Encoding ===============================//
059        
060        /** Returns the XML Element representing the concrete implmenetation of
061         * the <code>TableColumnSorter</code> interface. */
062        public abstract Element getXmlElement();
063        
064        /** Creates a new TableColumnSorter instance using the information provided
065        * in a given JDOM Element.
066        * @throws JDOMException if there are parsing errors or the element does
067        *         not contain all necessary information.
068        */
069        public static TableColumnSorter getInstance( Element element ) 
070                                                         throws JDOMException
071        {
072            if (!element.getName().equals("TableColumnSorter"))
073              throw new JDOMException ("Incorrect element. " + 
074                      "Expeced TableColumnSorter element. Got" +
075                      " " + element.getName() + " element");
076    
077            Attribute attr = element.getAttribute("variant");
078            if (attr == null)
079                throw new JDOMException("No TableColumnSorter variant specified");
080            String variant = attr.getValue();
081            
082            if (variant.equalsIgnoreCase("Default"))
083            {
084                return DefaultTableColumnSorter.getInstance(element);
085            }
086                
087            throw new JDOMException ("Unknown TableColumnSorter variant: " + variant);
088        }
089        
090    }