source: josm/trunk/src/org/openstreetmap/josm/gui/MainMenu.java@ 12655

Last change on this file since 12655 was 12652, checked in by michael2402, 7 years ago

Apply #15167: Merge OSM and overpass download dialog. Patch by bafonins

  • Property svn:eol-style set to native
File size: 39.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trc;
7
8import java.awt.Component;
9import java.awt.GraphicsEnvironment;
10import java.awt.event.KeyEvent;
11import java.util.ArrayList;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Locale;
15import java.util.Map;
16
17import javax.swing.JCheckBoxMenuItem;
18import javax.swing.JMenu;
19import javax.swing.JMenuBar;
20import javax.swing.JMenuItem;
21import javax.swing.JPopupMenu;
22import javax.swing.JSeparator;
23import javax.swing.KeyStroke;
24import javax.swing.event.MenuEvent;
25import javax.swing.event.MenuListener;
26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.actions.AboutAction;
29import org.openstreetmap.josm.actions.AddNodeAction;
30import org.openstreetmap.josm.actions.AlignInCircleAction;
31import org.openstreetmap.josm.actions.AlignInLineAction;
32import org.openstreetmap.josm.actions.AutoScaleAction;
33import org.openstreetmap.josm.actions.ChangesetManagerToggleAction;
34import org.openstreetmap.josm.actions.CloseChangesetAction;
35import org.openstreetmap.josm.actions.CombineWayAction;
36import org.openstreetmap.josm.actions.CopyAction;
37import org.openstreetmap.josm.actions.CopyCoordinatesAction;
38import org.openstreetmap.josm.actions.CreateCircleAction;
39import org.openstreetmap.josm.actions.CreateMultipolygonAction;
40import org.openstreetmap.josm.actions.DeleteAction;
41import org.openstreetmap.josm.actions.DeleteLayerAction;
42import org.openstreetmap.josm.actions.DialogsToggleAction;
43import org.openstreetmap.josm.actions.DistributeAction;
44import org.openstreetmap.josm.actions.DownloadAction;
45import org.openstreetmap.josm.actions.DownloadNotesInViewAction;
46import org.openstreetmap.josm.actions.DownloadOsmInViewAction;
47import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
48import org.openstreetmap.josm.actions.DownloadReferrersAction;
49import org.openstreetmap.josm.actions.DuplicateAction;
50import org.openstreetmap.josm.actions.ExitAction;
51import org.openstreetmap.josm.actions.ExpertToggleAction;
52import org.openstreetmap.josm.actions.FollowLineAction;
53import org.openstreetmap.josm.actions.FullscreenToggleAction;
54import org.openstreetmap.josm.actions.GpxExportAction;
55import org.openstreetmap.josm.actions.HelpAction;
56import org.openstreetmap.josm.actions.HistoryInfoAction;
57import org.openstreetmap.josm.actions.HistoryInfoWebAction;
58import org.openstreetmap.josm.actions.InfoAction;
59import org.openstreetmap.josm.actions.InfoWebAction;
60import org.openstreetmap.josm.actions.JoinAreasAction;
61import org.openstreetmap.josm.actions.JoinNodeWayAction;
62import org.openstreetmap.josm.actions.JosmAction;
63import org.openstreetmap.josm.actions.JumpToAction;
64import org.openstreetmap.josm.actions.MergeLayerAction;
65import org.openstreetmap.josm.actions.MergeNodesAction;
66import org.openstreetmap.josm.actions.MergeSelectionAction;
67import org.openstreetmap.josm.actions.MirrorAction;
68import org.openstreetmap.josm.actions.MoveAction;
69import org.openstreetmap.josm.actions.MoveNodeAction;
70import org.openstreetmap.josm.actions.NewAction;
71import org.openstreetmap.josm.actions.OpenFileAction;
72import org.openstreetmap.josm.actions.OpenLocationAction;
73import org.openstreetmap.josm.actions.OrthogonalizeAction;
74import org.openstreetmap.josm.actions.OrthogonalizeAction.Undo;
75import org.openstreetmap.josm.actions.PasteAction;
76import org.openstreetmap.josm.actions.PasteAtSourcePositionAction;
77import org.openstreetmap.josm.actions.PasteTagsAction;
78import org.openstreetmap.josm.actions.PreferenceToggleAction;
79import org.openstreetmap.josm.actions.PreferencesAction;
80import org.openstreetmap.josm.actions.PurgeAction;
81import org.openstreetmap.josm.actions.RedoAction;
82import org.openstreetmap.josm.actions.ReportBugAction;
83import org.openstreetmap.josm.actions.RestartAction;
84import org.openstreetmap.josm.actions.ReverseWayAction;
85import org.openstreetmap.josm.actions.SaveAction;
86import org.openstreetmap.josm.actions.SaveAsAction;
87import org.openstreetmap.josm.actions.SearchNotesDownloadAction;
88import org.openstreetmap.josm.actions.SelectAllAction;
89import org.openstreetmap.josm.actions.SelectNonBranchingWaySequencesAction;
90import org.openstreetmap.josm.actions.SessionSaveAsAction;
91import org.openstreetmap.josm.actions.ShowStatusReportAction;
92import org.openstreetmap.josm.actions.SimplifyWayAction;
93import org.openstreetmap.josm.actions.SplitWayAction;
94import org.openstreetmap.josm.actions.UnGlueAction;
95import org.openstreetmap.josm.actions.UnJoinNodeWayAction;
96import org.openstreetmap.josm.actions.UndoAction;
97import org.openstreetmap.josm.actions.UnselectAllAction;
98import org.openstreetmap.josm.actions.UpdateDataAction;
99import 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.gui.dialogs.MenuItemSearchDialog;
116import org.openstreetmap.josm.gui.io.RecentlyOpenedFilesMenu;
117import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
118import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
119import org.openstreetmap.josm.gui.mappaint.MapPaintMenu;
120import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
121import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
122import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSearchAction;
123import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSearchPrimitiveDialog;
124import org.openstreetmap.josm.tools.Shortcut;
125
126/**
127 * This is the JOSM main menu bar. It is overwritten to initialize itself and provide all menu
128 * entries as member variables (sort of collect them).
129 *
130 * It also provides possibilities to attach new menu entries (used by plugins).
131 *
132 * @author Immanuel.Scholz
133 */
134public class MainMenu extends JMenuBar {
135
136 public enum WINDOW_MENU_GROUP { ALWAYS, TOGGLE_DIALOG, VOLATILE }
137
138 /* File menu */
139 /** File / New Layer **/
140 public final NewAction newAction = new NewAction();
141 /** File / Open... **/
142 public final OpenFileAction openFile = new OpenFileAction();
143 /** File / Open Recent > **/
144 public final RecentlyOpenedFilesMenu recentlyOpened = new RecentlyOpenedFilesMenu();
145 /** File / Open Location... **/
146 public final OpenLocationAction openLocation = new OpenLocationAction();
147 /** File / Delete Layer **/
148 public final DeleteLayerAction deleteLayerAction = new DeleteLayerAction();
149 /** File / Save **/
150 public final SaveAction save = SaveAction.getInstance();
151 /** File / Save As... **/
152 public final SaveAsAction saveAs = SaveAsAction.getInstance();
153 /** File / Session > Save Session As... **/
154 public SessionSaveAsAction sessionSaveAs;
155 /** File / Export to GPX... **/
156 public final GpxExportAction gpxExport = new GpxExportAction();
157 /** File / Download from OSM... **/
158 public final DownloadAction download = new DownloadAction();
159 /** File / Download in current view **/
160 public final DownloadOsmInViewAction downloadInView = new DownloadOsmInViewAction();
161 /** File / Download object... **/
162 public final DownloadPrimitiveAction downloadPrimitive = new DownloadPrimitiveAction();
163 /** File / Download notes in current view **/
164 public final DownloadNotesInViewAction downloadNotesInView = DownloadNotesInViewAction.newActionWithNoteIcon();
165 /** File / Search Notes... **/
166 public final SearchNotesDownloadAction searchNotes = new SearchNotesDownloadAction();
167 /** File / Download parent ways/relations... **/
168 public final DownloadReferrersAction downloadReferrers = new DownloadReferrersAction();
169 /** File / Close open changesets... **/
170 public final CloseChangesetAction closeChangesetAction = new CloseChangesetAction();
171 /** File / Update data **/
172 public final JosmAction update = new UpdateDataAction();
173 /** File / Update selection **/
174 public final JosmAction updateSelection = new UpdateSelectionAction();
175 /** File / Update modified **/
176 public final JosmAction updateModified = new UpdateModifiedAction();
177 /** File / Upload data **/
178 public final JosmAction upload = new UploadAction();
179 /** File / Upload selection **/
180 public final JosmAction uploadSelection = new UploadSelectionAction();
181 /** File / Restart **/
182 public final RestartAction restart = new RestartAction();
183 /** File / Exit **/
184 public final ExitAction exit = new ExitAction();
185
186 /* Edit menu */
187 /** Edit / Undo... */
188 public final UndoAction undo = new UndoAction();
189 /** Edit / Redo */
190 public final RedoAction redo = new RedoAction();
191 /** Edit / Copy */
192 public final CopyAction copy = new CopyAction();
193 /** Edit / Copy Coordinates */
194 public final JosmAction copyCoordinates = new CopyCoordinatesAction();
195 /** Edit / Paste */
196 public final PasteAction paste = new PasteAction();
197 /** Edit / Paste at source */
198 private final PasteAtSourcePositionAction pasteAtSource = new PasteAtSourcePositionAction();
199 /** Edit / Paste Tags */
200 public final PasteTagsAction pasteTags = new PasteTagsAction();
201 /** Edit / Duplicate */
202 public final DuplicateAction duplicate = new DuplicateAction();
203 /** Edit / Delete */
204 public final DeleteAction delete = new DeleteAction();
205 /** Edit / Purge... */
206 public final JosmAction purge = new PurgeAction();
207 /** Edit / Merge layer */
208 public final MergeLayerAction merge = new MergeLayerAction();
209 /** Edit / Merge selection */
210 public final MergeSelectionAction mergeSelected = new MergeSelectionAction();
211 /** Edit / Search... */
212 public final SearchAction search = new SearchAction();
213 /** Edit / Preferences */
214 public final PreferencesAction preferences = new PreferencesAction();
215
216 /* View menu */
217 /** View / Wireframe View */
218 public final WireframeToggleAction wireFrameToggleAction = new WireframeToggleAction();
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("/ToggleDialogs"));
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 * Initialize the main menu.
636 * @since 10340
637 */
638 public void initialize() {
639 moreToolsMenu.setVisible(false);
640 dataMenu.setVisible(false);
641 gpsMenu.setVisible(false);
642
643 add(fileMenu, newAction);
644 add(fileMenu, openFile);
645 fileMenu.add(recentlyOpened);
646 add(fileMenu, openLocation);
647 add(fileMenu, deleteLayerAction);
648 fileMenu.addSeparator();
649 add(fileMenu, save);
650 add(fileMenu, saveAs);
651 sessionSaveAs = new SessionSaveAsAction();
652 ExpertToggleAction.addVisibilitySwitcher(fileMenu.add(sessionSaveAs));
653 add(fileMenu, gpxExport, true);
654 fileMenu.addSeparator();
655 add(fileMenu, download);
656 add(fileMenu, downloadInView, true);
657 add(fileMenu, downloadPrimitive);
658 add(fileMenu, searchNotes);
659 add(fileMenu, downloadNotesInView);
660 add(fileMenu, downloadReferrers);
661 add(fileMenu, update);
662 add(fileMenu, updateSelection);
663 add(fileMenu, updateModified);
664 fileMenu.addSeparator();
665 add(fileMenu, upload);
666 add(fileMenu, uploadSelection);
667 Component sep = new JPopupMenu.Separator();
668 fileMenu.add(sep);
669 ExpertToggleAction.addVisibilitySwitcher(sep);
670 add(fileMenu, closeChangesetAction, true);
671 fileMenu.addSeparator();
672 add(fileMenu, restart);
673 add(fileMenu, exit);
674
675 add(editMenu, undo);
676 MainApplication.undoRedo.addCommandQueueListener(undo);
677 add(editMenu, redo);
678 MainApplication.undoRedo.addCommandQueueListener(redo);
679 editMenu.addSeparator();
680 add(editMenu, copy);
681 add(editMenu, copyCoordinates, true);
682 add(editMenu, paste);
683 add(editMenu, pasteAtSource, true);
684 add(editMenu, pasteTags);
685 add(editMenu, duplicate);
686 add(editMenu, delete);
687 add(editMenu, purge, true);
688 editMenu.addSeparator();
689 add(editMenu, merge);
690 add(editMenu, mergeSelected);
691 editMenu.addSeparator();
692 add(editMenu, search);
693 add(editMenu, presetSearchPrimitiveAction);
694 editMenu.addSeparator();
695 add(editMenu, preferences);
696
697 // -- wireframe toggle action
698 final JCheckBoxMenuItem wireframe = new JCheckBoxMenuItem(wireFrameToggleAction);
699 viewMenu.add(wireframe);
700 wireframe.setAccelerator(wireFrameToggleAction.getShortcut().getKeyStroke());
701 wireFrameToggleAction.addButtonModel(wireframe.getModel());
702
703 viewMenu.add(new MapPaintMenu());
704 viewMenu.addSeparator();
705 add(viewMenu, new ZoomInAction());
706 add(viewMenu, new ZoomOutAction());
707 viewMenu.addSeparator();
708 for (String mode : AutoScaleAction.MODES) {
709 AutoScaleAction autoScaleAction = new AutoScaleAction(mode);
710 autoScaleActions.put(mode, autoScaleAction);
711 add(viewMenu, autoScaleAction);
712 }
713
714 // -- viewport follow toggle action
715 ViewportFollowToggleAction viewportFollowToggleAction = new ViewportFollowToggleAction();
716 final JCheckBoxMenuItem vft = new JCheckBoxMenuItem(viewportFollowToggleAction);
717 ExpertToggleAction.addVisibilitySwitcher(vft);
718 viewMenu.add(vft);
719 vft.setAccelerator(viewportFollowToggleAction.getShortcut().getKeyStroke());
720 viewportFollowToggleAction.addButtonModel(vft.getModel());
721
722 if (Main.platform.canFullscreen()) {
723 // -- fullscreen toggle action
724 fullscreenToggleAction = new FullscreenToggleAction();
725 final JCheckBoxMenuItem fullscreen = new JCheckBoxMenuItem(fullscreenToggleAction);
726 viewMenu.addSeparator();
727 viewMenu.add(fullscreen);
728 fullscreen.setAccelerator(fullscreenToggleAction.getShortcut().getKeyStroke());
729 fullscreenToggleAction.addButtonModel(fullscreen.getModel());
730 }
731
732 // -- dialogs panel toggle action
733 final JCheckBoxMenuItem dialogsToggle = new JCheckBoxMenuItem(dialogsToggleAction);
734 dialogsToggle.setAccelerator(dialogsToggleAction.getShortcut().getKeyStroke());
735 dialogsToggleAction.addButtonModel(dialogsToggle.getModel());
736 viewMenu.add(dialogsToggle);
737
738 add(viewMenu, jumpToAct, true);
739 viewMenu.addSeparator();
740 add(viewMenu, info);
741 add(viewMenu, infoweb);
742 add(viewMenu, historyinfo);
743 add(viewMenu, historyinfoweb);
744 viewMenu.addSeparator();
745 viewMenu.add(new PreferenceToggleAction(tr("Main toolbar"),
746 tr("Toggles the visibility of the main toolbar (i.e., the horizontal toolbar)"),
747 "toolbar.visible", true).getCheckbox());
748 viewMenu.add(new PreferenceToggleAction(tr("Edit toolbar"),
749 tr("Toggles the visibility of the edit toolbar (i.e., the vertical tool)"),
750 "sidetoolbar.visible", true).getCheckbox());
751 // -- expert mode toggle action
752 final JCheckBoxMenuItem expertItem = new JCheckBoxMenuItem(ExpertToggleAction.getInstance());
753 viewMenu.add(expertItem);
754 ExpertToggleAction.getInstance().addButtonModel(expertItem.getModel());
755
756 add(presetsMenu, presetSearchAction);
757 add(presetsMenu, presetSearchPrimitiveAction);
758 add(presetsMenu, PreferencesAction.forPreferenceSubTab(tr("Preset preferences"),
759 tr("Click to open the tagging presets tab in the preferences"), TaggingPresetPreference.class));
760 presetsMenu.addSeparator();
761
762 add(imageryMenu, PreferencesAction.forPreferenceTab(tr("Imagery preferences"),
763 tr("Click to open the imagery tab in the preferences"), ImageryPreference.class));
764
765 add(selectionMenu, selectAll);
766 add(selectionMenu, unselectAll);
767 add(selectionMenu, nonBranchingWaySequences);
768
769 add(toolsMenu, splitWay);
770 add(toolsMenu, combineWay);
771 toolsMenu.addSeparator();
772 add(toolsMenu, reverseWay);
773 add(toolsMenu, simplifyWay);
774 toolsMenu.addSeparator();
775 add(toolsMenu, alignInCircle);
776 add(toolsMenu, alignInLine);
777 add(toolsMenu, distribute);
778 add(toolsMenu, ortho);
779 add(toolsMenu, mirror, true);
780 toolsMenu.addSeparator();
781 add(toolsMenu, followLine, true);
782 add(toolsMenu, addNode, true);
783 add(toolsMenu, moveNode, true);
784 add(toolsMenu, createCircle);
785 toolsMenu.addSeparator();
786 add(toolsMenu, mergeNodes);
787 add(toolsMenu, joinNodeWay);
788 add(toolsMenu, moveNodeOntoWay);
789 add(toolsMenu, unJoinNodeWay);
790 add(toolsMenu, unglueNodes);
791 toolsMenu.addSeparator();
792 add(toolsMenu, joinAreas);
793 add(toolsMenu, createMultipolygon);
794 add(toolsMenu, updateMultipolygon);
795
796 // -- changeset manager toggle action
797 final JCheckBoxMenuItem mi = MainMenu.addWithCheckbox(windowMenu, changesetManager,
798 MainMenu.WINDOW_MENU_GROUP.ALWAYS);
799 changesetManager.addButtonModel(mi.getModel());
800
801 if (!Main.pref.getBoolean("audio.menuinvisible", false)) {
802 showAudioMenu(true);
803 }
804
805 Main.pref.addPreferenceChangeListener(e -> {
806 if ("audio.menuinvisible".equals(e.getKey())) {
807 showAudioMenu(!Boolean.parseBoolean(e.getNewValue().toString()));
808 }
809 });
810
811 add(helpMenu, new MenuItemSearchDialog.Action());
812 helpMenu.addSeparator();
813 add(helpMenu, statusreport);
814 add(helpMenu, reportbug);
815 helpMenu.addSeparator();
816
817 add(helpMenu, help);
818 add(helpMenu, about);
819
820 windowMenu.addMenuListener(menuSeparatorHandler);
821
822 new PresetsMenuEnabler(presetsMenu);
823 }
824
825 /**
826 * Search main menu for items with {@code textToFind} in title.
827 * @param textToFind The text to find
828 * @param skipPresets whether to skip the {@link #presetsMenu} in the search
829 * @return not null list of found menu items.
830 */
831 public List<JMenuItem> findMenuItems(String textToFind, boolean skipPresets) {
832 // Explicitly use default locale in this case, because we're looking for translated strings
833 textToFind = textToFind.toLowerCase(Locale.getDefault());
834 List<JMenuItem> result = new ArrayList<>();
835 for (int i = 0; i < getMenuCount(); i++) {
836 if (getMenu(i) != null && (!skipPresets || presetsMenu != getMenu(i))) {
837 findMenuItems(getMenu(i), textToFind, result);
838 }
839 }
840 return result;
841 }
842
843 /**
844 * Recursive walker for menu items. Only menu items with action are selected. If menu item
845 * contains {@code textToFind} it's appended to result.
846 * @param menu menu in which search will be performed
847 * @param textToFind The text to find
848 * @param result resulting list of menu items
849 */
850 private static void findMenuItems(final JMenu menu, final String textToFind, final List<JMenuItem> result) {
851 for (int i = 0; i < menu.getItemCount(); i++) {
852 JMenuItem menuItem = menu.getItem(i);
853 if (menuItem == null) continue;
854
855 // Explicitly use default locale in this case, because we're looking for translated strings
856 if (menuItem.getAction() != null && menuItem.getText().toLowerCase(Locale.getDefault()).contains(textToFind)) {
857 result.add(menuItem);
858 }
859
860 // Go recursive if needed
861 if (menuItem instanceof JMenu) {
862 findMenuItems((JMenu) menuItem, textToFind, result);
863 }
864 }
865 }
866
867 protected void showAudioMenu(boolean showMenu) {
868 if (showMenu && audioMenu == null) {
869 audioMenu = addMenu("Audio", /* I18N: mnemonic: U */ trc("menu", "Audio"), KeyEvent.VK_U, defaultMenuPos, ht("/Menu/Audio"));
870 add(audioMenu, audioPlayPause);
871 add(audioMenu, audioNext);
872 add(audioMenu, audioPrev);
873 add(audioMenu, audioFwd);
874 add(audioMenu, audioBack);
875 add(audioMenu, audioSlower);
876 add(audioMenu, audioFaster);
877 validate();
878 } else if (!showMenu && audioMenu != null) {
879 remove(audioMenu);
880 audioMenu.removeAll();
881 audioMenu = null;
882 validate();
883 }
884 }
885
886 static class PresetsMenuEnabler implements ActiveLayerChangeListener {
887 private final JMenu presetsMenu;
888
889 PresetsMenuEnabler(JMenu presetsMenu) {
890 this.presetsMenu = presetsMenu;
891 MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(this);
892 }
893
894 @Override
895 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
896 presetsMenu.setEnabled(e.getSource().getEditLayer() != null);
897 }
898 }
899}
Note: See TracBrowser for help on using the repository browser.