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 org.jbudget.gui.BasicDialog;
037    import org.jbudget.io.DataManager;
038    
039    /**
040     *
041     * @author petrov
042     */
043    public class LoginDialog extends BasicDialog
044    {
045    
046        private final LoginPanel loginPanel = new LoginPanel();
047        private boolean selectionMade = false;
048    
049        /**
050         * Creates a new instance of LoginDialog
051         */
052        public LoginDialog(JFrame parent, String message, boolean isModal)
053        {
054            super(parent, isModal, false);
055    
056            setTitle("Login");
057    
058            if (message != null && message.length() > 0)
059            {
060                Font font = getFont();
061                Font messageFont =
062                        font.deriveFont(Font.BOLD, (float) (font.getSize() + 2));
063                loginPanel.messageLabel.setFont(messageFont);
064                loginPanel.messageLabel.setText(message);
065            } 
066            else
067            {
068                loginPanel.messagePanel.setPreferredSize(new Dimension(200, 0));
069            }
070    
071            inputPanel.setLayout(new BorderLayout());
072            inputPanel.add(loginPanel, BorderLayout.CENTER);
073    
074            //if enter is pressed "enter" in the password field click OK programmatically
075            loginPanel.passwdField.addActionListener(new ActionListener()
076            {
077    
078                public void actionPerformed(ActionEvent ev)
079                {
080                    okButton.doClick();
081                }
082            });
083    
084            pack();
085            setLocationRelativeTo(parent);
086            setVisible(true);
087        }
088        
089        @Override
090        public void dispose() 
091        {
092            loginPanel.dispose();
093        }
094    
095        public boolean okAction()
096        {
097            selectionMade = loginPanel.getSelectedUid() >= 0;
098            return selectionMade;
099        }
100    
101        public boolean wasSelectionMade()
102        {
103            return selectionMade;
104        }
105    
106        public String getUserName()
107        {
108            if (loginPanel.getSelectedUid() < 0)
109            {
110                return "";
111            }
112            return DataManager.getInstance().getUser(loginPanel.getSelectedUid()).getName();
113        }
114    
115        public String getPasswd()
116        {
117            return loginPanel.getPasswd();
118        }
119    }