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