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 * MonthPanel.java
024 *
025 * Created on May 8, 2006, 2:44 PM
026 */
027
028 package org.jbudget.gui.month;
029
030 import java.awt.BorderLayout;
031 import java.util.Calendar;
032 import java.util.GregorianCalendar;
033 import java.util.List;
034 import javax.swing.JPanel;
035 import javax.swing.JTable;
036 import javax.swing.event.ListSelectionEvent;
037 import javax.swing.event.ListSelectionListener;
038 import javax.swing.table.TableColumnModel;
039 import org.jbudget.Core.MoneyPit;
040 import org.jbudget.Core.Month;
041 import org.jbudget.Core.TransactionFilter;
042 import org.jbudget.gui.FilterChangeDispatcher;
043 import org.jbudget.gui.FilterChangeListener;
044
045 /**
046 * Creates a panel that corresponds to a given month.
047 * Shows calendar summary and transactions.
048 *
049 * @author petrov
050 */
051 public class MonthPanel extends JPanel
052 implements FilterChangeListener
053 {
054 /** The Month instance that is associated with this object */
055 private final Month month;
056
057 /** Day by day table of balances/transactions */
058 private final CombinedTablePanel datesPanel;
059
060 /** Internal calendar instance*/
061 private static final Calendar calendar = new GregorianCalendar();
062
063 //========================================================================//
064
065 /** Creates a new instance of MonthPanel */
066 public MonthPanel(Month month,
067 DatesInfoTableModel.State modelState,
068 DatesInfoTable.RenderingState renderingState)
069 {
070 datesPanel = new CombinedTablePanel(month, modelState, renderingState);
071
072 setLayout(new BorderLayout());
073 add(datesPanel, BorderLayout.CENTER);
074
075 initializeActions();
076
077 this.month = month;
078 FilterChangeDispatcher.getInstance().addListener(this);
079 }
080
081 /** Call before hiding the panel */
082 public void dispose()
083 {
084 FilterChangeDispatcher.getInstance().removeListener(this);
085 datesPanel.dispose();
086 }
087
088 public void setTableState(DatesInfoTableModel.State modelState) {
089 datesPanel.getTableModel().setState(modelState);
090 }
091
092 public void setRenderingState(DatesInfoTable.RenderingState renderingState) {
093 datesPanel.setRenderingState(renderingState);
094 }
095
096 public void setColumnColors(DatesInfoTable.Colors colors) {
097 datesPanel.setColumnColors(colors);
098 }
099
100 /** Returns the DatesInfoTable object associated with this panel */
101 public JTable getDatesInfoTable() {
102 return this.datesPanel.getDatesInfoTable();
103 }
104
105 public Month getMonth() {
106 return month;
107 }
108
109 /** Returns a list of days (rows in the table) selected by the user. */
110 public List<Integer> getSelectedDays() {
111 return datesPanel.getSelectedDays();
112 }
113
114 /** Initializes actions (button events etc...)*/
115 private void initializeActions()
116 {
117 // An object that keeps track of selected cells in the calendar and
118 // generates a filter for the transaction list.
119 ListSelectionListener datesPanelSelectionListener =
120 new ListSelectionListener()
121 {
122 public void valueChanged(ListSelectionEvent ev)
123 {
124 if (ev.getValueIsAdjusting())
125 return;
126
127 JTable datesInfoTable = datesPanel.getDatesInfoTable();
128 TableColumnModel columnModel = datesInfoTable.getColumnModel();
129 JTable datesTable = datesPanel.getDatesTable();
130
131 int[] selectedRows = datesInfoTable.getSelectedRows();
132 int[] selectedColumns = datesInfoTable.getSelectedColumns();
133
134 if (selectedRows.length == 0)
135 return;
136
137 TransactionFilter filter = new TransactionFilter();
138
139 filter.showAllSources = false;
140 filter.showAllDestinations = false;
141 filter.requireDestAndSource = false;
142
143 filter.clearDestinations();
144 filter.clearSources();
145 for (int i = 0; i < selectedColumns.length; i++)
146 {
147 List<MoneyPit> pits = datesPanel.getTableModel(
148 ).getItemsForColumn(columnModel.getColumn(
149 selectedColumns[i]).getModelIndex());
150
151 for (MoneyPit pit : pits)
152 {
153 if (pit.canBeSource()) {
154 filter.addSource(pit);
155 }
156 if (pit.canBeDestination()) {
157 filter.addDestination(pit);
158 }
159 }
160 }
161
162 int firstDay = ((Integer) (datesTable.getValueAt(
163 selectedRows[0], 0))).intValue();
164 int lastDay = ((Integer) (datesTable.getValueAt(
165 selectedRows[selectedRows.length - 1], 0))).intValue();
166
167 synchronized (calendar)
168 {
169 calendar.setTime(month.startingDate);
170 calendar.set(Calendar.DAY_OF_MONTH, firstDay);
171 filter.startingDate = calendar.getTime();
172 calendar.set(Calendar.DAY_OF_MONTH, lastDay);
173 filter.endingDate = calendar.getTime();
174 }
175
176 FilterChangeDispatcher.getInstance(
177 ).filterUpdated(filter, MonthPanel.this);
178 }
179 };
180
181 datesPanel.getDatesInfoTable().getSelectionModel(
182 ).addListSelectionListener(datesPanelSelectionListener);
183 datesPanel.getDatesInfoTable().getColumnModel().getSelectionModel(
184 ).addListSelectionListener(datesPanelSelectionListener);
185 }
186
187 /** Method that is called when the state of the TransactionFilter used
188 * by several GUI compoenents changes.
189 *
190 * @param newFilter the new filter object.
191 * @param initiator reference to the object that initiated the change.
192 */
193 public void filterUpdated(TransactionFilter newFilter, Object initiator)
194 {
195 if (initiator == this)
196 return;
197
198 datesPanel.getDatesInfoTable().clearSelection();
199 datesPanel.getDatesTable().clearSelection();
200 }
201 }