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.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  		// Sets the default GPS-unit serial port.
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); // TODO Fix: Exit gracefully.
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  }