3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
7 import java.awt.event.AdjustmentEvent;
8 import java.awt.event.AdjustmentListener;
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;
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;
29 * Class to allow selection of points and photos
30 * as a visual component
32 public class SelectorDisplay extends GenericDisplay
35 private JLabel _trackpointsLabel = null;
36 private JLabel _filenameLabel = null;
38 private JScrollBar _scroller = null;
39 private boolean _ignoreScrollEvents = false;
42 private JList _photoList = null;
43 private PhotoListModel _photoListModel = null;
45 private JList _waypointList = null;
46 private WaypointListModel _waypointListModel = null;
49 private static final int SCROLLBAR_INTERVAL = 50;
50 // number of rows in lists
51 private static final int NUM_LIST_ENTRIES = 7;
56 * @param inTrackInfo Track info object
58 public SelectorDisplay(TrackInfo inTrackInfo)
61 setLayout(new BorderLayout());
63 JPanel mainPanel = new JPanel();
64 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
65 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
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))
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);
84 _scroller = new JScrollBar(JScrollBar.HORIZONTAL, 0, SCROLLBAR_INTERVAL, 0, 100);
85 _scroller.addAdjustmentListener(new AdjustmentListener() {
86 public void adjustmentValueChanged(AdjustmentEvent e)
88 selectPoint(e.getValue());
91 _scroller.setEnabled(false);
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))
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)
105 if (!e.getValueIsAdjusting()) selectWaypoint(_waypointList.getSelectedIndex());
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)
115 if (!e.getValueIsAdjusting()) selectPhoto(_photoList.getSelectedIndex());
117 listsPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.photos")));
118 listsPanel.add(new JScrollPane(_photoList));
119 listsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
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);
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));
136 * Select the specified point
137 * @param inValue value to select
139 private void selectPoint(int inValue)
141 if (_track != null && !_ignoreScrollEvents)
143 _trackInfo.getSelection().selectPoint(inValue);
149 * Select the specified photo
150 * @param inPhotoIndex index of selected photo
152 private void selectPhoto(int inPhotoIndex)
154 _trackInfo.selectPhoto(inPhotoIndex);
159 * Select the specified waypoint
160 * @param inWaypointIndex index of selected waypoint
162 private void selectWaypoint(int inWaypointIndex)
164 if (inWaypointIndex >= 0)
166 _trackInfo.selectPoint(_waypointListModel.getWaypoint(inWaypointIndex));
172 * Notification that Track has been updated
174 public void dataUpdated(byte inUpdateType)
177 if (_track == null || _track.getNumPoints() <= 0)
179 _trackpointsLabel.setText(I18nManager.getText("details.notrack"));
180 _filenameLabel.setText("");
184 _trackpointsLabel.setText(I18nManager.getText("details.track.points") + ": "
185 + _track.getNumPoints());
186 int numFiles = _trackInfo.getFileInfo().getNumFiles();
189 _filenameLabel.setText(I18nManager.getText("details.track.file") + ": "
190 + _trackInfo.getFileInfo().getFilename());
192 else if (numFiles > 1)
194 _filenameLabel.setText(I18nManager.getText("details.track.numfiles") + ": "
197 else _filenameLabel.setText("");
200 // Update scroller settings
201 int currentPointIndex = _trackInfo.getSelection().getCurrentPointIndex();
202 _ignoreScrollEvents = true;
203 if (_track == null || _track.getNumPoints() < 2)
205 // careful to avoid event loops here
206 // _scroller.setValue(0);
207 _scroller.setEnabled(false);
211 _scroller.setMaximum(_track.getNumPoints() + SCROLLBAR_INTERVAL);
212 if (currentPointIndex >= 0)
213 _scroller.setValue(currentPointIndex);
214 _scroller.setEnabled(true);
216 _ignoreScrollEvents = false;
218 // update waypoints and photos if necessary
220 (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.WAYPOINTS_MODIFIED)) > 0)
222 _waypointListModel.fireChanged();
225 (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.PHOTOS_MODIFIED)) > 0)
227 _photoListModel.fireChanged();
229 // Deselect selected waypoint if selected point has since changed
230 if (_waypointList.getSelectedIndex() >= 0)
232 if (_trackInfo.getCurrentPoint() == null
233 || !_waypointListModel.getWaypoint(_waypointList.getSelectedIndex()).equals(_trackInfo.getCurrentPoint()))
235 // point is selected in list but different from current point - deselect
236 _waypointList.clearSelection();
239 // Do the same for the photos
240 if (_photoList.getSelectedIndex() >= 0)
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();
249 // Compare selected photo with selected point
250 if ( (photoPoint != null && (trackPoint == null || !photoPoint.equals(trackPoint)))
251 || (_trackInfo.getSelection().getCurrentPhotoIndex() < 0) )
253 // photo is selected in list but different from current point - deselect
254 _photoList.clearSelection();
255 _trackInfo.getSelection().deselectPhoto();