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 * DatesTableModel.java
024 *
025 * Created on April 15, 2006, 2:07 PM
026 */
027
028 package org.jbudget.gui.month;
029
030 import java.util.ArrayList;
031 import java.util.Calendar;
032 import java.util.Date;
033 import java.util.GregorianCalendar;
034 import java.util.List;
035 import javax.swing.table.AbstractTableModel;
036 import org.jbudget.Core.Month;
037 import org.jbudget.gui.GuiPreferences;
038
039 /**
040 * Implements a table model for a single row of the days of the month.
041 *
042 * @author petrov
043 */
044 public class DatesTableModel extends AbstractTableModel
045 {
046
047 /** The month which is represented by this table */
048 private final Month month;
049 /** The list of days of the month which will be displayed */
050 private final List<Integer> days = new ArrayList<Integer>(31);
051 /** A calendar instance. */
052 private final Calendar calendar = new GregorianCalendar();
053
054 /** Creates a new instance of DatesTableModel */
055 public DatesTableModel(Month month)
056 {
057 this.month = month;
058 initialize();
059 }
060
061
062 /** Creates a list of days that are displayed. This can be either
063 * all days of the month or just the days that have some activity
064 */
065 protected void initialize()
066 {
067 days.clear();
068 calendar.setTime(month.getFirstDay());
069 int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
070 int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
071 boolean showEmpty = GuiPreferences.getShowEmptyDays();
072 if (showEmpty)
073 for (int day = firstDay; day <= lastDay; day++)
074 days.add(new Integer(day));
075 else
076 for (int day = firstDay; day <= lastDay; day++)
077 if ( !month.getTransactions(day).isEmpty() )
078 days.add(day);
079 fireTableDataChanged();
080 }
081
082 @Override
083 public Class getColumnClass(int index) {return Integer.class;}
084
085 @Override
086 public String getColumnName(int index) {return "Date";}
087
088 public int getColumnCount() {return 1;}
089
090 public int getRowCount() {return days.size();}
091
092 public Object getValueAt(int rowIndex, int columnIndex)
093 {
094 return days.get(rowIndex);
095 }
096
097 public int getDayAt(int rowIndex)
098
099 {
100 return days.get(rowIndex).intValue();
101 }
102 public int findRow (Date date){
103 calendar.setTime(date);
104 int day = calendar.get(Calendar.DAY_OF_MONTH);
105 return day - 1;
106 }
107
108 }