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 gnu.io.PortInUseException;
29  import gnu.io.UnsupportedCommOperationException;
30  
31  import triptracker.client.gps.core.GPSClientModel;
32  
33  public class GPSSettingsController implements ActionListener {
34  	private final GPSClientModel model;
35  	private final GPSSettingsForm view;
36  	
37  	public GPSSettingsController(GPSClientModel model, GPSSettingsForm view) {
38  		this.model = model;
39  		this.view = view;
40  		
41  		// Add listener to the view.
42  		view.addBtnListener(this);
43  	}
44  	
45  	public void actionPerformed(ActionEvent event) {
46  		
47  		// Save button is clicked
48  		if (event.getSource() == view.saveButton) {
49  			// save values
50  			view.showMain();
51  			model.setCompression(view.getCompression());
52  
53  			// Selected port is different from current, closes old
54  			// and opens new port.
55  			if (!view.getPort().equals(model.getPort())) {
56  				view.setStatusMsg("Closing " + model.getPort() + ", and opening " + view.getPort());
57  				model.closeGPSPort();
58  				model.setPort(view.getPort());
59  				try {
60  					model.openGPSPort();
61  				} catch (PortInUseException e) {
62  					view.setStatusMsg(model.getPort() + " is already in use.");
63  				} catch (TooManyListenersException e) {
64  					view.setStatusMsg(model.getPort() + " has too many listeners.");
65  				} catch (UnsupportedCommOperationException e) {
66  					view.setStatusMsg("Unsupported comm operation");
67  				} catch (IOException e) {
68  					view.setStatusMsg("Comm error");
69  				}
70  			}
71  	
72  			if (view.getAutoTransfer()) {
73  				view.showRoutes();
74  			}
75  			
76  			model.autoTransfer(view.getAutoTransfer());
77  			
78  			// Ask if transfer buffered, transfer to server if positive feedback.
79  			if (model.isConnected() && view.getAutoTransfer()
80  					&& model.bufferFileExists()) {
81  				try {
82  					model.transferTmpCoords();
83  				} catch (FileNotFoundException e) {
84  					view.setStatusMsg("Buffer file was not found");
85  				} catch (IllegalStateException e) {
86  					view.setStatusMsg("Illegal state");
87  				} catch (IOException e) {
88  					view.setStatusMsg("Error occured buffering");
89  				}
90  				view.setStatusMsg("Transferred buffered coordinates");
91  			}
92  		}
93  		
94  		// Cancel button is clicked. Dont save anything.
95  		if (event.getSource() == view.cancelButton) {
96  			view.showMain();
97  		}  	  		
98  	}
99  }