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 * TransactionListPanelBase.java
024 *
025 * Created on May 1, 2006, 6:57 AM
026 */
027 package org.jbudget.gui.trans;
028
029 import java.awt.BorderLayout;
030 import java.awt.Color;
031 import java.awt.Component;
032 import java.awt.Frame;
033 import java.awt.event.ActionEvent;
034 import java.awt.event.ActionListener;
035 import java.awt.event.MouseAdapter;
036 import java.awt.event.MouseEvent;
037 import java.util.Calendar;
038 import java.util.GregorianCalendar;
039 import javax.swing.JFrame;
040 import javax.swing.JMenuItem;
041 import javax.swing.JPanel;
042 import javax.swing.JPopupMenu;
043 import javax.swing.JScrollPane;
044 import javax.swing.JTable;
045 import javax.swing.border.LineBorder;
046 import javax.swing.event.TableModelListener;
047 import javax.swing.table.TableCellEditor;
048 import org.jbudget.Core.Transaction;
049 import org.jbudget.Core.User;
050 import org.jbudget.io.DataManager;
051
052 /**
053 * A panel that displays a list of transactions in a scroll window.
054 * @author petrov
055 */
056 abstract public class TransactionListPanelBase extends JPanel
057 {
058 //==========================================================================
059 //
060 // Data Fields
061 //
062 //==========================================================================
063
064 /** List model */
065 protected TransactionListModelBase tableModel;
066
067 /** The list */
068 protected final Table listComponent = new Table();
069
070 // GUI Components //
071 private final JScrollPane scrollPane;
072 protected final TransactionRenderer cellRenderer;
073 protected final TransactionCellEditor cellEditor;
074 /** An instance of calendar used with the list */
075 private final Calendar calendar = new GregorianCalendar();
076 /** Name of the items that we are dealing with.
077 * 'Transaction' or 'Allocation'
078 */
079 protected String itemName = "";
080
081 //==========================================================================
082 //
083 // Constructors / Initializers
084 //
085 //==========================================================================
086
087 /**
088 * Creates a new instance of TransactionListPanelBase
089 */
090 public TransactionListPanelBase()
091 {
092 // Creating the table that contains the list of transactions
093
094 tableModel = createTableModel();
095
096 listComponent.setModel(tableModel);
097 listComponent.setRowHeight(50);
098 listComponent.setShowGrid(false);
099 cellRenderer = createCellRenderer();
100 listComponent.setDefaultRenderer(Transaction.class, cellRenderer);
101
102 cellEditor = createCellEditor();
103 listComponent.setDefaultEditor(Transaction.class, cellEditor);
104
105 // setting up the GUI
106
107 setBorder(new LineBorder(Color.GRAY));
108
109 setLayout(new BorderLayout());
110 scrollPane = new JScrollPane(listComponent);
111 add(scrollPane , BorderLayout.CENTER);
112
113 // Initializing the popup window
114 listComponent.addMouseListener(new MouseAdapter()
115 {
116
117 @Override
118 public void mousePressed(MouseEvent ev)
119 {
120 checkPopup(ev);
121 }
122
123 @Override
124 public void mouseReleased(MouseEvent ev)
125 {
126 checkPopup(ev);
127 }
128
129 private void checkPopup(MouseEvent ev)
130 {
131 int x = ev.getX();
132 int y = ev.getY();
133 if (ev.isPopupTrigger()) {
134 int row = listComponent.rowAtPoint(ev.getPoint());
135 listComponent.changeSelection(row, 0, false, false);
136 Transaction transaction = (Transaction) listComponent.getValueAt(
137 listComponent.rowAtPoint(ev.getPoint()), 0);
138 PopupMenu menu = new PopupMenu(transaction);
139 menu.show(listComponent, x, y);
140 }
141 }
142 });
143
144 scrollPane.addMouseListener(new MouseAdapter()
145 {
146
147 @Override
148 public void mousePressed(MouseEvent ev)
149 {
150 checkPopup(ev);
151 }
152
153 @Override
154 public void mouseReleased(MouseEvent ev)
155 {
156 listComponent.stopEditing();
157 checkPopup(ev);
158 }
159
160 private void checkPopup(MouseEvent ev)
161 {
162 int x = ev.getX();
163 int y = ev.getY();
164 if (ev.isPopupTrigger()) {
165 PopupMenu menu = new PopupMenu(null);
166 menu.show(scrollPane, x, y);
167 }
168 }
169 });
170 }
171
172 public void dispose()
173 {
174 listComponent.stopEditing();
175 tableModel.dispose();
176 }
177
178 //==========================================================================
179 //
180 // Methods.
181 //
182 //==========================================================================
183 /** Creates a table model. */
184 abstract protected TransactionListModelBase createTableModel();
185
186 /** Creates a cell editor. */
187 abstract protected TransactionCellEditor createCellEditor();
188
189 /** Creates a cell cellRenderer. */
190 abstract protected TransactionRenderer createCellRenderer();
191
192 /** Returns a cell editor component used for editing the transactions */
193 public TableCellEditor getCellEditor()
194 {
195 return cellEditor;
196 }
197
198 /** updates the list */
199 public void updateList()
200 {
201 int index = listComponent.getSelectedRow();
202 Object selected = null;
203 if (index > -1) {
204 selected = listComponent.getValueAt(index, 0);
205 }
206 tableModel.initialize();
207 index = tableModel.indexOf(selected);
208 if (index > -1) {
209 listComponent.changeSelection(index, 0, false, false);
210 }
211 }
212
213 /** Returns the table model used by this component. */
214 public TransactionListModelBase getModel() {
215 return tableModel;
216 }
217
218 public void stopCellEditing()
219 {
220 if (listComponent.isEditing())
221 listComponent.stopEditing();
222 }
223
224 //==========================================================================
225 //
226 // Inner Classes
227 //
228 //==========================================================================
229
230 /** A menu that allows user to add/remove transactions/allocations. */
231 private class PopupMenu extends JPopupMenu
232 {
233
234 private final JMenuItem removeItem = new JMenuItem("Remove " + itemName);
235 private final JMenuItem addItem = new JMenuItem("Add " + itemName);
236 private final Transaction transaction;
237
238 PopupMenu(Transaction t)
239 {
240 this.transaction = t;
241 User user = DataManager.getInstance().getCurrentUser();
242 if (transaction != null &&
243 (!transaction.isAutomatic()) &&
244 (transaction.getUID() == user.getUID() || user == User.SYSTEM))
245 {
246 add(removeItem);
247 }
248 add(addItem);
249
250 removeItem.addActionListener(new ActionListener()
251 {
252
253 public void actionPerformed(ActionEvent e)
254 {
255 removeTransaction(transaction);
256 updateList();
257 }
258 });
259
260 addItem.addActionListener(new ActionListener()
261 {
262 public void actionPerformed(ActionEvent e)
263 {
264 calendar.setTimeInMillis(System.currentTimeMillis());
265
266 //Looking for the parent frame
267 Component parent = TransactionListPanelBase.this.getParent();
268 // Find the first parent frame and use it as the parent of the
269 // dialog.
270 while (!(parent instanceof JFrame) &&
271 !(parent instanceof Frame))
272 {
273 if (parent == null)
274 break;
275 parent = parent.getParent();
276 }
277
278 TransactionEditorDialog dialog =
279 new TransactionEditorDialog((Frame) parent, calendar.getTime(),
280 TransactionListPanelBase.this instanceof AllocationListPanel)
281 {
282 @Override
283 public boolean okAction()
284 {
285 Transaction tr = getNewTransaction();
286 if (tr == null) {
287 return true;
288 }
289 TransactionListPanelBase.this.addTransaction(tr);
290 TransactionListPanelBase.this.updateList();
291 return true;
292 }
293 };
294
295 dialog.setTitle(getDialogTitle());
296 }
297 });
298 }
299 }
300
301 /** An extension of the TableModelListenere inteface that allows external
302 * events to force a table to finish editing a cell. */
303 public interface ModelListener extends TableModelListener
304 {
305 void stopEditing();
306 }
307
308 public class Table extends JTable implements ModelListener
309 {
310 public void stopEditing()
311 {
312 if (cellEditor != null) {
313 cellEditor.stopCellEditing();
314 }
315 }
316 }
317
318 //==========================================================================
319 //
320 // Public Abstract Methods
321 //
322 //==========================================================================
323 /** Removes transaction or allocation from a corresponding object */
324 abstract public void removeTransaction(Transaction transaction);
325
326 /** Adds transaction to the corresponding date structures. */
327 abstract public void addTransaction(Transaction transaction);
328
329 /** Sets the title for the transaction/allocation dialog */
330 abstract public String getDialogTitle();
331 }