3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.event.InputEvent;
6 import java.awt.event.KeyEvent;
8 import javax.swing.ImageIcon;
9 import javax.swing.JButton;
10 import javax.swing.JFrame;
11 import javax.swing.JMenu;
12 import javax.swing.JMenuBar;
13 import javax.swing.JMenuItem;
14 import javax.swing.JToolBar;
15 import javax.swing.KeyStroke;
18 import tim.prune.DataSubscriber;
19 import tim.prune.I18nManager;
20 import tim.prune.data.PhotoList;
21 import tim.prune.data.Selection;
22 import tim.prune.data.Track;
23 import tim.prune.data.TrackInfo;
26 * Class to manage the menu bar and tool bar,
27 * including enabling and disabling the items
29 public class MenuManager implements DataSubscriber
31 private JFrame _parent = null;
32 private App _app = null;
33 private Track _track = null;
34 private Selection _selection = null;
35 private PhotoList _photos = null;
37 // Menu items which need enabling/disabling
38 private JMenuItem _saveItem = null;
39 private JMenuItem _exportKmlItem = null;
40 private JMenuItem _exportGpxItem = null;
41 private JMenuItem _exportPovItem = null;
42 private JMenuItem _undoItem = null;
43 private JMenuItem _clearUndoItem = null;
44 private JMenuItem _editPointItem = null;
45 private JMenuItem _editWaypointNameItem = null;
46 private JMenuItem _deletePointItem = null;
47 private JMenuItem _deleteRangeItem = null;
48 private JMenuItem _deleteDuplicatesItem = null;
49 private JMenuItem _compressItem = null;
50 private JMenuItem _interpolateItem = null;
51 private JMenuItem _selectAllItem = null;
52 private JMenuItem _selectNoneItem = null;
53 private JMenuItem _selectStartItem = null;
54 private JMenuItem _selectEndItem = null;
55 private JMenuItem _reverseItem = null;
56 private JMenu _rearrangeMenu = null;
57 private JMenuItem _rearrangeStartItem = null;
58 private JMenuItem _rearrangeEndItem = null;
59 private JMenuItem _rearrangeNearestItem = null;
60 private JMenuItem _show3dItem = null;
61 private JMenuItem _saveExifItem = null;
62 private JMenuItem _connectPhotoItem = null;
63 private JMenuItem _deletePhotoItem = null;
64 private JMenuItem _disconnectPhotoItem = null;
65 private JMenuItem _correlatePhotosItem = null;
67 // ActionListeners for reuse by menu and toolbar
68 private ActionListener _openFileAction = null;
69 private ActionListener _addPhotoAction = null;
70 private ActionListener _saveAction = null;
71 private ActionListener _undoAction = null;
72 private ActionListener _editPointAction = null;
73 private ActionListener _selectStartAction = null;
74 private ActionListener _selectEndAction = null;
75 private ActionListener _connectPhotoAction = null;
77 // Toolbar buttons which need enabling/disabling
78 private JButton _saveButton = null;
79 private JButton _undoButton = null;
80 private JButton _editPointButton = null;
81 private JButton _selectStartButton = null;
82 private JButton _selectEndButton = null;
83 private JButton _connectPhotoButton = null;
88 * @param inParent parent object for dialogs
89 * @param inApp application to call on menu actions
90 * @param inTrackInfo track info object
92 public MenuManager(JFrame inParent, App inApp, TrackInfo inTrackInfo)
96 _track = inTrackInfo.getTrack();
97 _selection = inTrackInfo.getSelection();
98 _photos = inTrackInfo.getPhotoList();
103 * Create a JMenuBar containing all menu items
106 public JMenuBar createMenuBar()
108 JMenuBar menubar = new JMenuBar();
109 JMenu fileMenu = new JMenu(I18nManager.getText("menu.file"));
111 JMenuItem openMenuItem = new JMenuItem(I18nManager.getText("menu.file.open"));
112 openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
113 _openFileAction = new ActionListener() {
114 public void actionPerformed(ActionEvent e)
119 openMenuItem.addActionListener(_openFileAction);
120 fileMenu.add(openMenuItem);
122 JMenuItem addPhotosMenuItem = new JMenuItem(I18nManager.getText("menu.file.addphotos"));
123 _addPhotoAction = new ActionListener() {
124 public void actionPerformed(ActionEvent e)
129 addPhotosMenuItem.addActionListener(_addPhotoAction);
130 fileMenu.add(addPhotosMenuItem);
132 _saveItem = new JMenuItem(I18nManager.getText("menu.file.save"), KeyEvent.VK_S);
133 _saveAction = new ActionListener() {
134 public void actionPerformed(ActionEvent e)
139 _saveItem.addActionListener(_saveAction);
140 _saveItem.setEnabled(false);
141 fileMenu.add(_saveItem);
143 _exportKmlItem = new JMenuItem(I18nManager.getText("menu.file.exportkml"));
144 _exportKmlItem.addActionListener(new ActionListener() {
145 public void actionPerformed(ActionEvent e)
150 _exportKmlItem.setEnabled(false);
151 fileMenu.add(_exportKmlItem);
153 _exportGpxItem = new JMenuItem(I18nManager.getText("menu.file.exportgpx"));
154 _exportGpxItem.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent e)
160 _exportGpxItem.setEnabled(false);
161 fileMenu.add(_exportGpxItem);
163 _exportPovItem = new JMenuItem(I18nManager.getText("menu.file.exportpov"));
164 _exportPovItem.addActionListener(new ActionListener() {
165 public void actionPerformed(ActionEvent e)
170 _exportPovItem.setEnabled(false);
171 fileMenu.add(_exportPovItem);
172 fileMenu.addSeparator();
173 JMenuItem exitMenuItem = new JMenuItem(I18nManager.getText("menu.file.exit"));
174 exitMenuItem.addActionListener(new ActionListener() {
175 public void actionPerformed(ActionEvent e)
180 fileMenu.add(exitMenuItem);
181 menubar.add(fileMenu);
182 JMenu editMenu = new JMenu(I18nManager.getText("menu.edit"));
183 editMenu.setMnemonic(KeyEvent.VK_E);
184 _undoItem = new JMenuItem(I18nManager.getText("menu.edit.undo"));
185 _undoAction = new ActionListener() {
186 public void actionPerformed(ActionEvent e)
191 _undoItem.addActionListener(_undoAction);
192 _undoItem.setEnabled(false);
193 editMenu.add(_undoItem);
194 _clearUndoItem = new JMenuItem(I18nManager.getText("menu.edit.clearundo"));
195 _clearUndoItem.addActionListener(new ActionListener() {
196 public void actionPerformed(ActionEvent e)
201 _clearUndoItem.setEnabled(false);
202 editMenu.add(_clearUndoItem);
203 editMenu.addSeparator();
204 _editPointItem = new JMenuItem(I18nManager.getText("menu.edit.editpoint"));
205 _editPointAction = new ActionListener() {
206 public void actionPerformed(ActionEvent e)
208 _app.editCurrentPoint();
211 _editPointItem.addActionListener(_editPointAction);
212 _editPointItem.setEnabled(false);
213 editMenu.add(_editPointItem);
214 _editWaypointNameItem = new JMenuItem(I18nManager.getText("menu.edit.editwaypointname"));
215 _editWaypointNameItem.addActionListener(new ActionListener() {
216 public void actionPerformed(ActionEvent e)
218 _app.editCurrentPointName();
221 _editWaypointNameItem.setEnabled(false);
222 editMenu.add(_editWaypointNameItem);
223 _deletePointItem = new JMenuItem(I18nManager.getText("menu.edit.deletepoint"));
224 _deletePointItem.addActionListener(new ActionListener() {
225 public void actionPerformed(ActionEvent e)
227 _app.deleteCurrentPoint();
230 _deletePointItem.setEnabled(false);
231 editMenu.add(_deletePointItem);
232 _deleteRangeItem = new JMenuItem(I18nManager.getText("menu.edit.deleterange"));
233 _deleteRangeItem.addActionListener(new ActionListener() {
234 public void actionPerformed(ActionEvent e)
236 _app.deleteSelectedRange();
239 _deleteRangeItem.setEnabled(false);
240 editMenu.add(_deleteRangeItem);
241 _deleteDuplicatesItem = new JMenuItem(I18nManager.getText("menu.edit.deleteduplicates"));
242 _deleteDuplicatesItem.addActionListener(new ActionListener() {
243 public void actionPerformed(ActionEvent e)
245 _app.deleteDuplicates();
248 _deleteDuplicatesItem.setEnabled(false);
249 editMenu.add(_deleteDuplicatesItem);
250 _compressItem = new JMenuItem(I18nManager.getText("menu.edit.compress"));
251 _compressItem.addActionListener(new ActionListener() {
252 public void actionPerformed(ActionEvent e)
254 _app.compressTrack();
257 _compressItem.setEnabled(false);
258 editMenu.add(_compressItem);
259 editMenu.addSeparator();
260 _interpolateItem = new JMenuItem(I18nManager.getText("menu.edit.interpolate"));
261 _interpolateItem.addActionListener(new ActionListener() {
262 public void actionPerformed(ActionEvent e)
264 _app.interpolateSelection();
267 _interpolateItem.setEnabled(false);
268 editMenu.add(_interpolateItem);
269 _reverseItem = new JMenuItem(I18nManager.getText("menu.edit.reverse"));
270 _reverseItem.addActionListener(new ActionListener() {
271 public void actionPerformed(ActionEvent e)
276 _reverseItem.setEnabled(false);
277 editMenu.add(_reverseItem);
278 // Rearrange waypoints
279 _rearrangeMenu = new JMenu(I18nManager.getText("menu.edit.rearrange"));
280 _rearrangeMenu.setEnabled(false);
281 _rearrangeStartItem = new JMenuItem(I18nManager.getText("menu.edit.rearrange.start"));
282 _rearrangeStartItem.addActionListener(new ActionListener() {
283 public void actionPerformed(ActionEvent e)
285 _app.rearrangeWaypoints(App.REARRANGE_TO_START);
288 _rearrangeStartItem.setEnabled(true);
289 _rearrangeMenu.add(_rearrangeStartItem);
290 _rearrangeEndItem = new JMenuItem(I18nManager.getText("menu.edit.rearrange.end"));
291 _rearrangeEndItem.addActionListener(new ActionListener() {
292 public void actionPerformed(ActionEvent e)
294 _app.rearrangeWaypoints(App.REARRANGE_TO_END);
297 _rearrangeEndItem.setEnabled(true);
298 _rearrangeMenu.add(_rearrangeEndItem);
299 _rearrangeNearestItem = new JMenuItem(I18nManager.getText("menu.edit.rearrange.nearest"));
300 _rearrangeNearestItem.addActionListener(new ActionListener() {
301 public void actionPerformed(ActionEvent e)
303 _app.rearrangeWaypoints(App.REARRANGE_TO_NEAREST);
306 _rearrangeNearestItem.setEnabled(true);
307 _rearrangeMenu.add(_rearrangeNearestItem);
308 editMenu.add(_rearrangeMenu);
309 menubar.add(editMenu);
312 JMenu selectMenu = new JMenu(I18nManager.getText("menu.select"));
313 _selectAllItem = new JMenuItem(I18nManager.getText("menu.select.all"));
314 _selectAllItem.setEnabled(false);
315 _selectAllItem.addActionListener(new ActionListener() {
316 public void actionPerformed(ActionEvent e)
321 selectMenu.add(_selectAllItem);
322 _selectNoneItem = new JMenuItem(I18nManager.getText("menu.select.none"));
323 _selectNoneItem.setEnabled(false);
324 _selectNoneItem.addActionListener(new ActionListener() {
325 public void actionPerformed(ActionEvent e)
330 selectMenu.add(_selectNoneItem);
331 selectMenu.addSeparator();
332 _selectStartItem = new JMenuItem(I18nManager.getText("menu.select.start"));
333 _selectStartItem.setEnabled(false);
334 _selectStartAction = new ActionListener() {
335 public void actionPerformed(ActionEvent e)
337 _selection.selectRangeStart();
340 _selectStartItem.addActionListener(_selectStartAction);
341 selectMenu.add(_selectStartItem);
342 _selectEndItem = new JMenuItem(I18nManager.getText("menu.select.end"));
343 _selectEndItem.setEnabled(false);
344 _selectEndAction = new ActionListener() {
345 public void actionPerformed(ActionEvent e)
347 _selection.selectRangeEnd();
350 _selectEndItem.addActionListener(_selectEndAction);
351 selectMenu.add(_selectEndItem);
352 menubar.add(selectMenu);
355 JMenu photoMenu = new JMenu(I18nManager.getText("menu.photo"));
356 addPhotosMenuItem = new JMenuItem(I18nManager.getText("menu.file.addphotos"));
357 addPhotosMenuItem.addActionListener(_addPhotoAction);
358 photoMenu.add(addPhotosMenuItem);
359 _saveExifItem = new JMenuItem(I18nManager.getText("menu.photo.saveexif"));
360 _saveExifItem.addActionListener(new ActionListener() {
361 public void actionPerformed(ActionEvent e)
366 _saveExifItem.setEnabled(false);
367 photoMenu.add(_saveExifItem);
368 _connectPhotoItem = new JMenuItem(I18nManager.getText("menu.photo.connect"));
369 _connectPhotoAction = new ActionListener() {
370 public void actionPerformed(ActionEvent e)
372 _app.connectPhotoToPoint();
375 _connectPhotoItem.addActionListener(_connectPhotoAction);
376 _connectPhotoItem.setEnabled(false);
377 photoMenu.addSeparator();
378 photoMenu.add(_connectPhotoItem);
380 _disconnectPhotoItem = new JMenuItem(I18nManager.getText("menu.photo.disconnect"));
381 _disconnectPhotoItem.addActionListener(new ActionListener() {
382 public void actionPerformed(ActionEvent e)
384 _app.disconnectPhotoFromPoint();
387 _disconnectPhotoItem.setEnabled(false);
388 photoMenu.add(_disconnectPhotoItem);
389 _deletePhotoItem = new JMenuItem(I18nManager.getText("menu.photo.delete"));
390 _deletePhotoItem.addActionListener(new ActionListener() {
391 public void actionPerformed(ActionEvent e)
393 _app.deleteCurrentPhoto();
396 _deletePhotoItem.setEnabled(false);
397 photoMenu.add(_deletePhotoItem);
398 photoMenu.addSeparator();
399 // correlate all photos
400 _correlatePhotosItem = new JMenuItem(I18nManager.getText("menu.photo.correlate"));
401 _correlatePhotosItem.addActionListener(new ActionListener() {
402 public void actionPerformed(ActionEvent e)
404 _app.beginCorrelatePhotos();
407 _correlatePhotosItem.setEnabled(false);
408 photoMenu.add(_correlatePhotosItem);
409 menubar.add(photoMenu);
411 // Add 3d menu (whether java3d available or not)
412 JMenu threeDMenu = new JMenu(I18nManager.getText("menu.3d"));
413 _show3dItem = new JMenuItem(I18nManager.getText("menu.3d.show3d"));
414 _show3dItem.addActionListener(new ActionListener() {
415 public void actionPerformed(ActionEvent e)
420 _show3dItem.setEnabled(false);
421 threeDMenu.add(_show3dItem);
422 menubar.add(threeDMenu);
425 JMenu helpMenu = new JMenu(I18nManager.getText("menu.help"));
426 JMenuItem helpItem = new JMenuItem(I18nManager.getText("menu.help"));
427 helpItem.addActionListener(new ActionListener() {
428 public void actionPerformed(ActionEvent e)
433 helpMenu.add(helpItem);
434 JMenuItem aboutItem = new JMenuItem(I18nManager.getText("menu.help.about"));
435 aboutItem.addActionListener(new ActionListener() {
436 public void actionPerformed(ActionEvent e)
438 new AboutScreen(_parent).show();
441 helpMenu.add(aboutItem);
442 menubar.add(helpMenu);
449 * Create a JToolBar containing all toolbar buttons
450 * @return toolbar containing buttons
452 public JToolBar createToolBar()
454 JToolBar toolbar = new JToolBar();
456 JButton openFileButton = new JButton(new ImageIcon(getClass().getResource("images/add_textfile_icon.png")));
457 openFileButton.setToolTipText(I18nManager.getText("menu.file.open"));
458 openFileButton.addActionListener(_openFileAction);
459 toolbar.add(openFileButton);
461 JButton addPhotoButton = new JButton(new ImageIcon(getClass().getResource("images/add_photo_icon.png")));
462 addPhotoButton.setToolTipText(I18nManager.getText("menu.file.addphotos"));
463 addPhotoButton.addActionListener(_addPhotoAction);
464 toolbar.add(addPhotoButton);
466 _saveButton = new JButton(new ImageIcon(getClass().getResource("images/save_icon.gif")));
467 _saveButton.setToolTipText(I18nManager.getText("menu.file.save"));
468 _saveButton.addActionListener(_saveAction);
469 _saveButton.setEnabled(false);
470 toolbar.add(_saveButton);
472 _undoButton = new JButton(new ImageIcon(getClass().getResource("images/undo_icon.gif")));
473 _undoButton.setToolTipText(I18nManager.getText("menu.edit.undo"));
474 _undoButton.addActionListener(_undoAction);
475 _undoButton.setEnabled(false);
476 toolbar.add(_undoButton);
478 _editPointButton = new JButton(new ImageIcon(getClass().getResource("images/edit_point_icon.gif")));
479 _editPointButton.setToolTipText(I18nManager.getText("menu.edit.editpoint"));
480 _editPointButton.addActionListener(_editPointAction);
481 _editPointButton.setEnabled(false);
482 toolbar.add(_editPointButton);
484 _selectStartButton = new JButton(new ImageIcon(getClass().getResource("images/set_start_icon.png")));
485 _selectStartButton.setToolTipText(I18nManager.getText("menu.select.start"));
486 _selectStartButton.addActionListener(_selectStartAction);
487 _selectStartButton.setEnabled(false);
488 toolbar.add(_selectStartButton);
489 _selectEndButton = new JButton(new ImageIcon(getClass().getResource("images/set_end_icon.png")));
490 _selectEndButton.setToolTipText(I18nManager.getText("menu.select.end"));
491 _selectEndButton.addActionListener(_selectEndAction);
492 _selectEndButton.setEnabled(false);
493 toolbar.add(_selectEndButton);
494 _connectPhotoButton = new JButton(new ImageIcon(getClass().getResource("images/connect_photo_icon.png")));
495 _connectPhotoButton.setToolTipText(I18nManager.getText("menu.photo.connect"));
496 _connectPhotoButton.addActionListener(_connectPhotoAction);
497 _connectPhotoButton.setEnabled(false);
498 toolbar.add(_connectPhotoButton);
500 toolbar.setFloatable(false);
506 * Method to update menu when file loaded
508 public void informFileLoaded()
510 // save, undo, delete enabled
511 _saveItem.setEnabled(true);
512 _undoItem.setEnabled(true);
513 _deleteDuplicatesItem.setEnabled(true);
514 _compressItem.setEnabled(true);
519 * @see tim.prune.DataSubscriber#dataUpdated(tim.prune.data.Track)
521 public void dataUpdated(byte inUpdateType)
523 boolean hasData = (_track != null && _track.getNumPoints() > 0);
524 // set functions which require data
525 _saveItem.setEnabled(hasData);
526 _saveButton.setEnabled(hasData);
527 _exportKmlItem.setEnabled(hasData);
528 _exportGpxItem.setEnabled(hasData);
529 _exportPovItem.setEnabled(hasData);
530 _deleteDuplicatesItem.setEnabled(hasData);
531 _compressItem.setEnabled(hasData);
532 _rearrangeMenu.setEnabled(hasData && _track.hasMixedData());
533 _selectAllItem.setEnabled(hasData);
534 _selectNoneItem.setEnabled(hasData);
535 if (_show3dItem != null)
536 _show3dItem.setEnabled(hasData);
537 // is undo available?
538 boolean hasUndo = !_app.getUndoStack().isEmpty();
539 _undoItem.setEnabled(hasUndo);
540 _undoButton.setEnabled(hasUndo);
541 _clearUndoItem.setEnabled(hasUndo);
542 // is there a current point?
543 boolean hasPoint = (hasData && _selection.getCurrentPointIndex() >= 0);
544 _editPointItem.setEnabled(hasPoint);
545 _editPointButton.setEnabled(hasPoint);
546 _editWaypointNameItem.setEnabled(hasPoint);
547 _deletePointItem.setEnabled(hasPoint);
548 _selectStartItem.setEnabled(hasPoint);
549 _selectStartButton.setEnabled(hasPoint);
550 _selectEndItem.setEnabled(hasPoint);
551 _selectEndButton.setEnabled(hasPoint);
552 // are there any photos?
553 boolean anyPhotos = _photos != null && _photos.getNumPhotos() > 0;
554 _saveExifItem.setEnabled(anyPhotos);
555 // is there a current photo?
556 boolean hasPhoto = anyPhotos && _selection.getCurrentPhotoIndex() >= 0;
557 // connect is only available when current photo is not connected to current point
558 boolean connectAvailable = hasPhoto && hasPoint
559 && _track.getPoint(_selection.getCurrentPointIndex()).getPhoto() == null;
560 _connectPhotoItem.setEnabled(connectAvailable);
561 _connectPhotoButton.setEnabled(connectAvailable);
562 _disconnectPhotoItem.setEnabled(hasPhoto && _photos.getPhoto(_selection.getCurrentPhotoIndex()) != null
563 && _photos.getPhoto(_selection.getCurrentPhotoIndex()).getDataPoint() != null);
564 _correlatePhotosItem.setEnabled(anyPhotos && hasData);
565 _deletePhotoItem.setEnabled(hasPhoto);
566 // is there a current range?
567 boolean hasRange = (hasData && _selection.hasRangeSelected());
568 _deleteRangeItem.setEnabled(hasRange);
569 _interpolateItem.setEnabled(hasRange
570 && (_selection.getEnd() - _selection.getStart()) == 1);
571 _reverseItem.setEnabled(hasRange);