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.util.List;
23  
24  import javax.swing.BorderFactory;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JTextArea;
28  
29  import triptracker.client.net.MapSocket;
30  import triptracker.client.net.MapSocketAdapter;
31  import se.datadosen.component.RiverLayout;
32  import triptracker.core.Coordinate;
33  import triptracker.core.Route;
34  import triptracker.core.User;
35  import triptracker.core.Utils;
36  
37  /***
38   * Panel containing information about route, user and others.
39   *
40   */
41  @SuppressWarnings("serial")
42  public class InfoPanel extends SuperPanel {// implements MapSocketListener {
43  //	private MapSocket model; 
44  	double distance;
45  	double width;
46  	double timeSpent;
47  	double averageSpeed;
48  	Coordinate startCoord;
49  	Coordinate stopCoord;
50  	
51  	protected final TabbedForm form;
52  
53  	private JLabel dist, routeWidth, time, speed, routeUser;
54  	private JTextArea routeInfo;
55  	private JPanel mainPanel;
56  
57  	
58  	public InfoPanel(MapSocket model, TabbedForm form) {
59  //		this.model = model;
60  		this.form = form;
61  		
62  		this.setLayout(new RiverLayout());
63  		
64  		routeUser = new JLabel();
65  		dist = new JLabel();
66  		time = new JLabel();
67  		speed = new JLabel();
68  		routeInfo = new JTextArea(5,25);
69  		routeInfo.setBorder(BorderFactory.createEtchedBorder());
70  		routeInfo.setLineWrap(true);
71          routeInfo.setEditable(false);
72          routeWidth = new JLabel();
73          routeWidth.setText("" + width);
74          
75          mainPanel = new JPanel();
76          mainPanel.setLayout(new RiverLayout());
77          mainPanel.setBorder(BorderFactory.createTitledBorder(null, "Route information"));
78          mainPanel.add(new JLabel("Route owner: "));
79          mainPanel.add("tab", routeUser);
80          mainPanel.add("br", new JLabel("Route distance: "));
81          mainPanel.add("tab", dist);
82  		//this.add("br", new JLabel("Route width: "));		
83  		//this.add("tab", routeWidth);
84          mainPanel.add("br", new JLabel("Time spent:"));		
85          mainPanel.add("tab", time);
86          mainPanel.add("br", new JLabel("Average speed: "));		
87          mainPanel.add("tab", speed);
88          mainPanel.add("br", new JLabel("Route description: "));
89          mainPanel.add("tab", routeInfo);
90          
91          this.add(mainPanel);
92  		
93  		routeUser.setText("N/A");
94  		dist.setText("N/A");
95  		speed.setText("N/A");
96  		time.setText("N/A");
97  		routeWidth.setText("N/A");
98  		
99  	//	new InfoController(model, this);
100 		model.addListener(new InfoPanelListener());
101 	}
102 	
103 	/*** Sets route to display info of */
104 	public void setRoute(Route route) {
105 		super.setRoute(route);
106 		setDescription(route);
107 	}
108 	
109 	public void setRouteList(List<Coordinate> coordList) {
110 		this.coordList = coordList;
111 		calcRoute(coordList);
112 	}
113 	
114 	public void setUser(User user){
115 		super.setUser(user);
116 		routeUser.setText(user.getUsername());
117 	}
118 
119 	/*** Sets route description */
120 	public void setDescription(Route route) {
121 		if (route == null)
122 			return;
123 		routeInfo.setText(route.getDescription());
124 	}
125 	
126 	/***
127 	 * Calculates information about route average speed, distance and time
128 	 * spent.
129 	 */
130 	public void calcRoute(List<Coordinate> coordList) {
131 //		if (coordList == null)
132 //			return;
133 		
134 		distance = calcDist(coordList);
135 		calcWidth(coordList);
136 		
137 		startCoord = coordList.get(0);
138 		stopCoord = coordList.get(coordList.size() - 1);
139 
140 		double hoursSpent = ((double) (stopCoord.getDate().getTime()
141 				- startCoord.getDate().getTime())) / (1000 * 60 * 60);
142 		averageSpeed = calcSpeed(hoursSpent, distance);
143 		time.setText(Utils.friendlyString(startCoord.getDate(),
144 				stopCoord.getDate()));
145 		speed.setText(Utils.roundStr(averageSpeed, 1) + " km/h");
146 	}
147 	
148 	/*** Calculates route distance */
149 	public double calcSpeed(double timeSpent, double distance) {
150 		return (distance / timeSpent);
151 	}
152 	
153 	/*** Calculates route distance */
154 	public double calcDist(List<Coordinate> coordList) {
155 		distance = Coordinate.haversineDist(coordList);
156 		dist.setText(Utils.roundStr(distance, 3) + " km");
157 		return distance;
158 	}
159 	
160 	/*** Calculates with from west to east. For the moment not correct... */
161     public void calcWidth(List<Coordinate> coordList) {
162 //		limits[0] = xMin, limits[1] = yMin, limits[2] = xMax, limits[3] = yMax
163 		double[] limits = { Double.MAX_VALUE, Double.MAX_VALUE,
164 				Double.MIN_VALUE, Double.MIN_VALUE };
165 		for (Coordinate coord : coordList) {
166 			if (coord.getX() < limits[0])
167 				limits[0] = coord.getX();
168 			if (coord.getY() < limits[1])
169 				limits[1] = coord.getY();
170 			if (coord.getX() > limits[2])
171 				limits[2] = coord.getX();
172 			if (coord.getY() > limits[3])
173 				limits[3] = coord.getY();
174 //			System.out.println("Size: " + limits[0] + ", " + limits[1] + ", "
175 //					+ limits[2] + ", " + limits[3] + ". Coords: " + coord.getX()
176 //					+ ", " + coord.getY());
177 		}
178 		double avgY = ((limits[3] + limits[1]) / 2);
179 		width = Coordinate.haversineDist(limits[0], avgY, limits[2], avgY);
180 		routeWidth.setText(Utils.roundStr(width, 1));
181 	}
182 	
183 	private class InfoPanelListener extends MapSocketAdapter {
184 //		/*** {@inheritDoc} */
185 //		@Override
186 //		public void userReceived(User user) {
187 //			routeUser.setText(user.getUsername());
188 //		}
189 		
190 //		/*** {@inheritDoc} */
191 //		@Override
192 //		public void routeReceived(int routeId, List<Coordinate> coords) {
193 //			calcRoute(coords);
194 //		}
195 	}
196 }