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

Last change on this file since 8792 was 8792, checked in by simon04, 9 years ago

fix #11895 see #11765 - Some menu entries are no longer translated

Regression from r8721.

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