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