]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/DetailsDisplay.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / gui / DetailsDisplay.java
1 package tim.prune.gui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.Font;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.text.NumberFormat;
10
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
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;
30
31 /**
32  * Class to hold point details and selection details
33  * as a visual component
34  */
35 public class DetailsDisplay extends GenericDisplay
36 {
37         // Point details
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;
42
43         // Range details
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;
49         private JLabel _paceLabel = null;
50
51         // Photo details
52         private JLabel _photoLabel = null;
53         private PhotoThumbnail _photoThumbnail = null;
54         private JLabel _photoTimestampLabel = null;
55         private JLabel _photoConnectedLabel = null;
56
57         // Units
58         private JComboBox _coordFormatDropdown = null;
59         private JComboBox _distUnitsDropdown = null;
60         // Formatter
61         private NumberFormat _distanceFormatter = NumberFormat.getInstance();
62
63         // Cached labels
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 Altitude.Format LABEL_POINT_ALTITUDE_FORMAT = Altitude.Format.NO_FORMAT;
79
80
81         /**
82          * Constructor
83          * @param inTrackInfo Track info object
84          */
85         public DetailsDisplay(TrackInfo inTrackInfo)
86         {
87                 super(inTrackInfo);
88                 setLayout(new BorderLayout());
89
90                 JPanel mainPanel = new JPanel();
91                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
92                 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
93
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))
99                 );
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);
120
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))
126                 );
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                 _paceLabel = new JLabel("");
143                 rangeDetailsPanel.add(_paceLabel);
144                 _altRangeLabel = new JLabel("");
145                 rangeDetailsPanel.add(_altRangeLabel);
146                 _updownLabel = new JLabel("");
147                 rangeDetailsPanel.add(_updownLabel);
148                 rangeDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
149
150                 // photo details panel
151                 JPanel photoDetailsPanel = new JPanel();
152                 photoDetailsPanel.setLayout(new BoxLayout(photoDetailsPanel, BoxLayout.Y_AXIS));
153                 photoDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
154                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
155                 );
156                 JLabel photoDetailsLabel = new JLabel(I18nManager.getText("details.photodetails"));
157                 photoDetailsLabel.setFont(biggerFont);
158                 photoDetailsPanel.add(photoDetailsLabel);
159                 _photoLabel = new JLabel(I18nManager.getText("details.nophoto"));
160                 photoDetailsPanel.add(_photoLabel);
161                 _photoTimestampLabel = new JLabel("");
162                 photoDetailsPanel.add(_photoTimestampLabel);
163                 _photoConnectedLabel = new JLabel("");
164                 photoDetailsPanel.add(_photoConnectedLabel);
165                 _photoThumbnail = new PhotoThumbnail();
166                 _photoThumbnail.setVisible(false);
167                 _photoThumbnail.setPreferredSize(new Dimension(100, 100));
168                 photoDetailsPanel.add(_photoThumbnail);
169
170                 // add the details panels to the main panel
171                 mainPanel.add(pointDetailsPanel);
172                 mainPanel.add(Box.createVerticalStrut(5));
173                 mainPanel.add(rangeDetailsPanel);
174                 mainPanel.add(Box.createVerticalStrut(5));
175                 mainPanel.add(photoDetailsPanel);
176                 mainPanel.add(Box.createVerticalStrut(5));
177                 // add the main panel at the top
178                 add(mainPanel, BorderLayout.NORTH);
179
180                 // Add format, units selection
181                 JPanel lowerPanel = new JPanel();
182                 lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.Y_AXIS));
183                 JLabel coordFormatLabel = new JLabel(I18nManager.getText("details.coordformat") + ": ");
184                 coordFormatLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
185                 lowerPanel.add(coordFormatLabel);
186                 String[] coordFormats = {I18nManager.getText("units.original"), I18nManager.getText("units.degminsec"),
187                         I18nManager.getText("units.degmin"), I18nManager.getText("units.deg")};
188                 _coordFormatDropdown = new JComboBox(coordFormats);
189                 _coordFormatDropdown.addActionListener(new ActionListener() {
190                         public void actionPerformed(ActionEvent e)
191                         {
192                                 dataUpdated(DataSubscriber.UNITS_CHANGED);
193                         }
194                 });
195                 lowerPanel.add(_coordFormatDropdown);
196                 _coordFormatDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
197                 JLabel unitsLabel = new JLabel(I18nManager.getText("details.distanceunits") + ": ");
198                 unitsLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
199                 lowerPanel.add(unitsLabel);
200                 String[] distUnits = {I18nManager.getText("units.kilometres"), I18nManager.getText("units.miles")};
201                 _distUnitsDropdown = new JComboBox(distUnits);
202                 if (!Config.getConfigBoolean(Config.KEY_METRIC_UNITS)) {_distUnitsDropdown.setSelectedIndex(1);}
203                 _distUnitsDropdown.addActionListener(new ActionListener() {
204                         public void actionPerformed(ActionEvent e)
205                         {
206                                 dataUpdated(DataSubscriber.UNITS_CHANGED);
207                                 Config.setConfigBoolean(Config.KEY_METRIC_UNITS, _distUnitsDropdown.getSelectedIndex() == 0);
208                         }
209                 });
210                 lowerPanel.add(_distUnitsDropdown);
211                 _distUnitsDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
212                 add(lowerPanel, BorderLayout.SOUTH);
213         }
214
215
216         /**
217          * Notification that Track has been updated
218          * @param inUpdateType byte to specify what has been updated
219          */
220         public void dataUpdated(byte inUpdateType)
221         {
222                 // Update current point data, if any
223                 DataPoint currentPoint = _trackInfo.getCurrentPoint();
224                 Selection selection = _trackInfo.getSelection();
225                 int currentPointIndex = selection.getCurrentPointIndex();
226                 _speedLabel.setText("");
227                 Distance.Units distUnits = _distUnitsDropdown.getSelectedIndex()==0?Distance.Units.KILOMETRES:Distance.Units.MILES;
228                 String distUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kilometres.short":"units.miles.short");
229                 String speedUnitsStr = I18nManager.getText(_distUnitsDropdown.getSelectedIndex()==0?"units.kmh":"units.mph");
230                 if (_track == null || currentPoint == null)
231                 {
232                         _indexLabel.setText(I18nManager.getText("details.nopointselection"));
233                         _latLabel.setText("");
234                         _longLabel.setText("");
235                         _altLabel.setText("");
236                         _timeLabel.setText("");
237                         _nameLabel.setText("");
238                 }
239                 else
240                 {
241                         _indexLabel.setText(LABEL_POINT_SELECTED
242                                 + (currentPointIndex+1) + " " + I18nManager.getText("details.index.of")
243                                 + " " + _track.getNumPoints());
244                         _latLabel.setText(makeCoordinateLabel(LABEL_POINT_LATITUDE, currentPoint.getLatitude(), _coordFormatDropdown.getSelectedIndex()));
245                         _longLabel.setText(makeCoordinateLabel(LABEL_POINT_LONGITUDE, currentPoint.getLongitude(), _coordFormatDropdown.getSelectedIndex()));
246                         _altLabel.setText(currentPoint.hasAltitude()?
247                                 (LABEL_POINT_ALTITUDE + currentPoint.getAltitude().getValue() + getAltitudeUnitsLabel(currentPoint.getAltitude().getFormat()))
248                                 :"");
249                         if (currentPoint.getTimestamp().isValid())
250                         {
251                                 if (currentPointIndex > 0 && currentPointIndex < (_trackInfo.getTrack().getNumPoints()-1))
252                                 {
253                                         DataPoint prevPoint = _trackInfo.getTrack().getPoint(currentPointIndex - 1);
254                                         DataPoint nextPoint = _trackInfo.getTrack().getPoint(currentPointIndex + 1);
255                                         if (prevPoint.getTimestamp().isValid() && nextPoint.getTimestamp().isValid())
256                                         {
257                                                 // use total distance and total time between neighbouring points
258                                                 long diff = nextPoint.getTimestamp().getSecondsSince(prevPoint.getTimestamp());
259                                                 if (diff < 1000 && diff > 0)
260                                                 {
261                                                         double rads = DataPoint.calculateRadiansBetween(prevPoint, currentPoint) +
262                                                                 DataPoint.calculateRadiansBetween(currentPoint, nextPoint);
263                                                         double dist = Distance.convertRadiansToDistance(rads, distUnits);
264                                                         String speed = roundedNumber(3600 * dist / diff) + " " + speedUnitsStr;
265                                                         _speedLabel.setText(I18nManager.getText("fieldname.speed") + ": " + speed);
266                                                 }
267                                         }
268                                 }
269                                 _timeLabel.setText(LABEL_POINT_TIMESTAMP + currentPoint.getTimestamp().getText());
270                         }
271                         else {
272                                 _timeLabel.setText("");
273                         }
274                         String name = currentPoint.getWaypointName();
275                         if (name != null && !name.equals(""))
276                         {
277                                 _nameLabel.setText(LABEL_POINT_WAYPOINTNAME + name);
278                         }
279                         else _nameLabel.setText("");
280                 }
281
282                 // Update range details
283                 if (_track == null || !selection.hasRangeSelected())
284                 {
285                         _rangeLabel.setText(I18nManager.getText("details.norangeselection"));
286                         _distanceLabel.setText("");
287                         _movingDistanceLabel.setText("");
288                         _durationLabel.setText("");
289                         _altRangeLabel.setText("");
290                         _updownLabel.setText("");
291                         _aveSpeedLabel.setText("");
292                         _aveMovingSpeedLabel.setText("");
293                         _paceLabel.setText("");
294                 }
295                 else
296                 {
297                         _rangeLabel.setText(LABEL_RANGE_SELECTED
298                                 + (selection.getStart()+1) + " " + I18nManager.getText("details.range.to")
299                                 + " " + (selection.getEnd()+1));
300                         _distanceLabel.setText(LABEL_RANGE_DISTANCE + roundedNumber(selection.getDistance(distUnits)) + " " + distUnitsStr);
301                         if (selection.getHasMultipleSegments()) {
302                                 _movingDistanceLabel.setText(LABEL_RANGE_MOVINGDISTANCE + roundedNumber(selection.getMovingDistance(distUnits)) + " " + distUnitsStr);
303                         }
304                         else {
305                                 _movingDistanceLabel.setText(null);
306                         }
307                         if (selection.getNumSeconds() > 0)
308                         {
309                                 _durationLabel.setText(LABEL_RANGE_DURATION + buildDurationString(selection.getNumSeconds()));
310                                 _aveSpeedLabel.setText(I18nManager.getText("details.range.avespeed") + ": "
311                                         + roundedNumber(selection.getDistance(distUnits)/selection.getNumSeconds()*3600.0) + " " + speedUnitsStr);
312                                 if (selection.getHasMultipleSegments()) {
313                                         _aveMovingSpeedLabel.setText(I18nManager.getText("details.range.avemovingspeed") + ": "
314                                                 + roundedNumber(selection.getMovingDistance(distUnits)/selection.getMovingSeconds()*3600.0) + " " + speedUnitsStr);
315                                 }
316                                 else {
317                                         _aveMovingSpeedLabel.setText(null);
318                                 }
319                                 if (Config.getConfigBoolean(Config.KEY_SHOW_PACE)) {
320                                         _paceLabel.setText(I18nManager.getText("details.range.pace") + ": "
321                                                 + buildDurationString((long) (selection.getNumSeconds()/selection.getDistance(distUnits)))
322                                                 + " / " + distUnitsStr);
323                                 }
324                                 else {
325                                         _paceLabel.setText("");
326                                 }
327                         }
328                         else {
329                                 _durationLabel.setText("");
330                                 _aveSpeedLabel.setText("");
331                                 _aveMovingSpeedLabel.setText("");
332                                 _paceLabel.setText("");
333                         }
334                         String altUnitsLabel = getAltitudeUnitsLabel(selection.getAltitudeFormat());
335                         IntegerRange altRange = selection.getAltitudeRange();
336                         if (altRange.getMinimum() >= 0 && altRange.getMaximum() >= 0)
337                         {
338                                 _altRangeLabel.setText(LABEL_RANGE_ALTITUDE
339                                         + altRange.getMinimum() + altUnitsLabel + " "
340                                         + I18nManager.getText("details.altitude.to") + " "
341                                         + altRange.getMaximum() + altUnitsLabel);
342                                 _updownLabel.setText(LABEL_RANGE_CLIMB + selection.getClimb() + altUnitsLabel
343                                         + LABEL_RANGE_DESCENT + selection.getDescent() + altUnitsLabel);
344                         }
345                         else
346                         {
347                                 _altRangeLabel.setText("");
348                                 _updownLabel.setText("");
349                         }
350                 }
351                 // show photo details and thumbnail
352                 Photo currentPhoto = _trackInfo.getPhotoList().getPhoto(_trackInfo.getSelection().getCurrentPhotoIndex());
353                 if ((currentPoint == null || currentPoint.getPhoto() == null) && currentPhoto == null)
354                 {
355                         // no photo, hide details
356                         _photoLabel.setText(I18nManager.getText("details.nophoto"));
357                         _photoTimestampLabel.setText("");
358                         _photoConnectedLabel.setText("");
359                         _photoThumbnail.setVisible(false);
360                 }
361                 else
362                 {
363                         if (currentPhoto == null) {currentPhoto = currentPoint.getPhoto();}
364                         _photoLabel.setText(I18nManager.getText("details.photofile") + ": " + currentPhoto.getFile().getName());
365                         _photoTimestampLabel.setText(LABEL_POINT_TIMESTAMP + currentPhoto.getTimestamp().getText());
366                         _photoConnectedLabel.setText(I18nManager.getText("details.photo.connected") + ": "
367                                 + (currentPhoto.getCurrentStatus() == Photo.Status.NOT_CONNECTED ?
368                                         I18nManager.getText("dialog.about.no"):I18nManager.getText("dialog.about.yes")));
369                         _photoThumbnail.setVisible(true);
370                         _photoThumbnail.setPhoto(currentPhoto);
371                 }
372                 _photoThumbnail.repaint();
373         }
374
375
376         /**
377          * Choose the appropriate altitude units label for the specified format
378          * @param inFormat altitude format
379          * @return language-sensitive string
380          */
381         private static String getAltitudeUnitsLabel(Altitude.Format inFormat)
382         {
383                 if (inFormat == LABEL_POINT_ALTITUDE_FORMAT && LABEL_POINT_ALTITUDE_UNITS != null)
384                         return LABEL_POINT_ALTITUDE_UNITS;
385                 LABEL_POINT_ALTITUDE_FORMAT = inFormat;
386                 if (inFormat == Altitude.Format.METRES)
387                         return " " + I18nManager.getText("units.metres.short");
388                 return " " + I18nManager.getText("units.feet.short");
389         }
390
391
392         /**
393          * Construct an appropriate coordinate label using the selected format
394          * @param inPrefix prefix of label
395          * @param inCoordinate coordinate
396          * @param inFormat index of format selection dropdown
397          * @return language-sensitive string
398          */
399         private static String makeCoordinateLabel(String inPrefix, Coordinate inCoordinate, int inFormat)
400         {
401                 String coord = null;
402                 switch (inFormat) {
403                         case 1: // degminsec
404                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN_SEC); break;
405                         case 2: // degmin
406                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN); break;
407                         case 3: // degrees
408                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG); break;
409                         default: // just as it was
410                                 coord = inCoordinate.output(Coordinate.FORMAT_NONE);
411                 }
412                 return inPrefix + coord;
413         }
414
415
416         /**
417          * Build a String to describe a time duration
418          * @param inNumSecs number of seconds
419          * @return time as a string, days, hours, mins, secs as appropriate
420          */
421         private static String buildDurationString(long inNumSecs)
422         {
423                 if (inNumSecs <= 0L) return "";
424                 if (inNumSecs < 60L) return "" + inNumSecs + I18nManager.getText("display.range.time.secs");
425                 if (inNumSecs < 3600L) return "" + (inNumSecs / 60) + I18nManager.getText("display.range.time.mins")
426                         + " " + (inNumSecs % 60) + I18nManager.getText("display.range.time.secs");
427                 if (inNumSecs < 86400L) return "" + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours")
428                         + " " + ((inNumSecs / 60) % 60) + I18nManager.getText("display.range.time.mins");
429                 if (inNumSecs < 432000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days")
430                         + " " + (inNumSecs / 60 / 60) % 24 + I18nManager.getText("display.range.time.hours");
431                 if (inNumSecs < 8640000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days");
432                 return "big";
433         }
434
435
436         /**
437          * Format a number to a sensible precision
438          * @param inDist distance
439          * @return formatted String
440          */
441         private String roundedNumber(double inDist)
442         {
443                 // Set precision of formatter
444                 int numDigits = 0;
445                 if (inDist < 1.0)
446                         numDigits = 3;
447                 else if (inDist < 10.0)
448                         numDigits = 2;
449                 else if (inDist < 100.0)
450                         numDigits = 1;
451                 // set formatter
452                 _distanceFormatter.setMaximumFractionDigits(numDigits);
453                 _distanceFormatter.setMinimumFractionDigits(numDigits);
454                 return _distanceFormatter.format(inDist);
455         }
456 }