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