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.FileNotFoundException;
25 import java.io.IOException;
26
27 import triptracker.client.gps.core.GPSClientModel;
28 import triptracker.client.ui.Form;
29 import triptracker.core.Route;
30
31 public class RoutesController implements ActionListener {
32 private final GPSClientModel model;
33
34 private final RoutesForm view;
35
36 private final MainForm mainForm;
37
38 public RoutesController(GPSClientModel model, RoutesForm view, Form mainForm) {
39 this.model = model;
40 this.view = view;
41 this.mainForm = (MainForm) mainForm;
42
43
44 view.addBtnListener(this);
45 }
46
47 public void actionPerformed(ActionEvent event) {
48
49
50 if (event.getSource() == view.newButton) {
51 if (model.isLoggedIn()) {
52 model.createRoute(view.getNewRouteDesc());
53
54 view.setStatusMsg("Active route: "
55 + model.getActiveRoute().getDescription());
56
57 }
58
59 view.showMain();
60 }
61
62
63 if (event.getSource() == view.trackButton) {
64
65 Route route = new Route(model.getRouteId(view.getRoute()));
66 if (model.isLoggedIn()) {
67 model.setRouteOnServer(route);
68 System.out.println("trying to set route with id: "
69 + route.getRouteId());
70 }
71 model.setActiveRoute(view.getRoute());
72 view.setStatusMsg("Active route: "
73 + view.getRoute().getDescription());
74 view.showMain();
75 }
76
77
78 if (event.getSource() == view.cancelButton) {
79
80 view.showMain();
81 if (!model.getTransferState())
82 model.changeTransferState();
83
84 if (model.getActiveRoute().getRouteId() == -1 && model.isLoggedIn())
85 view
86 .showInfoDialog(
87 "No active route is set",
88 "There is no active route\nIf you transfer coordinates to server,\nthey will be lost!\n\nFor setting a active route, \nchoose Routes -> Show routes from the menubar");
89 }
90
91
92 if (model.bufferFileExists()
93 && model.getActiveRoute().getRouteId() != -1) {
94 if (!model.getAutoTransferState()) {
95 if (model.getTransferState())
96 model.changeTransferState();
97 if (!view.transferBufferedDialog()) {
98 try {
99 if (model.transferTmpCoords()) {
100 view
101 .setStatusMsg("Buffered files sucessfully transferred");
102 }
103 mainForm.transferToServer();
104 if (!model.getTransferState())
105 model.changeTransferState();
106 } catch (FileNotFoundException e) {
107 view.setStatusMsg("Buffer file now found");
108 view.showInfoDialog("Buffer file not found",
109 "The buffer file was not found");
110 } catch (IllegalStateException e) {
111 view
112 .setStatusMsg("!!: Could not delete the buffer file");
113 } catch (IOException e) {
114 view
115 .setStatusMsg("Something went wrong sending the buffered coordinates");
116 }
117 } else {
118 if (model.getTransferState()) {
119 model.changeTransferState();
120 }
121 mainForm.bufferToFile();
122 view.setStatusMsg("Buffer to file");
123 System.out.println("buffer to filee!");
124 }
125 } else {
126 try {
127 if (model.getActiveRoute().getRouteId() != -1) {
128 if (model.transferTmpCoords()) {
129 view
130 .setStatusMsg("Buffered files sucessfully transferred");
131 }
132 }
133 mainForm.transferToServer();
134 if (!model.getTransferState())
135 model.changeTransferState();
136 } catch (FileNotFoundException e) {
137 view.setStatusMsg("Buffer file now found");
138 view.showInfoDialog("Buffer file not found",
139 "The buffer file was not found");
140 } catch (IllegalStateException e) {
141 view.setStatusMsg("!!: Could not delete the buffer file");
142 } catch (IOException e) {
143 view
144 .setStatusMsg("Something went wrong sending the buffered coordinates");
145 }
146 }
147
148 }
149 }
150 }