3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
7 import java.awt.GridLayout;
8 import java.awt.event.AdjustmentEvent;
9 import java.awt.event.AdjustmentListener;
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;
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;
30 * Class to allow selection of points and photos
31 * as a visual component
33 public class SelectorDisplay extends GenericDisplay
36 private JLabel _trackpointsLabel = null;
37 private JLabel _filenameLabel = null;
39 private JScrollBar _scroller = null;
40 private boolean _ignoreScrollEvents = false;
43 private JList _photoList = null;
44 private PhotoListModel _photoListModel = null;
46 private JList _waypointList = null;
47 private WaypointListModel _waypointListModel = null;
50 private static final int SCROLLBAR_INTERVAL = 50;
51 // number of rows in lists
52 private static final int NUM_LIST_ENTRIES = 7;
57 * @param inTrackInfo Track info object
59 public SelectorDisplay(TrackInfo inTrackInfo)
62 setLayout(new BorderLayout());
64 JPanel mainPanel = new JPanel();
65 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
66 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
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))
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);
85 _scroller = new JScrollBar(JScrollBar.HORIZONTAL, 0, SCROLLBAR_INTERVAL, 0, 100);
86 _scroller.addAdjustmentListener(new AdjustmentListener() {
87 public void adjustmentValueChanged(AdjustmentEvent e)
89 selectPoint(e.getValue());
92 _scroller.setEnabled(false);
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))
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)
106 if (!e.getValueIsAdjusting()) selectWaypoint(_waypointList.getSelectedIndex());
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);
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)
120 if (!e.getValueIsAdjusting()) selectPhoto(_photoList.getSelectedIndex());
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);
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));
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));
145 * Select the specified point
146 * @param inValue value to select
148 private void selectPoint(int inValue)
150 if (_track != null && !_ignoreScrollEvents)
152 _trackInfo.getSelection().selectPoint(inValue);
158 * Select the specified photo
159 * @param inPhotoIndex index of selected photo
161 private void selectPhoto(int inPhotoIndex)
163 _trackInfo.selectPhoto(inPhotoIndex);
168 * Select the specified waypoint
169 * @param inWaypointIndex index of selected waypoint
171 private void selectWaypoint(int inWaypointIndex)
173 if (inWaypointIndex >= 0)
175 _trackInfo.selectPoint(_waypointListModel.getWaypoint(inWaypointIndex));
181 * Notification that Track has been updated
183 public void dataUpdated(byte inUpdateType)
186 if (_track == null || _track.getNumPoints() <= 0)
188 _trackpointsLabel.setText(I18nManager.getText("details.notrack"));
189 _filenameLabel.setText("");
193 _trackpointsLabel.setText(I18nManager.getText("details.track.points") + ": "
194 + _track.getNumPoints());
195 int numFiles = _trackInfo.getFileInfo().getNumFiles();
198 _filenameLabel.setText(I18nManager.getText("details.track.file") + ": "
199 + _trackInfo.getFileInfo().getFilename());
201 else if (numFiles > 1)
203 _filenameLabel.setText(I18nManager.getText("details.track.numfiles") + ": "
206 else _filenameLabel.setText("");
209 // Update scroller settings
210 int currentPointIndex = _trackInfo.getSelection().getCurrentPointIndex();
211 _ignoreScrollEvents = true;
212 if (_track == null || _track.getNumPoints() < 2)
214 // careful to avoid event loops here
215 // _scroller.setValue(0);
216 _scroller.setEnabled(false);
220 _scroller.setMaximum(_track.getNumPoints() + SCROLLBAR_INTERVAL);
221 if (currentPointIndex >= 0)
222 _scroller.setValue(currentPointIndex);
223 _scroller.setEnabled(true);
225 _ignoreScrollEvents = false;
227 // update waypoints and photos if necessary
229 (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.WAYPOINTS_MODIFIED)) > 0)
231 _waypointListModel.fireChanged();
234 (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.PHOTOS_MODIFIED)) > 0)
236 _photoListModel.fireChanged();
238 // Deselect selected waypoint if selected point has since changed
239 if (_waypointList.getSelectedIndex() >= 0)
241 if (_trackInfo.getCurrentPoint() == null
242 || _waypointList.getSelectedIndex() >= _waypointListModel.getSize()
243 || !_waypointListModel.getWaypoint(_waypointList.getSelectedIndex()).equals(_trackInfo.getCurrentPoint()))
245 // point is selected in list but different from current point - deselect
246 _waypointList.clearSelection();
249 // Do the same for the photos
250 if (_photoList.getSelectedIndex() >= 0)
252 DataPoint trackPoint = _trackInfo.getCurrentPoint();
253 Photo selectedPhoto = _photoListModel.getPhoto(_photoList.getSelectedIndex());
254 // Get selected Photo, if it's still there
255 DataPoint photoPoint = null;
256 if (selectedPhoto != null) {
257 photoPoint = _photoListModel.getPhoto(_photoList.getSelectedIndex()).getDataPoint();
259 // Compare selected photo with selected point
260 if ( (photoPoint != null && (trackPoint == null || !photoPoint.equals(trackPoint)))
261 || (_trackInfo.getSelection().getCurrentPhotoIndex() < 0) )
263 // photo is selected in list but different from current point - deselect
264 _photoList.clearSelection();
265 _trackInfo.getSelection().deselectPhoto();