3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.text.NumberFormat;
11 import javax.swing.BorderFactory;
12 import javax.swing.Box;
13 import javax.swing.BoxLayout;
14 import javax.swing.JComboBox;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.border.EtchedBorder;
19 import tim.prune.Config;
20 import tim.prune.DataSubscriber;
21 import tim.prune.I18nManager;
22 import tim.prune.data.Altitude;
23 import tim.prune.data.Coordinate;
24 import tim.prune.data.DataPoint;
25 import tim.prune.data.Distance;
26 import tim.prune.data.IntegerRange;
27 import tim.prune.data.Photo;
28 import tim.prune.data.PhotoStatus;
29 import tim.prune.data.Selection;
30 import tim.prune.data.TrackInfo;
33 * Class to hold point details and selection details
34 * as a visual component
36 public class DetailsDisplay extends GenericDisplay
39 private JLabel _indexLabel = null;
40 private JLabel _latLabel = null, _longLabel = null;
41 private JLabel _altLabel = null, _nameLabel = null;
42 private JLabel _timeLabel = null, _speedLabel = null;
45 private JLabel _rangeLabel = null;
46 private JLabel _distanceLabel = null, _movingDistanceLabel = null;
47 private JLabel _durationLabel = null;
48 private JLabel _altRangeLabel = null, _updownLabel = null;
49 private JLabel _aveSpeedLabel = null, _aveMovingSpeedLabel = null;
52 private JLabel _photoLabel = null;
53 private PhotoThumbnail _photoThumbnail = null;
54 private JLabel _photoTimestampLabel = null;
55 private JLabel _photoConnectedLabel = null;
58 private JComboBox _coordFormatDropdown = null;
59 private JComboBox _distUnitsDropdown = null;
61 private NumberFormat _distanceFormatter = NumberFormat.getInstance();
64 private static final String LABEL_POINT_SELECTED = I18nManager.getText("details.index.selected") + ": ";
65 private static final String LABEL_POINT_LATITUDE = I18nManager.getText("fieldname.latitude") + ": ";
66 private static final String LABEL_POINT_LONGITUDE = I18nManager.getText("fieldname.longitude") + ": ";
67 private static final String LABEL_POINT_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
68 private static final String LABEL_POINT_TIMESTAMP = I18nManager.getText("fieldname.timestamp") + ": ";
69 private static final String LABEL_POINT_WAYPOINTNAME = I18nManager.getText("fieldname.waypointname") + ": ";
70 private static final String LABEL_RANGE_SELECTED = I18nManager.getText("details.range.selected") + ": ";
71 private static final String LABEL_RANGE_DURATION = I18nManager.getText("fieldname.duration") + ": ";
72 private static final String LABEL_RANGE_DISTANCE = I18nManager.getText("fieldname.distance") + ": ";
73 private static final String LABEL_RANGE_MOVINGDISTANCE = I18nManager.getText("fieldname.movingdistance") + ": ";
74 private static final String LABEL_RANGE_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
75 private static final String LABEL_RANGE_CLIMB = I18nManager.getText("details.range.climb") + ": ";
76 private static final String LABEL_RANGE_DESCENT = ", " + I18nManager.getText("details.range.descent") + ": ";
77 private static String LABEL_POINT_ALTITUDE_UNITS = null;
78 private static int LABEL_POINT_ALTITUDE_FORMAT = Altitude.FORMAT_NONE;
83 * @param inTrackInfo Track info object
85 public DetailsDisplay(TrackInfo inTrackInfo)
88 setLayout(new BorderLayout());
90 JPanel mainPanel = new JPanel();
91 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
92 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
94 // Point details panel
95 JPanel pointDetailsPanel = new JPanel();
96 pointDetailsPanel.setLayout(new BoxLayout(pointDetailsPanel, BoxLayout.Y_AXIS));
97 pointDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
98 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
100 JLabel pointDetailsLabel = new JLabel(I18nManager.getText("details.pointdetails"));
101 Font biggerFont = pointDetailsLabel.getFont();
102 biggerFont = biggerFont.deriveFont(Font.BOLD, biggerFont.getSize2D() + 2.0f);
103 pointDetailsLabel.setFont(biggerFont);
104 pointDetailsPanel.add(pointDetailsLabel);
105 _indexLabel = new JLabel(I18nManager.getText("details.nopointselection"));
106 pointDetailsPanel.add(_indexLabel);
107 _latLabel = new JLabel("");
108 pointDetailsPanel.add(_latLabel);
109 _longLabel = new JLabel("");
110 pointDetailsPanel.add(_longLabel);
111 _altLabel = new JLabel("");
112 pointDetailsPanel.add(_altLabel);
113 _timeLabel = new JLabel("");
114 pointDetailsPanel.add(_timeLabel);
115 _speedLabel = new JLabel("");
116 pointDetailsPanel.add(_speedLabel);
117 _nameLabel = new JLabel("");
118 pointDetailsPanel.add(_nameLabel);
119 pointDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
121 // range details panel
122 JPanel rangeDetailsPanel = new JPanel();
123 rangeDetailsPanel.setLayout(new BoxLayout(rangeDetailsPanel, BoxLayout.Y_AXIS));
124 rangeDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
125 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
127 JLabel rangeDetailsLabel = new JLabel(I18nManager.getText("details.rangedetails"));
128 rangeDetailsLabel.setFont(biggerFont);
129 rangeDetailsPanel.add(rangeDetailsLabel);
130 _rangeLabel = new JLabel(I18nManager.getText("details.norangeselection"));
131 rangeDetailsPanel.add(_rangeLabel);
132 _distanceLabel = new JLabel("");
133 rangeDetailsPanel.add(_distanceLabel);
134 _movingDistanceLabel = new JLabel("");
135 rangeDetailsPanel.add(_movingDistanceLabel);
136 _durationLabel = new JLabel("");
137 rangeDetailsPanel.add(_durationLabel);
138 _aveSpeedLabel = new JLabel("");
139 rangeDetailsPanel.add(_aveSpeedLabel);
140 _aveMovingSpeedLabel = new JLabel("");
141 rangeDetailsPanel.add(_aveMovingSpeedLabel);
142 _altRangeLabel = new JLabel("");
143 rangeDetailsPanel.add(_altRangeLabel);
144 _updownLabel = new JLabel("");
145 rangeDetailsPanel.add(_updownLabel);
146 rangeDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
148 // photo details panel
149 JPanel photoDetailsPanel = new JPanel();
150 photoDetailsPanel.setLayout(new BoxLayout(photoDetailsPanel, BoxLayout.Y_AXIS));
151 photoDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
152 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
154 JLabel photoDetailsLabel = new JLabel(I18nManager.getText("details.photodetails"));
155 photoDetailsLabel.setFont(biggerFont);
156 photoDetailsPanel.add(photoDetailsLabel);
157 _photoLabel = new JLabel(I18nManager.getText("details.nophoto"));
158 photoDetailsPanel.add(_photoLabel);
159 _photoTimestampLabel = new JLabel("");
160 photoDetailsPanel.add(_photoTimestampLabel);
161 _photoConnectedLabel = new JLabel("");
162 photoDetailsPanel.add(_photoConnectedLabel);
163 _photoThumbnail = new PhotoThumbnail();
164 _photoThumbnail.setVisible(false);
165 _photoThumbnail.setPreferredSize(new Dimension(100, 100));
166 photoDetailsPanel.add(_photoThumbnail);
168 // add the details panels to the main panel
169 mainPanel.add(pointDetailsPanel);
170 mainPanel.add(Box.createVerticalStrut(5));
171 mainPanel.add(rangeDetailsPanel);
172 mainPanel.add(Box.createVerticalStrut(5));
173 mainPanel.add(photoDetailsPanel);
174 mainPanel.add(Box.createVerticalStrut(5));
175 // add the main panel at the top
176 add(mainPanel, BorderLayout.NORTH);
178 // Add format, units selection
179 JPanel lowerPanel = new JPanel();
180 lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.Y_AXIS));
181 JLabel coordFormatLabel = new JLabel(I18nManager.getText("details.coordformat") + ": ");
182 coordFormatLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
183 lowerPanel.add(coordFormatLabel);
184 String[] coordFormats = {I18nManager.getText("units.original"), I18nManager.getText("units.degminsec"),
185 I18nManager.getText("units.degmin"), I18nManager.getText("units.deg")};
186 _coordFormatDropdown = new JComboBox(coordFormats);
187 _coordFormatDropdown.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent e)
190 dataUpdated(DataSubscriber.UNITS_CHANGED);
193 lowerPanel.add(_coordFormatDropdown);
194 _coordFormatDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
195 JLabel unitsLabel = new JLabel(I18nManager.getText("details.distanceunits") + ": ");
196 unitsLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
197 lowerPanel.add(unitsLabel);
198 String[] distUnits = {I18nManager.getText("units.kilometres"), I18nManager.getText("units.miles")};
199 _distUnitsDropdown = new JComboBox(distUnits);
200 if (!Config.getUseMetricUnits()) {_distUnitsDropdown.setSelectedIndex(1);}
201 _distUnitsDropdown.addActionListener(new ActionListener() {
202 public void actionPerformed(ActionEvent e)
204 dataUpdated(DataSubscriber.UNITS_CHANGED);
207 lowerPanel.add(_distUnitsDropdown);
208 _distUnitsDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
209 add(lowerPanel, BorderLayout.SOUTH);
214 * Notification that Track has been updated
215 * @param inUpdateType byte to specify what has been updated
217 public void dataUpdated(byte inUpdateType)
219 // Update current point data, if any
220 DataPoint currentPoint = _trackInfo.getCurrentPoint();
221 Selection selection = _trackInfo.getSelection();
222 int currentPointIndex = selection.getCurrentPointIndex();
223 _speedLabel.setText("");
224 int distUnits = _distUnitsDropdown.getSelectedIndex()==0?Distance.UNITS_KILOMETRES:Distance.UNITS_MILES;
225 String distUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kilometres.short":"units.miles.short");
226 String speedUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kmh":"units.mph");
227 if (_track == null || currentPoint == null)
229 _indexLabel.setText(I18nManager.getText("details.nopointselection"));
230 _latLabel.setText("");
231 _longLabel.setText("");
232 _altLabel.setText("");
233 _timeLabel.setText("");
234 _nameLabel.setText("");
238 _indexLabel.setText(LABEL_POINT_SELECTED
239 + (currentPointIndex+1) + " " + I18nManager.getText("details.index.of")
240 + " " + _track.getNumPoints());
241 _latLabel.setText(makeCoordinateLabel(LABEL_POINT_LATITUDE, currentPoint.getLatitude(), _coordFormatDropdown.getSelectedIndex()));
242 _longLabel.setText(makeCoordinateLabel(LABEL_POINT_LONGITUDE, currentPoint.getLongitude(), _coordFormatDropdown.getSelectedIndex()));
243 _altLabel.setText(currentPoint.hasAltitude()?
244 (LABEL_POINT_ALTITUDE + currentPoint.getAltitude().getValue() + getAltitudeUnitsLabel(currentPoint.getAltitude().getFormat()))
246 if (currentPoint.getTimestamp().isValid())
248 if (currentPointIndex > 0 && currentPointIndex < (_trackInfo.getTrack().getNumPoints()-1)) {
249 DataPoint prevPoint = _trackInfo.getTrack().getPoint(currentPointIndex - 1);
250 DataPoint nextPoint = _trackInfo.getTrack().getPoint(currentPointIndex + 1);
251 if (prevPoint.getTimestamp().isValid() && nextPoint.getTimestamp().isValid())
253 // use total distance and total time between neighbouring points
254 long diff = nextPoint.getTimestamp().getSecondsSince(prevPoint.getTimestamp());
256 double rads = DataPoint.calculateRadiansBetween(prevPoint, currentPoint) +
257 DataPoint.calculateRadiansBetween(currentPoint, nextPoint);
258 double dist = Distance.convertRadiansToDistance(rads, distUnits);
259 String speed = roundedNumber(3600 * dist / diff) + " " + speedUnitsStr;
260 _speedLabel.setText(I18nManager.getText("details.speed") + ": " + speed);
264 _timeLabel.setText(LABEL_POINT_TIMESTAMP + currentPoint.getTimestamp().getText());
267 _timeLabel.setText("");
269 String name = currentPoint.getWaypointName();
270 if (name != null && !name.equals(""))
272 _nameLabel.setText(LABEL_POINT_WAYPOINTNAME + name);
274 else _nameLabel.setText("");
277 // Update range details
278 if (_track == null || !selection.hasRangeSelected())
280 _rangeLabel.setText(I18nManager.getText("details.norangeselection"));
281 _distanceLabel.setText("");
282 _movingDistanceLabel.setText("");
283 _durationLabel.setText("");
284 _altRangeLabel.setText("");
285 _updownLabel.setText("");
286 _aveSpeedLabel.setText("");
287 _aveMovingSpeedLabel.setText("");
291 _rangeLabel.setText(LABEL_RANGE_SELECTED
292 + (selection.getStart()+1) + " " + I18nManager.getText("details.range.to")
293 + " " + (selection.getEnd()+1));
294 _distanceLabel.setText(LABEL_RANGE_DISTANCE + roundedNumber(selection.getDistance(distUnits)) + " " + distUnitsStr);
295 _movingDistanceLabel.setText(LABEL_RANGE_MOVINGDISTANCE + roundedNumber(selection.getMovingDistance(distUnits)) + " " + distUnitsStr);
296 if (selection.getNumSeconds() > 0)
298 _durationLabel.setText(LABEL_RANGE_DURATION + buildDurationString(selection.getNumSeconds()));
299 _aveSpeedLabel.setText(I18nManager.getText("details.range.avespeed") + ": "
300 + roundedNumber(selection.getDistance(distUnits)/selection.getNumSeconds()*3600.0) + " " + speedUnitsStr);
301 _aveMovingSpeedLabel.setText(I18nManager.getText("details.range.avemovingspeed") + ": "
302 + roundedNumber(selection.getMovingDistance(distUnits)/selection.getMovingSeconds()*3600.0) + " " + speedUnitsStr);
305 _durationLabel.setText("");
306 _aveSpeedLabel.setText("");
307 _aveMovingSpeedLabel.setText("");
309 String altUnitsLabel = getAltitudeUnitsLabel(selection.getAltitudeFormat());
310 IntegerRange altRange = selection.getAltitudeRange();
311 if (altRange.getMinimum() >= 0 && altRange.getMaximum() >= 0)
313 _altRangeLabel.setText(LABEL_RANGE_ALTITUDE
314 + altRange.getMinimum() + altUnitsLabel + " "
315 + I18nManager.getText("details.altitude.to") + " "
316 + altRange.getMaximum() + altUnitsLabel);
317 _updownLabel.setText(LABEL_RANGE_CLIMB + selection.getClimb() + altUnitsLabel
318 + LABEL_RANGE_DESCENT + selection.getDescent() + altUnitsLabel);
322 _altRangeLabel.setText("");
323 _updownLabel.setText("");
326 // show photo details and thumbnail
327 Photo currentPhoto = _trackInfo.getPhotoList().getPhoto(_trackInfo.getSelection().getCurrentPhotoIndex());
328 if (_track == null || ( (currentPoint == null || currentPoint.getPhoto() == null) && currentPhoto == null))
330 // no photo, hide details
331 _photoLabel.setText(I18nManager.getText("details.nophoto"));
332 _photoTimestampLabel.setText("");
333 _photoConnectedLabel.setText("");
334 _photoThumbnail.setVisible(false);
338 if (currentPhoto == null) {currentPhoto = currentPoint.getPhoto();}
339 _photoLabel.setText(I18nManager.getText("details.photofile") + ": " + currentPhoto.getFile().getName());
340 _photoLabel.setText(LABEL_POINT_TIMESTAMP + currentPhoto.getTimestamp().getText());
341 _photoConnectedLabel.setText(I18nManager.getText("details.photo.connected") + ": "
342 + (currentPhoto.getCurrentStatus() == PhotoStatus.NOT_CONNECTED ?
343 I18nManager.getText("dialog.about.no"):I18nManager.getText("dialog.about.yes")));
344 _photoThumbnail.setVisible(true);
345 _photoThumbnail.setPhoto(currentPhoto);
347 _photoThumbnail.repaint();
352 * Choose the appropriate altitude units label for the specified format
353 * @param inFormat altitude format
354 * @return language-sensitive string
356 private static String getAltitudeUnitsLabel(int inFormat)
358 if (inFormat == LABEL_POINT_ALTITUDE_FORMAT && LABEL_POINT_ALTITUDE_UNITS != null)
359 return LABEL_POINT_ALTITUDE_UNITS;
360 LABEL_POINT_ALTITUDE_FORMAT = inFormat;
361 if (inFormat == Altitude.FORMAT_METRES)
362 return " " + I18nManager.getText("units.metres.short");
363 return " " + I18nManager.getText("units.feet.short");
368 * Construct an appropriate coordinate label using the selected format
369 * @param inPrefix prefix of label
370 * @param inCoordinate coordinate
371 * @param inFormat index of format selection dropdown
372 * @return language-sensitive string
374 private static String makeCoordinateLabel(String inPrefix, Coordinate inCoordinate, int inFormat)
379 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN_SEC); break;
381 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN); break;
383 coord = inCoordinate.output(Coordinate.FORMAT_DEG); break;
384 default: // just as it was
385 coord = inCoordinate.output(Coordinate.FORMAT_NONE);
387 return inPrefix + coord;
392 * Build a String to describe a time duration
393 * @param inNumSecs number of seconds
394 * @return time as a string, days, hours, mins, secs as appropriate
396 private static String buildDurationString(long inNumSecs)
398 if (inNumSecs <= 0L) return "";
399 if (inNumSecs < 60L) return "" + inNumSecs + I18nManager.getText("display.range.time.secs");
400 if (inNumSecs < 3600L) return "" + (inNumSecs / 60) + I18nManager.getText("display.range.time.mins")
401 + " " + (inNumSecs % 60) + I18nManager.getText("display.range.time.secs");
402 if (inNumSecs < 86400L) return "" + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours")
403 + " " + ((inNumSecs / 60) % 60) + I18nManager.getText("display.range.time.mins");
404 if (inNumSecs < 432000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days")
405 + " " + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours");
406 if (inNumSecs < 8640000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days");
412 * Format a number to a sensible precision
413 * @param inDist distance
414 * @return formatted String
416 private String roundedNumber(double inDist)
418 // Set precision of formatter
422 else if (inDist < 10.0)
424 else if (inDist < 100.0)
427 _distanceFormatter.setMaximumFractionDigits(numDigits);
428 _distanceFormatter.setMinimumFractionDigits(numDigits);
429 return _distanceFormatter.format(inDist);