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     * IncomeSourceEditor.java
024     *
025     * Created on April 10, 2006, 5:07 PM
026     */
027    
028    package org.jbudget.gui.budget;
029    
030    import java.awt.BorderLayout;
031    import java.text.NumberFormat;
032    import java.text.ParseException;
033    import java.util.Collections;
034    import java.util.List;
035    import java.util.Vector;
036    import javax.swing.DefaultComboBoxModel;
037    import org.jbudget.Core.AutomaticTransaction;
038    import org.jbudget.Core.IncomeSource;
039    import org.jbudget.Core.User;
040    import org.jbudget.gui.BasicDialog;
041    import org.jbudget.gui.templ.IncomeSourcePane;
042    import org.jbudget.io.DataManager;
043    import org.jbudget.util.Pair;
044    
045    /** Creates a gui for editing an income source.
046     *
047     * @author petrov
048     */
049    public class IncomeSourceEditor extends BasicDialog
050    {
051      private final IncomeSource initialSource;
052      IncomeSource newIncomeSource;
053      
054      private final IncomeSourcePane pane = new IncomeSourcePane();
055      
056      /** used to display currency */
057      static NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
058          
059      /** Creates a new instance of IncomeSourceEditor */
060      public IncomeSourceEditor(java.awt.Frame parent,
061          IncomeSource theInitialSource)
062      {
063        super(parent, false);
064        initialSource = theInitialSource;
065        setTitle("Income Source Editor");
066        
067        Vector<User> users = new Vector<User>();
068        List<Pair<Integer,String>> userList = 
069          DataManager.getInstance().getValidUsers(false, false);
070        for ( Pair<Integer,String> userInfo : userList)
071          users.add(DataManager.getInstance().getUser(userInfo.first().intValue()));
072        Collections.sort(users);
073        users.add(User.UNKNOWN);
074        
075        pane.userComboBox.setModel(new DefaultComboBoxModel(users));
076        
077        inputPanel.add(pane, BorderLayout.CENTER);
078        
079        if (initialSource != null)
080        {
081          String name = initialSource.getName();
082          pane.nameText.setText(name);
083          pane.nameText.setCaretPosition(0);
084          pane.nameText.moveCaretPosition(name.length());
085          pane.amountText.setText(
086              currencyFormat.format(initialSource.getAmount()/100.0));
087          AutomaticTransaction.Type type = initialSource.getType();
088          if (type == AutomaticTransaction.Type.DAILY)
089            pane.typeComboBox.setSelectedIndex(0);
090          else if (type == AutomaticTransaction.Type.WEEKLY)
091            pane.typeComboBox.setSelectedIndex(1);
092          else if (type == AutomaticTransaction.Type.MONTHLY)
093            pane.typeComboBox.setSelectedIndex(2);
094          else if (type == AutomaticTransaction.Type.YEARLY)
095            pane.typeComboBox.setSelectedIndex(3);
096          pane.userComboBox.setSelectedItem(theInitialSource.getHolder());
097        }
098        else
099          pane.userComboBox.setSelectedItem(User.UNKNOWN);
100        
101        pack();
102        
103        if (parent != null)
104        {
105          setLocation(parent.getX() + parent.getWidth()/2 - getWidth()/2,
106              parent.getY() + parent.getHeight()/2 - getHeight()/2);
107        }
108        
109        setVisible(true);
110      }
111      
112      /** Executed automatically once the OK button is pressed */
113      public boolean okAction()
114      {
115        String name = pane.nameText.getText();
116        
117        long amount = 0;
118        try {
119          amount = (long)(
120            currencyFormat.parse(pane.amountText.getText()).doubleValue()*100);
121        }
122        catch (ParseException ex)
123        {
124          try {
125            amount = (long)(Double.parseDouble(pane.amountText.getText())*100);
126          } catch (NumberFormatException ex1) {
127            return false;
128          }
129        }
130        
131        
132        int index = 4 - pane.typeComboBox.getSelectedIndex();
133        AutomaticTransaction.Type type = AutomaticTransaction.Type.getByID(index);
134        
135        newIncomeSource = new IncomeSource(name, amount,
136                                 (User)pane.userComboBox.getSelectedItem(), type);
137        
138        return true;
139      }
140      
141    }