Ticket #10882: MainMenu.java

File MainMenu.java, 43.7 KB (added by strump, 11 years ago)

Changed source file

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