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