1   package org.wcb.gui.forms.auth;
2   
3   import org.wcb.gui.util.AuthenticationUtility;
4   import org.wcb.gui.util.UIHelper;
5   import org.jdesktop.swingx.VerticalLayout;
6   
7   import javax.swing.*;
8   import java.awt.*;
9   import java.util.logging.Logger;
10  import java.util.logging.Level;
11  
12  /**
13   * <small>
14   * <p/>
15   * Copyright (c)  2006  wbogaardt.
16   * This library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * This library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
29   * <p/>
30   * $File:  $ <br>
31   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 27, 2006 11:50:08 AM $ <br>
32   * </small>
33   *
34   * @author wbogaardt
35   *         Use this panel for normal loging that is user name and password.
36   */
37  
38  public class JLoginPanel extends JPanel {
39  
40      private static final Logger LOG = Logger.getLogger(JLoginPanel.class.getName());
41      private NameComponent namePanel;
42      private JPasswordField passwordField;
43      public enum Status {NOT_STARTED, IN_PROGRESS, FAILED, CANCELLED, SUCCEEDED}
44      private JPanel loginPanel;
45      private JPanel contentPanel;
46      private AuthenticationUtility authservice;
47      private JLabel messageLabel;
48      private JLabel errorMessageLabel;
49      private JPanel progressPanel;
50      private JLabel progressMessageLabel;
51      private Cursor oldCursor;
52      private JCheckBox rememberNameCb;
53  
54      public JLoginPanel() {
55          init();
56          LOG.log(Level.INFO, "JLogin Panel initialized");
57      }
58  
59      protected void init() {
60          authservice = new AuthenticationUtility();
61  
62          JLabel bannerLabel = new JLabel(UIHelper.getIcon("org/wcb/resources/gui/login-title.png"));
63  
64          //create the message and hyperlink and hide them
65          errorMessageLabel = new JLabel();
66          errorMessageLabel.setVerticalTextPosition(SwingConstants.TOP);
67          errorMessageLabel.setOpaque(true);
68          errorMessageLabel.setBackground(new Color(255, 215, 215));
69          errorMessageLabel.setBorder(BorderFactory.createCompoundBorder(
70                  BorderFactory.createLineBorder(errorMessageLabel.getBackground().darker()),
71                  BorderFactory.createEmptyBorder(5, 7, 5, 5)));
72          errorMessageLabel.setVisible(false);
73  
74          //create the default label
75          messageLabel = new JLabel("Enter your user name and password");
76          if(isNewUser()) {
77              messageLabel.setText("You appear to be a new user enter your new login.");
78          }
79          messageLabel.setOpaque(true);
80          messageLabel.setFont(messageLabel.getFont().deriveFont(Font.BOLD));
81          messageLabel.setBorder(BorderFactory.createEmptyBorder(12, 12, 7, 11));
82  
83          loginPanel = createLoginPanel();
84          loginPanel.setBorder(BorderFactory.createEmptyBorder(0, 36, 7, 11));
85  
86          contentPanel = new JPanel(new VerticalLayout());
87          contentPanel.add(messageLabel);
88          contentPanel.add(loginPanel);
89          errorMessageLabel.setBorder(BorderFactory.createCompoundBorder(
90                  BorderFactory.createMatteBorder(0, 36, 0, 11, contentPanel.getBackground()),
91                  errorMessageLabel.getBorder()));
92          contentPanel.add(errorMessageLabel);
93  
94          //create the progress panel
95          progressPanel = new JPanel(new GridBagLayout());
96          progressMessageLabel = new JLabel("Please Wait");
97          progressMessageLabel.setFont(progressMessageLabel.getFont().deriveFont(Font.BOLD));
98          JProgressBar pb = new JProgressBar();
99          pb.setIndeterminate(true);
100         progressPanel.add(progressMessageLabel, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 11, 11), 0, 0));
101         progressPanel.add(pb, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 24, 11, 7), 0, 0));
102 
103         setLayout(new BorderLayout());
104         add(bannerLabel, BorderLayout.NORTH);
105         add(contentPanel, BorderLayout.CENTER);
106         //initialize panel
107         if (authservice.isRememberName()) {
108             namePanel.setUserName(authservice.getUsername());
109             rememberNameCb.setSelected(true);
110         }
111     }
112 
113     private JPanel createLoginPanel() {
114         JPanel tempLoginPanel = new JPanel();
115         namePanel = new SimpleNamePanel();
116         JLabel nameLabel = new JLabel("Name");
117         nameLabel.setLabelFor(namePanel.getComponent());
118 
119         passwordField = new JPasswordField("", 15);
120         JLabel passwordLabel = new JLabel("Password");
121         passwordLabel.setLabelFor(passwordField);
122 
123         rememberNameCb = new JCheckBox("Remember Username");
124 
125         // name label
126         tempLoginPanel.setLayout(new GridBagLayout());
127         GridBagConstraints gridBagConstraints = new GridBagConstraints();
128         gridBagConstraints.gridx = 0;
129         gridBagConstraints.gridy = 0;
130         gridBagConstraints.anchor = GridBagConstraints.LINE_START;
131         gridBagConstraints.insets = new Insets(0, 0, 5, 11);
132         tempLoginPanel.add(nameLabel, gridBagConstraints);
133 
134         gridBagConstraints = new GridBagConstraints();
135         gridBagConstraints.gridx = 1;
136         gridBagConstraints.gridy = 0;
137         gridBagConstraints.gridwidth = 1;
138         gridBagConstraints.anchor = GridBagConstraints.LINE_START;
139         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
140         gridBagConstraints.weightx = 1.0;
141         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
142         tempLoginPanel.add(namePanel.getComponent(), gridBagConstraints);
143 
144         //Password label
145         gridBagConstraints = new GridBagConstraints();
146         gridBagConstraints.gridx = 0;
147         gridBagConstraints.gridy = 1;
148         gridBagConstraints.anchor = GridBagConstraints.LINE_START;
149         gridBagConstraints.insets = new Insets(0, 0, 5, 11);
150         tempLoginPanel.add(passwordLabel, gridBagConstraints);
151 
152         gridBagConstraints = new GridBagConstraints();
153         gridBagConstraints.gridx = 1;
154         gridBagConstraints.gridy = 1;
155         gridBagConstraints.gridwidth = 1;
156         gridBagConstraints.anchor = GridBagConstraints.LINE_START;
157         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
158         gridBagConstraints.weightx = 1.0;
159         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
160         tempLoginPanel.add(passwordField, gridBagConstraints);
161 
162         gridBagConstraints = new GridBagConstraints();
163         gridBagConstraints.gridx = 0;
164         gridBagConstraints.gridy = 2;
165         gridBagConstraints.anchor = GridBagConstraints.LINE_START;
166         gridBagConstraints.gridwidth = 2;
167         gridBagConstraints.insets = new Insets(0, 0, 5, 11);
168         tempLoginPanel.add(rememberNameCb, gridBagConstraints);
169 
170         return tempLoginPanel;
171     }
172 
173     public boolean startLogin() {
174         oldCursor = getCursor();
175         boolean returnValue = false;
176         try {
177             authservice.setRememberUsername(rememberNameCb.isSelected());
178             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
179             progressMessageLabel.setText("Please Wait");
180             String username = namePanel.getUserName();
181             String password = new String(passwordField.getPassword());
182             if(isNewUser()) {
183                 //new user
184                 authservice.createNewUser(username, password);
185                 returnValue = true;
186             } else if(!authservice.authenticateUser(username, password)) {
187                 //user failed authentication.
188                 remove(progressPanel);
189                 add(contentPanel, BorderLayout.CENTER);
190                 errorMessageLabel.setText("Invalid Login");
191                 errorMessageLabel.setVisible(true);
192                 revalidate();
193                 repaint();
194                 returnValue = false;
195             } else {
196                 //user authenticated
197                 returnValue = true;
198             }
199         } catch(Exception ex) {
200             LOG.log(Level.WARNING, "Authentication exception while logging in", ex);
201             returnValue = false;
202         } finally {
203             setCursor(oldCursor);
204         }
205         return returnValue;
206     }
207 
208     private boolean isNewUser(){
209         return authservice.getUsername().equalsIgnoreCase("") && authservice.getPassword().equalsIgnoreCase("");
210     }
211 
212     public boolean updateUser(String newPassword) {
213         String password = new String(passwordField.getPassword());
214         boolean returnVal = authservice.updatePassword(password, newPassword);
215         if (!returnVal) {
216             //user failed update authentication.
217             remove(progressPanel);
218             add(contentPanel, BorderLayout.CENTER);
219             errorMessageLabel.setText("Invalid old password");
220             errorMessageLabel.setVisible(true);
221             revalidate();
222             repaint();
223         }
224         return returnVal;
225     }
226 
227     //--------------------------------- Default NamePanel Implementations
228     public static interface NameComponent {
229         public String getUserName();
230         public void setUserName(String userName);
231         public JComponent getComponent();
232     }
233 
234     /**
235      * If a UserNameStore is not used, then this text field is presented allowing the user
236      * to simply enter their user name
237      */
238     public static final class SimpleNamePanel extends JTextField implements NameComponent {
239         public SimpleNamePanel() {
240             super("", 15);
241         }
242         public String getUserName() {
243             return getText();
244         }
245         public void setUserName(String userName) {
246             setText(userName);
247         }
248         public JComponent getComponent() {
249             return this;
250         }
251     }
252 
253 }