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.TrackInfo;
28 * Class to allow selection of points and photos
29 * as a visual component
31 public class SelectorDisplay extends GenericDisplay
34 private JLabel _trackpointsLabel = null;
35 private JLabel _filenameLabel = null;
37 private JScrollBar _scroller = null;
38 private boolean _ignoreScrollEvents = false;
41 private JList _photoList = null;
42 private PhotoListModel _photoListModel = null;
44 private JList _waypointList = null;
45 private WaypointListModel _waypointListModel = null;
48 private static final int SCROLLBAR_INTERVAL = 50;
49 // number of rows in lists
50 private static final int NUM_LIST_ENTRIES = 7;
55 * @param inTrackInfo Track info object
57 public SelectorDisplay(TrackInfo inTrackInfo)
60 setLayout(new BorderLayout());
62 JPanel mainPanel = new JPanel();
63 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
64 mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
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))
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);
83 _scroller = new JScrollBar(JScrollBar.HORIZONTAL, 0, SCROLLBAR_INTERVAL, 0, 100);
84 _scroller.addAdjustmentListener(new AdjustmentListener() {
85 public void adjustmentValueChanged(AdjustmentEvent e)
87 selectPoint(e.getValue());
90 _scroller.setEnabled(false);
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))
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)
104 if (!e.getValueIsAdjusting()) selectWaypoint(_waypointList.getSelectedIndex());
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);
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)
118 if (!e.getValueIsAdjusting()) {
119 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.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() -1 + 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 // Make sure correct photo is selected
250 if (_photoListModel.getSize() > 0)
252 int photoIndex = _trackInfo.getSelection().getCurrentPhotoIndex();
253 int listSelection = _photoList.getSelectedIndex();
254 // Change listbox selection if indexes not equal
255 if (listSelection != photoIndex)
257 if (photoIndex < 0) {
258 _photoList.clearSelection();
261 _photoList.setSelectedIndex(photoIndex);