View Javadoc

1   /*
2    * Trip Tracker, a real-time position tracking system for the Internet.
3    * Copyright (C) 2006  Team Trip Tracker
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
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  		// Add listeners to the view.
44  		view.addBtnListener(this);
45  	}
46  
47  	public void actionPerformed(ActionEvent event) {
48  
49  		// New button is clicked
50  		if (event.getSource() == view.newButton) {
51  			if (model.isLoggedIn()) {
52  				model.createRoute(view.getNewRouteDesc());
53  				// System.out.println(view.getNewRouteDesc());
54  				view.setStatusMsg("Active route: "
55  						+ model.getActiveRoute().getDescription());
56  				// model.setActiveRoute(model.getActiveRoute());
57  			}
58  
59  			view.showMain();
60  		}
61  
62  		// Resume button is clicked
63  		if (event.getSource() == view.trackButton) {
64  			// save values
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  		// Cancel button is clicked
78  		if (event.getSource() == view.cancelButton) {
79  			// dont save
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  		// Checks if there is buffered coordinates to transfer
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 }