source: josm/trunk/src/org/openstreetmap/josm/gui/MainMenu.java@ 6246

Last change on this file since 6246 was 6246, checked in by Don-vip, 11 years ago

Sonar/FindBugs - various bugfixes / violation fixes

  • Property svn:eol-style set to native
File size: 33.5 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.Component;
9import java.awt.Toolkit;
10import java.awt.event.KeyEvent;
11import java.util.HashMap;
12import java.util.Map;
13
14import javax.swing.JCheckBoxMenuItem;
15import javax.swing.JMenu;
16import javax.swing.JMenuBar;
17import javax.swing.JMenuItem;
18import javax.swing.JPopupMenu;
19import javax.swing.JSeparator;
20import javax.swing.KeyStroke;
21import javax.swing.event.MenuEvent;
22import javax.swing.event.MenuListener;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.actions.AboutAction;
26import org.openstreetmap.josm.actions.AddNodeAction;
27import org.openstreetmap.josm.actions.AlignInCircleAction;
28import org.openstreetmap.josm.actions.AlignInLineAction;
29import org.openstreetmap.josm.actions.AutoScaleAction;
30import org.openstreetmap.josm.actions.ChangesetManagerToggleAction;
31import org.openstreetmap.josm.actions.CloseChangesetAction;
32import org.openstreetmap.josm.actions.CombineWayAction;
33import org.openstreetmap.josm.actions.CopyAction;
34import org.openstreetmap.josm.actions.CopyCoordinatesAction;
35import org.openstreetmap.josm.actions.CreateCircleAction;
36import org.openstreetmap.josm.actions.CreateMultipolygonAction;
37import org.openstreetmap.josm.actions.DeleteAction;
38import org.openstreetmap.josm.actions.DialogsToggleAction;
39import org.openstreetmap.josm.actions.DistributeAction;
40import org.openstreetmap.josm.actions.DownloadAction;
41import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
42import org.openstreetmap.josm.actions.DownloadReferrersAction;
43import org.openstreetmap.josm.actions.DuplicateAction;
44import org.openstreetmap.josm.actions.ExitAction;
45import org.openstreetmap.josm.actions.ExpertToggleAction;
46import org.openstreetmap.josm.actions.FollowLineAction;
47import org.openstreetmap.josm.actions.FullscreenToggleAction;
48import org.openstreetmap.josm.actions.GpxExportAction;
49import org.openstreetmap.josm.actions.HelpAction;
50import org.openstreetmap.josm.actions.HistoryInfoAction;
51import org.openstreetmap.josm.actions.HistoryInfoWebAction;
52import org.openstreetmap.josm.actions.InfoAction;
53import org.openstreetmap.josm.actions.InfoWebAction;
54import org.openstreetmap.josm.actions.JoinAreasAction;
55import org.openstreetmap.josm.actions.JoinNodeWayAction;
56import org.openstreetmap.josm.actions.JosmAction;
57import org.openstreetmap.josm.actions.JumpToAction;
58import org.openstreetmap.josm.actions.MergeLayerAction;
59import org.openstreetmap.josm.actions.MergeNodesAction;
60import org.openstreetmap.josm.actions.MergeSelectionAction;
61import org.openstreetmap.josm.actions.MirrorAction;
62import org.openstreetmap.josm.actions.MoveAction;
63import org.openstreetmap.josm.actions.MoveNodeAction;
64import org.openstreetmap.josm.actions.NewAction;
65import org.openstreetmap.josm.actions.OpenFileAction;
66import org.openstreetmap.josm.actions.OpenLocationAction;
67import org.openstreetmap.josm.actions.OrthogonalizeAction;
68import org.openstreetmap.josm.actions.OrthogonalizeAction.Undo;
69import org.openstreetmap.josm.actions.PasteAction;
70import org.openstreetmap.josm.actions.PasteTagsAction;
71import org.openstreetmap.josm.actions.PreferenceToggleAction;
72import org.openstreetmap.josm.actions.PreferencesAction;
73import org.openstreetmap.josm.actions.PurgeAction;
74import org.openstreetmap.josm.actions.RedoAction;
75import org.openstreetmap.josm.actions.RestartAction;
76import org.openstreetmap.josm.actions.ReverseWayAction;
77import org.openstreetmap.josm.actions.SaveAction;
78import org.openstreetmap.josm.actions.SaveAsAction;
79import org.openstreetmap.josm.actions.SelectAllAction;
80import org.openstreetmap.josm.actions.SessionLoadAction;
81import org.openstreetmap.josm.actions.SessionSaveAsAction;
82import org.openstreetmap.josm.actions.ShowStatusReportAction;
83import org.openstreetmap.josm.actions.SimplifyWayAction;
84import org.openstreetmap.josm.actions.SplitWayAction;
85import org.openstreetmap.josm.actions.ToggleGPXLinesAction;
86import org.openstreetmap.josm.actions.UnGlueAction;
87import org.openstreetmap.josm.actions.UndoAction;
88import org.openstreetmap.josm.actions.UnselectAllAction;
89import org.openstreetmap.josm.actions.UpdateDataAction;
90import org.openstreetmap.josm.actions.UpdateModifiedAction;
91import org.openstreetmap.josm.actions.UpdateSelectionAction;
92import org.openstreetmap.josm.actions.UploadAction;
93import org.openstreetmap.josm.actions.UploadSelectionAction;
94import org.openstreetmap.josm.actions.ViewportFollowToggleAction;
95import org.openstreetmap.josm.actions.WireframeToggleAction;
96import org.openstreetmap.josm.actions.ZoomInAction;
97import org.openstreetmap.josm.actions.ZoomOutAction;
98import org.openstreetmap.josm.actions.audio.AudioBackAction;
99import org.openstreetmap.josm.actions.audio.AudioFasterAction;
100import org.openstreetmap.josm.actions.audio.AudioFwdAction;
101import org.openstreetmap.josm.actions.audio.AudioNextAction;
102import org.openstreetmap.josm.actions.audio.AudioPlayPauseAction;
103import org.openstreetmap.josm.actions.audio.AudioPrevAction;
104import org.openstreetmap.josm.actions.audio.AudioSlowerAction;
105import org.openstreetmap.josm.actions.search.SearchAction;
106import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
107import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
108import org.openstreetmap.josm.gui.io.RecentlyOpenedFilesMenu;
109import org.openstreetmap.josm.gui.layer.Layer;
110import org.openstreetmap.josm.gui.mappaint.MapPaintMenu;
111import org.openstreetmap.josm.gui.tagging.TaggingPresetSearchAction;
112import org.openstreetmap.josm.tools.ImageProvider;
113import org.openstreetmap.josm.tools.Shortcut;
114
115/**
116 * This is the JOSM main menu bar. It is overwritten to initialize itself and provide all menu
117 * entries as member variables (sort of collect them).
118 *
119 * It also provides possibilities to attach new menu entries (used by plugins).
120 *
121 * @author Immanuel.Scholz
122 */
123public class MainMenu extends JMenuBar {
124
125 /* File menu */
126 /** File -> New Layer **/
127 public final NewAction newAction = new NewAction();
128 /** File -> Open... **/
129 public final OpenFileAction openFile = new OpenFileAction();
130 /** File -> Open Recent > **/
131 public final RecentlyOpenedFilesMenu recentlyOpened = new RecentlyOpenedFilesMenu();
132 /** File -> Open Location... **/
133 public final OpenLocationAction openLocation = new OpenLocationAction();
134 /** File -> Save **/
135 public final SaveAction save = SaveAction.getInstance();
136 /** File -> Save As... **/
137 public final SaveAsAction saveAs = SaveAsAction.getInstance();
138 /** File -> Session > Load Session **/
139 public SessionLoadAction sessionLoad;
140 /** File -> Session > Save Session As... **/
141 public SessionSaveAsAction sessionSaveAs;
142 /** File -> Export to GPX... **/
143 public final GpxExportAction gpxExport = new GpxExportAction();
144 /** File -> Download from OSM... **/
145 public final DownloadAction download = new DownloadAction();
146 /** File -> Download object... **/
147 public final DownloadPrimitiveAction downloadPrimitive = new DownloadPrimitiveAction();
148 /** File -> Download parent ways/relations... **/
149 public final DownloadReferrersAction downloadReferrers = new DownloadReferrersAction();
150 /** File -> Close open changesets... **/
151 public final CloseChangesetAction closeChangesetAction = new CloseChangesetAction();
152 /** File -> Update data **/
153 public final JosmAction update = new UpdateDataAction();
154 /** File -> Update selection **/
155 public final JosmAction updateSelection = new UpdateSelectionAction();
156 /** File -> Update modified **/
157 public final JosmAction updateModified = new UpdateModifiedAction();
158 /** File -> Upload data **/
159 public final JosmAction upload = new UploadAction();
160 /** File -> Upload selection **/
161 public final JosmAction uploadSelection = new UploadSelectionAction();
162 /** File -> Restart **/
163 public final RestartAction restart = new RestartAction();
164 /** File -> Exit **/
165 public final ExitAction exit = new ExitAction();
166
167 /* Edit menu */
168 /** Edit -> Undo... */
169 public final UndoAction undo = new UndoAction();
170 /** Edit -> Redo */
171 public final RedoAction redo = new RedoAction();
172 /** Edit -> Copy */
173 public final CopyAction copy = new CopyAction();
174 /** Edit -> Copy Coordinates */
175 public final JosmAction copyCoordinates = new CopyCoordinatesAction();
176 /** Edit -> Paste */
177 public final PasteAction paste = new PasteAction();
178 /** Edit -> Paste Tags */
179 public final PasteTagsAction pasteTags = new PasteTagsAction();
180 /** Edit -> Duplicate */
181 public final DuplicateAction duplicate = new DuplicateAction();
182 /** Edit -> Delete */
183 public final DeleteAction delete = new DeleteAction();
184 /** Edit -> Purge... */
185 public final JosmAction purge = new PurgeAction();
186 /** Edit -> Merge layer */
187 public final MergeLayerAction merge = new MergeLayerAction();
188 /** Edit -> Merge selection */
189 public final MergeSelectionAction mergeSelected = new MergeSelectionAction();
190 /** Edit -> Search... */
191 public final SearchAction search = new SearchAction();
192 /** Edit -> Preferences */
193 public final PreferencesAction preferences = new PreferencesAction();
194
195 /* View menu */
196 /** View -> Wireframe View */
197 public final WireframeToggleAction wireFrameToggleAction = new WireframeToggleAction();
198 public final JosmAction toggleGPXLines = new ToggleGPXLinesAction();
199 /** View -> Advanced info */
200 public final InfoAction info = new InfoAction();
201 /** View -> Advanced info (web) */
202 public final InfoWebAction infoweb = new InfoWebAction();
203 /** View -> History */
204 public final HistoryInfoAction historyinfo = new HistoryInfoAction();
205 /** View -> History (web) */
206 public final HistoryInfoWebAction historyinfoweb = new HistoryInfoWebAction();
207 /** View -> "Zoom to"... actions */
208 public final Map<String, AutoScaleAction> autoScaleActions = new HashMap<String, AutoScaleAction>();
209
210 /* Tools menu */
211 public final JosmAction splitWay = new SplitWayAction();
212 public final JosmAction combineWay = new CombineWayAction();
213 public final JosmAction reverseWay = new ReverseWayAction();
214 public final JosmAction simplifyWay = new SimplifyWayAction();
215 public final JosmAction alignInCircle = new AlignInCircleAction();
216 public final JosmAction alignInLine = new AlignInLineAction();
217 public final JosmAction distribute = new DistributeAction();
218 public final OrthogonalizeAction ortho = new OrthogonalizeAction();
219 public final JosmAction orthoUndo = new Undo(); // action is not shown in the menu. Only triggered by shortcut
220 public final JosmAction mirror = new MirrorAction();
221 public final JosmAction followLine = new FollowLineAction();
222 public final AddNodeAction addnode = new AddNodeAction();
223 public final MoveNodeAction movenode = new MoveNodeAction();
224 public final JosmAction createCircle = new CreateCircleAction();
225 public final JosmAction mergeNodes = new MergeNodesAction();
226 public final JosmAction joinNodeWay = new JoinNodeWayAction();
227 public final JosmAction unglueNodes = new UnGlueAction();
228 public final JosmAction joinAreas = new JoinAreasAction();
229 public final JosmAction createMultipolygon = new CreateMultipolygonAction();
230
231 /* Selection menu */
232 /** Selection -> Select All */
233 public final SelectAllAction selectAll = new SelectAllAction();
234 /** Selection -> Unselect All */
235 public final UnselectAllAction unselectAll = new UnselectAllAction();
236
237 /* Audio menu */
238 public final JosmAction audioPlayPause = new AudioPlayPauseAction();
239 public final JosmAction audioNext = new AudioNextAction();
240 public final JosmAction audioPrev = new AudioPrevAction();
241 public final JosmAction audioFwd = new AudioFwdAction();
242 public final JosmAction audioBack = new AudioBackAction();
243 public final JosmAction audioFaster = new AudioFasterAction();
244 public final JosmAction audioSlower = new AudioSlowerAction();
245
246 /* Windows Menu */
247 /** Windows -> Changeset Manager */
248 public final ChangesetManagerToggleAction changesetManager = new ChangesetManagerToggleAction();
249
250 /* Help menu */
251 /** Help -> Help */
252 public final HelpAction help = new HelpAction();
253 /** Help -> About */
254 public final AboutAction about = new AboutAction();
255 /** Help -> Show Status Report */
256 public final ShowStatusReportAction statusreport = new ShowStatusReportAction();
257
258 /**
259 * fileMenu contains I/O actions
260 */
261 public final JMenu fileMenu = addMenu(marktr("File"), KeyEvent.VK_F, 0, ht("/Menu/File"));
262 /**
263 * sessionMenu is a submenu of File menu containing all session actions
264 */
265 public final JMenu sessionMenu = new JMenu(tr("Session"));
266 /**
267 * editMenu contains editing actions
268 */
269 public final JMenu editMenu = addMenu(marktr("Edit"), KeyEvent.VK_E, 1, ht("/Menu/Edit"));
270 /**
271 * viewMenu contains display actions (zoom, map styles, etc.)
272 */
273 public final JMenu viewMenu = addMenu(marktr("View"), KeyEvent.VK_V, 2, ht("/Menu/View"));
274 /**
275 * toolsMenu contains different geometry manipulation actions from JOSM core (most used)
276 * The plugins should use other menus
277 */
278 public final JMenu toolsMenu = addMenu(marktr("Tools"), KeyEvent.VK_T, 3, ht("/Menu/Tools"));
279 /**
280 * moreToolsMenu contains geometry-related actions from all the plugins
281 * @since 6082 (moved from Utilsplugin2)
282 */
283 public final JMenu moreToolsMenu = addMenu(marktr("More tools"), KeyEvent.VK_M, 4, ht("/Menu/MoreTools"));
284 /**
285 * dataMenu contains plugin actions that are related to certain tagging schemes (addressing opening hours),
286 * importing external data and using external web APIs
287 * @since 6082
288 */
289 public final JMenu dataMenu = addMenu(marktr("Data"), KeyEvent.VK_D, 5, ht("/Menu/Data"));
290 /**
291 * selectionMenu contains all actions related to selecting different objects
292 * @since 6082 (moved from Utilsplugin2)
293 */
294 public final JMenu selectionMenu = addMenu(marktr("Selection"), KeyEvent.VK_N, 6, ht("/Menu/Selection"));
295 /**
296 * presetsMenu contains presets actions (search, presets tree)
297 */
298 public final JMenu presetsMenu = addMenu(marktr("Presets"), KeyEvent.VK_P, 7, ht("/Menu/Presets"));
299 /**
300 * submenu in Imagery menu that contains plugin-managed additional imagery layers
301 * @since 6097
302 */
303 public final JMenu imagerySubMenu = new JMenu(tr("More..."));
304 /**
305 * imageryMenu contains all imagery-related actions
306 */
307 public final ImageryMenu imageryMenu = addMenu(new ImageryMenu(imagerySubMenu), marktr("Imagery"), KeyEvent.VK_I, 8, ht("/Menu/Imagery"));
308 /**
309 * gpsMenu contains all plugin actions that are related
310 * to using GPS data, including opening, uploading and real-time tracking
311 * @since 6082
312 */
313 public final JMenu gpsMenu = addMenu(marktr("GPS"), KeyEvent.VK_G, 9, ht("/Menu/GPS"));
314 /** the window menu is split into several groups. The first is for windows that can be opened from
315 * this menu any time, e.g. the changeset editor. The second group is for toggle dialogs and the third
316 * group is for currently open windows that cannot be toggled, e.g. relation editors. It's recommended
317 * to use WINDOW_MENU_GROUP to determine the group integer.
318 */
319 public final JMenu windowMenu = addMenu(marktr("Windows"), KeyEvent.VK_W, 10, ht("/Menu/Windows"));
320 public static enum WINDOW_MENU_GROUP { ALWAYS, TOGGLE_DIALOG, VOLATILE }
321
322 /**
323 * audioMenu contains all audio-related actions. Be careful, this menu is not guaranteed to be displayed at all
324 */
325 public JMenu audioMenu = null;
326 /**
327 * helpMenu contains JOSM general actions (Help, About, etc.)
328 */
329 public final JMenu helpMenu = addMenu(marktr("Help"), KeyEvent.VK_H, 11, ht("/Menu/Help"));
330
331 private static final int defaultMenuPos = 11;
332
333 public final JosmAction moveUpAction = new MoveAction(MoveAction.Direction.UP);
334 public final JosmAction moveDownAction = new MoveAction(MoveAction.Direction.DOWN);
335 public final JosmAction moveLeftAction = new MoveAction(MoveAction.Direction.LEFT);
336 public final JosmAction moveRightAction = new MoveAction(MoveAction.Direction.RIGHT);
337 public final JumpToAction jumpToAct = new JumpToAction();
338
339 public final TaggingPresetSearchAction presetSearchAction = new TaggingPresetSearchAction();
340 public final DialogsToggleAction dialogsToggleAction = new DialogsToggleAction();
341 public FullscreenToggleAction fullscreenToggleAction = null;
342
343 /** this menu listener hides unnecessary JSeparators in a menu list but does not remove them.
344 * If at a later time the separators are required, they will be made visible again. Intended
345 * usage is make menus not look broken if separators are used to group the menu and some of
346 * these groups are empty.
347 */
348 public final static MenuListener menuSeparatorHandler = new MenuListener() {
349 @Override
350 public void menuCanceled(MenuEvent arg0) {}
351 @Override
352 public void menuDeselected(MenuEvent arg0) {}
353 @Override
354 public void menuSelected(MenuEvent a) {
355 if(!(a.getSource() instanceof JMenu))
356 return;
357 final JPopupMenu m = ((JMenu) a.getSource()).getPopupMenu();
358 for(int i=0; i < m.getComponentCount()-1; i++) {
359 if(!(m.getComponent(i) instanceof JSeparator)) {
360 continue;
361 }
362 // hide separator if the next menu item is one as well
363 ((JSeparator) m.getComponent(i)).setVisible(!(m.getComponent(i+1) instanceof JSeparator));
364 }
365 // hide separator at the end of the menu
366 if(m.getComponent(m.getComponentCount()-1) instanceof JSeparator) {
367 ((JSeparator) m.getComponent(m.getComponentCount()-1)).setVisible(false);
368 }
369 }
370 };
371
372 /**
373 * @since 6088
374 * @return the default position of tnew top-level menus
375 */
376 public int getDefaultMenuPos() {
377 return defaultMenuPos;
378 }
379
380 /**
381 * Add a JosmAction at the end of a menu.
382 *
383 * This method handles all the shortcut handling. It also makes sure that actions that are
384 * handled by the OS are not duplicated on the menu.
385 * @param menu the menu to add the action to
386 * @param action the action that should get a menu item
387 * @return the created menu item
388 */
389 public static JMenuItem add(JMenu menu, JosmAction action) {
390 return add(menu, action, false);
391 }
392
393 /**
394 * Add a JosmAction at the end of a menu.
395 *
396 * This method handles all the shortcut handling. It also makes sure that actions that are
397 * handled by the OS are not duplicated on the menu.
398 * @param menu the menu to add the action to
399 * @param action the action that should get a menu item
400 * @param isExpert whether the entry should only be visible if the expert mode is activated
401 * @return the created menu item
402 */
403 public static JMenuItem add(JMenu menu, JosmAction action, boolean isExpert) {
404 return add(menu, action, isExpert, null);
405 }
406
407 /**
408 * Add a JosmAction at the end of a menu.
409 *
410 * This method handles all the shortcut handling. It also makes sure that actions that are
411 * handled by the OS are not duplicated on the menu.
412 * @param menu the menu to add the action to
413 * @param action the action that should get a menu item
414 * @param isExpert whether the entry should only be visible if the expert mode is activated
415 * @param index an integer specifying the position at which to add the action
416 * @return the created menu item
417 */
418 public static JMenuItem add(JMenu menu, JosmAction action, boolean isExpert, Integer index) {
419 if (action.getShortcut().getAutomatic())
420 return null;
421 final JMenuItem menuitem;
422 if (index == null) {
423 menuitem = menu.add(action);
424 } else {
425 menuitem = menu.insert(action, index);
426 }
427 if (isExpert) {
428 ExpertToggleAction.addVisibilitySwitcher(menuitem);
429 }
430 KeyStroke ks = action.getShortcut().getKeyStroke();
431 if (ks != null) {
432 menuitem.setAccelerator(ks);
433 }
434 // some menus are hidden before they are populated with some items by plugins
435 if (!menu.isVisible()) menu.setVisible(true);
436 return menuitem;
437 }
438
439 /**
440 * Add the JosmAction {@code actionToBeInserted} directly below {@code existingMenuEntryAction}.
441 *
442 * This method handles all the shortcut handling. It also makes sure that actions that are
443 * handled by the OS are not duplicated on the menu.
444 * @param menu the menu to add the action to
445 * @param actionToBeInserted the action that should get a menu item directly below {@code existingMenuEntryAction}
446 * @param isExpert whether the entry should only be visible if the expert mode is activated
447 * @param existingMenuEntryAction an action already added to the menu {@code menu}, the action {@code actionToBeInserted} is added directly below
448 * @return the created menu item
449 */
450 public static JMenuItem addAfter(JMenu menu, JosmAction actionToBeInserted, boolean isExpert, JosmAction existingMenuEntryAction) {
451 int i = 0;
452 for (Component c : menu.getMenuComponents()) {
453 if (c instanceof JMenuItem && ((JMenuItem) c).getAction() == existingMenuEntryAction) {
454 break;
455 }
456 i++;
457 }
458 return add(menu, actionToBeInserted, isExpert, i + 1);
459 }
460
461 /**
462 * Add a JosmAction to a menu.
463 *
464 * This method handles all the shortcut handling. It also makes sure that actions that are
465 * handled by the OS are not duplicated on the menu.
466 * @param menu to add the action to
467 * @param action the action that should get a menu item
468 * @param group the item should be added to. Groups are split by a separator.
469 * 0 is the first group, -1 will add the item to the end.
470 * @return The created menu item
471 */
472 public static <E extends Enum<E>> JMenuItem add(JMenu menu, JosmAction action, Enum<E> group) {
473 if (action.getShortcut().getAutomatic())
474 return null;
475 int i = getInsertionIndexForGroup(menu, group.ordinal());
476 JMenuItem menuitem = (JMenuItem) menu.add(new JMenuItem(action), i);
477 KeyStroke ks = action.getShortcut().getKeyStroke();
478 if (ks != null) {
479 menuitem.setAccelerator(ks);
480 }
481 return menuitem;
482 }
483
484 /**
485 * Add a JosmAction to a menu and automatically prints accelerator if available.
486 * Also adds a checkbox that may be toggled.
487 * @param menu to add the action to
488 * @param action the action that should get a menu item
489 * @param group the item should be added to. Groups are split by a separator. Use
490 * one of the enums that are defined for some of the menus to tell in which
491 * group the item should go.
492 * @return The created menu item
493 */
494 public static <E extends Enum<E>> JCheckBoxMenuItem addWithCheckbox(JMenu menu, JosmAction action, Enum<E> group) {
495 int i = getInsertionIndexForGroup(menu, group.ordinal());
496 final JCheckBoxMenuItem mi = (JCheckBoxMenuItem) menu.add(new JCheckBoxMenuItem(action), i);
497 final KeyStroke ks = action.getShortcut().getKeyStroke();
498 if (ks != null) {
499 mi.setAccelerator(ks);
500 }
501 return mi;
502 }
503
504 /** finds the correct insertion index for a given group and adds separators if necessary */
505 private static int getInsertionIndexForGroup(JMenu menu, int group) {
506 if(group < 0)
507 return -1;
508 // look for separator that *ends* the group (or stop at end of menu)
509 int i;
510 for(i=0; i < menu.getItemCount() && group >= 0; i++) {
511 if(menu.getItem(i) == null) {
512 group--;
513 }
514 }
515 // insert before separator that ends the group
516 if(group < 0) {
517 i--;
518 }
519 // not enough separators have been found, add them
520 while(group > 0) {
521 menu.addSeparator();
522 group--;
523 i++;
524 }
525 return i;
526 }
527
528 public JMenu addMenu(String name, int mnemonicKey, int position, String relativeHelpTopic) {
529 final JMenu menu = new JMenu(tr(name));
530 int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
531 int menuItemHeight = new JMenu().add(newAction).getPreferredSize().height;
532 MenuScroller.setScrollerFor(menu, screenHeight / menuItemHeight);
533 return addMenu(menu, name, mnemonicKey, position, relativeHelpTopic);
534 }
535
536 public <T extends JMenu> T addMenu(T menu, String name, int mnemonicKey, int position, String relativeHelpTopic) {
537 Shortcut.registerShortcut("menu:" + name, tr("Menu: {0}", tr(name)), mnemonicKey,
538 Shortcut.MNEMONIC).setMnemonic(menu);
539 add(menu, position);
540 menu.putClientProperty("help", relativeHelpTopic);
541 return menu;
542 }
543
544 /**
545 * Constructs a new {@code MainMenu}.
546 */
547 public MainMenu() {
548 JMenuItem current;
549
550 moreToolsMenu.setVisible(false);
551 dataMenu.setVisible(false);
552 gpsMenu.setVisible(false);
553
554 add(fileMenu, newAction);
555 add(fileMenu, openFile);
556 fileMenu.add(recentlyOpened);
557 add(fileMenu, openLocation);
558 fileMenu.addSeparator();
559 add(fileMenu, save);
560 add(fileMenu, saveAs);
561 sessionMenu.setToolTipText(tr("Save and load the current session (list of layers, etc.)"));
562 sessionMenu.setIcon(ImageProvider.get("session"));
563 sessionSaveAs = new SessionSaveAsAction();
564 sessionLoad = new SessionLoadAction();
565 add(sessionMenu, sessionSaveAs);
566 add(sessionMenu, sessionLoad);
567 fileMenu.add(sessionMenu);
568 ExpertToggleAction.addVisibilitySwitcher(sessionMenu);
569 add(fileMenu, gpxExport, true);
570 fileMenu.addSeparator();
571 add(fileMenu, download);
572 add(fileMenu, downloadPrimitive);
573 add(fileMenu, downloadReferrers);
574 add(fileMenu, update);
575 add(fileMenu, updateSelection);
576 add(fileMenu, updateModified);
577 fileMenu.addSeparator();
578 add(fileMenu, upload);
579 add(fileMenu, uploadSelection);
580 Component sep = new JPopupMenu.Separator();
581 fileMenu.add(sep);
582 ExpertToggleAction.addVisibilitySwitcher(sep);
583 add(fileMenu, closeChangesetAction, true);
584 fileMenu.addSeparator();
585 add(fileMenu, restart);
586 add(fileMenu, exit);
587
588 add(editMenu, undo);
589 Main.main.undoRedo.addCommandQueueListener(undo);
590 add(editMenu, redo);
591 Main.main.undoRedo.addCommandQueueListener(redo);
592 editMenu.addSeparator();
593 add(editMenu, copy);
594 add(editMenu, copyCoordinates, true);
595 add(editMenu, paste);
596 add(editMenu, pasteTags);
597 add(editMenu, duplicate);
598 add(editMenu, delete);
599 add(editMenu, purge, true);
600 editMenu.addSeparator();
601 add(editMenu,merge);
602 add(editMenu,mergeSelected);
603 editMenu.addSeparator();
604 add(editMenu, search);
605 editMenu.addSeparator();
606 add(editMenu, preferences);
607
608 // -- wireframe toggle action
609 final JCheckBoxMenuItem wireframe = new JCheckBoxMenuItem(wireFrameToggleAction);
610 viewMenu.add(wireframe);
611 wireframe.setAccelerator(wireFrameToggleAction.getShortcut().getKeyStroke());
612 wireFrameToggleAction.addButtonModel(wireframe.getModel());
613
614 viewMenu.add(new MapPaintMenu());
615 viewMenu.addSeparator();
616 add(viewMenu, new ZoomInAction());
617 add(viewMenu, new ZoomOutAction());
618 viewMenu.addSeparator();
619 for (String mode : AutoScaleAction.MODES) {
620 AutoScaleAction autoScaleAction = new AutoScaleAction(mode);
621 autoScaleActions.put(mode, autoScaleAction);
622 add(viewMenu, autoScaleAction);
623 }
624
625 // -- viewport follow toggle action
626 ViewportFollowToggleAction viewportFollowToggleAction = new ViewportFollowToggleAction();
627 final JCheckBoxMenuItem vft = new JCheckBoxMenuItem(viewportFollowToggleAction);
628 ExpertToggleAction.addVisibilitySwitcher(vft);
629 viewMenu.add(vft);
630 vft.setAccelerator(viewportFollowToggleAction.getShortcut().getKeyStroke());
631 viewportFollowToggleAction.addButtonModel(vft.getModel());
632
633 if(!Main.applet && Main.platform.canFullscreen()) {
634 // -- fullscreen toggle action
635 fullscreenToggleAction = new FullscreenToggleAction();
636 final JCheckBoxMenuItem fullscreen = new JCheckBoxMenuItem(fullscreenToggleAction);
637 viewMenu.addSeparator();
638 viewMenu.add(fullscreen);
639 fullscreen.setAccelerator(fullscreenToggleAction.getShortcut().getKeyStroke());
640 fullscreenToggleAction.addButtonModel(fullscreen.getModel());
641 }
642
643 // -- dialogs panel toggle action
644 final JCheckBoxMenuItem dialogsToggle = new JCheckBoxMenuItem(dialogsToggleAction);
645 dialogsToggle.setAccelerator(dialogsToggleAction.getShortcut().getKeyStroke());
646 dialogsToggleAction.addButtonModel(dialogsToggle.getModel());
647 viewMenu.add(dialogsToggle);
648
649 viewMenu.addSeparator();
650 add(viewMenu, info);
651 add(viewMenu, infoweb);
652 add(viewMenu, historyinfo);
653 add(viewMenu, historyinfoweb);
654 viewMenu.addSeparator();
655 viewMenu.add(new PreferenceToggleAction(tr("Edit toolbar"),
656 tr("Toggles the visibility of the edit toolbar (i.e., the vertical tool)"),
657 "sidetoolbar.visible", true).getCheckbox());
658 // -- expert mode toggle action
659 final JCheckBoxMenuItem expertItem = new JCheckBoxMenuItem(ExpertToggleAction.getInstance());
660 viewMenu.add(expertItem);
661 ExpertToggleAction.getInstance().addButtonModel(expertItem.getModel());
662
663 add(presetsMenu, presetSearchAction);
664 presetsMenu.addSeparator();
665
666 add(selectionMenu, selectAll);
667 add(selectionMenu, unselectAll);
668
669 add(toolsMenu, splitWay);
670 add(toolsMenu, combineWay);
671 toolsMenu.addSeparator();
672 add(toolsMenu, reverseWay);
673 add(toolsMenu, simplifyWay);
674 toolsMenu.addSeparator();
675 add(toolsMenu, alignInCircle);
676 add(toolsMenu, alignInLine);
677 add(toolsMenu, distribute);
678 add(toolsMenu, ortho);
679 add(toolsMenu, mirror, true);
680 toolsMenu.addSeparator();
681 add(toolsMenu, followLine, true);
682 add(toolsMenu, addnode, true);
683 add(toolsMenu, movenode, true);
684 add(toolsMenu, createCircle);
685 toolsMenu.addSeparator();
686 add(toolsMenu, mergeNodes);
687 add(toolsMenu, joinNodeWay);
688 add(toolsMenu, unglueNodes);
689 toolsMenu.addSeparator();
690 add(toolsMenu, joinAreas);
691 add(toolsMenu, createMultipolygon);
692
693 // -- changeset manager toggle action
694 final JCheckBoxMenuItem mi = MainMenu.addWithCheckbox(windowMenu, changesetManager,
695 MainMenu.WINDOW_MENU_GROUP.ALWAYS);
696 changesetManager.addButtonModel(mi.getModel());
697
698 if (!Main.pref.getBoolean("audio.menuinvisible", false)) {
699 showAudioMenu(true);
700 }
701
702 Main.pref.addPreferenceChangeListener(new PreferenceChangedListener() {
703 @Override
704 public void preferenceChanged(PreferenceChangeEvent e) {
705 if (e.getKey().equals("audio.menuinvisible")) {
706 showAudioMenu(!Boolean.parseBoolean(e.getNewValue().toString()));
707 }
708 }
709 });
710
711 helpMenu.add(statusreport);
712
713 current = helpMenu.add(help); // FIXME why is help not a JosmAction?
714 current.setAccelerator(Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1,
715 Shortcut.DIRECT).getKeyStroke());
716 add(helpMenu, about);
717
718
719 windowMenu.addMenuListener(menuSeparatorHandler);
720
721 new PresetsMenuEnabler(presetsMenu).refreshEnabled();
722 }
723
724 protected void showAudioMenu(boolean showMenu) {
725 if (showMenu && audioMenu == null) {
726 audioMenu = addMenu(marktr("Audio"), KeyEvent.VK_U, defaultMenuPos, ht("/Menu/Audio"));
727 add(audioMenu, audioPlayPause);
728 add(audioMenu, audioNext);
729 add(audioMenu, audioPrev);
730 add(audioMenu, audioFwd);
731 add(audioMenu, audioBack);
732 add(audioMenu, audioSlower);
733 add(audioMenu, audioFaster);
734 validate();
735 } else if (!showMenu && audioMenu != null) {
736 remove(audioMenu);
737 audioMenu.removeAll();
738 audioMenu = null;
739 validate();
740 }
741 }
742
743 static class PresetsMenuEnabler implements MapView.LayerChangeListener {
744 private JMenu presetsMenu;
745 public PresetsMenuEnabler(JMenu presetsMenu) {
746 MapView.addLayerChangeListener(this);
747 this.presetsMenu = presetsMenu;
748 }
749 /**
750 * Refreshes the enabled state
751 *
752 */
753 protected void refreshEnabled() {
754 presetsMenu.setEnabled(Main.isDisplayingMapView()
755 && Main.map.mapView.getEditLayer() != null
756 );
757 }
758
759 @Override
760 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
761 refreshEnabled();
762 }
763
764 @Override
765 public void layerAdded(Layer newLayer) {
766 refreshEnabled();
767 }
768
769 @Override
770 public void layerRemoved(Layer oldLayer) {
771 refreshEnabled();
772 }
773 }
774}
Note: See TracBrowser for help on using the repository browser.