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