3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
7 import java.awt.Insets;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.text.NumberFormat;
12 import javax.swing.BorderFactory;
13 import javax.swing.Box;
14 import javax.swing.BoxLayout;
15 import javax.swing.JButton;
16 import javax.swing.JComboBox;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.border.EtchedBorder;
21 import tim.prune.DataSubscriber;
22 import tim.prune.FunctionLibrary;
23 import tim.prune.GenericFunction;
24 import tim.prune.I18nManager;
25 import tim.prune.UpdateMessageBroker;
26 import tim.prune.config.Config;
27 import tim.prune.data.Altitude;
28 import tim.prune.data.Coordinate;
29 import tim.prune.data.DataPoint;
30 import tim.prune.data.Distance;
31 import tim.prune.data.Field;
32 import tim.prune.data.IntegerRange;
33 import tim.prune.data.Photo;
34 import tim.prune.data.Selection;
35 import tim.prune.data.TrackInfo;
38 * Class to hold point details and selection details
39 * as a visual component
41 public class DetailsDisplay extends GenericDisplay
44 private JLabel _indexLabel = null;
45 private JLabel _latLabel = null, _longLabel = null;
46 private JLabel _altLabel = null;
47 private JLabel _timeLabel = null, _speedLabel = null;
48 private JLabel _nameLabel = null, _typeLabel = null;
51 private JLabel _rangeLabel = null;
52 private JLabel _distanceLabel = null;
53 private JLabel _durationLabel = null;
54 private JLabel _altRangeLabel = null, _updownLabel = null;
55 private JLabel _aveSpeedLabel = null;
58 private JLabel _photoLabel = null;
59 private PhotoThumbnail _photoThumbnail = null;
60 private JLabel _photoTimestampLabel = null;
61 private JLabel _photoConnectedLabel = null;
62 private JPanel _rotationButtons = null;
65 private JComboBox _coordFormatDropdown = null;
66 private JComboBox _distUnitsDropdown = null;
68 private NumberFormat _distanceFormatter = NumberFormat.getInstance();
71 private static final String LABEL_POINT_SELECTED = I18nManager.getText("details.index.selected") + ": ";
72 private static final String LABEL_POINT_LATITUDE = I18nManager.getText("fieldname.latitude") + ": ";
73 private static final String LABEL_POINT_LONGITUDE = I18nManager.getText("fieldname.longitude") + ": ";
74 private static final String LABEL_POINT_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
75 private static final String LABEL_POINT_TIMESTAMP = I18nManager.getText("fieldname.timestamp") + ": ";
76 private static final String LABEL_POINT_WAYPOINTNAME = I18nManager.getText("fieldname.waypointname") + ": ";
77 private static final String LABEL_POINT_WAYPOINTTYPE = I18nManager.getText("fieldname.waypointtype") + ": ";
78 private static final String LABEL_RANGE_SELECTED = I18nManager.getText("details.range.selected") + ": ";
79 private static final String LABEL_RANGE_DURATION = I18nManager.getText("fieldname.duration") + ": ";
80 private static final String LABEL_RANGE_DISTANCE = I18nManager.getText("fieldname.distance") + ": ";
81 private static final String LABEL_RANGE_ALTITUDE = I18nManager.getText("fieldname.altitude") + ": ";
82 private static final String LABEL_RANGE_CLIMB = I18nManager.getText("details.range.climb") + ": ";
83 private static final String LABEL_RANGE_DESCENT = ", " + I18nManager.getText("details.range.descent") + ": ";
84 private static String LABEL_POINT_ALTITUDE_UNITS = null;
85 private static Altitude.Format LABEL_POINT_ALTITUDE_FORMAT = Altitude.Format.NO_FORMAT;
90 * @param inTrackInfo Track info object
92 public DetailsDisplay(TrackInfo inTrackInfo)
95 setLayout(new BorderLayout());
97 JPanel mainPanel = new JPanel();
98 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
99 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
101 // Point details panel
102 JPanel pointDetailsPanel = new JPanel();
103 pointDetailsPanel.setLayout(new BoxLayout(pointDetailsPanel, BoxLayout.Y_AXIS));
104 pointDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
105 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
107 JLabel pointDetailsLabel = new JLabel(I18nManager.getText("details.pointdetails"));
108 Font biggerFont = pointDetailsLabel.getFont();
109 biggerFont = biggerFont.deriveFont(Font.BOLD, biggerFont.getSize2D() + 2.0f);
110 pointDetailsLabel.setFont(biggerFont);
111 pointDetailsPanel.add(pointDetailsLabel);
112 _indexLabel = new JLabel(I18nManager.getText("details.nopointselection"));
113 pointDetailsPanel.add(_indexLabel);
114 _latLabel = new JLabel("");
115 pointDetailsPanel.add(_latLabel);
116 _longLabel = new JLabel("");
117 pointDetailsPanel.add(_longLabel);
118 _altLabel = new JLabel("");
119 pointDetailsPanel.add(_altLabel);
120 _timeLabel = new JLabel("");
121 pointDetailsPanel.add(_timeLabel);
122 _speedLabel = new JLabel("");
123 pointDetailsPanel.add(_speedLabel);
124 _nameLabel = new JLabel("");
125 pointDetailsPanel.add(_nameLabel);
126 _typeLabel = new JLabel("");
127 pointDetailsPanel.add(_typeLabel);
128 pointDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
130 // range details panel
131 JPanel rangeDetailsPanel = new JPanel();
132 rangeDetailsPanel.setLayout(new BoxLayout(rangeDetailsPanel, BoxLayout.Y_AXIS));
133 rangeDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
134 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
136 JLabel rangeDetailsLabel = new JLabel(I18nManager.getText("details.rangedetails"));
137 rangeDetailsLabel.setFont(biggerFont);
138 rangeDetailsPanel.add(rangeDetailsLabel);
139 _rangeLabel = new JLabel(I18nManager.getText("details.norangeselection"));
140 rangeDetailsPanel.add(_rangeLabel);
141 _distanceLabel = new JLabel("");
142 rangeDetailsPanel.add(_distanceLabel);
143 _durationLabel = new JLabel("");
144 rangeDetailsPanel.add(_durationLabel);
145 _aveSpeedLabel = new JLabel("");
146 rangeDetailsPanel.add(_aveSpeedLabel);
147 _altRangeLabel = new JLabel("");
148 rangeDetailsPanel.add(_altRangeLabel);
149 _updownLabel = new JLabel("");
150 rangeDetailsPanel.add(_updownLabel);
151 rangeDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
153 // photo details panel
154 JPanel photoDetailsPanel = new JPanel();
155 photoDetailsPanel.setLayout(new BoxLayout(photoDetailsPanel, BoxLayout.Y_AXIS));
156 photoDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
157 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
159 JLabel photoDetailsLabel = new JLabel(I18nManager.getText("details.photodetails"));
160 photoDetailsLabel.setFont(biggerFont);
161 photoDetailsPanel.add(photoDetailsLabel);
162 _photoLabel = new JLabel(I18nManager.getText("details.nophoto"));
163 photoDetailsPanel.add(_photoLabel);
164 _photoTimestampLabel = new JLabel("");
165 photoDetailsPanel.add(_photoTimestampLabel);
166 _photoConnectedLabel = new JLabel("");
167 photoDetailsPanel.add(_photoConnectedLabel);
168 _photoThumbnail = new PhotoThumbnail();
169 _photoThumbnail.setVisible(false);
170 _photoThumbnail.setPreferredSize(new Dimension(100, 100));
171 photoDetailsPanel.add(_photoThumbnail);
173 JButton rotLeft = makeRotateButton(IconManager.ROTATE_LEFT, FunctionLibrary.FUNCTION_ROTATE_PHOTO_LEFT);
174 JButton rotRight = makeRotateButton(IconManager.ROTATE_RIGHT, FunctionLibrary.FUNCTION_ROTATE_PHOTO_RIGHT);
175 _rotationButtons = new JPanel();
176 _rotationButtons.add(rotLeft);
177 _rotationButtons.add(rotRight);
178 _rotationButtons.setAlignmentX(Component.LEFT_ALIGNMENT);
179 _rotationButtons.setVisible(false);
180 photoDetailsPanel.add(_rotationButtons);
182 // add the details panels to the main panel
183 mainPanel.add(pointDetailsPanel);
184 mainPanel.add(Box.createVerticalStrut(5));
185 mainPanel.add(rangeDetailsPanel);
186 mainPanel.add(Box.createVerticalStrut(5));
187 mainPanel.add(photoDetailsPanel);
188 mainPanel.add(Box.createVerticalStrut(5));
189 // add the main panel at the top
190 add(mainPanel, BorderLayout.NORTH);
192 // Add format, units selection
193 JPanel lowerPanel = new JPanel();
194 lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.Y_AXIS));
195 JLabel coordFormatLabel = new JLabel(I18nManager.getText("details.coordformat") + ": ");
196 coordFormatLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
197 lowerPanel.add(coordFormatLabel);
198 String[] coordFormats = {I18nManager.getText("units.original"), I18nManager.getText("units.degminsec"),
199 I18nManager.getText("units.degmin"), I18nManager.getText("units.deg")};
200 _coordFormatDropdown = new JComboBox(coordFormats);
201 _coordFormatDropdown.addActionListener(new ActionListener() {
202 public void actionPerformed(ActionEvent e)
204 dataUpdated(DataSubscriber.UNITS_CHANGED);
207 lowerPanel.add(_coordFormatDropdown);
208 _coordFormatDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
209 JLabel unitsLabel = new JLabel(I18nManager.getText("details.distanceunits") + ": ");
210 unitsLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
211 lowerPanel.add(unitsLabel);
212 String[] distUnits = {I18nManager.getText("units.kilometres"), I18nManager.getText("units.miles")};
213 _distUnitsDropdown = new JComboBox(distUnits);
214 if (!Config.getConfigBoolean(Config.KEY_METRIC_UNITS)) {_distUnitsDropdown.setSelectedIndex(1);}
215 _distUnitsDropdown.addActionListener(new ActionListener() {
216 public void actionPerformed(ActionEvent e)
218 Config.setConfigBoolean(Config.KEY_METRIC_UNITS, _distUnitsDropdown.getSelectedIndex() == 0);
219 UpdateMessageBroker.informSubscribers(DataSubscriber.UNITS_CHANGED);
222 lowerPanel.add(_distUnitsDropdown);
223 _distUnitsDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
224 add(lowerPanel, BorderLayout.SOUTH);
229 * Notification that Track has been updated
230 * @param inUpdateType byte to specify what has been updated
232 public void dataUpdated(byte inUpdateType)
234 // Update current point data, if any
235 DataPoint currentPoint = _trackInfo.getCurrentPoint();
236 Selection selection = _trackInfo.getSelection();
237 int currentPointIndex = selection.getCurrentPointIndex();
238 _speedLabel.setText("");
239 Distance.Units distUnits = _distUnitsDropdown.getSelectedIndex()==0?Distance.Units.KILOMETRES:Distance.Units.MILES;
240 String distUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kilometres.short":"units.miles.short");
241 String speedUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kmh":"units.mph");
242 if (_track == null || currentPoint == null)
244 _indexLabel.setText(I18nManager.getText("details.nopointselection"));
245 _latLabel.setText("");
246 _longLabel.setText("");
247 _altLabel.setText("");
248 _timeLabel.setText("");
249 _nameLabel.setText("");
250 _typeLabel.setText("");
254 _indexLabel.setText(LABEL_POINT_SELECTED
255 + (currentPointIndex+1) + " " + I18nManager.getText("details.index.of")
256 + " " + _track.getNumPoints());
257 _latLabel.setText(makeCoordinateLabel(LABEL_POINT_LATITUDE, currentPoint.getLatitude(), _coordFormatDropdown.getSelectedIndex()));
258 _longLabel.setText(makeCoordinateLabel(LABEL_POINT_LONGITUDE, currentPoint.getLongitude(), _coordFormatDropdown.getSelectedIndex()));
259 _altLabel.setText(currentPoint.hasAltitude()?
260 (LABEL_POINT_ALTITUDE + currentPoint.getAltitude().getValue() + getAltitudeUnitsLabel(currentPoint.getAltitude().getFormat()))
262 if (currentPoint.getTimestamp().isValid())
264 if (currentPointIndex > 0 && currentPointIndex < (_trackInfo.getTrack().getNumPoints()-1))
266 DataPoint prevPoint = _trackInfo.getTrack().getPoint(currentPointIndex - 1);
267 DataPoint nextPoint = _trackInfo.getTrack().getPoint(currentPointIndex + 1);
268 if (prevPoint.getTimestamp().isValid() && nextPoint.getTimestamp().isValid())
270 // use total distance and total time between neighbouring points
271 long diff = nextPoint.getTimestamp().getSecondsSince(prevPoint.getTimestamp());
272 if (diff < 1000 && diff > 0)
274 double rads = DataPoint.calculateRadiansBetween(prevPoint, currentPoint) +
275 DataPoint.calculateRadiansBetween(currentPoint, nextPoint);
276 double dist = Distance.convertRadiansToDistance(rads, distUnits);
277 String speed = roundedNumber(3600 * dist / diff) + " " + speedUnitsStr;
278 _speedLabel.setText(I18nManager.getText("fieldname.speed") + ": " + speed);
282 _timeLabel.setText(LABEL_POINT_TIMESTAMP + currentPoint.getTimestamp().getText());
285 _timeLabel.setText("");
288 final String name = currentPoint.getWaypointName();
289 if (name != null && !name.equals(""))
291 _nameLabel.setText(LABEL_POINT_WAYPOINTNAME + name);
293 else _nameLabel.setText("");
295 final String type = currentPoint.getFieldValue(Field.WAYPT_TYPE);
296 if (type != null && !type.equals("")) {
297 _typeLabel.setText(LABEL_POINT_WAYPOINTTYPE + type);
299 else _typeLabel.setText("");
302 // Update range details
303 if (_track == null || !selection.hasRangeSelected())
305 _rangeLabel.setText(I18nManager.getText("details.norangeselection"));
306 _distanceLabel.setText("");
307 _durationLabel.setText("");
308 _altRangeLabel.setText("");
309 _updownLabel.setText("");
310 _aveSpeedLabel.setText("");
314 _rangeLabel.setText(LABEL_RANGE_SELECTED
315 + (selection.getStart()+1) + " " + I18nManager.getText("details.range.to")
316 + " " + (selection.getEnd()+1));
317 _distanceLabel.setText(LABEL_RANGE_DISTANCE + roundedNumber(selection.getDistance(distUnits)) + " " + distUnitsStr);
318 if (selection.getNumSeconds() > 0)
320 _durationLabel.setText(LABEL_RANGE_DURATION + DisplayUtils.buildDurationString(selection.getNumSeconds()));
321 _aveSpeedLabel.setText(I18nManager.getText("details.range.avespeed") + ": "
322 + roundedNumber(selection.getDistance(distUnits)/selection.getNumSeconds()*3600.0) + " " + speedUnitsStr);
325 _durationLabel.setText("");
326 _aveSpeedLabel.setText("");
328 String altUnitsLabel = getAltitudeUnitsLabel(selection.getAltitudeFormat());
329 IntegerRange altRange = selection.getAltitudeRange();
330 if (altRange.getMinimum() >= 0 && altRange.getMaximum() >= 0)
332 _altRangeLabel.setText(LABEL_RANGE_ALTITUDE
333 + altRange.getMinimum() + altUnitsLabel + " "
334 + I18nManager.getText("details.altitude.to") + " "
335 + altRange.getMaximum() + altUnitsLabel);
336 _updownLabel.setText(LABEL_RANGE_CLIMB + selection.getClimb() + altUnitsLabel
337 + LABEL_RANGE_DESCENT + selection.getDescent() + altUnitsLabel);
341 _altRangeLabel.setText("");
342 _updownLabel.setText("");
345 // show photo details and thumbnail
346 Photo currentPhoto = _trackInfo.getPhotoList().getPhoto(_trackInfo.getSelection().getCurrentPhotoIndex());
347 if ((currentPoint == null || currentPoint.getPhoto() == null) && currentPhoto == null)
349 // no photo, hide details
350 _photoLabel.setText(I18nManager.getText("details.nophoto"));
351 _photoTimestampLabel.setText("");
352 _photoConnectedLabel.setText("");
353 _photoThumbnail.setVisible(false);
354 _rotationButtons.setVisible(false);
358 if (currentPhoto == null) {currentPhoto = currentPoint.getPhoto();}
359 _photoLabel.setText(I18nManager.getText("details.photofile") + ": " + currentPhoto.getFile().getName());
360 _photoTimestampLabel.setText(LABEL_POINT_TIMESTAMP + currentPhoto.getTimestamp().getText());
361 _photoConnectedLabel.setText(I18nManager.getText("details.photo.connected") + ": "
362 + (currentPhoto.getCurrentStatus() == Photo.Status.NOT_CONNECTED ?
363 I18nManager.getText("dialog.about.no"):I18nManager.getText("dialog.about.yes")));
364 _photoThumbnail.setVisible(true);
365 _photoThumbnail.setPhoto(currentPhoto);
366 _rotationButtons.setVisible(true);
367 if ((inUpdateType & DataSubscriber.PHOTOS_MODIFIED) > 0) {_photoThumbnail.refresh();}
369 _photoThumbnail.repaint();
374 * Choose the appropriate altitude units label for the specified format
375 * @param inFormat altitude format
376 * @return language-sensitive string
378 private static String getAltitudeUnitsLabel(Altitude.Format inFormat)
380 if (inFormat == LABEL_POINT_ALTITUDE_FORMAT && LABEL_POINT_ALTITUDE_UNITS != null)
381 return LABEL_POINT_ALTITUDE_UNITS;
382 LABEL_POINT_ALTITUDE_FORMAT = inFormat;
383 if (inFormat == Altitude.Format.METRES)
384 return " " + I18nManager.getText("units.metres.short");
385 return " " + I18nManager.getText("units.feet.short");
390 * Construct an appropriate coordinate label using the selected format
391 * @param inPrefix prefix of label
392 * @param inCoordinate coordinate
393 * @param inFormat index of format selection dropdown
394 * @return language-sensitive string
396 private static String makeCoordinateLabel(String inPrefix, Coordinate inCoordinate, int inFormat)
401 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN_SEC); break;
403 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN); break;
405 coord = inCoordinate.output(Coordinate.FORMAT_DEG); break;
406 default: // just as it was
407 coord = inCoordinate.output(Coordinate.FORMAT_NONE);
409 // Fix broken degree signs (due to unicode mangling)
410 final char brokenDeg = 65533;
411 if (coord.indexOf(brokenDeg) >= 0) {
412 coord = coord.replaceAll(String.valueOf(brokenDeg), "\u00B0");
414 return inPrefix + restrictDP(coord);
419 * Format a number to a sensible precision
420 * @param inDist distance
421 * @return formatted String
423 private String roundedNumber(double inDist)
425 // Set precision of formatter
429 else if (inDist < 10.0)
431 else if (inDist < 100.0)
434 _distanceFormatter.setMaximumFractionDigits(numDigits);
435 _distanceFormatter.setMinimumFractionDigits(numDigits);
436 return _distanceFormatter.format(inDist);
440 * Restrict the given coordinate to a limited number of decimal places for display
441 * @param inCoord coordinate string
442 * @return chopped string
444 private static String restrictDP(String inCoord)
446 final int DECIMAL_PLACES = 7;
447 if (inCoord == null) return "";
448 final int dotPos = Math.max(inCoord.lastIndexOf('.'), inCoord.lastIndexOf(','));
450 final int chopPos = dotPos + DECIMAL_PLACES;
451 if (chopPos < (inCoord.length()-1)) {
452 return inCoord.substring(0, chopPos);
459 * Create a little button for rotating the current photo
460 * @param inIcon icon to use (from IconManager)
461 * @param inFunction function to call (from FunctionLibrary)
462 * @return button object
464 private static JButton makeRotateButton(String inIcon, GenericFunction inFunction)
466 JButton button = new JButton(IconManager.getImageIcon(inIcon));
467 button.setToolTipText(I18nManager.getText(inFunction.getNameKey()));
468 button.setMargin(new Insets(0, 2, 0, 2));
469 button.addActionListener(new FunctionLauncher(inFunction));