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    /*
024     * InitializationWisardDialog.java
025     *
026     * Created on July 7, 2007, 10:31 AM
027     */
028    
029    package org.jbudget.gui.user;
030    
031    import java.awt.BorderLayout;
032    import java.awt.Color;
033    import java.awt.Dimension;
034    import java.awt.event.ActionEvent;
035    import java.awt.event.ActionListener;
036    import javax.swing.JButton;
037    import javax.swing.JDialog;
038    import javax.swing.JFrame;
039    import javax.swing.JPanel;
040    import javax.swing.SpringLayout;
041    import javax.swing.event.CaretEvent;
042    import javax.swing.event.CaretListener;
043    import org.jbudget.Core.Account;
044    import org.jbudget.Core.User;
045    import org.jbudget.gui.templ.InitializeWisardPage1;
046    import org.jbudget.gui.templ.InitializeWisardPage2;
047    import org.jbudget.io.DataManager;
048    import org.jbudget.util.Pair;
049    
050    /**
051     * A class that runs an initialization wisard if user selects an empty directory
052     * to be used for storing data. The wisard asks the user to set up the 'system' 
053     * password and to create user accounts.
054     *
055     * @author petrov
056     */
057    public class InitializationWisardDialog extends JDialog
058    {
059        private final InitializeWisardPage1 wisardPage1 = new InitializeWisardPage1();
060        private final InitializeWisardPage2 wisardPage2 = new InitializeWisardPage2();
061        
062        private final JButton nextButton = new JButton("Next");
063        private final JButton addUserButton = new JButton("Add User");
064        private final JButton finishButton = new JButton("Finish");
065        
066        /** Creates a new instance of InitializationWisardDialog */
067        public InitializationWisardDialog(JFrame parent)
068        {
069            arrangeGui();
070            initializeActions();
071            pack();
072            if (parent != null)
073            {
074              setLocation(parent.getX() + parent.getWidth()/2 - getWidth()/2,
075                  parent.getY() + parent.getHeight()/2 - getHeight()/2);
076            }
077            else
078              setLocationRelativeTo(null);
079            setModal(true);
080            setVisible(true);
081        }
082        
083        /** Arranges gui components */
084        private void arrangeGui()
085        {
086            JPanel innerPanel = new JPanel();
087            innerPanel.setLayout(new BorderLayout());
088            innerPanel.add(wisardPage1, BorderLayout.CENTER);
089            JPanel controlPanel = new JPanel();
090            controlPanel.setPreferredSize(new Dimension(0,40));
091            controlPanel.setMinimumSize(new Dimension(100, 30));
092            innerPanel.add(controlPanel, BorderLayout.SOUTH);
093            SpringLayout layout = new SpringLayout();
094    
095            layout.putConstraint(SpringLayout.EAST, nextButton, -10, 
096                                 SpringLayout.EAST, controlPanel);
097            layout.putConstraint(SpringLayout.NORTH, nextButton, 5, 
098                                 SpringLayout.NORTH, controlPanel);
099            controlPanel.setLayout(layout);
100            controlPanel.add(nextButton);
101            
102            wisardPage1.passwordsMatchLabel.setText("");
103    
104            setContentPane(innerPanel);
105        }
106        
107        
108        /** Switch to the second page */
109        private void goToSecondPage()
110        {
111            JPanel innerPanel = new JPanel();
112            innerPanel.setLayout(new BorderLayout());
113            innerPanel.add(wisardPage2, BorderLayout.CENTER);
114            JPanel controlPanel = new JPanel();
115            controlPanel.setPreferredSize(new Dimension(0,40));
116            controlPanel.setMinimumSize(new Dimension(100, 30));
117            innerPanel.add(controlPanel, BorderLayout.SOUTH);
118            SpringLayout layout = new SpringLayout();
119    
120            layout.putConstraint(SpringLayout.EAST, finishButton, -10, 
121                                 SpringLayout.EAST, controlPanel);
122            layout.putConstraint(SpringLayout.NORTH, finishButton, 5, 
123                                 SpringLayout.NORTH, controlPanel);
124            
125            layout.putConstraint(SpringLayout.EAST, addUserButton, -5, 
126                                 SpringLayout.WEST, finishButton);
127            layout.putConstraint(SpringLayout.NORTH, addUserButton, 5, 
128                                 SpringLayout.NORTH, controlPanel);
129            
130            controlPanel.setLayout(layout);
131            controlPanel.add(finishButton);
132            controlPanel.add(addUserButton);
133            
134            wisardPage2.passwordsMatchLabel.setText("");
135    
136            setContentPane(innerPanel);
137            pack();
138        }
139        
140        /** Initialize actions. */
141        private void initializeActions()
142        {
143            CaretListener caretListener1 = new CaretListener(){
144                public void caretUpdate(CaretEvent e) {
145                    char[] password1 = wisardPage1.passwordField1.getPassword();
146                    char[] password2 = wisardPage1.passwordField2.getPassword();
147                    boolean match = password1.length == password2.length;
148                    
149                    for (int i = 0; i < password1.length && match; i++)
150                        match = match && password1[i] == password2[i];
151                    
152                    if (match)
153                    {
154                        wisardPage1.passwordsMatchLabel.setForeground(Color.GREEN);
155                        wisardPage1.passwordsMatchLabel.setText("Passwords Match");
156                    }
157                    else
158                    {
159                        wisardPage1.passwordsMatchLabel.setForeground(Color.RED);
160                        wisardPage1.passwordsMatchLabel.setText("Passwords Do Not Match");
161                    }
162                }
163            };
164            
165            wisardPage1.passwordField1.addCaretListener(caretListener1);
166            wisardPage1.passwordField2.addCaretListener(caretListener1);
167            
168            CaretListener caretListener2 = new CaretListener(){
169                public void caretUpdate(CaretEvent e) {
170                    char[] password1 = wisardPage2.passwordField1.getPassword();
171                    char[] password2 = wisardPage2.passwordField2.getPassword();
172                    boolean match = password1.length == password2.length;
173                    
174                    for (int i = 0; i < password1.length && match; i++)
175                        match = match && password1[i] == password2[i];
176                    
177                    if (match)
178                    {
179                        wisardPage2.passwordsMatchLabel.setForeground(Color.GREEN);
180                        wisardPage2.passwordsMatchLabel.setText("Passwords Match");
181                    }
182                    else
183                    {
184                        wisardPage2.passwordsMatchLabel.setForeground(Color.RED);
185                        wisardPage2.passwordsMatchLabel.setText("Passwords Do Not Match");
186                    }
187                }
188            };
189            
190            wisardPage2.passwordField1.addCaretListener(caretListener2);
191            wisardPage2.passwordField2.addCaretListener(caretListener2);
192            
193            
194            CaretListener caretListener3 = new CaretListener(){
195                public void caretUpdate(CaretEvent e) {
196                    String userName = wisardPage2.userNameField.getText();
197                    DataManager dataManager = DataManager.getInstance();
198                    
199                    boolean userExists = false;
200                    for (Pair<Integer,String> 
201                            user : dataManager.getValidUsers(true, false))
202                    {
203                      if (user.second().equals(userName))
204                      {
205                        wisardPage2.passwordsMatchLabel.setForeground(Color.RED);
206                        wisardPage2.passwordsMatchLabel.setText("User Exists");
207                        return;
208                      }
209                    }
210                    
211                    wisardPage2.passwordsMatchLabel.setText("");
212                }
213            };
214            
215            wisardPage2.userNameField.addCaretListener(caretListener3);
216            
217            nextButton.addActionListener(new ActionListener(){
218                public void actionPerformed(ActionEvent e){
219                    
220                    char[] password1 = wisardPage1.passwordField1.getPassword();
221                    char[] password2 = wisardPage1.passwordField2.getPassword();
222                    boolean match = password1.length == password2.length;
223                    
224                    for (int i = 0; i < password1.length && match; i++)
225                        match = match && password1[i] == password2[i];
226                    
227                    if (match)
228                    {
229                        User.SYSTEM.setPassoword(new String(password1));
230                        goToSecondPage();
231                    }
232                    else
233                    {
234                        wisardPage1.passwordsMatchLabel.setForeground(Color.RED);
235                        wisardPage1.passwordsMatchLabel.setText("Passwords Do Not Match");
236                    }
237                }
238            });
239            
240            addUserButton.addActionListener(new ActionListener(){
241                public void actionPerformed(ActionEvent e){
242                    
243                    String userName = wisardPage2.userNameField.getText();
244                    DataManager dataManager = DataManager.getInstance();
245                    
246                    for (Pair<Integer,String> 
247                            user : dataManager.getValidUsers(true, true))
248                    {
249                        if(user.second().equals(userName))
250                        {
251                          wisardPage2.passwordsMatchLabel.setForeground(Color.RED);
252                          wisardPage2.passwordsMatchLabel.setText("User Exists");
253                          return;
254                        }
255                    }
256                    
257                    char[] password1 = wisardPage2.passwordField1.getPassword();
258                    char[] password2 = wisardPage2.passwordField2.getPassword();
259                    boolean match = password1.length == password2.length;
260                    
261                    for (int i = 0; i < password1.length && match; i++)
262                        match = match && password1[i] == password2[i];
263                    
264                    if (match)
265                    {
266                        User newUser = new User(userName, new String(password1));
267    
268                        Account cashAccount = new Account("Cash");
269                        cashAccount.addAccountHolder(newUser.getUID());
270                        cashAccount.hide();
271                        dataManager.addAccount(cashAccount);
272    
273                        newUser.setCashAccountID(cashAccount.getID());
274    
275                        dataManager.addUser(newUser);
276                        
277                        wisardPage2.userNameField.setText("");
278                        wisardPage2.passwordField1.setText("");
279                        wisardPage2.passwordField2.setText("");
280                    }
281                    else
282                    {
283                        wisardPage1.passwordsMatchLabel.setForeground(Color.RED);
284                        wisardPage1.passwordsMatchLabel.setText("Passwords Do Not Match");
285                    }
286                    
287                }
288            });
289            
290            finishButton.addActionListener(new ActionListener(){
291                public void actionPerformed(ActionEvent e){
292                    setVisible(false);
293                    dispose();
294                }
295            });
296        }
297         
298    }