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.map.ui;
21  
22  import java.awt.event.ActionListener;
23  import java.awt.event.ItemListener;
24  import java.awt.event.KeyEvent;
25  import java.util.List;
26  
27  import javax.swing.BorderFactory;
28  import javax.swing.JButton;
29  import javax.swing.JCheckBox;
30  import javax.swing.JComboBox;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.JSpinner;
34  import javax.swing.JTextArea;
35  import javax.swing.SpinnerListModel;
36  import javax.swing.event.ChangeListener;
37  
38  import triptracker.client.net.MapSocket;
39  import triptracker.client.net.MapSocketAdapter;
40  import se.datadosen.component.RiverLayout;
41  import triptracker.core.ConnectionState;
42  import triptracker.core.Route;
43  import triptracker.core.User;
44  
45  @SuppressWarnings("serial")
46  public class ConfigPanel extends SuperPanel {
47  	protected static int nameCount = 1;
48  	protected final String name;
49  	protected final TabbedForm form;
50  	private MapSocket model; 
51  	
52  	private JComboBox userBox;
53  	private JComboBox routeBox;
54  	private JButton getUsersButton;
55  	private JButton gotoMapButton;
56  	private JTextArea routeInfo;
57  	private JLabel stateLable;
58  	
59  	private JCheckBox nrBox;
60  	private JCheckBox dotBox;
61  	private JCheckBox mapBox;
62  	private JSpinner thickSpinner;
63  	
64  	
65  	public ConfigPanel(MapSocket model, TabbedForm form) {
66  		this(LoginForm.class.getClass().getName() + "-" + nameCount++, model,
67  				form);
68  	}
69  	
70  	public ConfigPanel(String name, MapSocket model, TabbedForm form) {
71  		this.name = name;
72  		this.model = model;
73  		this.form = form;
74  		
75  		this.setLayout(new RiverLayout());
76  
77  		userBox = new JComboBox();
78  		routeBox = new JComboBox();
79  		routeInfo = new JTextArea(5, 25);
80  		routeInfo.setBorder(BorderFactory.createEtchedBorder());
81          routeInfo.setEditable(false);
82          routeInfo.setLineWrap(true);
83          routeInfo.setWrapStyleWord(true);
84          stateLable = new JLabel();
85          
86          //Map paint configuration elements
87          JPanel mapConfig = new JPanel();
88          mapConfig.setLayout(new RiverLayout());
89          mapConfig.setBorder(BorderFactory.createTitledBorder(null,
90  				"Map configurations"));
91          nrBox = new JCheckBox("Numbers");
92          nrBox.setSelected(false);
93          dotBox = new JCheckBox("Dots");
94          dotBox.setSelected(true);
95          mapBox = new JCheckBox("Map");
96          mapBox.setSelected(true);
97  		String[] intervalList = { "1", "2", "3", "4", "5", "6"}; 
98  		SpinnerListModel intervalModel = new SpinnerListModel(intervalList);
99          thickSpinner = new JSpinner(intervalModel);
100         mapConfig.add(nrBox);
101         mapConfig.add(dotBox);
102         mapConfig.add(mapBox);
103         mapConfig.add("br", new JLabel("Route thickness:"));
104         mapConfig.add(thickSpinner);
105 //        JScrollPane scrollPane = new JScrollPane(routeInfo,
106 //				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
107 //				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
108 		// routeBox.setSize(100,10);//new Dimension(5,20));
109 
110         JPanel routePanel = new JPanel();
111         routePanel.setLayout(new RiverLayout());
112         routePanel.setBorder(BorderFactory.createTitledBorder(null,"Select user and route"));
113         routePanel.add("left",getUsersButton = new JButton("Refresh"));
114         gotoMapButton =  new JButton("View selected");
115         gotoMapButton.setMnemonic(KeyEvent.VK_ENTER); 
116         routePanel.add(gotoMapButton);
117         routePanel.add("p left", new JLabel("Select user: "));
118         routePanel.add("tab",userBox);
119         routePanel.add("br",new JLabel("Select route: "));
120         routePanel.add("tab",routeBox);
121         routePanel.add("br", routeInfo);
122 //        routePanel.add("br",new JLabel("Route state:"));
123 //        routePanel.add("tab",stateLable);
124 //        
125 //        JPanel descPanel = new JPanel();
126 //        descPanel.setBorder(BorderFactory.createTitledBorder(null,"Description"));
127 //        descPanel.add(routeInfo);
128         
129 		this.add("br left", routePanel);
130 //		this.add("br", descPanel);
131 		this.add("br", mapConfig);
132 		
133 	//	center.add(stateLable,c);
134 		new ConfigController(model, this);
135 		model.addListener(new ConfigPanelListener());	
136 	}
137 	
138 	/*** Run when this window is activated by login.	 */
139 	public void start() {
140 		model.getUsers();
141 	}
142 	
143 	/*** Adds user elements to userBox */
144 	public void setUserBox(List<User> users) {
145 		userBox.removeAllItems();
146 		for (User user : users) {
147 			userBox.addItem(user);
148 		}	
149 		userBox.addItem(new User(0, "Realtime routes"));
150 	}
151 	
152 	/*** Adds route elements to routeBox */
153 	public void setRouteBox(List<Route> routes) {
154 		routeBox.removeAllItems();
155 		if (routes == null)
156 			return;
157 		for (Route route : routes) {
158 			routeBox.addItem(route);
159 		}
160 	}
161 	
162 	/*** Updates route information in routeInfo label */
163 	public void updateRouteInfo() {
164 		Route route = (Route) routeBox.getSelectedItem();
165 		if (route != null) {
166 			routeInfo.setText(route.getDescription() + "\r\n" +
167 			//		"Is active: " + route.isActive() + "\n" + 
168 			//		"Is public: " + route.getVisible() + "\n" + 
169 					"Last updated: " + route.getLastUpdateString());
170 		}
171 	}
172 	
173 	public void setState(String state) {
174 		stateLable.setText(state);
175 	}
176 	
177 	/*** Returns selected element in userBox */
178 	public void getBoxUser() {
179 		user = (User) userBox.getSelectedItem();
180 	}
181 	
182 	/*** Returns selected element in routeBox */
183 	public void getBoxRoute() {
184 		setRoute((Route) routeBox.getSelectedItem());
185 	}
186 	
187 	public int getThick() {
188 		return Integer.parseInt((String)thickSpinner.getValue());
189 	}
190 	
191 	@Override
192 	public String getName() {
193 		return name;
194 	}
195 	
196 	/*** Will change selected window to MapViewPanel */
197 	public void gotoMap() {
198 		form.setMapView(nrBox.isSelected(), dotBox.isSelected(), mapBox.isSelected(), getThick());
199 		model.getRoute(route);
200 		form.setInfo(route,user);
201 		form.gotoMap(realtime, route);
202 	}
203 	
204 	/*** Registers actionListener to getUserButton */
205 	public void addGetUserBtnListener(ActionListener listener) {
206 		getUsersButton.addActionListener(listener);
207 	}
208 	
209 	/*** Registers actionListener to gotoMapButton */
210 	public void addConfigBtnListener(ActionListener listener) {
211 		gotoMapButton.addActionListener(listener);
212 	}
213 	
214 	/*** Registers ItemListeners to userBox */
215 	public void addUserBoxListener(ItemListener listener) {
216 		userBox.addItemListener(listener);
217 	}
218 	
219 	/*** Registers actionListener to routeBox */
220 	public void addRouteBoxListener(ItemListener listener) {
221 		routeBox.addItemListener(listener);
222 	}
223 	
224 	/*** Registers actionListener to routeBox */
225 	public void addNrBoxListener(ItemListener listener) {
226 		nrBox.addItemListener(listener);
227 	}
228 	
229 	/*** Registers actionListener to routeBox */
230 	public void addDotBoxListener(ItemListener listener) {
231 		dotBox.addItemListener(listener);
232 	}
233 	
234 	/*** Registers actionListener to routeBox */
235 	public void addMapBoxListener(ItemListener listener) {
236 		mapBox.addItemListener(listener);
237 	}
238 	
239 	/*** Registers actionListener to routeBox */
240 	public void addThickSpinnerListener(ChangeListener listener) {
241 		thickSpinner.addChangeListener(listener);
242 	}
243 	
244 	private class ConfigPanelListener extends MapSocketAdapter {
245 		/*** {@inheritDoc} */
246 		@Override
247 		public void usersReceived(List<User> users) {
248 			setUserBox(users);
249 		}
250 
251 		/*** {@inheritDoc} */
252 		@Override
253 		public void routeList(List<Route> routes) {
254 			setRouteBox(routes);
255 		}
256 
257 		/*** {@inheritDoc} */
258 		@Override
259 		public void connectionUpdate(ConnectionState state) {
260 			switch (state){
261 			case CONNECTED:
262 				setState("Connected to server");
263 				break;
264 			case AUTH_SUCCESS:
265 				setState("Connected to server");
266 				break;
267 			case AUTH_FAILED:
268 				setState("No connection");
269 				break;
270 			case DISCONNECTED:
271 				setState("Disconnected from server");
272 				break;
273 			case WATCHING: // FIXME Move to routeUpdate.
274 				setState("Route is not beeing tracked");
275 				break;
276 			case TRACKING: // FIXME Move to routeUpdate.
277 				setState("Route is now beeing tracked");
278 				break;
279 			}
280 		}
281 	}
282 }