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.DeleteLayerAction; 036import org.openstreetmap.josm.actions.CombineWayAction; 037import org.openstreetmap.josm.actions.CopyAction; 038import org.openstreetmap.josm.actions.CopyCoordinatesAction; 039import org.openstreetmap.josm.actions.CreateCircleAction; 040import org.openstreetmap.josm.actions.CreateMultipolygonAction; 041import org.openstreetmap.josm.actions.DeleteAction; 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 407 @Override 408 public void menuDeselected(MenuEvent e) {} 409 410 @Override 411 public void menuSelected(MenuEvent a) { 412 if (!(a.getSource() instanceof JMenu)) 413 return; 414 final JPopupMenu m = ((JMenu) a.getSource()).getPopupMenu(); 415 for (int i = 0; i < m.getComponentCount()-1; i++) { 416 if (!(m.getComponent(i) instanceof JSeparator)) { 417 continue; 418 } 419 // hide separator if the next menu item is one as well 420 ((JSeparator) m.getComponent(i)).setVisible(!(m.getComponent(i+1) instanceof JSeparator)); 421 } 422 // hide separator at the end of the menu 423 if (m.getComponent(m.getComponentCount()-1) instanceof JSeparator) { 424 ((JSeparator) m.getComponent(m.getComponentCount()-1)).setVisible(false); 425 } 426 } 427 }; 428 429 /** 430 * @return the default position of new top-level menus 431 * @since 6088 432 */ 433 public int getDefaultMenuPos() { 434 return defaultMenuPos; 435 } 436 437 /** 438 * Add a JosmAction at the end of a menu. 439 * 440 * This method handles all the shortcut handling. It also makes sure that actions that are 441 * handled by the OS are not duplicated on the menu. 442 * @param menu the menu to add the action to 443 * @param action the action that should get a menu item 444 * @return the created menu item 445 */ 446 public static JMenuItem add(JMenu menu, JosmAction action) { 447 return add(menu, action, false); 448 } 449 450 /** 451 * Add a JosmAction at the end of a menu. 452 * 453 * This method handles all the shortcut handling. It also makes sure that actions that are 454 * handled by the OS are not duplicated on the menu. 455 * @param menu the menu to add the action to 456 * @param action the action that should get a menu item 457 * @param isExpert whether the entry should only be visible if the expert mode is activated 458 * @return the created menu item 459 */ 460 public static JMenuItem add(JMenu menu, JosmAction action, boolean isExpert) { 461 return add(menu, action, isExpert, null); 462 } 463 464 /** 465 * Add a JosmAction at the end of a menu. 466 * 467 * This method handles all the shortcut handling. It also makes sure that actions that are 468 * handled by the OS are not duplicated on the menu. 469 * @param menu the menu to add the action to 470 * @param action the action that should get a menu item 471 * @param isExpert whether the entry should only be visible if the expert mode is activated 472 * @param index an integer specifying the position at which to add the action 473 * @return the created menu item 474 */ 475 public static JMenuItem add(JMenu menu, JosmAction action, boolean isExpert, Integer index) { 476 if (action.getShortcut().isAutomatic()) 477 return null; 478 final JMenuItem menuitem; 479 if (index == null) { 480 menuitem = menu.add(action); 481 } else { 482 menuitem = menu.insert(action, index); 483 } 484 if (isExpert) { 485 ExpertToggleAction.addVisibilitySwitcher(menuitem); 486 } 487 KeyStroke ks = action.getShortcut().getKeyStroke(); 488 if (ks != null) { 489 menuitem.setAccelerator(ks); 490 } 491 // some menus are hidden before they are populated with some items by plugins 492 if (!menu.isVisible()) menu.setVisible(true); 493 return menuitem; 494 } 495 496 /** 497 * Add the JosmAction {@code actionToBeInserted} directly below {@code existingMenuEntryAction}. 498 * 499 * This method handles all the shortcut handling. It also makes sure that actions that are 500 * handled by the OS are not duplicated on the menu. 501 * @param menu the menu to add the action to 502 * @param actionToBeInserted the action that should get a menu item directly below {@code existingMenuEntryAction} 503 * @param isExpert whether the entry should only be visible if the expert mode is activated 504 * @param existingMenuEntryAction an action already added to the menu {@code menu}, 505 * the action {@code actionToBeInserted} is added directly below 506 * @return the created menu item 507 */ 508 public static JMenuItem addAfter(JMenu menu, JosmAction actionToBeInserted, boolean isExpert, JosmAction existingMenuEntryAction) { 509 int i = 0; 510 for (Component c : menu.getMenuComponents()) { 511 if (c instanceof JMenuItem && ((JMenuItem) c).getAction() == existingMenuEntryAction) { 512 break; 513 } 514 i++; 515 } 516 return add(menu, actionToBeInserted, isExpert, i + 1); 517 } 518 519 /** 520 * Add a JosmAction to a menu. 521 * 522 * This method handles all the shortcut handling. It also makes sure that actions that are 523 * handled by the OS are not duplicated on the menu. 524 * @param <E> group item enum type 525 * @param menu to add the action to 526 * @param action the action that should get a menu item 527 * @param group the item should be added to. Groups are split by a separator. 528 * 0 is the first group, -1 will add the item to the end. 529 * @return The created menu item 530 */ 531 public static <E extends Enum<E>> JMenuItem add(JMenu menu, JosmAction action, Enum<E> group) { 532 if (action.getShortcut().isAutomatic()) 533 return null; 534 int i = getInsertionIndexForGroup(menu, group.ordinal()); 535 JMenuItem menuitem = (JMenuItem) menu.add(new JMenuItem(action), i); 536 KeyStroke ks = action.getShortcut().getKeyStroke(); 537 if (ks != null) { 538 menuitem.setAccelerator(ks); 539 } 540 return menuitem; 541 } 542 543 /** 544 * Add a JosmAction to a menu and automatically prints accelerator if available. 545 * Also adds a checkbox that may be toggled. 546 * @param <E> group enum item type 547 * @param menu to add the action to 548 * @param action the action that should get a menu item 549 * @param group the item should be added to. Groups are split by a separator. Use 550 * one of the enums that are defined for some of the menus to tell in which 551 * group the item should go. 552 * @return The created menu item 553 */ 554 public static <E extends Enum<E>> JCheckBoxMenuItem addWithCheckbox(JMenu menu, JosmAction action, Enum<E> group) { 555 int i = getInsertionIndexForGroup(menu, group.ordinal()); 556 final JCheckBoxMenuItem mi = (JCheckBoxMenuItem) menu.add(new JCheckBoxMenuItem(action), i); 557 final KeyStroke ks = action.getShortcut().getKeyStroke(); 558 if (ks != null) { 559 mi.setAccelerator(ks); 560 } 561 return mi; 562 } 563 564 /** 565 * Finds the correct insertion index for a given group and adds separators if necessary 566 * @param menu menu 567 * @param group group number 568 * @return correct insertion index 569 */ 570 private static int getInsertionIndexForGroup(JMenu menu, int group) { 571 if (group < 0) 572 return -1; 573 // look for separator that *ends* the group (or stop at end of menu) 574 int i; 575 for (i = 0; i < menu.getItemCount() && group >= 0; i++) { 576 if (menu.getItem(i) == null) { 577 group--; 578 } 579 } 580 // insert before separator that ends the group 581 if (group < 0) { 582 i--; 583 } 584 // not enough separators have been found, add them 585 while (group > 0) { 586 menu.addSeparator(); 587 group--; 588 i++; 589 } 590 return i; 591 } 592 593 /** 594 * Creates a menu and adds it on the given position to the main menu. 595 * 596 * @param name the untranslated name (used as identifier for shortcut registration) 597 * @param translatedName the translated menu name (use {@code I18n.trc("menu", name)} to allow better internationalization 598 * @param mnemonicKey the mnemonic key to register 599 * @param position the position in the main menu 600 * @param relativeHelpTopic the relative help topic 601 * @return the newly created menu 602 */ 603 public JMenu addMenu(String name, String translatedName, int mnemonicKey, int position, String relativeHelpTopic) { 604 final JMenu menu = new JMenu(translatedName); 605 if (!GraphicsEnvironment.isHeadless()) { 606 MenuScroller.setScrollerFor(menu); 607 } 608 return addMenu(menu, name, mnemonicKey, position, relativeHelpTopic); 609 } 610 611 /** 612 * Adds the given menu on the given position to the main menu. 613 * @param <T> menu type 614 * 615 * @param menu the menu to add 616 * @param name the untranslated name (used as identifier for shortcut registration) 617 * @param mnemonicKey the mnemonic key to register 618 * @param position the position in the main menu 619 * @param relativeHelpTopic the relative help topic 620 * @return the given {@code }menu} 621 */ 622 public <T extends JMenu> T addMenu(T menu, String name, int mnemonicKey, int position, String relativeHelpTopic) { 623 Shortcut.registerShortcut("menu:" + name, tr("Menu: {0}", name), mnemonicKey, 624 Shortcut.MNEMONIC).setMnemonic(menu); 625 add(menu, position); 626 menu.putClientProperty("help", relativeHelpTopic); 627 return menu; 628 } 629 630 /** 631 * Constructs a new {@code MainMenu}. 632 */ 633 public MainMenu() { 634 moreToolsMenu.setVisible(false); 635 dataMenu.setVisible(false); 636 gpsMenu.setVisible(false); 637 638 add(fileMenu, newAction); 639 add(fileMenu, openFile); 640 fileMenu.add(recentlyOpened); 641 add(fileMenu, openLocation); 642 add(fileMenu, deleteLayerAction); 643 fileMenu.addSeparator(); 644 add(fileMenu, save); 645 add(fileMenu, saveAs); 646 sessionSaveAs = new SessionSaveAsAction(); 647 ExpertToggleAction.addVisibilitySwitcher(fileMenu.add(sessionSaveAs)); 648 add(fileMenu, gpxExport, true); 649 fileMenu.addSeparator(); 650 add(fileMenu, download); 651 add(fileMenu, overpassDownload, true); 652 add(fileMenu, downloadPrimitive); 653 add(fileMenu, searchNotes); 654 add(fileMenu, downloadNotesInView); 655 add(fileMenu, downloadReferrers); 656 add(fileMenu, update); 657 add(fileMenu, updateSelection); 658 add(fileMenu, updateModified); 659 fileMenu.addSeparator(); 660 add(fileMenu, upload); 661 add(fileMenu, uploadSelection); 662 Component sep = new JPopupMenu.Separator(); 663 fileMenu.add(sep); 664 ExpertToggleAction.addVisibilitySwitcher(sep); 665 add(fileMenu, closeChangesetAction, true); 666 fileMenu.addSeparator(); 667 add(fileMenu, restart); 668 add(fileMenu, exit); 669 670 add(editMenu, undo); 671 Main.main.undoRedo.addCommandQueueListener(undo); 672 add(editMenu, redo); 673 Main.main.undoRedo.addCommandQueueListener(redo); 674 editMenu.addSeparator(); 675 add(editMenu, copy); 676 add(editMenu, copyCoordinates, true); 677 add(editMenu, paste); 678 add(editMenu, pasteTags); 679 add(editMenu, duplicate); 680 add(editMenu, delete); 681 add(editMenu, purge, true); 682 editMenu.addSeparator(); 683 add(editMenu, merge); 684 add(editMenu, mergeSelected); 685 editMenu.addSeparator(); 686 add(editMenu, search); 687 add(editMenu, presetSearchPrimitiveAction); 688 editMenu.addSeparator(); 689 add(editMenu, preferences); 690 691 // -- wireframe toggle action 692 final JCheckBoxMenuItem wireframe = new JCheckBoxMenuItem(wireFrameToggleAction); 693 viewMenu.add(wireframe); 694 wireframe.setAccelerator(wireFrameToggleAction.getShortcut().getKeyStroke()); 695 wireFrameToggleAction.addButtonModel(wireframe.getModel()); 696 697 viewMenu.add(new MapPaintMenu()); 698 viewMenu.addSeparator(); 699 add(viewMenu, new ZoomInAction()); 700 add(viewMenu, new ZoomOutAction()); 701 viewMenu.addSeparator(); 702 for (String mode : AutoScaleAction.MODES) { 703 AutoScaleAction autoScaleAction = new AutoScaleAction(mode); 704 autoScaleActions.put(mode, autoScaleAction); 705 add(viewMenu, autoScaleAction); 706 } 707 708 // -- viewport follow toggle action 709 ViewportFollowToggleAction viewportFollowToggleAction = new ViewportFollowToggleAction(); 710 final JCheckBoxMenuItem vft = new JCheckBoxMenuItem(viewportFollowToggleAction); 711 ExpertToggleAction.addVisibilitySwitcher(vft); 712 viewMenu.add(vft); 713 vft.setAccelerator(viewportFollowToggleAction.getShortcut().getKeyStroke()); 714 viewportFollowToggleAction.addButtonModel(vft.getModel()); 715 716 if (Main.platform.canFullscreen()) { 717 // -- fullscreen toggle action 718 fullscreenToggleAction = new FullscreenToggleAction(); 719 final JCheckBoxMenuItem fullscreen = new JCheckBoxMenuItem(fullscreenToggleAction); 720 viewMenu.addSeparator(); 721 viewMenu.add(fullscreen); 722 fullscreen.setAccelerator(fullscreenToggleAction.getShortcut().getKeyStroke()); 723 fullscreenToggleAction.addButtonModel(fullscreen.getModel()); 724 } 725 726 // -- dialogs panel toggle action 727 final JCheckBoxMenuItem dialogsToggle = new JCheckBoxMenuItem(dialogsToggleAction); 728 dialogsToggle.setAccelerator(dialogsToggleAction.getShortcut().getKeyStroke()); 729 dialogsToggleAction.addButtonModel(dialogsToggle.getModel()); 730 viewMenu.add(dialogsToggle); 731 732 add(viewMenu, jumpToAct, true); 733 viewMenu.addSeparator(); 734 add(viewMenu, info); 735 add(viewMenu, infoweb); 736 add(viewMenu, historyinfo); 737 add(viewMenu, historyinfoweb); 738 viewMenu.addSeparator(); 739 viewMenu.add(new PreferenceToggleAction(tr("Main toolbar"), 740 tr("Toggles the visibility of the main toolbar (i.e., the horizontal toolbar)"), 741 "toolbar.visible", true).getCheckbox()); 742 viewMenu.add(new PreferenceToggleAction(tr("Edit toolbar"), 743 tr("Toggles the visibility of the edit toolbar (i.e., the vertical tool)"), 744 "sidetoolbar.visible", true).getCheckbox()); 745 // -- expert mode toggle action 746 final JCheckBoxMenuItem expertItem = new JCheckBoxMenuItem(ExpertToggleAction.getInstance()); 747 viewMenu.add(expertItem); 748 ExpertToggleAction.getInstance().addButtonModel(expertItem.getModel()); 749 750 add(presetsMenu, presetSearchAction); 751 add(presetsMenu, presetSearchPrimitiveAction); 752 add(presetsMenu, PreferencesAction.forPreferenceSubTab(tr("Preset preferences"), 753 tr("Click to open the tagging presets tab in the preferences"), TaggingPresetPreference.class)); 754 presetsMenu.addSeparator(); 755 756 add(imageryMenu, PreferencesAction.forPreferenceTab(tr("Imagery preferences"), 757 tr("Click to open the imagery tab in the preferences"), ImageryPreference.class)); 758 759 add(selectionMenu, selectAll); 760 add(selectionMenu, unselectAll); 761 add(selectionMenu, nonBranchingWaySequences); 762 763 add(toolsMenu, splitWay); 764 add(toolsMenu, combineWay); 765 toolsMenu.addSeparator(); 766 add(toolsMenu, reverseWay); 767 add(toolsMenu, simplifyWay); 768 toolsMenu.addSeparator(); 769 add(toolsMenu, alignInCircle); 770 add(toolsMenu, alignInLine); 771 add(toolsMenu, distribute); 772 add(toolsMenu, ortho); 773 add(toolsMenu, mirror, true); 774 toolsMenu.addSeparator(); 775 add(toolsMenu, followLine, true); 776 add(toolsMenu, addNode, true); 777 add(toolsMenu, moveNode, true); 778 add(toolsMenu, createCircle); 779 toolsMenu.addSeparator(); 780 add(toolsMenu, mergeNodes); 781 add(toolsMenu, joinNodeWay); 782 add(toolsMenu, moveNodeOntoWay); 783 add(toolsMenu, unJoinNodeWay); 784 add(toolsMenu, unglueNodes); 785 toolsMenu.addSeparator(); 786 add(toolsMenu, joinAreas); 787 add(toolsMenu, createMultipolygon); 788 add(toolsMenu, updateMultipolygon); 789 790 // -- changeset manager toggle action 791 final JCheckBoxMenuItem mi = MainMenu.addWithCheckbox(windowMenu, changesetManager, 792 MainMenu.WINDOW_MENU_GROUP.ALWAYS); 793 changesetManager.addButtonModel(mi.getModel()); 794 795 if (!Main.pref.getBoolean("audio.menuinvisible", false)) { 796 showAudioMenu(true); 797 } 798 799 Main.pref.addPreferenceChangeListener(new PreferenceChangedListener() { 800 @Override 801 public void preferenceChanged(PreferenceChangeEvent e) { 802 if ("audio.menuinvisible".equals(e.getKey())) { 803 showAudioMenu(!Boolean.parseBoolean(e.getNewValue().toString())); 804 } 805 } 806 }); 807 808 add(helpMenu, new MenuItemSearchDialog.Action()); 809 helpMenu.addSeparator(); 810 add(helpMenu, statusreport); 811 add(helpMenu, reportbug); 812 helpMenu.addSeparator(); 813 814 add(helpMenu, help); 815 add(helpMenu, about); 816 817 windowMenu.addMenuListener(menuSeparatorHandler); 818 819 new PresetsMenuEnabler(presetsMenu).refreshEnabled(); 820 } 821 822 private int getMaximumAvailableWidth() { 823 int maxWidth = getSize().width; 824 for (int i = 0; i < getMenuCount(); i++) { 825 JMenu menu = getMenu(i); 826 if (menu != null) { 827 maxWidth -= menu.getPreferredSize().width; 828 } 829 } 830 return maxWidth; 831 } 832 833 /** 834 * Search main menu for items with {@code textToFind} in title. 835 * @param textToFind The text to find 836 * @param skipPresets whether to skip the {@link #presetsMenu} in the search 837 * @return not null list of found menu items. 838 */ 839 public List<JMenuItem> findMenuItems(String textToFind, boolean skipPresets) { 840 // Explicitly use default locale in this case, because we're looking for translated strings 841 textToFind = textToFind.toLowerCase(Locale.getDefault()); 842 List<JMenuItem> result = new ArrayList<>(); 843 for (int i = 0; i < getMenuCount(); i++) { 844 if (getMenu(i) != null && (!skipPresets || presetsMenu != getMenu(i))) { 845 findMenuItems(getMenu(i), textToFind, result); 846 } 847 } 848 return result; 849 } 850 851 /** 852 * Recursive walker for menu items. Only menu items with action are selected. If menu item 853 * contains {@code textToFind} it's appended to result. 854 * @param menu menu in which search will be performed 855 * @param textToFind The text to find 856 * @param result resulting list of menu items 857 */ 858 private static void findMenuItems(final JMenu menu, final String textToFind, final List<JMenuItem> result) { 859 for (int i = 0; i < menu.getItemCount(); i++) { 860 JMenuItem menuItem = menu.getItem(i); 861 if (menuItem == null) continue; 862 863 // Explicitly use default locale in this case, because we're looking for translated strings 864 if (menuItem.getAction() != null && menuItem.getText().toLowerCase(Locale.getDefault()).contains(textToFind)) { 865 result.add(menuItem); 866 } 867 868 // Go recursive if needed 869 if (menuItem instanceof JMenu) { 870 findMenuItems((JMenu) menuItem, textToFind, result); 871 } 872 } 873 } 874 875 protected void showAudioMenu(boolean showMenu) { 876 if (showMenu && audioMenu == null) { 877 audioMenu = addMenu("Audio", /* I18N: mnemonic: U */ trc("menu", "Audio"), KeyEvent.VK_U, defaultMenuPos, ht("/Menu/Audio")); 878 add(audioMenu, audioPlayPause); 879 add(audioMenu, audioNext); 880 add(audioMenu, audioPrev); 881 add(audioMenu, audioFwd); 882 add(audioMenu, audioBack); 883 add(audioMenu, audioSlower); 884 add(audioMenu, audioFaster); 885 validate(); 886 } else if (!showMenu && audioMenu != null) { 887 remove(audioMenu); 888 audioMenu.removeAll(); 889 audioMenu = null; 890 validate(); 891 } 892 } 893 894 static class PresetsMenuEnabler implements MapView.LayerChangeListener { 895 private final JMenu presetsMenu; 896 897 PresetsMenuEnabler(JMenu presetsMenu) { 898 MapView.addLayerChangeListener(this); 899 this.presetsMenu = presetsMenu; 900 } 901 902 /** 903 * Refreshes the enabled state 904 */ 905 protected void refreshEnabled() { 906 presetsMenu.setEnabled(Main.main.hasEditLayer()); 907 } 908 909 @Override 910 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 911 refreshEnabled(); 912 } 913 914 @Override 915 public void layerAdded(Layer newLayer) { 916 refreshEnabled(); 917 } 918 919 @Override 920 public void layerRemoved(Layer oldLayer) { 921 refreshEnabled(); 922 } 923 } 924 925}