]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/SelectorDisplay.java
Version 4, January 2008
[GpsPrune.git] / tim / prune / gui / SelectorDisplay.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.GridLayout;
8 import java.awt.event.AdjustmentEvent;
9 import java.awt.event.AdjustmentListener;
10
11 import javax.swing.BorderFactory;
12 import javax.swing.Box;
13 import javax.swing.BoxLayout;
14 import javax.swing.JLabel;
15 import javax.swing.JList;
16 import javax.swing.JPanel;
17 import javax.swing.JScrollBar;
18 import javax.swing.JScrollPane;
19 import javax.swing.border.EtchedBorder;
20 import javax.swing.event.ListSelectionEvent;
21 import javax.swing.event.ListSelectionListener;
22
23 import tim.prune.DataSubscriber;
24 import tim.prune.I18nManager;
25 import tim.prune.data.DataPoint;
26 import tim.prune.data.Photo;
27 import tim.prune.data.TrackInfo;
28
29 /**
30  * Class to allow selection of points and photos
31  * as a visual component
32  */
33 public class SelectorDisplay extends GenericDisplay
34 {
35         // Track details
36         private JLabel _trackpointsLabel = null;
37         private JLabel _filenameLabel = null;
38         // Scroll bar
39         private JScrollBar _scroller = null;
40         private boolean _ignoreScrollEvents = false;
41
42         // Photos
43         private JList _photoList = null;
44         private PhotoListModel _photoListModel = null;
45         // Waypoints
46         private JList _waypointList = null;
47         private WaypointListModel _waypointListModel = null;
48
49         // scrollbar interval
50         private static final int SCROLLBAR_INTERVAL = 50;
51         // number of rows in lists
52         private static final int NUM_LIST_ENTRIES = 7;
53
54
55         /**
56          * Constructor
57          * @param inTrackInfo Track info object
58          */
59         public SelectorDisplay(TrackInfo inTrackInfo)
60         {
61                 super(inTrackInfo);
62                 setLayout(new BorderLayout());
63
64                 JPanel mainPanel = new JPanel();
65                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
66                 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
67
68                 // Track details panel
69                 JPanel trackDetailsPanel = new JPanel();
70                 trackDetailsPanel.setLayout(new BoxLayout(trackDetailsPanel, BoxLayout.Y_AXIS));
71                 trackDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
72                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
73                 );
74                 JLabel trackDetailsLabel = new JLabel(I18nManager.getText("details.trackdetails"));
75                 Font biggerFont = trackDetailsLabel.getFont();
76                 biggerFont = biggerFont.deriveFont(Font.BOLD, biggerFont.getSize2D() + 2.0f);
77                 trackDetailsLabel.setFont(biggerFont);
78                 trackDetailsPanel.add(trackDetailsLabel);
79                 _trackpointsLabel = new JLabel(I18nManager.getText("details.notrack"));
80                 trackDetailsPanel.add(_trackpointsLabel);
81                 _filenameLabel = new JLabel("");
82                 trackDetailsPanel.add(_filenameLabel);
83
84                 // Scroll bar
85                 _scroller = new JScrollBar(JScrollBar.HORIZONTAL, 0, SCROLLBAR_INTERVAL, 0, 100);
86                 _scroller.addAdjustmentListener(new AdjustmentListener() {
87                         public void adjustmentValueChanged(AdjustmentEvent e)
88                         {
89                                 selectPoint(e.getValue());
90                         }
91                 });
92                 _scroller.setEnabled(false);
93
94                 // Add panel for waypoints / photos
95                 JPanel listsPanel = new JPanel();
96                 listsPanel.setLayout(new GridLayout(0, 1));
97                 listsPanel.setBorder(BorderFactory.createCompoundBorder(
98                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
99                 );
100                 _waypointListModel = new WaypointListModel(_trackInfo.getTrack());
101                 _waypointList = new JList(_waypointListModel);
102                 _waypointList.setVisibleRowCount(NUM_LIST_ENTRIES);
103                 _waypointList.addListSelectionListener(new ListSelectionListener() {
104                         public void valueChanged(ListSelectionEvent e)
105                         {
106                                 if (!e.getValueIsAdjusting()) selectWaypoint(_waypointList.getSelectedIndex());
107                         }});
108                 JPanel waypointListPanel = new JPanel();
109                 waypointListPanel.setLayout(new BorderLayout());
110                 waypointListPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.waypoints")), BorderLayout.NORTH);
111                 waypointListPanel.add(new JScrollPane(_waypointList), BorderLayout.CENTER);
112                 listsPanel.add(waypointListPanel);
113                 // photo list
114                 _photoListModel = new PhotoListModel(_trackInfo.getPhotoList());
115                 _photoList = new JList(_photoListModel);
116                 _photoList.setVisibleRowCount(NUM_LIST_ENTRIES);
117                 _photoList.addListSelectionListener(new ListSelectionListener() {
118                         public void valueChanged(ListSelectionEvent e)
119                         {
120                                 if (!e.getValueIsAdjusting()) selectPhoto(_photoList.getSelectedIndex());
121                         }});
122                 JPanel photoListPanel = new JPanel();
123                 photoListPanel.setLayout(new BorderLayout());
124                 photoListPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.photos")), BorderLayout.NORTH);
125                 photoListPanel.add(new JScrollPane(_photoList), BorderLayout.CENTER);
126                 listsPanel.add(photoListPanel);
127                 listsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
128
129                 // add the controls to the main panel
130                 mainPanel.add(trackDetailsPanel);
131                 mainPanel.add(Box.createVerticalStrut(5));
132                 mainPanel.add(_scroller);
133                 mainPanel.add(Box.createVerticalStrut(5));
134
135                 // add the main panel at the top
136                 add(mainPanel, BorderLayout.NORTH);
137                 // and lists in the centre
138                 add(listsPanel, BorderLayout.CENTER);
139                 // set preferred width to be small
140                 setPreferredSize(new Dimension(100, 100));
141         }
142
143
144         /**
145          * Select the specified point
146          * @param inValue value to select
147          */
148         private void selectPoint(int inValue)
149         {
150                 if (_track != null && !_ignoreScrollEvents)
151                 {
152                         _trackInfo.getSelection().selectPoint(inValue);
153                 }
154         }
155
156
157         /**
158          * Select the specified photo
159          * @param inPhotoIndex index of selected photo
160          */
161         private void selectPhoto(int inPhotoIndex)
162         {
163                 _trackInfo.selectPhoto(inPhotoIndex);
164         }
165
166
167         /**
168          * Select the specified waypoint
169          * @param inWaypointIndex index of selected waypoint
170          */
171         private void selectWaypoint(int inWaypointIndex)
172         {
173                 if (inWaypointIndex >= 0)
174                 {
175                         _trackInfo.selectPoint(_waypointListModel.getWaypoint(inWaypointIndex));
176                 }
177         }
178
179
180         /**
181          * Notification that Track has been updated
182          */
183         public void dataUpdated(byte inUpdateType)
184         {
185                 // Update track data
186                 if (_track == null || _track.getNumPoints() <= 0)
187                 {
188                         _trackpointsLabel.setText(I18nManager.getText("details.notrack"));
189                         _filenameLabel.setText("");
190                 }
191                 else
192                 {
193                         _trackpointsLabel.setText(I18nManager.getText("details.track.points") + ": "
194                                 + _track.getNumPoints());
195                         int numFiles = _trackInfo.getFileInfo().getNumFiles();
196                         if (numFiles == 1)
197                         {
198                                 _filenameLabel.setText(I18nManager.getText("details.track.file") + ": "
199                                         + _trackInfo.getFileInfo().getFilename());
200                         }
201                         else if (numFiles > 1)
202                         {
203                                 _filenameLabel.setText(I18nManager.getText("details.track.numfiles") + ": "
204                                         + numFiles);
205                         }
206                         else _filenameLabel.setText("");
207                 }
208
209                 // Update scroller settings
210                 int currentPointIndex = _trackInfo.getSelection().getCurrentPointIndex();
211                 _ignoreScrollEvents = true;
212                 if (_track == null || _track.getNumPoints() < 2)
213                 {
214                         // careful to avoid event loops here
215                         // _scroller.setValue(0);
216                         _scroller.setEnabled(false);
217                 }
218                 else
219                 {
220                         _scroller.setMaximum(_track.getNumPoints() + SCROLLBAR_INTERVAL);
221                         if (currentPointIndex >= 0)
222                                 _scroller.setValue(currentPointIndex);
223                         _scroller.setEnabled(true);
224                 }
225                 _ignoreScrollEvents = false;
226
227                 // update waypoints and photos if necessary
228                 if ((inUpdateType |
229                         (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.WAYPOINTS_MODIFIED)) > 0)
230                 {
231                         _waypointListModel.fireChanged();
232                 }
233                 if ((inUpdateType |
234                         (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.PHOTOS_MODIFIED)) > 0)
235                 {
236                         _photoListModel.fireChanged();
237                 }
238                 // Deselect selected waypoint if selected point has since changed
239                 if (_waypointList.getSelectedIndex() >= 0)
240                 {
241                         if (_trackInfo.getCurrentPoint() == null
242                          || !_waypointListModel.getWaypoint(_waypointList.getSelectedIndex()).equals(_trackInfo.getCurrentPoint()))
243                         {
244                                 // point is selected in list but different from current point - deselect
245                                 _waypointList.clearSelection();
246                         }
247                 }
248                 // Do the same for the photos
249                 if (_photoList.getSelectedIndex() >= 0)
250                 {
251                         DataPoint trackPoint = _trackInfo.getCurrentPoint();
252                         Photo selectedPhoto = _photoListModel.getPhoto(_photoList.getSelectedIndex());
253                         // Get selected Photo, if it's still there
254                         DataPoint photoPoint = null;
255                         if (selectedPhoto != null) {
256                                 photoPoint = _photoListModel.getPhoto(_photoList.getSelectedIndex()).getDataPoint();
257                         }
258                         // Compare selected photo with selected point
259                         if ( (photoPoint != null && (trackPoint == null || !photoPoint.equals(trackPoint)))
260                                 || (_trackInfo.getSelection().getCurrentPhotoIndex() < 0) )
261                         {
262                                 // photo is selected in list but different from current point - deselect
263                                 _photoList.clearSelection();
264                                 _trackInfo.getSelection().deselectPhoto();
265                         }
266                 }
267         }
268 }