1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 {
43
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
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
83
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
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
132
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
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
175
176
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
186
187
188
189
190 /*** {@inheritDoc} */
191
192
193
194
195 }
196 }