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