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 * AutoAllocationEditor.java
024 *
025 * Created on April 4, 2006, 1:46 PM
026 */
027 package org.jbudget.gui.trans;
028
029 import java.awt.BorderLayout;
030 import java.awt.event.ActionEvent;
031 import java.awt.event.ActionListener;
032 import java.awt.event.ItemEvent;
033 import java.awt.event.ItemListener;
034 import java.text.ParseException;
035 import org.jbudget.Core.AutomaticTransaction;
036 import org.jbudget.Core.Budget;
037 import org.jbudget.Core.ExpenseAllocation;
038 import org.jbudget.Core.ExpenseCategory;
039 import org.jbudget.Core.MoneyPit;
040 import org.jbudget.gui.BasicDialog;
041 import org.jbudget.gui.budget.BudgetItemPane;
042 import org.jbudget.gui.trans.models.ExpensesModel;
043 import org.jbudget.gui.trans.models.ModelBase;
044 import org.jbudget.io.DataManager;
045 import org.jbudget.util.CurrencyFormater;
046
047 /**
048 * A class that brings up a gui form for editing
049 * allocations. It is used to create new allocations and edit
050 * existing allocations. When an existing allocation is changed a new instace
051 * of the ExpenseAllocation is created and added to the budget, the old
052 * allocation is removed.
053 *
054 * @author petrov
055 */
056 public class AutoAllocationEditor extends BasicDialog
057 {
058
059 //================================ DATA =================================//
060
061 final private ExpenseAllocation initialAllocation;
062 final private ExpenseCategory expenseCategory;
063 private ExpenseAllocation newAllocation = null;
064 final private Budget budget;
065 final private BudgetItemPane itemPane = new BudgetItemPane();
066
067 //================================ Methods ===============================//
068
069 /**
070 * Creates a new instance of AutoAllocationEditor
071 */
072 public AutoAllocationEditor(java.awt.Frame parent, Budget budget)
073 {
074 super(parent, false);
075 this.budget = budget;
076 this.initialAllocation = null;
077 this.expenseCategory = null;
078
079 initialize(parent);
080 }
081
082 /**
083 * Creates a new instance of AutoAllocationEditor
084 */
085 public AutoAllocationEditor(java.awt.Frame parent, Budget budget,
086 ExpenseAllocation allocation)
087 {
088 super(parent, false);
089 this.budget = budget;
090 this.initialAllocation = allocation;
091 this.expenseCategory = null;
092
093 initialize(parent);
094 }
095
096 /**
097 * Creates a new instance of AutoAllocationEditor
098 */
099 public AutoAllocationEditor(java.awt.Frame parent, Budget budget,
100 ExpenseAllocation allocation, ExpenseCategory expenseCategory)
101 {
102 super(parent, false);
103 this.budget = budget;
104 this.initialAllocation = allocation;
105 this.expenseCategory = expenseCategory;
106
107 initialize(parent);
108 }
109
110 /** initializes the gui */
111 private void initialize(java.awt.Frame parent)
112 {
113 setTitle("Auto Allocation");
114
115 inputPanel.add(itemPane, BorderLayout.CENTER);
116 itemPane.mainLabel.setText("Expense allocation");
117 itemPane.itemLabel.setText("Allocation of ");
118
119 itemPane.sourceComboBox.setModel(new SourcesModel());
120 itemPane.sourceComboBox.setSelectedIndex(0);
121 itemPane.sourceComboBox.setEnabled(false);
122
123 itemPane.destComboBox.setModel(new ExpensesModel(
124 budget, DataManager.getInstance().getCurrentUser()));
125 if (expenseCategory != null)
126 itemPane.destComboBox.setSelectedItem(new MoneyPit(expenseCategory, budget));
127 else
128 itemPane.destComboBox.setSelectedIndex(0);
129
130 // initializing actions.
131
132 //Selecting the type of allocation changes the labels on the
133 // Radio Buttons.
134 itemPane.typeComboBox.addActionListener(new ActionListener()
135 {
136
137 public void actionPerformed(ActionEvent e)
138 {
139 int index = itemPane.typeComboBox.getSelectedIndex();
140 switch (index)
141 {
142 //Daily
143 case 0:
144 itemPane.firstDayRB.setEnabled(false);
145 itemPane.lastDayRB.setEnabled(false);
146 itemPane.otherRB.setEnabled(false);
147 itemPane.timeText.setEnabled(false);
148 break;
149
150 //Weekly
151 case 1:
152 itemPane.firstDayRB.setEnabled(true);
153 itemPane.lastDayRB.setEnabled(true);
154 itemPane.otherRB.setEnabled(true);
155 itemPane.timeText.setEnabled(itemPane.otherRB.isSelected());
156 itemPane.firstDayRB.setText(
157 "Applied on the first day of the week.");
158 itemPane.lastDayRB.setText(
159 "Applied on the last day of the week.");
160 break;
161
162 //Monthly
163 case 2:
164 itemPane.firstDayRB.setEnabled(true);
165 itemPane.lastDayRB.setEnabled(true);
166 itemPane.otherRB.setEnabled(true);
167 itemPane.timeText.setEnabled(itemPane.otherRB.isSelected());
168 itemPane.firstDayRB.setText(
169 "Applied on the first day of the month.");
170 itemPane.lastDayRB.setText(
171 "Applied on the last day of the month.");
172 break;
173
174 //Yearly
175 case 3:
176 itemPane.firstDayRB.setEnabled(true);
177 itemPane.lastDayRB.setEnabled(true);
178 itemPane.otherRB.setEnabled(true);
179 itemPane.timeText.setEnabled(itemPane.otherRB.isSelected());
180 itemPane.firstDayRB.setText(
181 "Applied on the first day of the year.");
182 itemPane.lastDayRB.setText(
183 "Applied on the last day of the year.");
184 }
185 }
186 });
187
188 //if otherRB is selected the timeText is activated.
189 itemPane.otherRB.addItemListener(new ItemListener()
190 {
191
192 public void itemStateChanged(ItemEvent e)
193 {
194 itemPane.timeText.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
195 }
196 });
197
198 //Initializing from the initialAllocation.
199 if (initialAllocation != null)
200 {
201 int time = initialAllocation.getTime();
202 switch (time)
203 {
204 case 1:
205 itemPane.firstDayRB.doClick();
206 break;
207 case -1:
208 itemPane.lastDayRB.doClick();
209 break;
210 default:
211 itemPane.otherRB.doClick();
212 itemPane.timeText.setText(Integer.toString(time));
213 }
214
215 itemPane.amountText.setText(
216 CurrencyFormater.format(initialAllocation.getAmount()));
217 ExpenseAllocation.Type type = initialAllocation.getType();
218 if (type == AutomaticTransaction.Type.DAILY)
219 itemPane.typeComboBox.setSelectedIndex(0);
220 else if (type == AutomaticTransaction.Type.WEEKLY)
221 itemPane.typeComboBox.setSelectedIndex(1);
222 else if (type == AutomaticTransaction.Type.MONTHLY)
223 itemPane.typeComboBox.setSelectedIndex(2);
224 else if (type == AutomaticTransaction.Type.YEARLY)
225 itemPane.typeComboBox.setSelectedIndex(3);
226
227 MoneyPit destination = initialAllocation.getDestination();
228
229 assert destination.getType() == MoneyPit.Type.EXPENSE;
230 itemPane.destComboBox.setSelectedItem(destination.getContent());
231 }
232
233 // Displaying
234 pack();
235
236 if (parent != null)
237 setLocation(parent.getX() + parent.getWidth() / 2 - getWidth() / 2,
238 parent.getY() + parent.getHeight() / 2 - getHeight() / 2);
239
240 setVisible(true);
241 }
242
243 protected boolean okAction()
244 {
245 int index = 4 - itemPane.typeComboBox.getSelectedIndex();
246 AutomaticTransaction.Type type = AutomaticTransaction.Type.getByID(index);
247 if (type == AutomaticTransaction.Type.UNKNOWN)
248 return false;
249
250 long amount = 0;
251 try
252 {
253 amount = CurrencyFormater.parse(itemPane.amountText.getText());
254 }
255 catch (ParseException ex)
256 {
257 try
258 {
259 amount = (long) (Double.parseDouble(itemPane.amountText.getText()) * 100);
260 }
261 catch (NumberFormatException ex1)
262 {
263 return false;
264 }
265 }
266 if (amount == 0L)
267 return false;
268
269 int time = 1;
270 if (itemPane.lastDayRB.isSelected())
271 time = -1;
272 else if (itemPane.otherRB.isSelected())
273 try
274 {
275 time = Integer.parseInt(itemPane.timeText.getText());
276 if (time <= 0)
277 time = 1;
278 }
279 catch (NumberFormatException ex)
280 {
281 return false;
282 }
283
284 MoneyPit destination = (MoneyPit)itemPane.destComboBox.getSelectedItem();
285
286 if (destination == null || destination == MoneyPit.NONE)
287 return false;
288
289 ExpenseCategory category = destination.getExpenseCategory();
290
291 newAllocation = new ExpenseAllocation(
292 category, budget, amount, type, time);
293 return (initialAllocation == null ||
294 !(getNewAllocation().isACopy(initialAllocation)));
295 }
296
297
298 public ExpenseAllocation getNewAllocation()
299 {
300 return newAllocation;
301 }
302
303 //=========================== Inner Classes ==============================//
304
305 public static class SourcesModel extends ModelBase
306 {
307 public SourcesModel()
308 {
309 itemsList.add(noneLabel);
310 }
311 }
312 }