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

Last change on this file since 10619 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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