001     
002    /*****************************************************************************
003    
004        jBudget.
005        Copyright (C) 2006 Dmitri Petrov
006    
007        This program is free software: you can redistribute it and/or modify
008        it under the terms of the GNU General Public License as published by
009        the Free Software Foundation, either version 3 of the License, or
010        (at your option) any later version.
011    
012        This program is distributed in the hope that it will be useful,
013        but WITHOUT ANY WARRANTY; without even the implied warranty of
014        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015        GNU General Public License for more details.
016    
017        You should have received a copy of the GNU General Public License
018        along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     
020    ******************************************************************************/
021    
022    /*
023     * UserPreferences.java
024     *
025     * Created on November 11, 2006, 5:50 PM
026     */
027    package org.jbudget.gui.user;
028    
029    import java.awt.Dimension;
030    import java.text.ParseException;
031    import java.util.Collections;
032    import java.util.LinkedList;
033    import java.util.List;
034    import org.jdom.Attribute;
035    import org.jdom.Element;
036    import org.jdom.JDOMException;
037    import org.jbudget.Core.Month;
038    import org.jbudget.gui.month.DatesInfoTable;
039    import org.jbudget.gui.month.DatesInfoTableModel;
040    import org.jbudget.gui.month.DefaultTableColumnSorter;
041    import org.jbudget.gui.month.TableColumnSorter;
042    import org.jbudget.util.DateFormater;
043    import org.jbudget.util.Pair;
044    
045    /**
046     * A class for storing user preferences.
047     *
048     * @author petrov
049     */
050    public class UserPreferences
051    {
052        //============================== DATA =================================//
053        
054        /** An object that stores the state of the DatesInfoTable. What columns
055         * should be hidden/shown, should columns representing some categories be
056         * combined with other categories?
057         *@see DatesInfoTableModel.State
058         */
059        private DatesInfoTableModel.State tableState =
060                new DatesInfoTableModel.State();
061        
062        /** An object that stores the preferences used in rendering the month tables */
063        private DatesInfoTable.RenderingState renderingState =
064                new DatesInfoTable.RenderingState();
065        
066        /** An object that stores the colors associated with accounts and 
067         * expense categories.
068         */
069        private DatesInfoTable.Colors colors =
070                new DatesInfoTable.Colors();
071        
072        /** A list of months that a user has opened */
073        List<Pair<Integer, Integer>> openMonths =
074                new LinkedList<Pair<Integer, Integer>>();
075        
076        /** An object used for sorting the table columns. */
077        private TableColumnSorter columnSorter = new DefaultTableColumnSorter();
078        
079        /** Size of the transaction and allocation windows. */
080        private Dimension transactionWindowSize = new Dimension(200, 70);
081    
082        //============================  CONSTRUCTOR ============================//
083        
084        /** Creates a new instance of UserPreferences */
085        public UserPreferences()
086        {
087        }
088    
089        //============================== ACCESSORS ==============================//
090        
091        public DatesInfoTableModel.State getTableState()
092        {
093            return tableState;
094        }
095    
096        public void setTableState(DatesInfoTableModel.State tableState)
097        {
098            this.tableState = tableState;
099        }
100    
101        public DatesInfoTable.RenderingState getRenderingState()
102        {
103            return renderingState;
104        }
105    
106        public void setRenderingState(DatesInfoTable.RenderingState renderingState)
107        {
108            this.renderingState = renderingState;
109        }
110    
111        public DatesInfoTable.Colors getColors()
112        {
113            return colors;
114        }
115    
116        public void setColors(DatesInfoTable.Colors colors)
117        {
118            this.colors = colors;
119        }
120    
121        /** Returns an unmodifaiable list of Month, Year pairs that represent
122         * opened months.
123         */
124        public List<Pair<Integer, Integer>> getOpenedMonths()
125        {
126            return Collections.unmodifiableList(openMonths);
127        }
128    
129        public void setOpenMonths(List<Month> months)
130        {
131            openMonths.clear();
132            for (Month month : months)
133                openMonths.add(new Pair(new Integer(month.getMonth()),
134                        new Integer(month.getYear())));
135        }
136        
137        /** Returns the TableColumnSorter object used by the user. */
138        public TableColumnSorter getColumnSorter() 
139        {
140            return columnSorter;
141        }
142        
143        /** Changes the TableColumnSorter object. */
144        public void setColumnSorter(TableColumnSorter columnSorter)
145        {
146            this.columnSorter = columnSorter;
147        }
148        
149        /** Returns the width of the trasnaction window. */
150        public Dimension getTransactionWindowSize() 
151        {
152            return transactionWindowSize;
153        }
154        
155        /** Changes the width of the transaction window stored in the preferences. */
156        public void setTransactionWindowSize(Dimension size)
157        {
158            transactionWindowSize = size;
159        }
160        
161    
162        //============================== XML ELEMENTS ===========================//
163        
164        public Element getXmlElement()
165        {
166            Element element = new Element("user_preferences");
167            element.addContent(tableState.getXmlElement());
168            element.addContent(renderingState.getXmlElement());
169            element.addContent(colors.getXmlelement());
170    
171            DateFormater formater = new DateFormater();
172    
173            Element e1 = new Element("open_months");
174            for (Pair<Integer, Integer> monthInfo : openMonths)
175            {
176                Element e2 = new Element("month_info");
177                e2.setAttribute("month", formater.formatMonth(monthInfo.first().intValue()));
178                e2.setAttribute("year", monthInfo.second().toString());
179                e1.addContent(e2);
180            }
181            element.addContent(e1);
182            element.addContent(columnSorter.getXmlElement());
183            
184            Element size_element = new Element("transaction_window_size");
185            size_element.setAttribute("width", 
186                                      Integer.toString(transactionWindowSize.width));
187            size_element.setAttribute("height", 
188                                      Integer.toString(transactionWindowSize.height));
189            element.addContent(size_element);
190            
191            return element;
192        }
193    
194        public static UserPreferences getInstance(Element element)
195                throws JDOMException
196        {
197            UserPreferences userPreferences = new UserPreferences();
198    
199            Element tableStateElement = element.getChild("column_model_state");
200            if (tableStateElement != null)
201                userPreferences.tableState =
202                        DatesInfoTableModel.State.getInstance(tableStateElement);
203    
204            Element renderingStateElement = element.getChild("rendering_state");
205            if (renderingStateElement != null)
206                userPreferences.renderingState =
207                        DatesInfoTable.RenderingState.getInstance(renderingStateElement);
208    
209            Element colorsElement = element.getChild("colors");
210            if (colorsElement != null)
211                userPreferences.colors =
212                        DatesInfoTable.Colors.getInstance(colorsElement);
213            
214            Element columnSorterElement = 
215                    element.getChild(TableColumnSorter.xmlNodeName);
216            if (columnSorterElement != null)
217                userPreferences.columnSorter = 
218                        TableColumnSorter.getInstance(columnSorterElement);
219    
220            DateFormater formater = new DateFormater();
221    
222            Element openedMonthsElement = element.getChild("open_months");
223            List<Element> monthElements = openedMonthsElement.getChildren();
224            for (Element e : monthElements)
225            {
226                Attribute attr = e.getAttribute("month");
227                if (attr == null)
228                    throw new JDOMException("UserPreferences.getInstance: No month attribute");
229                int month;
230                try
231                {
232                    month = formater.parseMonth(attr.getValue());
233                }
234                catch (ParseException ex)
235                {
236                    throw new JDOMException(ex.getMessage());
237                }
238    
239                attr = e.getAttribute("year");
240                if (attr == null)
241                    throw new JDOMException("UserPreferences.getInstance: No year attribute");
242                int year = attr.getIntValue();
243    
244                userPreferences.openMonths.add(new Pair(new Integer(month),
245                        new Integer(year)));
246    
247            }
248            
249            Element size_element = element.getChild("transaction_window_size");
250            
251            if (size_element != null)
252            {
253                int width = 
254                        Integer.parseInt(size_element.getAttributeValue("width"));
255                int height =
256                        Integer.parseInt(size_element.getAttributeValue("height"));
257                
258                userPreferences.transactionWindowSize = 
259                                    new Dimension(width, height);
260            }
261    
262            return userPreferences;
263        }
264    }