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

Last change on this file since 6597 was 6597, checked in by simon04, 10 years ago

see #9492 - split "Create Multipolyon" in % and "Update Multipolygon"; for the latter obtain the relation from the selected ways (if unambiguous)

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