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.Color;
23  import java.awt.event.ActionListener;
24  import java.util.List;
25  
26  import javax.swing.BorderFactory;
27  import javax.swing.JButton;
28  import javax.swing.JComboBox;
29  import javax.swing.JComponent;
30  import javax.swing.JOptionPane;
31  import javax.swing.JPanel;
32  import javax.swing.JTextArea;
33  
34  import triptracker.client.gps.core.GPSClientModel;
35  import triptracker.client.net.GPSSocketAdapter;
36  import triptracker.client.ui.Form;
37  import se.datadosen.component.RiverLayout;
38  import triptracker.core.Route;
39  
40  public class RoutesForm extends GPSSocketAdapter implements Form {
41  	protected static int nameCount = 1;
42  
43  	protected final String name;
44  
45  	private GPSClientModel model;
46  
47  	private GPSGui view;
48  
49  	private MainForm mainForm;
50  
51  	private final JPanel mainPanel, routePanel, newPanel, newBorderPanel,
52  			routeBorderPanel;
53  
54  	protected final JButton trackButton, cancelButton, newButton;
55  
56  	private JComboBox routeBox;
57  
58  	private JTextArea newDesc;
59  
60  	Route newRoute;
61  
62  	/***
63  	 * The constructor creates a JPanel for the login information, and registers
64  	 * as a form on the <code>view</code>.
65  	 * 
66  	 * @param view JComponent form manager
67  	 * @param model GPS model
68  	 */
69  	public RoutesForm(GPSGui view, GPSClientModel model, Form mainForm) {
70  		this(view, RoutesForm.class.getClass().getName() + "-" + nameCount++,
71  				model, mainForm);
72  	}
73  
74  	/***
75  	 * The constructor creates a JPanel for the login information, and registers
76  	 * as a form on the <code>view</code>. The form name should be unique for
77  	 * the given form manager.
78  	 * 
79  	 * @param view parent JComponent form manager
80  	 * @param name unique form name
81  	 * @param model GPS model
82  	 */
83  	public RoutesForm(GPSGui view, String name, GPSClientModel model,
84  			Form mainForm) {
85  		this.name = name;
86  		this.model = model;
87  		this.view = view;
88  		this.mainForm = (MainForm) mainForm;
89  
90  		routeBox = new JComboBox();
91  
92  		trackButton = new JButton("Resume route");
93  		newButton = new JButton("New");
94  		cancelButton = new JButton("Continiue without setting a active route");
95  
96  		newDesc = new JTextArea("Enter description", 3, 20);
97  		newDesc.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1,
98  				Color.BLACK));
99  
100 		routePanel = new JPanel();
101 		routePanel.setBorder(BorderFactory.createTitledBorder(null,
102 				"Select a route to resume"));
103 		routePanel.add(routeBox);
104 		routePanel.add(trackButton);
105 
106 		newPanel = new JPanel();
107 		newPanel.setBorder(BorderFactory.createTitledBorder(null,
108 				"Create a new Route"));
109 		newPanel.add(newDesc);
110 		newPanel.add(newButton);
111 
112 		newBorderPanel = new JPanel();
113 		newBorderPanel.setBorder(BorderFactory
114 				.createEmptyBorder(10, 10, 10, 10));
115 		newBorderPanel.add(newPanel);
116 
117 		routeBorderPanel = new JPanel();
118 		routeBorderPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
119 				10));
120 		routeBorderPanel.add(routePanel);
121 
122 		mainPanel = new JPanel();
123 		mainPanel.setLayout(new RiverLayout());
124 		mainPanel.add("center", routeBorderPanel);
125 		mainPanel.add("p center", newBorderPanel);
126 		mainPanel.add("p hfill", cancelButton);
127 
128 		view.register(this);
129 		model.addSocketListener(this);
130 		new RoutesController(model, this, mainForm);
131 	}
132 
133 	/***
134 	 * Register a listener on the login button.
135 	 * 
136 	 * @param listener
137 	 */
138 	public void addBtnListener(ActionListener listener) {
139 		trackButton.addActionListener(listener);
140 		newButton.addActionListener(listener);
141 		cancelButton.addActionListener(listener);
142 
143 	}
144 
145 	//	public void addComponentListener(ComponentListener listener) {
146 	//		panel.addComponentListener(listener);
147 	//	}
148 
149 	public JComponent getFormComponent() {
150 		return mainPanel;
151 	}
152 
153 	public String getName() {
154 		return name;
155 	}
156 
157 	public void showMain() {
158 		view.showMainForm();
159 	}
160 
161 	public void refreshView() {
162 	}
163 
164 	@Override
165 	public void routeList(List<Route> routes) {
166 		System.out.println("Recieved routes");
167 		setRouteBox(routes);
168 	}
169 
170 	@Override
171 	public void routeCreated(int rId) {
172 		setStatusMsg("Route was sucsessfully created (id " + rId + ")");
173 		Route route = new Route(rId, 11, getNewRouteDesc());
174 		model.setActiveRoute(route);
175 		mainForm.setActiveRoute(route.getDescription());
176 	}
177 
178 	/*** Adds route elements to routeBox */
179 	public void setRouteBox(List<Route> routes) {
180 		routeBox.removeAllItems();
181 		for (Route route : routes) {
182 			routeBox.addItem(route);
183 		}
184 
185 	}
186 
187 	/*** Returns selected element in routeBox */
188 	public Route getRoute() {
189 		return (Route) routeBox.getSelectedItem();
190 	}
191 
192 	/***
193 	 * Sets the status msg
194 	 * 
195 	 * @param msg msg in the statusbar
196 	 */
197 	public void setStatusMsg(String msg) {
198 		view.setStatusMsg(msg);
199 	}
200 
201 	/***
202 	 * Dialog created to ask if transfer buffered coordinates
203 	 * 
204 	 * @return true if transfer
205 	 */
206 	public boolean transferBufferedDialog() {
207 		Object[] options = { "Continiue buffering", "Transfer buffered" };
208 		int n = JOptionPane.showOptionDialog(null,
209 				"Buffered coordinates exists, what would you like to do?",
210 				"Buffered coordinates exists",
211 				JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
212 				null, options, options[1]);
213 		return (n == 0);
214 	}
215 
216 	/***
217 	 * Creates an info dialog with the give heading and info
218 	 * 
219 	 * @param head the header of the dialog
220 	 * @param info the info message to give
221 	 */
222 	public void showInfoDialog(String head, String info) {
223 		view.showInfoDialog(head, info);
224 	}
225 
226 	/***
227 	 * Gets the description of the new route created
228 	 * 
229 	 * @return the description of the new route
230 	 */
231 	public String getNewRouteDesc() {
232 		return newDesc.getText();
233 	}
234 
235 	/***
236 	 * Gets the new route
237 	 * 
238 	 * @return the new route
239 	 */
240 	public Route getNewRoute() {
241 		return newRoute;
242 	}
243 }