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
RevLine 
[8378]1// License: GPL. For details, see LICENSE file.
[422]2package org.openstreetmap.josm.gui;
3
[2530]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[404]5import static org.openstreetmap.josm.tools.I18n.tr;
[8721]6import static org.openstreetmap.josm.tools.I18n.trc;
[404]7
[4843]8import java.awt.Component;
[8081]9import java.awt.DefaultFocusTraversalPolicy;
[8072]10import java.awt.Dimension;
[7075]11import java.awt.GraphicsEnvironment;
[8072]12import java.awt.event.ActionEvent;
[1416]13import java.awt.event.KeyEvent;
[8072]14import java.awt.event.KeyListener;
15import java.util.ArrayList;
[5958]16import java.util.HashMap;
[8072]17import java.util.List;
[8404]18import java.util.Locale;
[5958]19import java.util.Map;
[404]20
[8072]21import javax.swing.Action;
22import javax.swing.Box;
[486]23import javax.swing.JCheckBoxMenuItem;
[404]24import javax.swing.JMenu;
25import javax.swing.JMenuBar;
26import javax.swing.JMenuItem;
[4590]27import javax.swing.JPopupMenu;
28import javax.swing.JSeparator;
[8072]29import javax.swing.JTextField;
[450]30import javax.swing.KeyStroke;
[8072]31import javax.swing.MenuElement;
32import javax.swing.MenuSelectionManager;
33import javax.swing.event.DocumentEvent;
34import javax.swing.event.DocumentListener;
[4590]35import javax.swing.event.MenuEvent;
36import javax.swing.event.MenuListener;
[404]37
[486]38import org.openstreetmap.josm.Main;
[404]39import org.openstreetmap.josm.actions.AboutAction;
[1130]40import org.openstreetmap.josm.actions.AddNodeAction;
[404]41import org.openstreetmap.josm.actions.AlignInCircleAction;
42import org.openstreetmap.josm.actions.AlignInLineAction;
43import org.openstreetmap.josm.actions.AutoScaleAction;
[2689]44import org.openstreetmap.josm.actions.ChangesetManagerToggleAction;
[2530]45import org.openstreetmap.josm.actions.CloseChangesetAction;
[404]46import org.openstreetmap.josm.actions.CombineWayAction;
47import org.openstreetmap.josm.actions.CopyAction;
[4380]48import org.openstreetmap.josm.actions.CopyCoordinatesAction;
[996]49import org.openstreetmap.josm.actions.CreateCircleAction;
[3704]50import org.openstreetmap.josm.actions.CreateMultipolygonAction;
[768]51import org.openstreetmap.josm.actions.DeleteAction;
[5965]52import org.openstreetmap.josm.actions.DialogsToggleAction;
[1380]53import org.openstreetmap.josm.actions.DistributeAction;
[404]54import org.openstreetmap.josm.actions.DownloadAction;
[8212]55import org.openstreetmap.josm.actions.DownloadNotesInViewAction;
[2923]56import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
[1810]57import org.openstreetmap.josm.actions.DownloadReferrersAction;
[404]58import org.openstreetmap.josm.actions.DuplicateAction;
59import org.openstreetmap.josm.actions.ExitAction;
[4840]60import org.openstreetmap.josm.actions.ExpertToggleAction;
[4086]61import org.openstreetmap.josm.actions.FollowLineAction;
[2621]62import org.openstreetmap.josm.actions.FullscreenToggleAction;
[404]63import org.openstreetmap.josm.actions.GpxExportAction;
64import org.openstreetmap.josm.actions.HelpAction;
[1811]65import org.openstreetmap.josm.actions.HistoryInfoAction;
[4408]66import org.openstreetmap.josm.actions.HistoryInfoWebAction;
[1700]67import org.openstreetmap.josm.actions.InfoAction;
[4408]68import org.openstreetmap.josm.actions.InfoWebAction;
[2621]69import org.openstreetmap.josm.actions.JoinAreasAction;
[466]70import org.openstreetmap.josm.actions.JoinNodeWayAction;
[768]71import org.openstreetmap.josm.actions.JosmAction;
[3327]72import org.openstreetmap.josm.actions.JumpToAction;
[1890]73import org.openstreetmap.josm.actions.MergeLayerAction;
[422]74import org.openstreetmap.josm.actions.MergeNodesAction;
[1890]75import org.openstreetmap.josm.actions.MergeSelectionAction;
[1639]76import org.openstreetmap.josm.actions.MirrorAction;
[3327]77import org.openstreetmap.josm.actions.MoveAction;
[3266]78import org.openstreetmap.josm.actions.MoveNodeAction;
[404]79import org.openstreetmap.josm.actions.NewAction;
[1146]80import org.openstreetmap.josm.actions.OpenFileAction;
[1416]81import org.openstreetmap.josm.actions.OpenLocationAction;
82import org.openstreetmap.josm.actions.OrthogonalizeAction;
[5965]83import org.openstreetmap.josm.actions.OrthogonalizeAction.Undo;
[8684]84import org.openstreetmap.josm.actions.OverpassDownloadAction;
[404]85import org.openstreetmap.josm.actions.PasteAction;
[558]86import org.openstreetmap.josm.actions.PasteTagsAction;
[5965]87import org.openstreetmap.josm.actions.PreferenceToggleAction;
[404]88import org.openstreetmap.josm.actions.PreferencesAction;
[3431]89import org.openstreetmap.josm.actions.PurgeAction;
[404]90import org.openstreetmap.josm.actions.RedoAction;
[7624]91import org.openstreetmap.josm.actions.ReportBugAction;
[5857]92import org.openstreetmap.josm.actions.RestartAction;
[404]93import org.openstreetmap.josm.actions.ReverseWayAction;
94import org.openstreetmap.josm.actions.SaveAction;
95import org.openstreetmap.josm.actions.SaveAsAction;
[8071]96import org.openstreetmap.josm.actions.SearchNotesDownloadAction;
[404]97import org.openstreetmap.josm.actions.SelectAllAction;
[6544]98import org.openstreetmap.josm.actions.SelectNonBranchingWaySequencesAction;
[4668]99import org.openstreetmap.josm.actions.SessionLoadAction;
[4685]100import org.openstreetmap.josm.actions.SessionSaveAsAction;
[1416]101import org.openstreetmap.josm.actions.ShowStatusReportAction;
[2575]102import org.openstreetmap.josm.actions.SimplifyWayAction;
[404]103import org.openstreetmap.josm.actions.SplitWayAction;
[1416]104import org.openstreetmap.josm.actions.ToggleGPXLinesAction;
[858]105import org.openstreetmap.josm.actions.UnGlueAction;
[6253]106import org.openstreetmap.josm.actions.UnJoinNodeWayAction;
[404]107import org.openstreetmap.josm.actions.UndoAction;
108import org.openstreetmap.josm.actions.UnselectAllAction;
[1434]109import org.openstreetmap.josm.actions.UpdateDataAction;
[2682]110import org.openstreetmap.josm.actions.UpdateModifiedAction;
[1670]111import org.openstreetmap.josm.actions.UpdateSelectionAction;
[404]112import org.openstreetmap.josm.actions.UploadAction;
[2250]113import org.openstreetmap.josm.actions.UploadSelectionAction;
[3837]114import org.openstreetmap.josm.actions.ViewportFollowToggleAction;
[2530]115import org.openstreetmap.josm.actions.WireframeToggleAction;
[768]116import org.openstreetmap.josm.actions.ZoomInAction;
117import org.openstreetmap.josm.actions.ZoomOutAction;
[547]118import org.openstreetmap.josm.actions.audio.AudioBackAction;
[768]119import org.openstreetmap.josm.actions.audio.AudioFasterAction;
[547]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;
[563]124import org.openstreetmap.josm.actions.audio.AudioSlowerAction;
[404]125import org.openstreetmap.josm.actions.search.SearchAction;
[5661]126import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
127import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
[3710]128import org.openstreetmap.josm.gui.io.RecentlyOpenedFilesMenu;
[1808]129import org.openstreetmap.josm.gui.layer.Layer;
[5086]130import org.openstreetmap.josm.gui.mappaint.MapPaintMenu;
[6426]131import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
132import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
[3388]133import org.openstreetmap.josm.gui.tagging.TaggingPresetSearchAction;
[6738]134import org.openstreetmap.josm.gui.tagging.TaggingPresetSearchPrimitiveDialog;
[8072]135import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
[4668]136import org.openstreetmap.josm.tools.ImageProvider;
[1084]137import org.openstreetmap.josm.tools.Shortcut;
[3671]138
[422]139/**
[1130]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).
[1169]142 *
[422]143 * It also provides possibilities to attach new menu entries (used by plugins).
[1169]144 *
[422]145 * @author Immanuel.Scholz
146 */
147public class MainMenu extends JMenuBar {
148
[8510]149 public static enum WINDOW_MENU_GROUP { ALWAYS, TOGGLE_DIALOG, VOLATILE }
150
[1130]151 /* File menu */
[6830]152 /** File / New Layer **/
[1130]153 public final NewAction newAction = new NewAction();
[6830]154 /** File / Open... **/
[1146]155 public final OpenFileAction openFile = new OpenFileAction();
[6830]156 /** File / Open Recent > **/
[3710]157 public final RecentlyOpenedFilesMenu recentlyOpened = new RecentlyOpenedFilesMenu();
[6830]158 /** File / Open Location... **/
[1146]159 public final OpenLocationAction openLocation = new OpenLocationAction();
[6830]160 /** File / Save **/
[6180]161 public final SaveAction save = SaveAction.getInstance();
[6830]162 /** File / Save As... **/
[6180]163 public final SaveAsAction saveAs = SaveAsAction.getInstance();
[6830]164 /** File / Session > Load Session **/
[6180]165 public SessionLoadAction sessionLoad;
[6830]166 /** File / Session > Save Session As... **/
[6180]167 public SessionSaveAsAction sessionSaveAs;
[6830]168 /** File / Export to GPX... **/
[6180]169 public final GpxExportAction gpxExport = new GpxExportAction();
[6830]170 /** File / Download from OSM... **/
[1130]171 public final DownloadAction download = new DownloadAction();
[8684]172 /** File / Download from Overpass API... **/
173 public final OverpassDownloadAction overpassDownload = new OverpassDownloadAction();
[6830]174 /** File / Download object... **/
[2923]175 public final DownloadPrimitiveAction downloadPrimitive = new DownloadPrimitiveAction();
[8212]176 /** File / Download notes in current view **/
177 public final DownloadNotesInViewAction downloadNotesInView = DownloadNotesInViewAction.newActionWithNoteIcon();
[8071]178 /** File / Search Notes... **/
179 public final SearchNotesDownloadAction searchNotes = new SearchNotesDownloadAction();
[6830]180 /** File / Download parent ways/relations... **/
[1810]181 public final DownloadReferrersAction downloadReferrers = new DownloadReferrersAction();
[6830]182 /** File / Close open changesets... **/
[2115]183 public final CloseChangesetAction closeChangesetAction = new CloseChangesetAction();
[6830]184 /** File / Update data **/
[1434]185 public final JosmAction update = new UpdateDataAction();
[6830]186 /** File / Update selection **/
[1670]187 public final JosmAction updateSelection = new UpdateSelectionAction();
[6830]188 /** File / Update modified **/
[2682]189 public final JosmAction updateModified = new UpdateModifiedAction();
[6830]190 /** File / Upload data **/
[1130]191 public final JosmAction upload = new UploadAction();
[6830]192 /** File / Upload selection **/
[2250]193 public final JosmAction uploadSelection = new UploadSelectionAction();
[6830]194 /** File / Restart **/
[6180]195 public final RestartAction restart = new RestartAction();
[6830]196 /** File / Exit **/
[6180]197 public final ExitAction exit = new ExitAction();
[1023]198
[1130]199 /* Edit menu */
[6830]200 /** Edit / Undo... */
[1130]201 public final UndoAction undo = new UndoAction();
[6830]202 /** Edit / Redo */
[1130]203 public final RedoAction redo = new RedoAction();
[6830]204 /** Edit / Copy */
[6180]205 public final CopyAction copy = new CopyAction();
[6830]206 /** Edit / Copy Coordinates */
[4380]207 public final JosmAction copyCoordinates = new CopyCoordinatesAction();
[6830]208 /** Edit / Paste */
[3969]209 public final PasteAction paste = new PasteAction();
[6830]210 /** Edit / Paste Tags */
[6180]211 public final PasteTagsAction pasteTags = new PasteTagsAction();
[6830]212 /** Edit / Duplicate */
[6180]213 public final DuplicateAction duplicate = new DuplicateAction();
[6830]214 /** Edit / Delete */
[6180]215 public final DeleteAction delete = new DeleteAction();
[6830]216 /** Edit / Purge... */
[3431]217 public final JosmAction purge = new PurgeAction();
[6830]218 /** Edit / Merge layer */
[6180]219 public final MergeLayerAction merge = new MergeLayerAction();
[6830]220 /** Edit / Merge selection */
[6180]221 public final MergeSelectionAction mergeSelected = new MergeSelectionAction();
[6830]222 /** Edit / Search... */
[6180]223 public final SearchAction search = new SearchAction();
[6830]224 /** Edit / Preferences */
[6180]225 public final PreferencesAction preferences = new PreferencesAction();
[1023]226
[1130]227 /* View menu */
[6830]228 /** View / Wireframe View */
[3894]229 public final WireframeToggleAction wireFrameToggleAction = new WireframeToggleAction();
[1130]230 public final JosmAction toggleGPXLines = new ToggleGPXLinesAction();
[6830]231 /** View / Advanced info */
[3704]232 public final InfoAction info = new InfoAction();
[6830]233 /** View / Advanced info (web) */
[4408]234 public final InfoWebAction infoweb = new InfoWebAction();
[6830]235 /** View / History */
[3704]236 public final HistoryInfoAction historyinfo = new HistoryInfoAction();
[6830]237 /** View / History (web) */
[4408]238 public final HistoryInfoWebAction historyinfoweb = new HistoryInfoWebAction();
[6830]239 /** View / "Zoom to"... actions */
[8399]240 public final Map<String, AutoScaleAction> autoScaleActions = new HashMap<>();
[6830]241 /** View / Jump to position */
[6394]242 public final JumpToAction jumpToAct = new JumpToAction();
[1023]243
[1130]244 /* Tools menu */
[6830]245 /** Tools / Split Way */
[6412]246 public final SplitWayAction splitWay = new SplitWayAction();
[6830]247 /** Tools / Combine Way */
[6412]248 public final CombineWayAction combineWay = new CombineWayAction();
[6830]249 /** Tools / Reverse Ways */
[6412]250 public final ReverseWayAction reverseWay = new ReverseWayAction();
[6830]251 /** Tools / Simplify Way */
[6412]252 public final SimplifyWayAction simplifyWay = new SimplifyWayAction();
[6830]253 /** Tools / Align Nodes in Circle */
[6412]254 public final AlignInCircleAction alignInCircle = new AlignInCircleAction();
[6830]255 /** Tools / Align Nodes in Line */
[6412]256 public final AlignInLineAction alignInLine = new AlignInLineAction();
[6830]257 /** Tools / Distribute Nodes */
[6412]258 public final DistributeAction distribute = new DistributeAction();
[6830]259 /** Tools / Orthogonalize Shape */
[2268]260 public final OrthogonalizeAction ortho = new OrthogonalizeAction();
[6398]261 /** Orthogonalize undo. Action is not shown in the menu. Only triggered by shortcut */
[6412]262 public final Undo orthoUndo = new Undo();
[6830]263 /** Tools / Mirror */
[6412]264 public final MirrorAction mirror = new MirrorAction();
[6830]265 /** Tools / Follow line */
[6412]266 public final FollowLineAction followLine = new FollowLineAction();
[6830]267 /** Tools / Add Node... */
[6412]268 public final AddNodeAction addNode = new AddNodeAction();
[6830]269 /** Tools / Move Node... */
[6412]270 public final MoveNodeAction moveNode = new MoveNodeAction();
[6830]271 /** Tools / Create Circle */
[6412]272 public final CreateCircleAction createCircle = new CreateCircleAction();
[6830]273 /** Tools / Merge Nodes */
[6412]274 public final MergeNodesAction mergeNodes = new MergeNodesAction();
[6830]275 /** Tools / Join Node to Way */
[7131]276 public final JoinNodeWayAction joinNodeWay = JoinNodeWayAction.createJoinNodeToWayAction();
277 /** Tools / Join Way to Node */
278 public final JoinNodeWayAction moveNodeOntoWay = JoinNodeWayAction.createMoveNodeOntoWayAction();
[6830]279 /** Tools / Disconnect Node from Way */
[6412]280 public final UnJoinNodeWayAction unJoinNodeWay = new UnJoinNodeWayAction();
[6830]281 /** Tools / Unglue Ways */
[6412]282 public final UnGlueAction unglueNodes = new UnGlueAction();
[6830]283 /** Tools / Join overlapping Areas */
[6412]284 public final JoinAreasAction joinAreas = new JoinAreasAction();
[6830]285 /** Tools / Create multipolygon */
[6597]286 public final CreateMultipolygonAction createMultipolygon = new CreateMultipolygonAction(false);
[6830]287 /** Tools / Update multipolygon */
[6597]288 public final CreateMultipolygonAction updateMultipolygon = new CreateMultipolygonAction(true);
[450]289
[6180]290 /* Selection menu */
[6830]291 /** Selection / Select All */
[6180]292 public final SelectAllAction selectAll = new SelectAllAction();
[6830]293 /** Selection / Unselect All */
[6180]294 public final UnselectAllAction unselectAll = new UnselectAllAction();
[6830]295 /** Selection / Non-branching way sequences */
[6544]296 public final SelectNonBranchingWaySequencesAction nonBranchingWaySequences = new SelectNonBranchingWaySequencesAction();
[6180]297
[1130]298 /* Audio menu */
[6830]299 /** Audio / Play/Pause */
[1130]300 public final JosmAction audioPlayPause = new AudioPlayPauseAction();
[6830]301 /** Audio / Next marker */
[1130]302 public final JosmAction audioNext = new AudioNextAction();
[6830]303 /** Audio / Previous Marker */
[1130]304 public final JosmAction audioPrev = new AudioPrevAction();
[6830]305 /** Audio / Forward */
[1130]306 public final JosmAction audioFwd = new AudioFwdAction();
[6830]307 /** Audio / Back */
[1130]308 public final JosmAction audioBack = new AudioBackAction();
[6830]309 /** Audio / Faster */
[1130]310 public final JosmAction audioFaster = new AudioFasterAction();
[6830]311 /** Audio / Slower */
[1130]312 public final JosmAction audioSlower = new AudioSlowerAction();
[547]313
[6180]314 /* Windows Menu */
[6830]315 /** Windows / Changeset Manager */
[6180]316 public final ChangesetManagerToggleAction changesetManager = new ChangesetManagerToggleAction();
[6830]317
[1130]318 /* Help menu */
[6830]319 /** Help / Help */
[1130]320 public final HelpAction help = new HelpAction();
[6830]321 /** Help / About */
[6180]322 public final AboutAction about = new AboutAction();
[6830]323 /** Help / Show Status Report */
[6180]324 public final ShowStatusReportAction statusreport = new ShowStatusReportAction();
[7624]325 /** Help / Report bug */
326 public final ReportBugAction reportbug = new ReportBugAction();
[1023]327
[6180]328 /**
329 * fileMenu contains I/O actions
330 */
[8792]331 public final JMenu fileMenu = addMenu("File", /* I18N: mnemonic: F */ trc("menu", "File"), KeyEvent.VK_F, 0, ht("/Menu/File"));
[6180]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 */
[8792]339 public final JMenu editMenu = addMenu("Edit", /* I18N: mnemonic: E */ trc("menu", "Edit"), KeyEvent.VK_E, 1, ht("/Menu/Edit"));
[6180]340 /**
341 * viewMenu contains display actions (zoom, map styles, etc.)
342 */
[8792]343 public final JMenu viewMenu = addMenu("View", /* I18N: mnemonic: V */ trc("menu", "View"), KeyEvent.VK_V, 2, ht("/Menu/View"));
[6082]344 /**
345 * toolsMenu contains different geometry manipulation actions from JOSM core (most used)
346 * The plugins should use other menus
347 */
[8792]348 public final JMenu toolsMenu = addMenu("Tools", /* I18N: mnemonic: T */ trc("menu", "Tools"), KeyEvent.VK_T, 3, ht("/Menu/Tools"));
[6082]349 /**
350 * moreToolsMenu contains geometry-related actions from all the plugins
351 * @since 6082 (moved from Utilsplugin2)
352 */
[8792]353 public final JMenu moreToolsMenu = addMenu("More tools", /* I18N: mnemonic: M */ trc("menu", "More tools"), KeyEvent.VK_M, 4, ht("/Menu/MoreTools"));
[6082]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
[6830]357 * @since 6082
358 */
[8792]359 public final JMenu dataMenu = addMenu("Data", /* I18N: mnemonic: D */ trc("menu", "Data"), KeyEvent.VK_D, 5, ht("/Menu/Data"));
[6180]360 /**
[6082]361 * selectionMenu contains all actions related to selecting different objects
362 * @since 6082 (moved from Utilsplugin2)
363 */
[8792]364 public final JMenu selectionMenu = addMenu("Selection", /* I18N: mnemonic: N */ trc("menu", "Selection"), KeyEvent.VK_N, 6, ht("/Menu/Selection"));
[6180]365 /**
366 * presetsMenu contains presets actions (search, presets tree)
367 */
[8792]368 public final JMenu presetsMenu = addMenu("Presets", /* I18N: mnemonic: P */ trc("menu", "Presets"), KeyEvent.VK_P, 7, ht("/Menu/Presets"));
[6097]369 /**
[6830]370 * submenu in Imagery menu that contains plugin-managed additional imagery layers
[6097]371 * @since 6097
372 */
373 public final JMenu imagerySubMenu = new JMenu(tr("More..."));
[6180]374 /**
375 * imageryMenu contains all imagery-related actions
376 */
[8721]377 // CHECKSTYLE.OFF: LineLength
[8792]378 public final ImageryMenu imageryMenu = addMenu(new ImageryMenu(imagerySubMenu), /* I18N: mnemonic: I */ "Imagery", KeyEvent.VK_I, 8, ht("/Menu/Imagery"));
[8721]379 // CHECKSTYLE.ON: LineLength
[6180]380 /**
[6082]381 * gpsMenu contains all plugin actions that are related
382 * to using GPS data, including opening, uploading and real-time tracking
[6830]383 * @since 6082
[6082]384 */
[8792]385 public final JMenu gpsMenu = addMenu("GPS", /* I18N: mnemonic: G */ trc("menu", "GPS"), KeyEvent.VK_G, 9, ht("/Menu/GPS"));
[4590]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 */
[8792]391 public final JMenu windowMenu = addMenu("Windows", /* I18N: mnemonic: W */ trc("menu", "Windows"), KeyEvent.VK_W, 10, ht("/Menu/Windows"));
[4590]392
[6180]393 /**
394 * audioMenu contains all audio-related actions. Be careful, this menu is not guaranteed to be displayed at all
395 */
[1498]396 public JMenu audioMenu = null;
[6180]397 /**
398 * helpMenu contains JOSM general actions (Help, About, etc.)
399 */
[8792]400 public final JMenu helpMenu = addMenu("Help", /* I18N: mnemonic: H */ trc("menu", "Help"), KeyEvent.VK_H, 11, ht("/Menu/Help"));
[4668]401
[6246]402 private static final int defaultMenuPos = 11;
[422]403
[3327]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
[3388]409 public final TaggingPresetSearchAction presetSearchAction = new TaggingPresetSearchAction();
[6738]410 public final TaggingPresetSearchPrimitiveDialog.Action presetSearchPrimitiveAction = new TaggingPresetSearchPrimitiveDialog.Action();
[5965]411 public final DialogsToggleAction dialogsToggleAction = new DialogsToggleAction();
[4139]412 public FullscreenToggleAction fullscreenToggleAction = null;
[4590]413
[8072]414 /**
415 * Popup menu to display menu items search result.
416 */
417 private JPopupMenu searchResultsMenu = new JPopupMenu();
418
[4590]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 */
[6889]424 public static final MenuListener menuSeparatorHandler = new MenuListener() {
[4590]425 @Override
[8510]426 public void menuCanceled(MenuEvent e) {}
427
[4590]428 @Override
[8510]429 public void menuDeselected(MenuEvent e) {}
430
[4590]431 @Override
432 public void menuSelected(MenuEvent a) {
[8510]433 if (!(a.getSource() instanceof JMenu))
[4590]434 return;
435 final JPopupMenu m = ((JMenu) a.getSource()).getPopupMenu();
[8510]436 for (int i = 0; i < m.getComponentCount()-1; i++) {
437 if (!(m.getComponent(i) instanceof JSeparator)) {
[4590]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
[8510]444 if (m.getComponent(m.getComponentCount()-1) instanceof JSeparator) {
[4590]445 ((JSeparator) m.getComponent(m.getComponentCount()-1)).setVisible(false);
446 }
447 }
448 };
[6830]449
[6088]450 /**
[8419]451 * @return the default position of tnew top-level menus
[6088]452 * @since 6088
453 */
[6830]454 public int getDefaultMenuPos() {
455 return defaultMenuPos;
[6088]456 }
[4590]457
[1130]458 /**
[5089]459 * Add a JosmAction at the end of a menu.
[1169]460 *
[1130]461 * This method handles all the shortcut handling. It also makes sure that actions that are
[5089]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
[1130]466 */
[1180]467 public static JMenuItem add(JMenu menu, JosmAction action) {
[4843]468 return add(menu, action, false);
469 }
470
[5089]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 */
[4843]481 public static JMenuItem add(JMenu menu, JosmAction action, boolean isExpert) {
[5089]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) {
[8380]497 if (action.getShortcut().isAutomatic())
[4590]498 return null;
[5089]499 final JMenuItem menuitem;
500 if (index == null) {
501 menuitem = menu.add(action);
502 } else {
503 menuitem = menu.insert(action, index);
504 }
[4843]505 if (isExpert) {
506 ExpertToggleAction.addVisibilitySwitcher(menuitem);
507 }
508 KeyStroke ks = action.getShortcut().getKeyStroke();
509 if (ks != null) {
510 menuitem.setAccelerator(ks);
511 }
[6082]512 // some menus are hidden before they are populated with some items by plugins
513 if (!menu.isVisible()) menu.setVisible(true);
[4590]514 return menuitem;
515 }
516
517 /**
[5089]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
[8540]525 * @param existingMenuEntryAction an action already added to the menu {@code menu},
526 * the action {@code actionToBeInserted} is added directly below
[5089]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 /**
[4590]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
[5089]546 * @param action the action that should get a menu item
[4590]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.
[6180]549 * @return The created menu item
[4590]550 */
551 public static <E extends Enum<E>> JMenuItem add(JMenu menu, JosmAction action, Enum<E> group) {
[8380]552 if (action.getShortcut().isAutomatic())
[4590]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);
[1130]559 }
[1180]560 return menuitem;
[1130]561 }
[2372]562
[4590]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
[5089]567 * @param action the action that should get a menu item
[4590]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.
[6180]571 * @return The created menu item
[4590]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) {
[8510]585 if (group < 0)
[4590]586 return -1;
587 // look for separator that *ends* the group (or stop at end of menu)
588 int i;
[8510]589 for (i = 0; i < menu.getItemCount() && group >= 0; i++) {
590 if (menu.getItem(i) == null) {
[4590]591 group--;
592 }
593 }
594 // insert before separator that ends the group
[8510]595 if (group < 0) {
[4590]596 i--;
597 }
598 // not enough separators have been found, add them
[8510]599 while (group > 0) {
[4590]600 menu.addSeparator();
601 group--;
602 i++;
603 }
604 return i;
605 }
606
[8792]607 /**
608 * @deprecated Use {@link #addMenu(String, String, int, int, String)} instead.
609 */
610 // Kept for compatibility with plugins
611 @Deprecated
[2323]612 public JMenu addMenu(String name, int mnemonicKey, int position, String relativeHelpTopic) {
[8792]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);
[7075]628 if (!GraphicsEnvironment.isHeadless()) {
[7463]629 MenuScroller.setScrollerFor(menu);
[7075]630 }
[4593]631 return addMenu(menu, name, mnemonicKey, position, relativeHelpTopic);
[3737]632 }
633
[8792]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 */
[6180]644 public <T extends JMenu> T addMenu(T menu, String name, int mnemonicKey, int position, String relativeHelpTopic) {
[8792]645 Shortcut.registerShortcut("menu:" + name, tr("Menu: {0}", getName()), mnemonicKey,
[4975]646 Shortcut.MNEMONIC).setMnemonic(menu);
[1498]647 add(menu, position);
[2323]648 menu.putClientProperty("help", relativeHelpTopic);
[1498]649 return menu;
[1130]650 }
[1023]651
[6180]652 /**
653 * Constructs a new {@code MainMenu}.
654 */
[1130]655 public MainMenu() {
[6082]656 moreToolsMenu.setVisible(false);
657 dataMenu.setVisible(false);
658 gpsMenu.setVisible(false);
[6830]659
[1130]660 add(fileMenu, newAction);
[1146]661 add(fileMenu, openFile);
[3710]662 fileMenu.add(recentlyOpened);
[1146]663 add(fileMenu, openLocation);
[1130]664 fileMenu.addSeparator();
665 add(fileMenu, save);
666 add(fileMenu, saveAs);
[6129]667 sessionMenu.setToolTipText(tr("Save and load the current session (list of layers, etc.)"));
[7694]668 sessionMenu.setIcon(new ImageProvider("session").setSize(ImageProvider.ImageSizes.MENU).get());
[6129]669 sessionSaveAs = new SessionSaveAsAction();
670 sessionLoad = new SessionLoadAction();
671 add(sessionMenu, sessionSaveAs);
672 add(sessionMenu, sessionLoad);
673 fileMenu.add(sessionMenu);
674 ExpertToggleAction.addVisibilitySwitcher(sessionMenu);
[4843]675 add(fileMenu, gpxExport, true);
[1130]676 fileMenu.addSeparator();
677 add(fileMenu, download);
[8689]678 add(fileMenu, overpassDownload, true);
[2923]679 add(fileMenu, downloadPrimitive);
[8071]680 add(fileMenu, searchNotes);
[8212]681 add(fileMenu, downloadNotesInView);
[1810]682 add(fileMenu, downloadReferrers);
[2253]683 add(fileMenu, update);
684 add(fileMenu, updateSelection);
[2682]685 add(fileMenu, updateModified);
[2253]686 fileMenu.addSeparator();
[1130]687 add(fileMenu, upload);
[2250]688 add(fileMenu, uploadSelection);
[4843]689 Component sep = new JPopupMenu.Separator();
690 fileMenu.add(sep);
691 ExpertToggleAction.addVisibilitySwitcher(sep);
692 add(fileMenu, closeChangesetAction, true);
[2253]693 fileMenu.addSeparator();
[5857]694 add(fileMenu, restart);
[1130]695 add(fileMenu, exit);
[422]696
[1130]697 add(editMenu, undo);
[4908]698 Main.main.undoRedo.addCommandQueueListener(undo);
[1130]699 add(editMenu, redo);
[4908]700 Main.main.undoRedo.addCommandQueueListener(redo);
[1130]701 editMenu.addSeparator();
702 add(editMenu, copy);
[4843]703 add(editMenu, copyCoordinates, true);
[1130]704 add(editMenu, paste);
705 add(editMenu, pasteTags);
706 add(editMenu, duplicate);
[1219]707 add(editMenu, delete);
[4843]708 add(editMenu, purge, true);
[1130]709 editMenu.addSeparator();
[8510]710 add(editMenu, merge);
711 add(editMenu, mergeSelected);
[1890]712 editMenu.addSeparator();
[1130]713 add(editMenu, search);
[6738]714 add(editMenu, presetSearchPrimitiveAction);
[1130]715 editMenu.addSeparator();
716 add(editMenu, preferences);
[486]717
[2530]718 // -- wireframe toggle action
719 final JCheckBoxMenuItem wireframe = new JCheckBoxMenuItem(wireFrameToggleAction);
[1130]720 viewMenu.add(wireframe);
[2530]721 wireframe.setAccelerator(wireFrameToggleAction.getShortcut().getKeyStroke());
[4175]722 wireFrameToggleAction.addButtonModel(wireframe.getModel());
[2530]723
[5086]724 viewMenu.add(new MapPaintMenu());
[1219]725 viewMenu.addSeparator();
726 add(viewMenu, new ZoomInAction());
727 add(viewMenu, new ZoomOutAction());
728 viewMenu.addSeparator();
[2689]729 for (String mode : AutoScaleAction.MODES) {
[5958]730 AutoScaleAction autoScaleAction = new AutoScaleAction(mode);
731 autoScaleActions.put(mode, autoScaleAction);
[1219]732 add(viewMenu, autoScaleAction);
733 }
[397]734
[3837]735 // -- viewport follow toggle action
736 ViewportFollowToggleAction viewportFollowToggleAction = new ViewportFollowToggleAction();
737 final JCheckBoxMenuItem vft = new JCheckBoxMenuItem(viewportFollowToggleAction);
[4843]738 ExpertToggleAction.addVisibilitySwitcher(vft);
[3837]739 viewMenu.add(vft);
740 vft.setAccelerator(viewportFollowToggleAction.getShortcut().getKeyStroke());
741 viewportFollowToggleAction.addButtonModel(vft.getModel());
742
[8510]743 if (Main.platform.canFullscreen()) {
[3954]744 // -- fullscreen toggle action
[4139]745 fullscreenToggleAction = new FullscreenToggleAction();
[4266]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());
[1498]751 }
[5965]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
[6398]759 add(viewMenu, jumpToAct, true);
[3678]760 viewMenu.addSeparator();
761 add(viewMenu, info);
[4408]762 add(viewMenu, infoweb);
[3678]763 add(viewMenu, historyinfo);
[4840]764 add(viewMenu, historyinfoweb);
765 viewMenu.addSeparator();
[5092]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());
[4840]769 // -- expert mode toggle action
[4843]770 final JCheckBoxMenuItem expertItem = new JCheckBoxMenuItem(ExpertToggleAction.getInstance());
[4840]771 viewMenu.add(expertItem);
[4843]772 ExpertToggleAction.getInstance().addButtonModel(expertItem.getModel());
[1498]773
[3388]774 add(presetsMenu, presetSearchAction);
[6738]775 add(presetsMenu, presetSearchPrimitiveAction);
[6426]776 add(presetsMenu, PreferencesAction.forPreferenceSubTab(tr("Preset preferences"),
777 tr("Click to open the tagging presets tab in the preferences"), TaggingPresetPreference.class));
[4411]778 presetsMenu.addSeparator();
[6426]779
780 add(imageryMenu, PreferencesAction.forPreferenceTab(tr("Imagery preferences"),
781 tr("Click to open the imagery tab in the preferences"), ImageryPreference.class));
[6830]782
[6082]783 add(selectionMenu, selectAll);
784 add(selectionMenu, unselectAll);
[6544]785 add(selectionMenu, nonBranchingWaySequences);
[3388]786
[1130]787 add(toolsMenu, splitWay);
788 add(toolsMenu, combineWay);
789 toolsMenu.addSeparator();
790 add(toolsMenu, reverseWay);
[2575]791 add(toolsMenu, simplifyWay);
[1130]792 toolsMenu.addSeparator();
793 add(toolsMenu, alignInCircle);
794 add(toolsMenu, alignInLine);
[1380]795 add(toolsMenu, distribute);
[1130]796 add(toolsMenu, ortho);
[4843]797 add(toolsMenu, mirror, true);
[1130]798 toolsMenu.addSeparator();
[4843]799 add(toolsMenu, followLine, true);
[6412]800 add(toolsMenu, addNode, true);
801 add(toolsMenu, moveNode, true);
[1130]802 add(toolsMenu, createCircle);
803 toolsMenu.addSeparator();
804 add(toolsMenu, mergeNodes);
805 add(toolsMenu, joinNodeWay);
[7131]806 add(toolsMenu, moveNodeOntoWay);
[6253]807 add(toolsMenu, unJoinNodeWay);
[1130]808 add(toolsMenu, unglueNodes);
[3704]809 toolsMenu.addSeparator();
[2575]810 add(toolsMenu, joinAreas);
[3704]811 add(toolsMenu, createMultipolygon);
[6597]812 add(toolsMenu, updateMultipolygon);
[422]813
[4590]814 // -- changeset manager toggle action
[6180]815 final JCheckBoxMenuItem mi = MainMenu.addWithCheckbox(windowMenu, changesetManager,
[4590]816 MainMenu.WINDOW_MENU_GROUP.ALWAYS);
[6180]817 changesetManager.addButtonModel(mi.getModel());
[4590]818
[1222]819 if (!Main.pref.getBoolean("audio.menuinvisible", false)) {
[5661]820 showAudioMenu(true);
[1130]821 }
[6070]822
[5661]823 Main.pref.addPreferenceChangeListener(new PreferenceChangedListener() {
824 @Override
825 public void preferenceChanged(PreferenceChangeEvent e) {
[6990]826 if ("audio.menuinvisible".equals(e.getKey())) {
[5661]827 showAudioMenu(!Boolean.parseBoolean(e.getNewValue().toString()));
828 }
829 }
830 });
[547]831
[1416]832 helpMenu.add(statusreport);
[7624]833 helpMenu.add(reportbug);
834 helpMenu.addSeparator();
[1498]835
[8510]836 // FIXME why is help not a JosmAction?
837 helpMenu.add(help).setAccelerator(Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1,
[4982]838 Shortcut.DIRECT).getKeyStroke());
[1130]839 add(helpMenu, about);
[8072]840 add(Box.createHorizontalGlue());
[8081]841 final DisableShortcutsOnFocusGainedTextField searchField = createSearchField();
842 add(searchField);
[1808]843
[8081]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
[4590]853 windowMenu.addMenuListener(menuSeparatorHandler);
854
[1808]855 new PresetsMenuEnabler(presetsMenu).refreshEnabled();
[422]856 }
[6070]857
[8075]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
[8072]869 /**
870 * Create search field.
871 */
[8081]872 private DisableShortcutsOnFocusGainedTextField createSearchField() {
[8075]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 };
[8720]882 Shortcut searchFieldShortcut = Shortcut.registerShortcut("menu:search-field", tr("Search menu items"), KeyEvent.VK_R, Shortcut.MNEMONIC);
883 searchFieldShortcut.setFocusAccelerator(searchField);
[8072]884 searchField.setEditable(true);
[8075]885 searchField.setMaximumSize(new Dimension(200, helpMenu.getPreferredSize().height));
[8072]886 searchField.setHint(tr("Search menu items"));
[8720]887 searchField.setToolTipText(Main.platform.makeTooltip(tr("Search menu items"), searchFieldShortcut));
[8072]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) {
[8404]899 // Explicitely use default locale in this case, because we're looking for translated strings
900 textToFind = textToFind.toLowerCase(Locale.getDefault());
[8072]901 List<JMenuItem> result = new ArrayList<>();
902
903 // Iterate over main menus
904 for (MenuElement menuElement : getSubElements()) {
[8404]905 if (!(menuElement instanceof JMenu)) continue;
[8072]906
907 JMenu mainMenuItem = (JMenu) menuElement;
[8404]908 if (mainMenuItem.getAction() != null && mainMenuItem.getText().toLowerCase(Locale.getDefault()).contains(textToFind)) {
[8072]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) {
[8510]926 for (int i = 0; i < menu.getItemCount(); i++) {
[8072]927 JMenuItem menuItem = menu.getItem(i);
928 if (menuItem == null) continue;
929
[8404]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)) {
[8072]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
[5661]942 protected void showAudioMenu(boolean showMenu) {
943 if (showMenu && audioMenu == null) {
[8792]944 audioMenu = addMenu("Audio", /* I18N: mnemonic: U */ trc("menu", "Audio"), KeyEvent.VK_U, defaultMenuPos, ht("/Menu/Audio"));
[5661]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 }
[1808]960
[2626]961 static class PresetsMenuEnabler implements MapView.LayerChangeListener {
[1808]962 private JMenu presetsMenu;
[8510]963
[1808]964 public PresetsMenuEnabler(JMenu presetsMenu) {
[2621]965 MapView.addLayerChangeListener(this);
[1808]966 this.presetsMenu = presetsMenu;
967 }
968 /**
969 * Refreshes the enabled state
970 */
971 protected void refreshEnabled() {
[6336]972 presetsMenu.setEnabled(Main.main.hasEditLayer());
[1808]973 }
974
[5965]975 @Override
[1808]976 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
977 refreshEnabled();
978 }
979
[5965]980 @Override
[1808]981 public void layerAdded(Layer newLayer) {
982 refreshEnabled();
983 }
984
[5965]985 @Override
[1808]986 public void layerRemoved(Layer oldLayer) {
987 refreshEnabled();
988 }
989 }
[8072]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 */
[8365]995 private static class SearchFieldKeyListener implements KeyListener {
[8072]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();
[8510]1002 if (selection.length > 1) {
[8072]1003 MenuElement selectedElement = selection[selection.length-1];
[8510]1004 if (selectedElement instanceof JMenuItem) {
[8072]1005 JMenuItem selectedItem = (JMenuItem) selectedElement;
1006 Action menuAction = selectedItem.getAction();
1007 menuAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
[8441]1008 if (Main.isDebugEnabled()) {
1009 Main.debug(getClass().getName()+" consuming event "+e);
1010 }
[8072]1011 e.consume();
1012 }
1013 }
1014 }
1015 }
1016
1017 @Override
[8365]1018 public void keyTyped(KeyEvent e) {
1019 // Not used
1020 }
[8072]1021
1022 @Override
[8365]1023 public void keyReleased(KeyEvent e) {
1024 // Not used
1025 }
[8072]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) {
[8404]1055 // Explicitely use default locale in this case, because we're looking for translated strings
1056 searchTerm = searchTerm.trim().toLowerCase(Locale.getDefault());
[8072]1057
1058 if (searchTerm.equals(currentSearchText)) {
1059 return;
1060 }
1061 currentSearchText = searchTerm;
[8394]1062 if (searchTerm.isEmpty()) {
[8081]1063 // No text to search
[8072]1064 hideMenu();
1065 return;
1066 }
1067
1068 List<JMenuItem> searchResult = mainMenu.findMenuItems(currentSearchText);
[8510]1069 if (searchResult.isEmpty()) {
[8081]1070 // Nothing found
[8072]1071 hideMenu();
1072 return;
1073 }
1074
[8510]1075 if (searchResult.size() > 20) {
[8081]1076 // Too many items found...
[8072]1077 searchResult = searchResult.subList(0, 20);
1078 }
1079
[8081]1080 // Update Popup menu
[8072]1081 searchResultsMenu.removeAll();
1082 for (JMenuItem foundItem : searchResult) {
1083 searchResultsMenu.add(foundItem.getText()).setAction(foundItem.getAction());
1084 }
[8081]1085 // Put menu right under search field
[8072]1086 searchResultsMenu.pack();
[8081]1087 searchResultsMenu.show(mainMenu, searchField.getX(), searchField.getY() + searchField.getHeight());
[8072]1088
[8081]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();
[8072]1091 }
1092
1093 private void hideMenu() {
1094 searchResultsMenu.setVisible(false);
1095 }
1096 }
[422]1097}
Note: See TracBrowser for help on using the repository browser.