1 /*
2 * Copyright (C) 2000 Bill Bereza
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Bill Bereza
19 * email : bereza@pobox.com
20 * url : http://www.pobox.com/~bereza/
21 */
22 package net.bereza.money.gui;
23
24 import net.bereza.gui;
25 import net.bereza.db;
26 import net.bereza.money.db;
27
28 import java.io;
29 import java.util;
30 import java.text;
31 import java.math.BigDecimal;
32
33 import java.awt;
34 import java.awt.event;
35 import javax.swing;
36 import javax.swing.table;
37 import javax.swing.event;
38 import javax.swing.text;
39 import javax.swing.border;
40
41 /**
42 * TransactionTable is a JTable which by default uses
43 * a TransactionTableModel, and uses specific renderers for currency.
44 *
45 * @author $Author: bereza $
46 * @version $Revision: 1.4 $
47 * <p>
48 * $Log: TransactionTable.java,v $
49 * Revision 1.4 2001/07/27 15:41:21 bereza
50 * DEBUG flag stuff
51 *
52 * Revision 1.3 2000/04/15 14:30:30 bereza
53 * using DateFormat.SHORT for date column
54 *
55 * Revision 1.2 2000/03/30 01:57:36 bereza
56 * Added copyright header
57 *
58 * Revision 1.1 2000/03/29 02:03:17 bereza
59 * Initial revision
60 *
61 * <p>
62 * $Id: TransactionTable.java,v 1.4 2001/07/27 15:41:21 bereza Exp $
63 */
64 public class TransactionTable extends JTable
65 {
66 /** Construct an empty TransactionTable. */
67 public TransactionTable()
68 throws DatabaseException
69 {
70 this(null, null, null);
71 }
72
73 /**
74 * Construct a TransactionTable for the specified account,
75 * start date and end date.
76 * @param displayAccount account to get transaction for
77 * @param start get transactions >= this date
78 * @param end get transactions < this date
79 * @exception DatabaseException if there's a problem getting transactions
80 * from the DB
81 */
82 public TransactionTable(Account displayAccount,
83 Date start,
84 Date end)
85 throws DatabaseException
86 {
87 super(new TransactionTableModel(displayAccount, start, end));
88 setRenderers();
89 initColumnSizes();
90 }
91
92 /**
93 * This methods sets the specific renderers for Transactions.
94 */
95 private void setRenderers()
96 {
97 ResourceBundle guiRes;
98
99 guiRes=ResourceBundle.getBundle("net.bereza.money.gui.gui");
100
101 /** set up custom renderers */
102 TableColumnModel colMod=getColumnModel();
103 TableColumn tableCol;
104
105 tableCol=colMod.getColumn(TransactionTableModel.DATE_COLUMN);
106
107 /*
108 DateFormat df=new SimpleDateFormat(guiRes.getString(
109 "table.transaction.format.date"));
110 */
111 DateFormat df=DateFormat.getDateInstance(DateFormat.SHORT);
112 df.setTimeZone(DatabaseItem.getTimeZone());
113
114 tableCol.setCellRenderer(new DateRenderer(df));
115
116 tableCol=colMod.getColumn(TransactionTableModel.DEBIT_COLUMN);
117 tableCol.setCellRenderer(new CurrencyRenderer());
118 tableCol=colMod.getColumn(TransactionTableModel.CREDIT_COLUMN);
119 tableCol.setCellRenderer(new CurrencyRenderer());
120 tableCol=colMod.getColumn(TransactionTableModel.BALANCE_COLUMN);
121 tableCol.setCellRenderer(new CurrencyRenderer());
122 }
123
124 /*
125 * This method picks good column sizes.
126 * If all column heads are wider than the column's cells'
127 * contents, then you can just use column.sizeWidthToFit().
128 */
129 private void initColumnSizes()
130 {
131 TableColumn column = null;
132 Component comp = null;
133 int headerWidth = 0;
134 int cellWidth = 0;
135 TransactionTableModel model=(TransactionTableModel)getModel();
136 Object[] longValues = model.longValues;
137 boolean DEBUG=true;
138
139 if(DEBUG)
140 {
141 System.err.println("initColumnSizes(): "+model.getColumnCount()+
142 "columns");
143 }
144
145 TableCellRenderer headerRenderer;
146
147 for (int i = 0; i < model.getColumnCount(); i++) {
148 column = getColumnModel().getColumn(i);
149
150 headerRenderer=column.getHeaderRenderer();
151
152 if(headerRenderer == null)
153 {
154 headerRenderer=getTableHeader().getDefaultRenderer();
155 }
156
157 comp = headerRenderer.
158 getTableCellRendererComponent(
159 null, column.getHeaderValue(),
160 false, false, 0, 0);
161 headerWidth = comp.getPreferredSize().width;
162
163 comp = getDefaultRenderer(/*getModel().getColumnClass(i)*/ String.class).
164 getTableCellRendererComponent(
165 this, longValues[i],
166 false, false, 0, i);
167 cellWidth = comp.getPreferredSize().width;
168
169 if (DEBUG) {
170 System.err.println("Initializing width of column "
171 + i + ". "
172 + "headerWidth = " + headerWidth
173 + "; cellWidth = " + cellWidth);
174 }
175
176 //XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
177 column.setPreferredWidth(Math.max(headerWidth, cellWidth));
178 }
179 }
180
181 /**
182 * Set the monitor of the model's progress.
183 * @param pm kiwi ProgressObserver
184 */
185 public void setProgressObserver(kiwi.util.ProgressObserver mon)
186 {
187 getTransactions().setProgressObserver(mon);
188 }
189
190 /*
191 * @param displayAccount account to get transaction for
192 * @param start get transactions >= this date
193 * @param end get transactions < this date
194 * @exception DatabaseException if there's a problem getting transactions
195 * from the DB
196 */
197 public synchronized void setDisplay(Account displayAccount,
198 Date start,
199 Date end)
200 throws DatabaseException
201 {
202 if(getModel() instanceof TransactionTableModel)
203 {
204 ((TransactionTableModel)getModel()).setDisplay(displayAccount,
205 start,
206 end);
207 }
208 }
209
210 public TransactionTableModel getTransactions()
211 {
212 return (TransactionTableModel)getModel();
213 }
214
215 public static void main(String argv[])
216 {
217 Class dbc;
218
219 try
220 {
221 try
222 {
223 dbc=Class.forName("postgresql.Driver");
224
225 java.util.Properties props=new java.util.Properties();
226 props.put("user", "bereza");
227 props.put("password", "");
228
229 DatabaseItemFactory dbf=
230 new net.bereza.db.postgres.PostgresDatabaseItemFactory(
231 "jdbc:postgresql:money",
232 props
233 );
234
235 DatabaseItem.setFactory(dbf);
236 }
237 catch(Exception e)
238 {
239 e.printStackTrace(System.err);
240 }
241
242 JFrame tFrame=new JFrame("TransactionTable test");
243
244 Account account=(Account)DatabaseItem.getInstance(Account.class.getName(),
245 1);
246
247 Calendar start=Calendar.getInstance();
248 start.roll(Calendar.MONTH, false);
249
250 TransactionTable transTable=new TransactionTable(account,
251 start.getTime(),
252 new Date());
253 JScrollPane scrollPane = new JScrollPane(transTable);
254
255 //NewTransactionDialog dialog=new NewTransactionDialog(tFrame,
256 //"test input",
257 // "comment");
258
259 //tFrame.getContentPane().setLayout(new BorderLayout());
260
261 tFrame.getContentPane().add("Center", scrollPane);
262
263 tFrame.addWindowListener(new WindowAdapter()
264 {
265 public void windowClosing(WindowEvent e)
266 {
267 System.exit(0);
268 }
269 });
270
271 tFrame.pack();
272 tFrame.show();
273
274 /*
275 dialog.show();
276
277 Transaction trans=dialog.getTransaction();
278
279 if(dialog.isCancelled())
280 {
281 System.err.println("Cancelled");
282 }
283 else
284 {
285 System.out.println(trans);
286 }
287 */
288 }
289 catch(Exception e)
290 {
291 e.printStackTrace(System.err);
292 }
293 }
294 }
295