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  import java.util.TooManyListenersException;
27  
28  import javax.swing.event.ChangeEvent;
29  import javax.swing.event.ChangeListener;
30  
31  import gnu.io.PortInUseException;
32  import gnu.io.UnsupportedCommOperationException;
33  
34  import triptracker.client.gps.core.GPSClientModel;
35  import triptracker.client.net.GPSSocketAdapter;
36  import triptracker.core.Coordinate;
37  
38  /***
39   * Map client controller for Model View Controller (MVC) separation.
40   */
41  public class MainFormController extends GPSSocketAdapter implements
42  		ActionListener, ChangeListener {
43  	private final GPSClientModel model;
44  
45  	private final MainForm view;
46  
47  	public MainFormController(GPSClientModel model, MainForm view) {
48  		this.model = model;
49  		this.view = view;
50  
51  		// Add listeners to the view.
52  		view.addGPSBtnListener(this);
53  		view.addServerBtnListener(this);
54  		view.addSpinnerListener(this);
55  		view.createTimer(this);
56  
57  		// Adds socketlistener
58  		model.addSocketListener(this);
59  	}
60  
61  	/***
62  	 * Action is performed
63  	 */
64  	public void actionPerformed(ActionEvent event) {
65  		// GPS button is clicked
66  		if (event.getSource() == view.gpsButton) {
67  			gpsButton();
68  		}
69  
70  		// Server button is clicked
71  		if (event.getSource() == view.serverButton) {
72  			serverButton();
73  		}
74  
75  		// Timer
76  		if (event.getSource() == view.getTimer()) {
77  			if (model.currCoordSet()) {
78  				model.syncCoords();
79  				view.setLastUpdateField(model.getCoordDate());
80  				view.setLatLonFields(model.getStringLat(), model.getStringLon());
81  				view.increaseRecievedField();					
82  				if (model.getCurrentCoord() != null && model.getOldCoord() != null) {
83  					view.setDistField(Coordinate.haversineDist(model.getOldCoord(), model.getCurrentCoord()));
84  					System.out.println("calculating distance");
85  				}	
86  				if (model.getTransferState())
87  					view.setStatusMsg("Transferring position to server");
88  				else
89  					view.setStatusMsg("Buffering position to file");
90  				try {
91  					model.saveCoord();
92  				} catch (IOException e) {
93  					view.setStatusMsg("Could not save the coordinate.");
94  				}
95  			} else {
96  				view.setLatLonFields("--", "--");
97  				view.setStatusMsg("GPS-unit does not have fixed position.");
98  			}
99  		}
100 	}
101 
102 	// Listener for the intervalSpinner
103 	public void stateChanged(ChangeEvent event) {
104 		if (event.getSource() == view.intervalSpinner) {
105 			model.setInterval(view.getInterval() * 1000);
106 			System.out.println("New interval: " + model.getInterval());
107 			view.getTimer().setDelay(view.getInterval() * 1000);
108 		}
109 	}
110 
111 	/***
112 	 * A socket error has occured
113 	 */
114 	@Override
115 	public void socketError(IOException e) {
116 		view.setStatusMsg("Connection to server is lost");
117 		view.showErrorDialog("Connection lost", "Connection to server lost!");
118 		model.changeTransferState();
119 	}
120 
121 	/***
122 	 * Actions to perform when GPS button is clicked.
123 	 */
124 	private void gpsButton() {
125 		model.changeGPSState();
126 
127 		if (model.getGPSState()) {
128 			try {
129 				if (!view.timerIsRunning()) {
130 					view.startTimer();
131 				}
132 
133 				if (!model.isConnectedToGPS()) {
134 					model.openGPSPort();
135 				}
136 
137 				model.startRecieveFromGPS();
138 				view.recieveFromGPS();
139 			} catch (PortInUseException e) {
140 				view.setStatusMsg("!!: Port in use");
141 			} catch (TooManyListenersException e) {
142 				view.setStatusMsg("!!: Too Many listeners");
143 			} catch (UnsupportedCommOperationException e) {
144 				view.setStatusMsg("!!: Unsupported Comm opreation");
145 			} catch (IOException e) {
146 				view.setStatusMsg("!!: Problem recieve from GPS");
147 			} catch (IllegalArgumentException e) {
148 				view.setStatusMsg("!!: No valid ports found.");
149 			}
150 		} else {
151 			try {
152 				model.stopRecieveFromGPS();
153 			} catch (IllegalArgumentException e) {
154 				view.setStatusMsg("Error while trying to stop recieve from GPS-unit.");
155 			} catch (PortInUseException e) {
156 				view.setStatusMsg("Error: serial port is already in use.");
157 			} catch (TooManyListenersException e) {
158 				view.setStatusMsg("Error: port has too many listeners.");
159 			} catch (UnsupportedCommOperationException e) {
160 				view.setStatusMsg("Error while trying to stop recieve from GPS-unit.");
161 			} catch (IOException e) {
162 				view.setStatusMsg("Error while trying to stop recieve from GPS-unit.");
163 			}
164 			view.pauseFromGPS();
165 		}
166 	}
167 
168 	/***
169 	 * Checks if buffered file exists, and transfers if true in the buffer
170 	 * dialog.
171 	 */
172 	public void bufferFileExists() {
173 		// Checks if there is buffered coordinates to transfer.
174 		if (model.bufferFileExists() && model.getActiveRoute().getRouteId() != -1) {
175 			if (!model.getAutoTransferState()) {
176 				if (model.getTransferState())
177 					model.changeTransferState();
178 				if (!view.transferBufferedDialog()) {
179 					try {
180 						if (model.transferTmpCoords()) {
181 							view.setStatusMsg("Buffered files sucessfully transferred");
182 						}
183 						view.transferToServer();
184 						if (!model.getTransferState())
185 							model.changeTransferState();
186 					} catch (FileNotFoundException e) {
187 						view.setStatusMsg("Buffer file now found");
188 						view.showErrorDialog("Buffer file not found","The buffer file was not found");
189 					} catch (IllegalStateException e) {
190 						view.setStatusMsg("!!: Could not delete the buffer file");
191 					} catch (IOException e) {
192 						view.setStatusMsg("Something went wrong sending the buffered coordinates");
193 					} 
194 				} else {
195 					if (model.getTransferState()) {
196 						model.changeTransferState();
197 					}
198 					view.bufferToFile();
199 					view.setStatusMsg("Buffer to file");
200 					System.out.println("buffer to filee!");
201 				}
202 			} else {
203 				try {		
204 				if (model.transferTmpCoords()) {
205 					view.setStatusMsg("Buffered files sucessfully transferred");
206 				}
207 				view.transferToServer();
208 				if (!model.getTransferState())
209 					model.changeTransferState();
210 				} catch (FileNotFoundException e) {
211 					view.setStatusMsg("Buffer file now found");
212 					view.showErrorDialog("Buffer file not found","The buffer file was not found");
213 				} catch (IllegalStateException e) {
214 					view.setStatusMsg("!!: Could not delete the buffer file");
215 				} catch (IOException e) {
216 					view.setStatusMsg("Something went wrong sending the buffered coordinates");
217 				} 
218 			}
219 
220 		}
221 	}
222 
223 	public void serverButton() {
224 		if (model.isLoggedIn()) {
225 			model.changeTransferState();
226 
227 			if (model.getTransferState()) {
228 				view.transferToServer();
229 			} else {
230 				view.bufferToFile();
231 			}
232 
233 			if (model.getTransferState()) {
234 				if (model.getActiveRoute().getRouteId() == -1) {
235 					view.showRoutes();
236 					model.getRoutes(model.getUsername());
237 					view.showErrorDialog("No rute set",
238 							"No route set, please set route");
239 				}
240 
241 				// Checks if buffered file exists
242 				bufferFileExists();
243 			}
244 		} else {
245 			view.showLogin();
246 			view.setStatusMsg("Please connect before tranfer to server");
247 		}
248 	}
249 }