View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.wizard;
21  
22  import javax.swing.AbstractAction;
23  import javax.swing.Action;
24  import javax.swing.Icon;
25  import javax.swing.JTextField;
26  
27  import java.awt.GridBagLayout;
28  import java.awt.event.ActionEvent;
29  
30  import org.xnap.XNap;
31  import org.xnap.gui.AbstractSettingsPanel;
32  import org.xnap.gui.Messages;
33  import org.xnap.gui.component.MultiLineLabel;
34  import org.xnap.gui.component.ValidatedTextField;
35  import org.xnap.gui.component.XNapButton;
36  import org.xnap.gui.util.GridBagHelper;
37  import org.xnap.gui.util.IconHelper;
38  import org.xnap.util.StringHelper;
39  
40  public class GeneralWizardPanel extends AbstractSettingsPanel {
41      
42      //--- Data field(s) ---
43  
44      private JTextField jteUsername;
45      private JTextField jtePassword;
46      private JTextField jteEmail;
47  
48      //--- Constructor(s) ---
49  
50      public GeneralWizardPanel()
51      {
52  		setLayout(new GridBagLayout());
53  
54  		boolean firstTime = ("-1".equals(XNap.lastLaunchVersion));
55  
56  		GridBagHelper.add
57  			(this, new MultiLineLabel(Messages.LOGIN_NICK));
58  
59          GridBagHelper.addLabel(this, XNap.tr("Username"));
60          jteUsername = new ValidatedTextField
61  			((firstTime) ? "" : prefs.getUsername(), 20, StringHelper.EMAIL);
62          GridBagHelper.add(this, jteUsername);
63  	
64          GridBagHelper.addLabel(this, XNap.tr("Password"));
65          jtePassword = new ValidatedTextField
66  			((firstTime) ? "" : prefs.getPassword(), 20, StringHelper.EMAIL);
67          GridBagHelper.add(this, jtePassword);
68  
69          GridBagHelper.addLabel(this, XNap.tr("Email"));
70          jteEmail = new ValidatedTextField
71  			(prefs.getEmail(), 20, StringHelper.EMAIL);
72          GridBagHelper.add(this, jteEmail);
73  
74  		GridBagHelper.add
75  			(this, new XNapButton(new GenerateRandomAction()), false);
76  
77  		GridBagHelper.addVerticalSpacer(this);
78      }
79  
80      public void apply() 
81      {
82  		if (jteUsername.getText().length() > 0) {
83  			prefs.setUsername(jteUsername.getText());
84  		}
85  		if (jtePassword.getText().length() > 0) {
86  			prefs.setPassword(jtePassword.getText());
87  		}
88  		prefs.setEmail(jteEmail.getText());
89      }
90  
91      public String getDescription()
92      {
93  		return XNap.tr("Setup your personal settings.");
94      }
95  
96      public Icon getIcon()
97      {
98  		return IconHelper.getWizardIcon("gohome.png");
99      }
100 
101     public String getTitle()
102     {
103 		return XNap.tr("General Settings");
104     }
105 
106     private class GenerateRandomAction extends AbstractAction {
107 
108         public GenerateRandomAction() 
109 		{
110             putValue(Action.NAME, XNap.tr("Generate Random"));
111             putValue(Action.SHORT_DESCRIPTION,
112 					 XNap.tr("Generates a random username and password."));
113         }
114 
115         public void actionPerformed(ActionEvent event) 
116 		{
117 			jteUsername.setText(StringHelper.randomString(8));
118 			jtePassword.setText(StringHelper.randomString(8));
119         }
120 
121     }
122 	
123 }