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