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

Last change on this file since 9365 was 9347, checked in by simon04, 8 years ago

fix #12224 - Dialog for "Search menu items"

This places the "Search menu items" functionality in the help menu
instead of placing a text field at the very right of the main menu.

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