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

Last change on this file since 10766 was 10766, checked in by Don-vip, 8 years ago

fix #13290 - pasting from other layer changes objects position (modified patch by michael2402) - gsoc-core

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