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