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.ListSelectionModel;
20 import javax.swing.border.EtchedBorder;
21 import javax.swing.event.ListSelectionEvent;
22 import javax.swing.event.ListSelectionListener;
24 import tim.prune.DataSubscriber;
25 import tim.prune.I18nManager;
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 GridLayout(0, 1));
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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
118 _photoList.addListSelectionListener(new ListSelectionListener() {
119 public void valueChanged(ListSelectionEvent e)
121 if (!e.getValueIsAdjusting()) {
122 selectPhoto(_photoList.getSelectedIndex());
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);
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));
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));
148 * Select the specified point
149 * @param inValue value to select
151 private void selectPoint(int inValue)
153 if (_track != null && !_ignoreScrollEvents)
155 _trackInfo.selectPoint(inValue);
161 * Select the specified photo
162 * @param inPhotoIndex index of selected photo
164 private void selectPhoto(int inPhotoIndex)
166 _trackInfo.selectPhoto(inPhotoIndex);
171 * Select the specified waypoint
172 * @param inWaypointIndex index of selected waypoint
174 private void selectWaypoint(int inWaypointIndex)
176 if (inWaypointIndex >= 0)
178 _trackInfo.selectPoint(_waypointListModel.getWaypoint(inWaypointIndex));
184 * Notification that Track has been updated
186 public void dataUpdated(byte inUpdateType)
189 if (_track == null || _track.getNumPoints() <= 0)
191 _trackpointsLabel.setText(I18nManager.getText("details.notrack"));
192 _filenameLabel.setText("");
196 _trackpointsLabel.setText(I18nManager.getText("details.track.points") + ": "
197 + _track.getNumPoints());
198 int numFiles = _trackInfo.getFileInfo().getNumFiles();
201 _filenameLabel.setText(I18nManager.getText("details.track.file") + ": "
202 + _trackInfo.getFileInfo().getFilename());
204 else if (numFiles > 1)
206 _filenameLabel.setText(I18nManager.getText("details.track.numfiles") + ": "
209 else _filenameLabel.setText("");
212 // Update scroller settings
213 int currentPointIndex = _trackInfo.getSelection().getCurrentPointIndex();
214 _ignoreScrollEvents = true;
215 if (_track == null || _track.getNumPoints() < 2)
217 // careful to avoid event loops here
218 // _scroller.setValue(0);
219 _scroller.setEnabled(false);
223 _scroller.setMaximum(_track.getNumPoints() -1 + SCROLLBAR_INTERVAL);
224 if (currentPointIndex >= 0)
225 _scroller.setValue(currentPointIndex);
226 _scroller.setEnabled(true);
228 _ignoreScrollEvents = false;
230 // update waypoints and photos if necessary
232 (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.WAYPOINTS_MODIFIED)) > 0)
234 _waypointListModel.fireChanged();
237 (DataSubscriber.DATA_ADDED_OR_REMOVED | DataSubscriber.DATA_EDITED | DataSubscriber.PHOTOS_MODIFIED)) > 0)
239 _photoListModel.fireChanged();
241 // Deselect selected waypoint if selected point has since changed
242 if (_waypointList.getSelectedIndex() >= 0)
244 if (_trackInfo.getCurrentPoint() == null
245 || _waypointList.getSelectedIndex() >= _waypointListModel.getSize()
246 || !_waypointListModel.getWaypoint(_waypointList.getSelectedIndex()).equals(_trackInfo.getCurrentPoint()))
248 // point is selected in list but different from current point - deselect
249 _waypointList.clearSelection();
252 // Make sure correct photo is selected
253 if (_photoListModel.getSize() > 0)
255 int photoIndex = _trackInfo.getSelection().getCurrentPhotoIndex();
256 int listSelection = _photoList.getSelectedIndex();
257 // Change listbox selection if indexes not equal
258 if (listSelection != photoIndex)
260 if (photoIndex < 0) {
261 _photoList.clearSelection();
264 _photoList.setSelectedIndex(photoIndex);