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 import triptracker.client.gps.core.GPSClientModel;
26
27 /***
28 * Map client controller for Model View Controller (MVC) separation.
29 */
30 public class GPSController implements ActionListener {
31 private final GPSClientModel model;
32 private final GPSGui view;
33
34 public GPSController(GPSClientModel model, GPSGui view) {
35 this.model = model;
36 this.view = view;
37
38
39 chooseDefaultPort();
40
41 if (view.getCurrentForm() == view.getLoginForm())
42 view.enableMenu(false);
43
44 view.addMenuListener(this);
45 }
46
47 /***
48 * Handles actions from the view.
49 */
50 public void actionPerformed(ActionEvent event) {
51 Object src = event.getSource();
52
53 if (src == view.logOnItem) {
54 view.showLoginForm();
55 } else if (src == view.logOffItem) {
56 if (view.showLogOffDialog()) {
57 try {
58 model.disconnectFromSocket();
59 } catch (IOException e) {
60 view.setStatusMsg("!!: Could not disconnect from socket");
61 }
62 view.enableRouteItem(false);
63 view.enableLogOnItem(true);
64 view.enableLogOffItem(false);
65 }
66 } else if (src == view.generalItem) {
67 view.showSettingsForm();
68 } else if (src == view.exitItem) {
69 System.exit(0);
70 } else if (src == view.showRoutesItem) {
71 model.getRoutes(model.getUsername());
72 view.showRoutesForm();
73 }
74 }
75
76 /***
77 * Set a default port, should be called in the constructor of
78 * the controller.
79 */
80 public void chooseDefaultPort() {
81 if (model.getPort() == null && model.getComPorts().iterator().hasNext())
82 model.setPort(model.getComPorts().iterator().next());
83 }
84 }