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.Selection;
29 import tim.prune.data.TrackInfo;
32 * Class to hold point details and selection details
33 * as a visual component
35 public class DetailsDisplay extends GenericDisplay
38 private JLabel _indexLabel = null;
39 private JLabel _latLabel = null, _longLabel = null;
40 private JLabel _altLabel = null, _nameLabel = null;
41 private JLabel _timeLabel = null, _speedLabel = null;
44 private JLabel _rangeLabel = null;
45 private JLabel _distanceLabel = null, _movingDistanceLabel = null;
46 private JLabel _durationLabel = null;
47 private JLabel _altRangeLabel = null, _updownLabel = null;
48 private JLabel _aveSpeedLabel = null, _aveMovingSpeedLabel = null;
51 private JLabel _photoLabel = null;
52 private PhotoThumbnail _photoThumbnail = null;
53 private JLabel _photoTimestampLabel = null;
54 private JLabel _photoConnectedLabel = null;
57 private JComboBox _coordFormatDropdown = null;
58 private JComboBox _distUnitsDropdown = null;
60 private NumberFormat _distanceFormatter = NumberFormat.getInstance();
63 private static final String LABEL_POINT_SELECTED = I18nManager.getText("details.index.selected") + ": ";
64 private static final String LABEL_POINT_LATITUDE = I18nManager.getText("fieldname.latitude") + ": ";
65 private static final String LABEL_POINT_LONGITUDE = I18nManager.getText("fieldname.longitude") + ": ";
66 private static final String LABEL_POINT_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
67 private static final String LABEL_POINT_TIMESTAMP = I18nManager.getText("fieldname.timestamp") + ": ";
68 private static final String LABEL_POINT_WAYPOINTNAME = I18nManager.getText("fieldname.waypointname") + ": ";
69 private static final String LABEL_RANGE_SELECTED = I18nManager.getText("details.range.selected") + ": ";
70 private static final String LABEL_RANGE_DURATION = I18nManager.getText("fieldname.duration") + ": ";
71 private static final String LABEL_RANGE_DISTANCE = I18nManager.getText("fieldname.distance") + ": ";
72 private static final String LABEL_RANGE_MOVINGDISTANCE = I18nManager.getText("fieldname.movingdistance") + ": ";
73 private static final String LABEL_RANGE_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
74 private static final String LABEL_RANGE_CLIMB = I18nManager.getText("details.range.climb") + ": ";
75 private static final String LABEL_RANGE_DESCENT = ", " + I18nManager.getText("details.range.descent") + ": ";
76 private static String LABEL_POINT_ALTITUDE_UNITS = null;
77 private static Altitude.Format LABEL_POINT_ALTITUDE_FORMAT = Altitude.Format.NO_FORMAT;
82 * @param inTrackInfo Track info object
84 public DetailsDisplay(TrackInfo inTrackInfo)
87 setLayout(new BorderLayout());
89 JPanel mainPanel = new JPanel();
90 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
91 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
93 // Point details panel
94 JPanel pointDetailsPanel = new JPanel();
95 pointDetailsPanel.setLayout(new BoxLayout(pointDetailsPanel, BoxLayout.Y_AXIS));
96 pointDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
97 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
99 JLabel pointDetailsLabel = new JLabel(I18nManager.getText("details.pointdetails"));
100 Font biggerFont = pointDetailsLabel.getFont();
101 biggerFont = biggerFont.deriveFont(Font.BOLD, biggerFont.getSize2D() + 2.0f);
102 pointDetailsLabel.setFont(biggerFont);
103 pointDetailsPanel.add(pointDetailsLabel);
104 _indexLabel = new JLabel(I18nManager.getText("details.nopointselection"));
105 pointDetailsPanel.add(_indexLabel);
106 _latLabel = new JLabel("");
107 pointDetailsPanel.add(_latLabel);
108 _longLabel = new JLabel("");
109 pointDetailsPanel.add(_longLabel);
110 _altLabel = new JLabel("");
111 pointDetailsPanel.add(_altLabel);
112 _timeLabel = new JLabel("");
113 pointDetailsPanel.add(_timeLabel);
114 _speedLabel = new JLabel("");
115 pointDetailsPanel.add(_speedLabel);
116 _nameLabel = new JLabel("");
117 pointDetailsPanel.add(_nameLabel);
118 pointDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
120 // range details panel
121 JPanel rangeDetailsPanel = new JPanel();
122 rangeDetailsPanel.setLayout(new BoxLayout(rangeDetailsPanel, BoxLayout.Y_AXIS));
123 rangeDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
124 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
126 JLabel rangeDetailsLabel = new JLabel(I18nManager.getText("details.rangedetails"));
127 rangeDetailsLabel.setFont(biggerFont);
128 rangeDetailsPanel.add(rangeDetailsLabel);
129 _rangeLabel = new JLabel(I18nManager.getText("details.norangeselection"));
130 rangeDetailsPanel.add(_rangeLabel);
131 _distanceLabel = new JLabel("");
132 rangeDetailsPanel.add(_distanceLabel);
133 _movingDistanceLabel = new JLabel("");
134 rangeDetailsPanel.add(_movingDistanceLabel);
135 _durationLabel = new JLabel("");
136 rangeDetailsPanel.add(_durationLabel);
137 _aveSpeedLabel = new JLabel("");
138 rangeDetailsPanel.add(_aveSpeedLabel);
139 _aveMovingSpeedLabel = new JLabel("");
140 rangeDetailsPanel.add(_aveMovingSpeedLabel);
141 _altRangeLabel = new JLabel("");
142 rangeDetailsPanel.add(_altRangeLabel);
143 _updownLabel = new JLabel("");
144 rangeDetailsPanel.add(_updownLabel);
145 rangeDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
147 // photo details panel
148 JPanel photoDetailsPanel = new JPanel();
149 photoDetailsPanel.setLayout(new BoxLayout(photoDetailsPanel, BoxLayout.Y_AXIS));
150 photoDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
151 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
153 JLabel photoDetailsLabel = new JLabel(I18nManager.getText("details.photodetails"));
154 photoDetailsLabel.setFont(biggerFont);
155 photoDetailsPanel.add(photoDetailsLabel);
156 _photoLabel = new JLabel(I18nManager.getText("details.nophoto"));
157 photoDetailsPanel.add(_photoLabel);
158 _photoTimestampLabel = new JLabel("");
159 photoDetailsPanel.add(_photoTimestampLabel);
160 _photoConnectedLabel = new JLabel("");
161 photoDetailsPanel.add(_photoConnectedLabel);
162 _photoThumbnail = new PhotoThumbnail();
163 _photoThumbnail.setVisible(false);
164 _photoThumbnail.setPreferredSize(new Dimension(100, 100));
165 photoDetailsPanel.add(_photoThumbnail);
167 // add the details panels to the main panel
168 mainPanel.add(pointDetailsPanel);
169 mainPanel.add(Box.createVerticalStrut(5));
170 mainPanel.add(rangeDetailsPanel);
171 mainPanel.add(Box.createVerticalStrut(5));
172 mainPanel.add(photoDetailsPanel);
173 mainPanel.add(Box.createVerticalStrut(5));
174 // add the main panel at the top
175 add(mainPanel, BorderLayout.NORTH);
177 // Add format, units selection
178 JPanel lowerPanel = new JPanel();
179 lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.Y_AXIS));
180 JLabel coordFormatLabel = new JLabel(I18nManager.getText("details.coordformat") + ": ");
181 coordFormatLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
182 lowerPanel.add(coordFormatLabel);
183 String[] coordFormats = {I18nManager.getText("units.original"), I18nManager.getText("units.degminsec"),
184 I18nManager.getText("units.degmin"), I18nManager.getText("units.deg")};
185 _coordFormatDropdown = new JComboBox(coordFormats);
186 _coordFormatDropdown.addActionListener(new ActionListener() {
187 public void actionPerformed(ActionEvent e)
189 dataUpdated(DataSubscriber.UNITS_CHANGED);
192 lowerPanel.add(_coordFormatDropdown);
193 _coordFormatDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
194 JLabel unitsLabel = new JLabel(I18nManager.getText("details.distanceunits") + ": ");
195 unitsLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
196 lowerPanel.add(unitsLabel);
197 String[] distUnits = {I18nManager.getText("units.kilometres"), I18nManager.getText("units.miles")};
198 _distUnitsDropdown = new JComboBox(distUnits);
199 if (!Config.getUseMetricUnits()) {_distUnitsDropdown.setSelectedIndex(1);}
200 _distUnitsDropdown.addActionListener(new ActionListener() {
201 public void actionPerformed(ActionEvent e)
203 dataUpdated(DataSubscriber.UNITS_CHANGED);
204 Config.setUseMetricUnits(_distUnitsDropdown.getSelectedIndex() == 0);
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 Distance.Units 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());
255 if (diff < 1000 && diff > 0)
257 double rads = DataPoint.calculateRadiansBetween(prevPoint, currentPoint) +
258 DataPoint.calculateRadiansBetween(currentPoint, nextPoint);
259 double dist = Distance.convertRadiansToDistance(rads, distUnits);
260 String speed = roundedNumber(3600 * dist / diff) + " " + speedUnitsStr;
261 _speedLabel.setText(I18nManager.getText("fieldname.speed") + ": " + speed);
265 _timeLabel.setText(LABEL_POINT_TIMESTAMP + currentPoint.getTimestamp().getText());
268 _timeLabel.setText("");
270 String name = currentPoint.getWaypointName();
271 if (name != null && !name.equals(""))
273 _nameLabel.setText(LABEL_POINT_WAYPOINTNAME + name);
275 else _nameLabel.setText("");
278 // Update range details
279 if (_track == null || !selection.hasRangeSelected())
281 _rangeLabel.setText(I18nManager.getText("details.norangeselection"));
282 _distanceLabel.setText("");
283 _movingDistanceLabel.setText("");
284 _durationLabel.setText("");
285 _altRangeLabel.setText("");
286 _updownLabel.setText("");
287 _aveSpeedLabel.setText("");
288 _aveMovingSpeedLabel.setText("");
292 _rangeLabel.setText(LABEL_RANGE_SELECTED
293 + (selection.getStart()+1) + " " + I18nManager.getText("details.range.to")
294 + " " + (selection.getEnd()+1));
295 _distanceLabel.setText(LABEL_RANGE_DISTANCE + roundedNumber(selection.getDistance(distUnits)) + " " + distUnitsStr);
296 _movingDistanceLabel.setText(LABEL_RANGE_MOVINGDISTANCE + roundedNumber(selection.getMovingDistance(distUnits)) + " " + distUnitsStr);
297 if (selection.getNumSeconds() > 0)
299 _durationLabel.setText(LABEL_RANGE_DURATION + buildDurationString(selection.getNumSeconds()));
300 _aveSpeedLabel.setText(I18nManager.getText("details.range.avespeed") + ": "
301 + roundedNumber(selection.getDistance(distUnits)/selection.getNumSeconds()*3600.0) + " " + speedUnitsStr);
302 _aveMovingSpeedLabel.setText(I18nManager.getText("details.range.avemovingspeed") + ": "
303 + roundedNumber(selection.getMovingDistance(distUnits)/selection.getMovingSeconds()*3600.0) + " " + speedUnitsStr);
306 _durationLabel.setText("");
307 _aveSpeedLabel.setText("");
308 _aveMovingSpeedLabel.setText("");
310 String altUnitsLabel = getAltitudeUnitsLabel(selection.getAltitudeFormat());
311 IntegerRange altRange = selection.getAltitudeRange();
312 if (altRange.getMinimum() >= 0 && altRange.getMaximum() >= 0)
314 _altRangeLabel.setText(LABEL_RANGE_ALTITUDE
315 + altRange.getMinimum() + altUnitsLabel + " "
316 + I18nManager.getText("details.altitude.to") + " "
317 + altRange.getMaximum() + altUnitsLabel);
318 _updownLabel.setText(LABEL_RANGE_CLIMB + selection.getClimb() + altUnitsLabel
319 + LABEL_RANGE_DESCENT + selection.getDescent() + altUnitsLabel);
323 _altRangeLabel.setText("");
324 _updownLabel.setText("");
327 // show photo details and thumbnail
328 Photo currentPhoto = _trackInfo.getPhotoList().getPhoto(_trackInfo.getSelection().getCurrentPhotoIndex());
329 if (_track == null || ( (currentPoint == null || currentPoint.getPhoto() == null) && currentPhoto == null))
331 // no photo, hide details
332 _photoLabel.setText(I18nManager.getText("details.nophoto"));
333 _photoTimestampLabel.setText("");
334 _photoConnectedLabel.setText("");
335 _photoThumbnail.setVisible(false);
339 if (currentPhoto == null) {currentPhoto = currentPoint.getPhoto();}
340 _photoLabel.setText(I18nManager.getText("details.photofile") + ": " + currentPhoto.getFile().getName());
341 _photoLabel.setText(LABEL_POINT_TIMESTAMP + currentPhoto.getTimestamp().getText());
342 _photoConnectedLabel.setText(I18nManager.getText("details.photo.connected") + ": "
343 + (currentPhoto.getCurrentStatus() == Photo.Status.NOT_CONNECTED ?
344 I18nManager.getText("dialog.about.no"):I18nManager.getText("dialog.about.yes")));
345 _photoThumbnail.setVisible(true);
346 _photoThumbnail.setPhoto(currentPhoto);
348 _photoThumbnail.repaint();
353 * Choose the appropriate altitude units label for the specified format
354 * @param inFormat altitude format
355 * @return language-sensitive string
357 private static String getAltitudeUnitsLabel(Altitude.Format inFormat)
359 if (inFormat == LABEL_POINT_ALTITUDE_FORMAT && LABEL_POINT_ALTITUDE_UNITS != null)
360 return LABEL_POINT_ALTITUDE_UNITS;
361 LABEL_POINT_ALTITUDE_FORMAT = inFormat;
362 if (inFormat == Altitude.Format.METRES)
363 return " " + I18nManager.getText("units.metres.short");
364 return " " + I18nManager.getText("units.feet.short");
369 * Construct an appropriate coordinate label using the selected format
370 * @param inPrefix prefix of label
371 * @param inCoordinate coordinate
372 * @param inFormat index of format selection dropdown
373 * @return language-sensitive string
375 private static String makeCoordinateLabel(String inPrefix, Coordinate inCoordinate, int inFormat)
380 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN_SEC); break;
382 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN); break;
384 coord = inCoordinate.output(Coordinate.FORMAT_DEG); break;
385 default: // just as it was
386 coord = inCoordinate.output(Coordinate.FORMAT_NONE);
388 return inPrefix + coord;
393 * Build a String to describe a time duration
394 * @param inNumSecs number of seconds
395 * @return time as a string, days, hours, mins, secs as appropriate
397 private static String buildDurationString(long inNumSecs)
399 if (inNumSecs <= 0L) return "";
400 if (inNumSecs < 60L) return "" + inNumSecs + I18nManager.getText("display.range.time.secs");
401 if (inNumSecs < 3600L) return "" + (inNumSecs / 60) + I18nManager.getText("display.range.time.mins")
402 + " " + (inNumSecs % 60) + I18nManager.getText("display.range.time.secs");
403 if (inNumSecs < 86400L) return "" + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours")
404 + " " + ((inNumSecs / 60) % 60) + I18nManager.getText("display.range.time.mins");
405 if (inNumSecs < 432000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days")
406 + " " + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours");
407 if (inNumSecs < 8640000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days");
413 * Format a number to a sensible precision
414 * @param inDist distance
415 * @return formatted String
417 private String roundedNumber(double inDist)
419 // Set precision of formatter
423 else if (inDist < 10.0)
425 else if (inDist < 100.0)
428 _distanceFormatter.setMaximumFractionDigits(numDigits);
429 _distanceFormatter.setMinimumFractionDigits(numDigits);
430 return _distanceFormatter.format(inDist);