1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.client.gps.ui;
21
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.io.IOException;
25
26 import triptracker.client.gps.core.GPSClientModel;
27 import triptracker.client.ui.SwingWorker;
28
29 public class LoginController implements ActionListener {
30 private final GPSClientModel model;
31
32 private final LoginForm view;
33
34 private SwingWorker worker;
35
36 public LoginController(GPSClientModel model, LoginForm view) {
37 this.model = model;
38 this.view = view;
39
40
41 view.addBtnListener(this);
42
43 }
44
45 public void actionPerformed(ActionEvent event) {
46
47
48 if (event.getSource() == view.loginButton) {
49 view.setEnableControls(false);
50
51
52 final String username = view.getUsername();
53 final String password = view.getPassword();
54 final String hostname = view.getHostname();
55
56 view.setStatusMsg("Trying to log on to " + hostname + "...");
57
58 worker = new ConnectionWorker(username, password);
59 worker.start();
60
61
62
63
64
65 } else if (event.getSource() == view.cancelButton) {
66 if (worker != null && worker.isAlive()) {
67 try {
68 model.disconnectFromSocket();
69 } catch (IOException e) {
70 e.printStackTrace();
71 }
72 }
73 view.showMain();
74 }
75 }
76
77 private class ConnectionWorker extends SwingWorker {
78 private IOException ioe = null;
79 private final String username;
80 private final String password;
81
82 public ConnectionWorker(String username, String password) {
83 this.username = username;
84 this.password = password;
85 }
86
87 @Override
88 public Object construct() {
89 try {
90 model.logon(username, password);
91 } catch (IOException ioe) {
92 this.ioe = ioe;
93 return false;
94 }
95 return true;
96 }
97
98 @Override
99 public void finished() {
100 if (ioe == null) {
101 view.setStatusMsg("Login successful");
102 view.showMain();
103 } else {
104 view.setStatusMsg("Login failed: " + ioe.getMessage());
105 }
106 view.setEnableControls(true);
107 }
108 }
109 }