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

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

jump to location only in expert mode + javadoc

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