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