1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.client.map.ui;
21
22 import java.awt.event.ActionListener;
23 import java.io.IOException;
24
25 import javax.swing.BorderFactory;
26 import javax.swing.JButton;
27 import javax.swing.JComponent;
28 import javax.swing.JLabel;
29 import javax.swing.JPanel;
30 import javax.swing.JTextField;
31
32 import triptracker.client.net.MapSocketAdapter;
33 import triptracker.client.net.MapSocket;
34 import triptracker.client.ui.Form;
35 import se.datadosen.component.RiverLayout;
36 import triptracker.core.ConnectionState;
37
38 public class LoginForm extends MapSocketAdapter implements Form {
39 protected static int nameCount = 1;
40 protected final String name;
41 private final MapClientGui view;
42
43
44 private final JPanel panel;
45 private final JTextField userField, passField;
46 private final JButton loginButton;
47 private final JLabel messageField;
48
49 /***
50 * The constructor creates a JPanel for the login information, and registers
51 * as a form on the <code>view</code>.
52 *
53 * @param view parent JComponent form manager
54 * @param model map model
55 */
56 public LoginForm(MapClientGui view, MapSocket model) {
57 this(view, LoginForm.class.getClass().getName() + "-" + nameCount++,
58 model);
59 }
60
61 /***
62 * The constructor creates a JPanel for the login information, and registers
63 * as a form on the <code>manager</code>. The form name should be unique for
64 * the given form manager.
65 *
66 * @param view JComponent form manager
67 * @param name unique form name
68 */
69 public LoginForm(MapClientGui view, String name, MapSocket model) {
70 this.name = name;
71
72 this.view = view;
73
74 userField = new JTextField("trip", 10);
75 passField = new JTextField("track", 10);
76 messageField = new JLabel("Please type username and password");
77 loginButton = new JButton("Log in");
78
79 panel = new JPanel();
80 panel.setBorder(BorderFactory.createEtchedBorder());
81 panel.setLayout(new RiverLayout());
82
83 panel.add("center", new JLabel("Username "));
84 panel.add("center", userField);
85 panel.add("br center", new JLabel("Password "));
86 panel.add("center", passField);
87
88
89 panel.add("p center", loginButton);
90 panel.add("br center", messageField);
91
92 JPanel panel1 = new JPanel();
93 panel1.add(panel);
94
95 view.register(this);
96 new LoginController(model, this);
97 model.addListener(this);
98 }
99
100 /***
101 * Register a listener on the login button.
102 *
103 * @param listener
104 */
105 public void addLoginBtnListener(ActionListener listener) {
106 loginButton.addActionListener(listener);
107 }
108
109 public JComponent getFormComponent() {
110 return panel;
111 }
112
113 public String getName() {
114 return name;
115 }
116
117 public String getUsername() {
118 return userField.getText();
119 }
120
121 public String getPassword() {
122 return passField.getText();
123 }
124
125 public void setMessage(String msg) {
126 messageField.setText(msg);
127 }
128
129 /***
130 * {@inheritDoc}
131 */
132 @Override
133 public void socketError(IOException e) {
134 setMessage("Error with socket: " + e.getLocalizedMessage());
135 }
136
137 /***
138 * {@inheritDoc}
139 */
140 @Override
141 public void connectionUpdate(ConnectionState state) {
142 switch (state) {
143 case CONNECTED:
144 setMessage("Connected to server");
145 break;
146 case AUTH_SUCCESS:
147 setMessage("Login verified");
148 view.showForm(view.getTabbedForm());
149 break;
150 case AUTH_FAILED:
151 setMessage("Login failed");
152 break;
153 case DISCONNECTED:
154 setMessage("Disconnected from server");
155 break;
156 case SUBSCRIBED:
157 break;
158 case UNSUBSCRIBED:
159 break;
160 }
161 }
162
163 public void refreshView() {
164
165 }
166 }