]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/DetailsDisplay.java
Version 7, February 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
50         // Photo details
51         private JLabel _photoLabel = null;
52         private PhotoThumbnail _photoThumbnail = null;
53         private JLabel _photoTimestampLabel = null;
54         private JLabel _photoConnectedLabel = null;
55
56         // Units
57         private JComboBox _coordFormatDropdown = null;
58         private JComboBox _distUnitsDropdown = null;
59         // Formatter
60         private NumberFormat _distanceFormatter = NumberFormat.getInstance();
61
62         // Cached labels
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;
78
79
80         /**
81          * Constructor
82          * @param inTrackInfo Track info object
83          */
84         public DetailsDisplay(TrackInfo inTrackInfo)
85         {
86                 super(inTrackInfo);
87                 setLayout(new BorderLayout());
88
89                 JPanel mainPanel = new JPanel();
90                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
91                 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
92
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))
98                 );
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);
119
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))
125                 );
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);
146
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))
152                 );
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);
166
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);
176
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)
188                         {
189                                 dataUpdated(DataSubscriber.UNITS_CHANGED);
190                         }
191                 });
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)
202                         {
203                                 dataUpdated(DataSubscriber.UNITS_CHANGED);
204                                 Config.setUseMetricUnits(_distUnitsDropdown.getSelectedIndex() == 0);
205                         }
206                 });
207                 lowerPanel.add(_distUnitsDropdown);
208                 _distUnitsDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
209                 add(lowerPanel, BorderLayout.SOUTH);
210         }
211
212
213         /**
214          * Notification that Track has been updated
215          * @param inUpdateType byte to specify what has been updated
216          */
217         public void dataUpdated(byte inUpdateType)
218         {
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)
228                 {
229                         _indexLabel.setText(I18nManager.getText("details.nopointselection"));
230                         _latLabel.setText("");
231                         _longLabel.setText("");
232                         _altLabel.setText("");
233                         _timeLabel.setText("");
234                         _nameLabel.setText("");
235                 }
236                 else
237                 {
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()))
245                                 :"");
246                         if (currentPoint.getTimestamp().isValid())
247                         {
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())
252                                         {
253                                                 // use total distance and total time between neighbouring points
254                                                 long diff = nextPoint.getTimestamp().getSecondsSince(prevPoint.getTimestamp());
255                                                 if (diff < 1000 && diff > 0)
256                                                 {
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);
262                                                 }
263                                         }
264                                 }
265                                 _timeLabel.setText(LABEL_POINT_TIMESTAMP + currentPoint.getTimestamp().getText());
266                         }
267                         else {
268                                 _timeLabel.setText("");
269                         }
270                         String name = currentPoint.getWaypointName();
271                         if (name != null && !name.equals(""))
272                         {
273                                 _nameLabel.setText(LABEL_POINT_WAYPOINTNAME + name);
274                         }
275                         else _nameLabel.setText("");
276                 }
277
278                 // Update range details
279                 if (_track == null || !selection.hasRangeSelected())
280                 {
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("");
289                 }
290                 else
291                 {
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)
298                         {
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);
304                         }
305                         else {
306                                 _durationLabel.setText("");
307                                 _aveSpeedLabel.setText("");
308                                 _aveMovingSpeedLabel.setText("");
309                         }
310                         String altUnitsLabel = getAltitudeUnitsLabel(selection.getAltitudeFormat());
311                         IntegerRange altRange = selection.getAltitudeRange();
312                         if (altRange.getMinimum() >= 0 && altRange.getMaximum() >= 0)
313                         {
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);
320                         }
321                         else
322                         {
323                                 _altRangeLabel.setText("");
324                                 _updownLabel.setText("");
325                         }
326                 }
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))
330                 {
331                         // no photo, hide details
332                         _photoLabel.setText(I18nManager.getText("details.nophoto"));
333                         _photoTimestampLabel.setText("");
334                         _photoConnectedLabel.setText("");
335                         _photoThumbnail.setVisible(false);
336                 }
337                 else
338                 {
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);
347                 }
348                 _photoThumbnail.repaint();
349         }
350
351
352         /**
353          * Choose the appropriate altitude units label for the specified format
354          * @param inFormat altitude format
355          * @return language-sensitive string
356          */
357         private static String getAltitudeUnitsLabel(Altitude.Format inFormat)
358         {
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");
365         }
366
367
368         /**
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
374          */
375         private static String makeCoordinateLabel(String inPrefix, Coordinate inCoordinate, int inFormat)
376         {
377                 String coord = null;
378                 switch (inFormat) {
379                         case 1: // degminsec
380                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN_SEC); break;
381                         case 2: // degmin
382                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN); break;
383                         case 3: // degrees
384                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG); break;
385                         default: // just as it was
386                                 coord = inCoordinate.output(Coordinate.FORMAT_NONE);
387                 }
388                 return inPrefix + coord;
389         }
390
391
392         /**
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
396          */
397         private static String buildDurationString(long inNumSecs)
398         {
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");
408                 return "big";
409         }
410
411
412         /**
413          * Format a number to a sensible precision
414          * @param inDist distance
415          * @return formatted String
416          */
417         private String roundedNumber(double inDist)
418         {
419                 // Set precision of formatter
420                 int numDigits = 0;
421                 if (inDist < 1.0)
422                         numDigits = 3;
423                 else if (inDist < 10.0)
424                         numDigits = 2;
425                 else if (inDist < 100.0)
426                         numDigits = 1;
427                 // set formatter
428                 _distanceFormatter.setMaximumFractionDigits(numDigits);
429                 _distanceFormatter.setMinimumFractionDigits(numDigits);
430                 return _distanceFormatter.format(inDist);
431         }
432 }