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