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 * TransactionEditorDialog.java
024 *
025 * Created on May 11, 2006, 4:11 PM
026 */
027
028 package org.jbudget.gui.trans;
029
030 import java.awt.BorderLayout;
031 import java.awt.Dimension;
032 import java.awt.event.ActionEvent;
033 import java.awt.event.ActionListener;
034 import java.awt.event.ComponentAdapter;
035 import java.awt.event.ComponentEvent;
036 import java.text.ParseException;
037 import java.util.Date;
038 import javax.swing.JButton;
039 import javax.swing.JOptionPane;
040 import javax.swing.JPopupMenu;
041 import org.jbudget.Core.Transaction;
042 import org.jbudget.exceptions.EmptyTransactionEx;
043 import org.jbudget.exceptions.InvalidSrcDestEx;
044 import org.jbudget.gui.BasicDialog;
045 import org.jbudget.io.DataManager;
046
047 /**
048 *
049 * @author petrov
050 */
051 public class TransactionEditorDialog extends BasicDialog
052 {
053
054 private final TransactionEditor transactionPanel;
055 private final boolean allocationMode;
056 private final JButton extraActionsButton = new JButton("Actions Menu");
057
058 /** Creates a new instance of TransactionEditorDialog
059 *@param allocationEditor if true a component that is used for editing
060 * allocations will be displayed, otherwise a component for editing
061 * transactions will be shown.
062 */
063 public TransactionEditorDialog(java.awt.Frame parent,
064 Transaction transaction,
065 boolean allocationEditor)
066 {
067 super(parent, false, false);
068 extraActionsButton.setToolTipText("Additional Actions");
069 allocationMode = allocationEditor;
070 if (allocationEditor)
071 transactionPanel = new TransactionEditor(TransactionEditor.AllocationMode);
072 else
073 {
074 transactionPanel = new TransactionEditor(TransactionEditor.TransactionMode);
075 addButton(extraActionsButton);
076 initializeActions();
077 }
078 inputPanel.setLayout(new BorderLayout());
079 inputPanel.add(transactionPanel);
080
081 addComponentListener(new ComponentAdapter() {
082 @Override
083 public void componentResized(ComponentEvent e)
084 {
085 TransactionEditorDialog dialog =
086 (TransactionEditorDialog)e.getComponent();
087 if (dialog.transactionPanel.getNTransfers() > 1)
088 return;
089
090 DataManager.getInstance()
091 .getCurrentUser()
092 .getUserPreferences()
093 .setTransactionWindowSize(dialog.getSize());
094 }
095
096 } );
097
098 pack();
099 setSize(DataManager.getInstance()
100 .getCurrentUser()
101 .getUserPreferences()
102 .getTransactionWindowSize());
103 setLocationRelativeTo(parent);
104 setVisible(true);
105 transactionPanel.setFocus();
106 }
107
108 /** Creates a new instance of TransactionEditorDialog
109 *@param allocationEditor if true a component that is used for editing
110 * allocations will be displayed, otherwise a component for editing
111 * transactions will be shown.
112 */
113 public TransactionEditorDialog(java.awt.Frame parent,
114 Date initialDate,
115 boolean allocationEditor)
116 {
117 super(parent, false, false);
118 extraActionsButton.setToolTipText("Additional Actions");
119 allocationMode = allocationEditor;
120 if (allocationEditor)
121 transactionPanel = new TransactionEditor(TransactionEditor.AllocationMode);
122 else
123 {
124 transactionPanel = new TransactionEditor(TransactionEditor.TransactionMode);
125 addButton(extraActionsButton);
126 initializeActions();
127 }
128
129 transactionPanel.setDate(initialDate);
130
131 inputPanel.setLayout(new BorderLayout());
132 inputPanel.add(transactionPanel);
133
134 addComponentListener(new ComponentAdapter() {
135 @Override
136 public void componentResized(ComponentEvent e)
137 {
138 TransactionEditorDialog dialog =
139 (TransactionEditorDialog)e.getComponent();
140
141 if (dialog.transactionPanel.getNTransfers() > 1)
142 return;
143
144 DataManager.getInstance()
145 .getCurrentUser()
146 .getUserPreferences()
147 .setTransactionWindowSize(e.getComponent().getSize());
148 }
149
150 } );
151
152 pack();
153 setSize(DataManager.getInstance()
154 .getCurrentUser()
155 .getUserPreferences()
156 .getTransactionWindowSize());
157 setLocationRelativeTo(parent);
158 setVisible(true);
159 transactionPanel.setFocus();
160 }
161
162 private void initializeActions()
163 {
164 extraActionsButton.addActionListener(new ActionListener()
165 {
166 public void actionPerformed(ActionEvent e)
167 {
168 JPopupMenu menu = transactionPanel.getExtraActionsMenu();
169 menu.show(extraActionsButton, 0, extraActionsButton.getHeight());
170 }
171 });
172
173 transactionPanel.addListener(new TransactionEditor.TransferChangeListener()
174 {
175 public void numberTransfersChanged()
176 {
177 Dimension prev_size = transactionPanel.getSize();
178 Dimension frame_size = getSize();
179 int extra_height = frame_size.height - prev_size.height;
180
181 Dimension pref_size = transactionPanel.getPreferredSize();
182 setSize(new Dimension(frame_size.width,
183 pref_size.height + extra_height));
184 }
185 });
186 }
187
188 public Transaction getNewTransaction()
189 {
190 try
191 {
192 Transaction tr = transactionPanel.getTransaction();
193 return tr;
194 }
195 catch (ParseException ex)
196 {
197 JOptionPane.showMessageDialog(null,
198 "Invalid amount string.",
199 "Error",
200 JOptionPane.ERROR_MESSAGE);
201 }
202 catch (InvalidSrcDestEx ex)
203 {
204 JOptionPane.showMessageDialog(null,
205 "Source and destination cannot be identical.",
206 "Error",
207 JOptionPane.ERROR_MESSAGE);
208 }
209 catch (EmptyTransactionEx ex)
210 {
211 JOptionPane.showMessageDialog(null,
212 "Empty transaction.",
213 "Error",
214 JOptionPane.ERROR_MESSAGE);
215 }
216
217 return null;
218 }
219
220 public boolean okAction()
221 {
222 try
223 {
224 Transaction transaction = transactionPanel.getTransaction();
225 if (transaction != null)
226 {
227 if (allocationMode)
228 DataManager.getInstance().addAllocation(transaction);
229 else
230 DataManager.getInstance().addTransaction(transaction);
231 return true;
232 }
233 }
234 catch (ParseException ex)
235 {
236 JOptionPane.showMessageDialog(null,
237 "Invalid amount string.",
238 "Error",
239 JOptionPane.ERROR_MESSAGE);
240 }
241 catch (InvalidSrcDestEx ex)
242 {
243 JOptionPane.showMessageDialog(null,
244 "Source and destination cannot be identical.",
245 "Error",
246 JOptionPane.ERROR_MESSAGE);
247 }
248 catch (EmptyTransactionEx ex)
249 {
250 JOptionPane.showMessageDialog(null,
251 "Empty transaction.",
252 "Error",
253 JOptionPane.ERROR_MESSAGE);
254 }
255
256 return false;
257 }
258 }