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