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

Last change on this file since 17534 was 17021, checked in by stoecker, 4 years ago

fix #19786, Add missing icons to View menu

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