]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/DetailsDisplay.java
Version 6, October 2008
[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.PhotoStatus;
29 import tim.prune.data.Selection;
30 import tim.prune.data.TrackInfo;
31
32 /**
33  * Class to hold point details and selection details
34  * as a visual component
35  */
36 public class DetailsDisplay extends GenericDisplay
37 {
38         // Point details
39         private JLabel _indexLabel = null;
40         private JLabel _latLabel = null, _longLabel = null;
41         private JLabel _altLabel = null, _nameLabel = null;
42         private JLabel _timeLabel = null, _speedLabel = null;
43
44         // Range details
45         private JLabel _rangeLabel = null;
46         private JLabel _distanceLabel = null, _movingDistanceLabel = null;
47         private JLabel _durationLabel = null;
48         private JLabel _altRangeLabel = null, _updownLabel = null;
49         private JLabel _aveSpeedLabel = null, _aveMovingSpeedLabel = 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 int LABEL_POINT_ALTITUDE_FORMAT = Altitude.FORMAT_NONE;
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                 _altRangeLabel = new JLabel("");
143                 rangeDetailsPanel.add(_altRangeLabel);
144                 _updownLabel = new JLabel("");
145                 rangeDetailsPanel.add(_updownLabel);
146                 rangeDetailsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
147
148                 // photo details panel
149                 JPanel photoDetailsPanel = new JPanel();
150                 photoDetailsPanel.setLayout(new BoxLayout(photoDetailsPanel, BoxLayout.Y_AXIS));
151                 photoDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
152                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
153                 );
154                 JLabel photoDetailsLabel = new JLabel(I18nManager.getText("details.photodetails"));
155                 photoDetailsLabel.setFont(biggerFont);
156                 photoDetailsPanel.add(photoDetailsLabel);
157                 _photoLabel = new JLabel(I18nManager.getText("details.nophoto"));
158                 photoDetailsPanel.add(_photoLabel);
159                 _photoTimestampLabel = new JLabel("");
160                 photoDetailsPanel.add(_photoTimestampLabel);
161                 _photoConnectedLabel = new JLabel("");
162                 photoDetailsPanel.add(_photoConnectedLabel);
163                 _photoThumbnail = new PhotoThumbnail();
164                 _photoThumbnail.setVisible(false);
165                 _photoThumbnail.setPreferredSize(new Dimension(100, 100));
166                 photoDetailsPanel.add(_photoThumbnail);
167
168                 // add the details panels to the main panel
169                 mainPanel.add(pointDetailsPanel);
170                 mainPanel.add(Box.createVerticalStrut(5));
171                 mainPanel.add(rangeDetailsPanel);
172                 mainPanel.add(Box.createVerticalStrut(5));
173                 mainPanel.add(photoDetailsPanel);
174                 mainPanel.add(Box.createVerticalStrut(5));
175                 // add the main panel at the top
176                 add(mainPanel, BorderLayout.NORTH);
177
178                 // Add format, units selection
179                 JPanel lowerPanel = new JPanel();
180                 lowerPanel.setLayout(new BoxLayout(lowerPanel, BoxLayout.Y_AXIS));
181                 JLabel coordFormatLabel = new JLabel(I18nManager.getText("details.coordformat") + ": ");
182                 coordFormatLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
183                 lowerPanel.add(coordFormatLabel);
184                 String[] coordFormats = {I18nManager.getText("units.original"), I18nManager.getText("units.degminsec"),
185                         I18nManager.getText("units.degmin"), I18nManager.getText("units.deg")};
186                 _coordFormatDropdown = new JComboBox(coordFormats);
187                 _coordFormatDropdown.addActionListener(new ActionListener() {
188                         public void actionPerformed(ActionEvent e)
189                         {
190                                 dataUpdated(DataSubscriber.UNITS_CHANGED);
191                         }
192                 });
193                 lowerPanel.add(_coordFormatDropdown);
194                 _coordFormatDropdown.setAlignmentX(Component.LEFT_ALIGNMENT);
195                 JLabel unitsLabel = new JLabel(I18nManager.getText("details.distanceunits") + ": ");
196                 unitsLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
197                 lowerPanel.add(unitsLabel);
198                 String[] distUnits = {I18nManager.getText("units.kilometres"), I18nManager.getText("units.miles")};
199                 _distUnitsDropdown = new JComboBox(distUnits);
200                 if (!Config.getUseMetricUnits()) {_distUnitsDropdown.setSelectedIndex(1);}
201                 _distUnitsDropdown.addActionListener(new ActionListener() {
202                         public void actionPerformed(ActionEvent e)
203                         {
204                                 dataUpdated(DataSubscriber.UNITS_CHANGED);
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                 int 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) {
256                                                         double rads = DataPoint.calculateRadiansBetween(prevPoint, currentPoint) +
257                                                                 DataPoint.calculateRadiansBetween(currentPoint, nextPoint);
258                                                         double dist = Distance.convertRadiansToDistance(rads, distUnits);
259                                                         String speed = roundedNumber(3600 * dist / diff) + " " + speedUnitsStr;
260                                                         _speedLabel.setText(I18nManager.getText("details.speed") + ": " + speed);
261                                                 }
262                                         }
263                                 }
264                                 _timeLabel.setText(LABEL_POINT_TIMESTAMP + currentPoint.getTimestamp().getText());
265                         }
266                         else {
267                                 _timeLabel.setText("");
268                         }
269                         String name = currentPoint.getWaypointName();
270                         if (name != null && !name.equals(""))
271                         {
272                                 _nameLabel.setText(LABEL_POINT_WAYPOINTNAME + name);
273                         }
274                         else _nameLabel.setText("");
275                 }
276
277                 // Update range details
278                 if (_track == null || !selection.hasRangeSelected())
279                 {
280                         _rangeLabel.setText(I18nManager.getText("details.norangeselection"));
281                         _distanceLabel.setText("");
282                         _movingDistanceLabel.setText("");
283                         _durationLabel.setText("");
284                         _altRangeLabel.setText("");
285                         _updownLabel.setText("");
286                         _aveSpeedLabel.setText("");
287                         _aveMovingSpeedLabel.setText("");
288                 }
289                 else
290                 {
291                         _rangeLabel.setText(LABEL_RANGE_SELECTED
292                                 + (selection.getStart()+1) + " " + I18nManager.getText("details.range.to")
293                                 + " " + (selection.getEnd()+1));
294                         _distanceLabel.setText(LABEL_RANGE_DISTANCE + roundedNumber(selection.getDistance(distUnits)) + " " + distUnitsStr);
295                         _movingDistanceLabel.setText(LABEL_RANGE_MOVINGDISTANCE + roundedNumber(selection.getMovingDistance(distUnits)) + " " + distUnitsStr);
296                         if (selection.getNumSeconds() > 0)
297                         {
298                                 _durationLabel.setText(LABEL_RANGE_DURATION + buildDurationString(selection.getNumSeconds()));
299                                 _aveSpeedLabel.setText(I18nManager.getText("details.range.avespeed") + ": "
300                                         + roundedNumber(selection.getDistance(distUnits)/selection.getNumSeconds()*3600.0) + " " + speedUnitsStr);
301                                 _aveMovingSpeedLabel.setText(I18nManager.getText("details.range.avemovingspeed") + ": "
302                                         + roundedNumber(selection.getMovingDistance(distUnits)/selection.getMovingSeconds()*3600.0) + " " + speedUnitsStr);
303                         }
304                         else {
305                                 _durationLabel.setText("");
306                                 _aveSpeedLabel.setText("");
307                                 _aveMovingSpeedLabel.setText("");
308                         }
309                         String altUnitsLabel = getAltitudeUnitsLabel(selection.getAltitudeFormat());
310                         IntegerRange altRange = selection.getAltitudeRange();
311                         if (altRange.getMinimum() >= 0 && altRange.getMaximum() >= 0)
312                         {
313                                 _altRangeLabel.setText(LABEL_RANGE_ALTITUDE
314                                         + altRange.getMinimum() + altUnitsLabel + " "
315                                         + I18nManager.getText("details.altitude.to") + " "
316                                         + altRange.getMaximum() + altUnitsLabel);
317                                 _updownLabel.setText(LABEL_RANGE_CLIMB + selection.getClimb() + altUnitsLabel
318                                         + LABEL_RANGE_DESCENT + selection.getDescent() + altUnitsLabel);
319                         }
320                         else
321                         {
322                                 _altRangeLabel.setText("");
323                                 _updownLabel.setText("");
324                         }
325                 }
326                 // show photo details and thumbnail
327                 Photo currentPhoto = _trackInfo.getPhotoList().getPhoto(_trackInfo.getSelection().getCurrentPhotoIndex());
328                 if (_track == null || ( (currentPoint == null || currentPoint.getPhoto() == null) && currentPhoto == null))
329                 {
330                         // no photo, hide details
331                         _photoLabel.setText(I18nManager.getText("details.nophoto"));
332                         _photoTimestampLabel.setText("");
333                         _photoConnectedLabel.setText("");
334                         _photoThumbnail.setVisible(false);
335                 }
336                 else
337                 {
338                         if (currentPhoto == null) {currentPhoto = currentPoint.getPhoto();}
339                         _photoLabel.setText(I18nManager.getText("details.photofile") + ": " + currentPhoto.getFile().getName());
340                         _photoLabel.setText(LABEL_POINT_TIMESTAMP + currentPhoto.getTimestamp().getText());
341                         _photoConnectedLabel.setText(I18nManager.getText("details.photo.connected") + ": "
342                                 + (currentPhoto.getCurrentStatus() == PhotoStatus.NOT_CONNECTED ?
343                                         I18nManager.getText("dialog.about.no"):I18nManager.getText("dialog.about.yes")));
344                         _photoThumbnail.setVisible(true);
345                         _photoThumbnail.setPhoto(currentPhoto);
346                 }
347                 _photoThumbnail.repaint();
348         }
349
350
351         /**
352          * Choose the appropriate altitude units label for the specified format
353          * @param inFormat altitude format
354          * @return language-sensitive string
355          */
356         private static String getAltitudeUnitsLabel(int inFormat)
357         {
358                 if (inFormat == LABEL_POINT_ALTITUDE_FORMAT && LABEL_POINT_ALTITUDE_UNITS != null)
359                         return LABEL_POINT_ALTITUDE_UNITS;
360                 LABEL_POINT_ALTITUDE_FORMAT = inFormat;
361                 if (inFormat == Altitude.FORMAT_METRES)
362                         return " " + I18nManager.getText("units.metres.short");
363                 return " " + I18nManager.getText("units.feet.short");
364         }
365
366
367         /**
368          * Construct an appropriate coordinate label using the selected format
369          * @param inPrefix prefix of label
370          * @param inCoordinate coordinate
371          * @param inFormat index of format selection dropdown
372          * @return language-sensitive string
373          */
374         private static String makeCoordinateLabel(String inPrefix, Coordinate inCoordinate, int inFormat)
375         {
376                 String coord = null;
377                 switch (inFormat) {
378                         case 1: // degminsec
379                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN_SEC); break;
380                         case 2: // degmin
381                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG_MIN); break;
382                         case 3: // degrees
383                                 coord = inCoordinate.output(Coordinate.FORMAT_DEG); break;
384                         default: // just as it was
385                                 coord = inCoordinate.output(Coordinate.FORMAT_NONE);
386                 }
387                 return inPrefix + coord;
388         }
389
390
391         /**
392          * Build a String to describe a time duration
393          * @param inNumSecs number of seconds
394          * @return time as a string, days, hours, mins, secs as appropriate
395          */
396         private static String buildDurationString(long inNumSecs)
397         {
398                 if (inNumSecs <= 0L) return "";
399                 if (inNumSecs < 60L) return "" + inNumSecs + I18nManager.getText("display.range.time.secs");
400                 if (inNumSecs < 3600L) return "" + (inNumSecs / 60) + I18nManager.getText("display.range.time.mins")
401                         + " " + (inNumSecs % 60) + I18nManager.getText("display.range.time.secs");
402                 if (inNumSecs < 86400L) return "" + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours")
403                         + " " + ((inNumSecs / 60) % 60) + I18nManager.getText("display.range.time.mins");
404                 if (inNumSecs < 432000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days")
405                         + " " + (inNumSecs / 60 / 60) + I18nManager.getText("display.range.time.hours");
406                 if (inNumSecs < 8640000L) return "" + (inNumSecs / 86400L) + I18nManager.getText("display.range.time.days");
407                 return "big";
408         }
409
410
411         /**
412          * Format a number to a sensible precision
413          * @param inDist distance
414          * @return formatted String
415          */
416         private String roundedNumber(double inDist)
417         {
418                 // Set precision of formatter
419                 int numDigits = 0;
420                 if (inDist < 1.0)
421                         numDigits = 3;
422                 else if (inDist < 10.0)
423                         numDigits = 2;
424                 else if (inDist < 100.0)
425                         numDigits = 1;
426                 // set formatter
427                 _distanceFormatter.setMaximumFractionDigits(numDigits);
428                 _distanceFormatter.setMinimumFractionDigits(numDigits);
429                 return _distanceFormatter.format(inDist);
430         }
431 }