]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/SelectorDisplay.java
48a5ac4967a95229f7cad5d94665022872636128
[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.TrackInfo;
26
27 /**
28  * Class to allow selection of points and photos
29  * as a visual component
30  */
31 public class SelectorDisplay extends GenericDisplay
32 {
33         // Track details
34         private JLabel _trackpointsLabel = null;
35         private JLabel _filenameLabel = null;
36         // Scroll bar
37         private JScrollBar _scroller = null;
38         private boolean _ignoreScrollEvents = false;
39
40         // Photos
41         private JList _photoList = null;
42         private PhotoListModel _photoListModel = null;
43         // Waypoints
44         private JList _waypointList = null;
45         private WaypointListModel _waypointListModel = null;
46
47         // scrollbar interval
48         private static final int SCROLLBAR_INTERVAL = 50;
49         // number of rows in lists
50         private static final int NUM_LIST_ENTRIES = 7;
51
52
53         /**
54          * Constructor
55          * @param inTrackInfo Track info object
56          */
57         public SelectorDisplay(TrackInfo inTrackInfo)
58         {
59                 super(inTrackInfo);
60                 setLayout(new BorderLayout());
61
62                 JPanel mainPanel = new JPanel();
63                 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
64                 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
65
66                 // Track details panel
67                 JPanel trackDetailsPanel = new JPanel();
68                 trackDetailsPanel.setLayout(new BoxLayout(trackDetailsPanel, BoxLayout.Y_AXIS));
69                 trackDetailsPanel.setBorder(BorderFactory.createCompoundBorder(
70                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
71                 );
72                 JLabel trackDetailsLabel = new JLabel(I18nManager.getText("details.trackdetails"));
73                 Font biggerFont = trackDetailsLabel.getFont();
74                 biggerFont = biggerFont.deriveFont(Font.BOLD, biggerFont.getSize2D() + 2.0f);
75                 trackDetailsLabel.setFont(biggerFont);
76                 trackDetailsPanel.add(trackDetailsLabel);
77                 _trackpointsLabel = new JLabel(I18nManager.getText("details.notrack"));
78                 trackDetailsPanel.add(_trackpointsLabel);
79                 _filenameLabel = new JLabel("");
80                 trackDetailsPanel.add(_filenameLabel);
81
82                 // Scroll bar
83                 _scroller = new JScrollBar(JScrollBar.HORIZONTAL, 0, SCROLLBAR_INTERVAL, 0, 100);
84                 _scroller.addAdjustmentListener(new AdjustmentListener() {
85                         public void adjustmentValueChanged(AdjustmentEvent e)
86                         {
87                                 selectPoint(e.getValue());
88                         }
89                 });
90                 _scroller.setEnabled(false);
91
92                 // Add panel for waypoints / photos
93                 JPanel listsPanel = new JPanel();
94                 listsPanel.setLayout(new GridLayout(0, 1));
95                 listsPanel.setBorder(BorderFactory.createCompoundBorder(
96                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
97                 );
98                 _waypointListModel = new WaypointListModel(_trackInfo.getTrack());
99                 _waypointList = new JList(_waypointListModel);
100                 _waypointList.setVisibleRowCount(NUM_LIST_ENTRIES);
101                 _waypointList.addListSelectionListener(new ListSelectionListener() {
102                         public void valueChanged(ListSelectionEvent e)
103                         {
104                                 if (!e.getValueIsAdjusting()) selectWaypoint(_waypointList.getSelectedIndex());
105                         }});
106                 JPanel waypointListPanel = new JPanel();
107                 waypointListPanel.setLayout(new BorderLayout());
108                 waypointListPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.waypoints")), BorderLayout.NORTH);
109                 waypointListPanel.add(new JScrollPane(_waypointList), BorderLayout.CENTER);
110                 listsPanel.add(waypointListPanel);
111                 // photo list
112                 _photoListModel = new PhotoListModel(_trackInfo.getPhotoList());
113                 _photoList = new JList(_photoListModel);
114                 _photoList.setVisibleRowCount(NUM_LIST_ENTRIES);
115                 _photoList.addListSelectionListener(new ListSelectionListener() {
116                         public void valueChanged(ListSelectionEvent e)
117                         {
118                                 if (!e.getValueIsAdjusting()) {
119                                         selectPhoto(_photoList.getSelectedIndex());
120                                 }
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.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() -1 + 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                          || _waypointList.getSelectedIndex() >= _waypointListModel.getSize()
243                          || !_waypointListModel.getWaypoint(_waypointList.getSelectedIndex()).equals(_trackInfo.getCurrentPoint()))
244                         {
245                                 // point is selected in list but different from current point - deselect
246                                 _waypointList.clearSelection();
247                         }
248                 }
249                 // Make sure correct photo is selected
250                 if (_photoListModel.getSize() > 0)
251                 {
252                         int photoIndex = _trackInfo.getSelection().getCurrentPhotoIndex();
253                         int listSelection = _photoList.getSelectedIndex();
254                         // Change listbox selection if indexes not equal
255                         if (listSelection != photoIndex)
256                         {
257                                 if (photoIndex < 0) {
258                                         _photoList.clearSelection();
259                                 }
260                                 else {
261                                         _photoList.setSelectedIndex(photoIndex);
262                                 }
263                         }
264                 }
265         }
266 }