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