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  
26  import triptracker.client.gps.core.GPSClientModel;
27  import triptracker.client.ui.SwingWorker;
28  
29  public class LoginController implements ActionListener {
30  	private final GPSClientModel model;
31  
32  	private final LoginForm view;
33  	
34  	private SwingWorker worker;
35  
36  	public LoginController(GPSClientModel model, LoginForm view) {
37  		this.model = model;
38  		this.view = view;
39  
40  		// Add listeners to the view.
41  		view.addBtnListener(this);
42  
43  	}
44  
45  	public void actionPerformed(ActionEvent event) {
46  
47  		// Login button is clicked
48  		if (event.getSource() == view.loginButton) {
49  			view.setEnableControls(false);
50  			
51  			// Getting variables ..
52  			final String username = view.getUsername();
53  			final String password = view.getPassword();
54  			final String hostname = view.getHostname();
55  
56  			view.setStatusMsg("Trying to log on to " + hostname + "...");
57  
58  			worker = new ConnectionWorker(username, password);
59  			worker.start();
60  
61  				
62  //			view.setStatusMsg("Checking information");
63  
64  			// Cancel button is clicked
65  		} else if (event.getSource() == view.cancelButton) {
66  			if (worker != null && worker.isAlive()) {
67  				try {
68  					model.disconnectFromSocket();
69  				} catch (IOException e) {
70  					e.printStackTrace();
71  				}
72  			}
73  			view.showMain();
74  		}
75  	}
76  
77  	private class ConnectionWorker extends SwingWorker {
78  		private IOException ioe = null;
79  		private final String username;
80  		private final String password;
81  		
82  		public ConnectionWorker(String username, String password) {
83  			this.username = username;
84  			this.password = password;
85  		}
86  	
87  		@Override
88  		public Object construct() {
89  			try {
90  				model.logon(username, password);
91  			} catch (IOException ioe) {
92  				this.ioe = ioe;
93  				return false;
94  			}
95  			return true;
96  		}
97  	
98  		@Override
99  		public void finished() {
100 			if (ioe == null) {
101 				view.setStatusMsg("Login successful");
102 				view.showMain();
103 			} else {
104 				view.setStatusMsg("Login failed: " + ioe.getMessage());
105 			}
106 			view.setEnableControls(true);
107 		}
108 	}
109 }