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