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