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;
18 import tim.prune.DataSubscriber;
19 import tim.prune.I18nManager;
20 import tim.prune.data.Altitude;
21 import tim.prune.data.Coordinate;
22 import tim.prune.data.DataPoint;
23 import tim.prune.data.Distance;
24 import tim.prune.data.IntegerRange;
25 import tim.prune.data.Photo;
26 import tim.prune.data.PhotoStatus;
27 import tim.prune.data.Selection;
28 import tim.prune.data.TrackInfo;
31 * Class to hold point details and selection details
32 * as a visual component
34 public class DetailsDisplay extends GenericDisplay
37 private JLabel _indexLabel = null;
38 private JLabel _latLabel = null, _longLabel = null;
39 private JLabel _altLabel = null, _nameLabel = null;
40 private JLabel _timeLabel = null, _speedLabel = null;
43 private JLabel _rangeLabel = null;
44 private JLabel _distanceLabel = null, _durationLabel = null;
45 private JLabel _altRangeLabel = null, _updownLabel = null;
46 private JLabel _aveSpeedLabel = null;
49 private JLabel _photoLabel = null;
50 private PhotoThumbnail _photoThumbnail = null;
51 private JLabel _photoConnectedLabel = null;
54 private JComboBox _coordFormatDropdown = null;
55 private JComboBox _distUnitsDropdown = null;
57 private NumberFormat _distanceFormatter = NumberFormat.getInstance();
60 private static final String LABEL_POINT_SELECTED1 = I18nManager.getText("details.index.selected") + ": ";
61 private static final String LABEL_POINT_LATITUDE = I18nManager.getText("fieldname.latitude") + ": ";
62 private static final String LABEL_POINT_LONGITUDE = I18nManager.getText("fieldname.longitude") + ": ";
63 private static final String LABEL_POINT_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
64 private static final String LABEL_POINT_TIMESTAMP = I18nManager.getText("fieldname.timestamp") + ": ";
65 private static final String LABEL_POINT_WAYPOINTNAME = I18nManager.getText("fieldname.waypointname") + ": ";
66 private static final String LABEL_RANGE_SELECTED1 = I18nManager.getText("details.range.selected") + ": ";
67 private static final String LABEL_RANGE_DURATION = I18nManager.getText("fieldname.duration") + ": ";
68 private static final String LABEL_RANGE_DISTANCE = I18nManager.getText("fieldname.distance") + ": ";
69 private static final String LABEL_RANGE_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
70 private static final String LABEL_RANGE_CLIMB = I18nManager.getText("details.range.climb") + ": ";
71 private static final String LABEL_RANGE_DESCENT = ", " + I18nManager.getText("details.range.descent") + ": ";
72 private static String LABEL_POINT_ALTITUDE_UNITS = null;
73 private static int LABEL_POINT_ALTITUDE_FORMAT = Altitude.FORMAT_NONE;
78 * @param inTrackInfo Track info object
80 public DetailsDisplay(TrackInfo inTrackInfo)
83 setLayout(new BorderLayout());
85 JPanel mainPanel = new JPanel();
86 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
87 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
89 // Point details panel
90 JPanel pointDetailsPanel = new JPanel();
91 pointDetailsPanel.setLayout(new BoxLayout(pointDetailsPanel, BoxLayout.Y_AXIS));
92 pointDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
93 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
95 JLabel pointDetailsLabel = new JLabel(I18nManager.getText("details.pointdetails"));
96 Font biggerFont = pointDetailsLabel.getFont();
97 biggerFont = biggerFont.deriveFont(Font.BOLD, biggerFont.getSize2D() + 2.0f);
98 pointDetailsLabel.setFont(biggerFont);
99 pointDetailsPanel.add(pointDetailsLabel);
100 _indexLabel = new JLabel(I18nManager.getText("details.nopointselection"));
101 pointDetailsPanel.add(_indexLabel);
102 _latLabel = new JLabel("");
103 pointDetailsPanel.add(_latLabel);
104 _longLabel = new JLabel("");
105 pointDetailsPanel.add(_longLabel);
106 _altLabel = new JLabel("");
107 pointDetailsPanel.add(_altLabel);
108 _timeLabel = new JLabel("");
109 pointDetailsPanel.add(_timeLabel);
110 _speedLabel = new JLabel("");
111 pointDetailsPanel.add(_speedLabel);
112 _nameLabel = new JLabel("");
113 pointDetailsPanel.add(_nameLabel);
114 pointDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
116 // range details panel
117 JPanel rangeDetailsPanel = new JPanel();
118 rangeDetailsPanel.setLayout(new BoxLayout(rangeDetailsPanel, BoxLayout.Y_AXIS));
119 rangeDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
120 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
122 JLabel rangeDetailsLabel = new JLabel(I18nManager.getText("details.rangedetails"));
123 rangeDetailsLabel.setFont(biggerFont);
124 rangeDetailsPanel.add(rangeDetailsLabel);
125 _rangeLabel = new JLabel(I18nManager.getText("details.norangeselection"));
126 rangeDetailsPanel.add(_rangeLabel);
127 _distanceLabel = new JLabel("");
128 rangeDetailsPanel.add(_distanceLabel);
129 _durationLabel = new JLabel("");
130 rangeDetailsPanel.add(_durationLabel);
131 _aveSpeedLabel = new JLabel("");
132 rangeDetailsPanel.add(_aveSpeedLabel);
133 _altRangeLabel = new JLabel("");
134 rangeDetailsPanel.add(_altRangeLabel);
135 _updownLabel = new JLabel("");
136 rangeDetailsPanel.add(_updownLabel);
137 rangeDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
139 // photo details panel
140 JPanel photoDetailsPanel = new JPanel();
141 photoDetailsPanel.setLayout(new BoxLayout(photoDetailsPanel, BoxLayout.Y_AXIS));
142 photoDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
143 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
145 JLabel photoDetailsLabel = new JLabel(I18nManager.getText("details.photodetails"));
146 photoDetailsLabel.setFont(biggerFont);
147 photoDetailsPanel.add(photoDetailsLabel);
148 _photoLabel = new JLabel(I18nManager.getText("details.nophoto"));
149 photoDetailsPanel.add(_photoLabel);
150 _photoConnectedLabel = new JLabel("");
151 photoDetailsPanel.add(_photoConnectedLabel);
152 _photoThumbnail = new PhotoThumbnail();
153 _photoThumbnail.setVisible(false);
154 _photoThumbnail.setPreferredSize(new Dimension(100, 100));
155 photoDetailsPanel.add(_photoThumbnail);
157 // add the details panels to the main panel
158 mainPanel.add(pointDetailsPanel);
159 mainPanel.add(Box.createVerticalStrut(5));
160 mainPanel.add(rangeDetailsPanel);
161 mainPanel.add(Box.createVerticalStrut(5));
162 mainPanel.add(photoDetailsPanel);
163 mainPanel.add(Box.createVerticalStrut(5));
164 // add the main panel at the top
165 add(mainPanel, BorderLayout.NORTH);
167 // Add format, units selection
168 JPanel lowerPanel = new JPanel();
169 lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.Y_AXIS));
170 JLabel coordFormatLabel = new JLabel(I18nManager.getText("details.coordformat") + ": ");
171 coordFormatLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
172 lowerPanel.add(coordFormatLabel);
173 String[] coordFormats = {I18nManager.getText("units.original"), I18nManager.getText("units.degminsec"),
174 I18nManager.getText("units.degmin"), I18nManager.getText("units.deg")};
175 _coordFormatDropdown = new JComboBox(coordFormats);
176 _coordFormatDropdown.addActionListener(new ActionListener() {
177 public void actionPerformed(ActionEvent e)
179 dataUpdated(DataSubscriber.UNITS_CHANGED);
182 lowerPanel.add(_coordFormatDropdown);
183 _coordFormatDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
184 JLabel unitsLabel = new JLabel(I18nManager.getText("details.distanceunits") + ": ");
185 unitsLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
186 lowerPanel.add(unitsLabel);
187 String[] distUnits = {I18nManager.getText("units.kilometres"), I18nManager.getText("units.miles")};
188 _distUnitsDropdown = new JComboBox(distUnits);
189 _distUnitsDropdown.addActionListener(new ActionListener() {
190 public void actionPerformed(ActionEvent e)
192 dataUpdated(DataSubscriber.UNITS_CHANGED);
195 lowerPanel.add(_distUnitsDropdown);
196 _distUnitsDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
197 add(lowerPanel, BorderLayout.SOUTH);
202 * Notification that Track has been updated
203 * @param inUpdateType byte to specify what has been updated
205 public void dataUpdated(byte inUpdateType)
207 // Update current point data, if any
208 DataPoint currentPoint = _trackInfo.getCurrentPoint();
209 Selection selection = _trackInfo.getSelection();
210 int currentPointIndex = selection.getCurrentPointIndex();
211 _speedLabel.setText("");
212 int distUnits = _distUnitsDropdown.getSelectedIndex()==0?Distance.UNITS_KILOMETRES:Distance.UNITS_MILES;
213 String distUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kilometres.short":"units.miles.short");
214 String speedUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kmh":"units.mph");
215 if (_track == null || currentPoint == null)
217 _indexLabel.setText(I18nManager.getText("details.nopointselection"));
218 _latLabel.setText("");
219 _longLabel.setText("");
220 _altLabel.setText("");
221 _timeLabel.setText("");
222 _nameLabel.setText("");
226 _indexLabel.setText(LABEL_POINT_SELECTED1
227 + (currentPointIndex+1) + " " + I18nManager.getText("details.index.of")
228 + " " + _track.getNumPoints());
229 _latLabel.setText(makeCoordinateLabel(LABEL_POINT_LATITUDE, currentPoint.getLatitude(), _coordFormatDropdown.getSelectedIndex()));
230 _longLabel.setText(makeCoordinateLabel(LABEL_POINT_LONGITUDE, currentPoint.getLongitude(), _coordFormatDropdown.getSelectedIndex()));
231 _altLabel.setText(LABEL_POINT_ALTITUDE
232 + (currentPoint.hasAltitude()?
233 (currentPoint.getAltitude().getValue() + getAltitudeUnitsLabel(currentPoint.getAltitude().getFormat())):
235 if (currentPoint.getTimestamp().isValid())
237 if (currentPointIndex > 0 && currentPointIndex < (_trackInfo.getTrack().getNumPoints()-1)) {
238 DataPoint prevPoint = _trackInfo.getTrack().getPoint(currentPointIndex - 1);
239 DataPoint nextPoint = _trackInfo.getTrack().getPoint(currentPointIndex + 1);
240 if (prevPoint.getTimestamp().isValid() && nextPoint.getTimestamp().isValid()) {
241 // use total distance and total time between neighbouring points
242 long diff = nextPoint.getTimestamp().getSecondsSince(prevPoint.getTimestamp());
244 double rads = DataPoint.calculateRadiansBetween(prevPoint, currentPoint) +
245 DataPoint.calculateRadiansBetween(currentPoint, nextPoint);
246 double dist = Distance.convertRadiansToDistance(rads, distUnits);
247 String speed = roundedNumber(3600 * dist / diff) + " " + speedUnitsStr;
248 _speedLabel.setText(I18nManager.getText("details.speed") + ": " + speed);
252 _timeLabel.setText(LABEL_POINT_TIMESTAMP + currentPoint.getTimestamp().getText());
255 _timeLabel.setText("");
257 String name = currentPoint.getWaypointName();
258 if (name != null && !name.equals(""))
260 _nameLabel.setText(LABEL_POINT_WAYPOINTNAME + name);
262 else _nameLabel.setText("");
265 // Update range details
266 if (_track == null || !selection.hasRangeSelected())
268 _rangeLabel.setText(I18nManager.getText("details.norangeselection"));
269 _distanceLabel.setText("");
270 _durationLabel.setText("");
271 _altRangeLabel.setText("");
272 _updownLabel.setText("");
276 _rangeLabel.setText(LABEL_RANGE_SELECTED1
277 + (selection.getStart()+1) + " " + I18nManager.getText("details.range.to")
278 + " " + (selection.getEnd()+1));
279 _distanceLabel.setText(LABEL_RANGE_DISTANCE + roundedNumber(selection.getDistance(distUnits)) + " " + distUnitsStr);
280 if (selection.getNumSeconds() > 0) {
281 _durationLabel.setText(LABEL_RANGE_DURATION + buildDurationString(selection.getNumSeconds()));
282 _aveSpeedLabel.setText(I18nManager.getText("details.range.avespeed") + ": "
283 + roundedNumber(selection.getDistance(distUnits)/selection.getNumSeconds()*3600.0) + " " + speedUnitsStr);
286 _durationLabel.setText("");
287 _aveSpeedLabel.setText("");
289 String altUnitsLabel = getAltitudeUnitsLabel(selection.getAltitudeFormat());
290 IntegerRange altRange = selection.getAltitudeRange();
291 if (altRange.getMinimum() >= 0 && altRange.getMaximum() >= 0)
293 _altRangeLabel.setText(LABEL_RANGE_ALTITUDE
294 + altRange.getMinimum() + altUnitsLabel + " "
295 + I18nManager.getText("details.altitude.to") + " "
296 + altRange.getMaximum() + altUnitsLabel);
297 _updownLabel.setText(LABEL_RANGE_CLIMB + selection.getClimb() + altUnitsLabel
298 + LABEL_RANGE_DESCENT + selection.getDescent() + altUnitsLabel);
302 _altRangeLabel.setText("");
303 _updownLabel.setText("");
306 // show photo details and thumbnail
307 Photo currentPhoto = _trackInfo.getPhotoList().getPhoto(_trackInfo.getSelection().getCurrentPhotoIndex());
308 if (_track == null || ( (currentPoint == null || currentPoint.getPhoto() == null) && currentPhoto == null))
310 // no photo, hide details
311 _photoLabel.setText(I18nManager.getText("details.nophoto"));
312 _photoConnectedLabel.setText("");
313 _photoThumbnail.setVisible(false);
317 if (currentPhoto == null) {currentPhoto = currentPoint.getPhoto();}
318 _photoLabel.setText(I18nManager.getText("details.photofile") + ": " + currentPhoto.getFile().getName());
319 _photoConnectedLabel.setText(I18nManager.getText("details.photo.connected") + ": "
320 + (currentPhoto.getCurrentStatus() == PhotoStatus.NOT_CONNECTED ?
321 I18nManager.getText("dialog.about.no"):I18nManager.getText("dialog.about.yes")));
322 _photoThumbnail.setVisible(true);
323 _photoThumbnail.setPhoto(currentPhoto);
325 _photoThumbnail.repaint();
330 * Choose the appropriate altitude units label for the specified format
331 * @param inFormat altitude format
332 * @return language-sensitive string
334 private static String getAltitudeUnitsLabel(int inFormat)
336 if (inFormat == LABEL_POINT_ALTITUDE_FORMAT && LABEL_POINT_ALTITUDE_UNITS != null)
337 return LABEL_POINT_ALTITUDE_UNITS;
338 LABEL_POINT_ALTITUDE_FORMAT = inFormat;
339 if (inFormat == Altitude.FORMAT_METRES)
340 return " " + I18nManager.getText("units.metres.short");
341 return " " + I18nManager.getText("units.feet.short");
346 * Construct an appropriate coordinate label using the selected format
347 * @param inPrefix prefix of label
348 * @param inCoordinate coordinate
349 * @param inFormat index of format selection dropdown
350 * @return language-sensitive string
352 private static String makeCoordinateLabel(String inPrefix, Coordinate inCoordinate, int inFormat)
357 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN_SEC); break;
359 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN); break;
361 coord = inCoordinate.output(Coordinate.FORMAT_DEG); break;
362 default: // just as it was
363 coord = inCoordinate.output(Coordinate.FORMAT_NONE);
365 return inPrefix + coord;
370 * Build a String to describe a time duration
371 * @param inNumSecs number of seconds
372 * @return time as a string, days, hours, mins, secs as appropriate
374 private static String buildDurationString(long inNumSecs)
376 if (inNumSecs <= 0L) return "";
377 if (inNumSecs < 60L) return "" + inNumSecs + I18nManager.getText("display.range.time.secs");
378 if (inNumSecs < 3600L) return "" + (inNumSecs / 60) + I18nManager.getText("display.range.time.mins")
379 + " " + (inNumSecs % 60) + I18nManager.getText("display.range.time.secs");
380 if (inNumSecs < 86400L) return "" + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours")
381 + " " + ((inNumSecs / 60) % 60) + I18nManager.getText("display.range.time.mins");
382 if (inNumSecs < 432000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days")
383 + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours");
384 if (inNumSecs < 8640000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days");
390 * Format a number to a sensible precision
391 * @param inDist distance
392 * @return formatted String
394 private String roundedNumber(double inDist)
396 // Set precision of formatter
400 else if (inDist < 10.0)
402 else if (inDist < 100.0)
405 _distanceFormatter.setMaximumFractionDigits(numDigits);
406 _distanceFormatter.setMinimumFractionDigits(numDigits);
407 return _distanceFormatter.format(inDist);