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