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     * LoginDialog.java
024     *
025     * Created on August 6, 2006, 7:48 PM
026     */
027    
028    package org.jbudget.gui.user;
029    
030    import java.awt.BorderLayout;
031    import java.awt.Dimension;
032    import java.awt.Font;
033    import java.awt.event.ActionEvent;
034    import java.awt.event.ActionListener;
035    import javax.swing.JFrame;
036    import javax.swing.JOptionPane;
037    import org.jbudget.gui.BasicDialog;
038    import org.jbudget.gui.GuiPreferences;
039    import org.jbudget.gui.templ.NewUserPanel;
040    
041    /**
042     *
043     * @author petrov
044     */
045    public class NewUserDialog extends BasicDialog
046    {
047      private final NewUserPanel newUserPanel = new NewUserPanel();
048      private final JFrame parent;
049      
050      private boolean selectionMade = false;
051      
052      
053      
054      
055      /**
056       * Creates a new instance of LoginDialog
057       */
058      public NewUserDialog(JFrame parent, String message, boolean isModal)
059      {
060        super(parent, isModal, false);
061        
062        this.parent = parent;
063        
064        setTitle("New User");
065        
066        if (message != null && message.length() > 0)
067        {
068          Font font = getFont();
069          Font messageFont = 
070              font.deriveFont(Font.BOLD, (float)(font.getSize() + 2));
071          newUserPanel.messageLabel.setFont(messageFont);
072          newUserPanel.messageLabel.setText(message);
073        }
074        else
075          newUserPanel.messagePanel.setPreferredSize(new Dimension(200, 0));
076        
077        inputPanel.setLayout(new BorderLayout());
078        inputPanel.add(newUserPanel, BorderLayout.CENTER);
079        
080        
081        //if enter is pressed in the name field transfer the focus to the
082        //first password field.
083        newUserPanel.nameField.addActionListener(new ActionListener(){
084          public void actionPerformed(ActionEvent ev){
085            if (newUserPanel.nameField.getText().length() > 0)
086              newUserPanel.nameField.transferFocus();
087          }
088        });
089        
090        //if enter is pressed in the first passwd field transfer the focus to the
091        //second password field.
092        newUserPanel.passwdField.addActionListener(new ActionListener(){
093          public void actionPerformed(ActionEvent ev){
094              newUserPanel.passwdField.transferFocus();
095          }
096        });
097        
098        //if enter is pressed in the second password field click OK programmatically
099        newUserPanel.passwdField1.addActionListener(new ActionListener(){
100          public void actionPerformed(ActionEvent ev){
101            okButton.doClick();
102          }
103        });
104        
105        pack();
106        
107        if (parent != null)
108        {
109          setLocation(parent.getX() + parent.getWidth()/2 - getWidth()/2,
110              parent.getY() + parent.getHeight()/2 - getHeight()/2);
111        }
112        
113        setVisible(true);
114      }
115      
116      public boolean okAction()
117      {
118        String userName = newUserPanel.nameField.getText();
119        if (userName.length() == 0)
120          return false;
121        
122        char[] passwd1 = newUserPanel.passwdField.getPassword();
123        char[] passwd2 = newUserPanel.passwdField1.getPassword();
124        
125        boolean flag = true;
126        if (passwd1.length != passwd2.length)
127          flag = false;
128        
129        if (flag)
130          for (int i = 0; i < passwd1.length; i++)
131            if (passwd1[i] != passwd2[i]){
132              flag = false;
133              break;
134            }
135        
136        if (!flag)
137        {
138          JOptionPane.showMessageDialog(
139              parent, "Passwords do not match.", "Error", 
140                              JOptionPane.WARNING_MESSAGE);
141          newUserPanel.passwdField.setText("");
142          newUserPanel.passwdField1.setText("");
143        }
144        
145        selectionMade = flag;
146        return flag;
147      }
148      
149      public boolean wasSelectionMade(){return selectionMade;}
150      
151      public String getUserName(){
152        String name = newUserPanel.nameField.getText();
153        while (name.startsWith(" "))
154          name = name.substring(1);
155        while (name.endsWith(" "))
156          name = name.substring(0, name.length()-1);
157        return name;
158      }
159      
160      public String getPasswd(){
161        String passwd = new String("");
162        try {
163          passwd = new String(newUserPanel.passwdField.getPassword());
164        } catch (NullPointerException ex){}
165        return passwd;
166      }
167      
168    }