]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/SelectorDisplay.java
Version 11, August 2010
[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.ListSelectionModel;
20 import javax.swing.border.EtchedBorder;
21 import javax.swing.event.ListSelectionEvent;
22 import javax.swing.event.ListSelectionListener;
23
24 import tim.prune.DataSubscriber;
25 import tim.prune.I18nManager;
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 GridLayout(0, 1));
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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
118                 _photoList.addListSelectionListener(new ListSelectionListener() {
119                         public void valueChanged(ListSelectionEvent e)
120                         {
121                                 if (!e.getValueIsAdjusting()) {
122                                         selectPhoto(_photoList.getSelectedIndex());
123                                 }
124                         }});
125                 JPanel photoListPanel = new JPanel();
126                 photoListPanel.setLayout(new BorderLayout());
127                 photoListPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.photos")), BorderLayout.NORTH);
128                 photoListPanel.add(new JScrollPane(_photoList), BorderLayout.CENTER);
129                 listsPanel.add(photoListPanel);
130                 listsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
131
132                 // add the controls to the main panel
133                 mainPanel.add(trackDetailsPanel);
134                 mainPanel.add(Box.createVerticalStrut(5));
135                 mainPanel.add(_scroller);
136                 mainPanel.add(Box.createVerticalStrut(5));
137
138                 // add the main panel at the top
139                 add(mainPanel, BorderLayout.NORTH);
140                 // and lists in the centre
141                 add(listsPanel, BorderLayout.CENTER);
142                 // set preferred width to be small
143                 setPreferredSize(new Dimension(100, 100));
144         }
145
146
147         /**
148          * Select the specified point
149          * @param inValue value to select
150          */
151         private void selectPoint(int inValue)
152         {
153                 if (_track != null && !_ignoreScrollEvents)
154                 {
155                         _trackInfo.selectPoint(inValue);
156                 }
157         }
158
159
160         /**
161          * Select the specified photo
162          * @param inPhotoIndex index of selected photo
163          */
164         private void selectPhoto(int inPhotoIndex)
165         {
166                 _trackInfo.selectPhoto(inPhotoIndex);
167         }
168
169
170         /**
171          * Select the specified waypoint
172          * @param inWaypointIndex index of selected waypoint
173          */
174         private void selectWaypoint(int inWaypointIndex)
175         {
176                 if (inWaypointIndex >= 0)
177                 {
178                         _trackInfo.selectPoint(_waypointListModel.getWaypoint(inWaypointIndex));
179                 }
180         }
181
182
183         /**
184          * Notification that Track has been updated
185          */
186         public void dataUpdated(byte inUpdateType)
187         {
188                 // Update track data
189                 if (_track == null || _track.getNumPoints() <= 0)
190                 {
191                         _trackpointsLabel.setText(I18nManager.getText("details.notrack"));
192                         _filenameLabel.setText("");
193                 }
194                 else
195                 {
196                         _trackpointsLabel.setText(I18nManager.getText("details.track.points") + ": "
197                                 + _track.getNumPoints());
198                         int numFiles = _trackInfo.getFileInfo().getNumFiles();
199                         if (numFiles == 1)
200                         {
201                                 _filenameLabel.setText(I18nManager.getText("details.track.file") + ": "
202                                         + _trackInfo.getFileInfo().getFilename());
203                         }
204                         else if (numFiles > 1)
205                         {
206                                 _filenameLabel.setText(I18nManager.getText("details.track.numfiles") + ": "
207                                         + numFiles);
208                         }
209                         else _filenameLabel.setText("");
210                 }
211
212                 // Update scroller settings
213                 int currentPointIndex = _trackInfo.getSelection().getCurrentPointIndex();
214                 _ignoreScrollEvents = true;
215                 if (_track == null || _track.getNumPoints() < 2)
216                 {
217                         // careful to avoid event loops here
218                         // _scroller.setValue(0);
219                         _scroller.setEnabled(false);
220                 }
221                 else
222                 {
223                         _scroller.setMaximum(_track.getNumPoints() -1 + SCROLLBAR_INTERVAL);
224                         if (currentPointIndex >= 0)
225                                 _scroller.setValue(currentPointIndex);
226                         _scroller.setEnabled(true);
227                 }
228                 _ignoreScrollEvents = false;
229
230                 // update waypoints and photos if necessary
231                 if ((inUpdateType |
232                         (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.WAYPOINTS_MODIFIED)) > 0)
233                 {
234                         _waypointListModel.fireChanged();
235                 }
236                 if ((inUpdateType |
237                         (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.PHOTOS_MODIFIED)) > 0)
238                 {
239                         _photoListModel.fireChanged();
240                 }
241                 // Deselect selected waypoint if selected point has since changed
242                 if (_waypointList.getSelectedIndex() >= 0)
243                 {
244                         if (_trackInfo.getCurrentPoint() == null
245                          || _waypointList.getSelectedIndex() >= _waypointListModel.getSize()
246                          || !_waypointListModel.getWaypoint(_waypointList.getSelectedIndex()).equals(_trackInfo.getCurrentPoint()))
247                         {
248                                 // point is selected in list but different from current point - deselect
249                                 _waypointList.clearSelection();
250                         }
251                 }
252                 // Make sure correct photo is selected
253                 if (_photoListModel.getSize() > 0)
254                 {
255                         int photoIndex = _trackInfo.getSelection().getCurrentPhotoIndex();
256                         int listSelection = _photoList.getSelectedIndex();
257                         // Change listbox selection if indexes not equal
258                         if (listSelection != photoIndex)
259                         {
260                                 if (photoIndex < 0) {
261                                         _photoList.clearSelection();
262                                 }
263                                 else {
264                                         _photoList.setSelectedIndex(photoIndex);
265                                 }
266                         }
267                 }
268         }
269 }