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

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

fix #10960 - Add note API search dialog (modified patch by ToeBee). Enable notes by default

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