source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetMenu.java@ 19050

Last change on this file since 19050 was 19050, checked in by taylor.smock, 6 weeks ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.MouseInfo;
8import java.awt.Point;
9import java.awt.PointerInfo;
10import java.awt.event.ActionEvent;
11import java.io.Serializable;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Comparator;
15import java.util.List;
16import java.util.Objects;
17
18import javax.swing.Action;
19import javax.swing.JMenu;
20import javax.swing.JMenuItem;
21import javax.swing.JPopupMenu;
22import javax.swing.JSeparator;
23
24import org.openstreetmap.josm.actions.JosmAction;
25import org.openstreetmap.josm.gui.MainApplication;
26import org.openstreetmap.josm.gui.MainFrame;
27import org.openstreetmap.josm.gui.MainMenu;
28import org.openstreetmap.josm.tools.AlphanumComparator;
29import org.openstreetmap.josm.tools.Logging;
30
31/**
32 * Menu that groups several presets from one topic.
33 * <p>
34 * Used, to create the nested directory structure in the preset main menu entry.
35 */
36public class TaggingPresetMenu extends TaggingPreset {
37 /** The menu to show users */
38 public JMenu menu; // set by TaggingPresets
39
40 private static final class PresetTextComparator implements Comparator<JMenuItem>, Serializable {
41 private static final long serialVersionUID = 1L;
42 @Override
43 public int compare(JMenuItem o1, JMenuItem o2) {
44 final MainMenu menu = MainApplication.getMenu();
45 // This is needed to keep the order of the search actions -> preferences
46 for (JosmAction action : Arrays.asList(menu.presetSearchAction, menu.presetSearchPrimitiveAction)) {
47 if (action.equals(o1.getAction())) {
48 return -1;
49 } else if (action.equals(o2.getAction())) {
50 return 1;
51 }
52 }
53 return AlphanumComparator.getInstance().compare(o1.getText(), o2.getText());
54 }
55 }
56
57 /**
58 * {@code TaggingPresetMenu} are considered equivalent if (and only if) their {@link #getRawName()} match.
59 */
60 @Override
61 public boolean equals(Object o) {
62 if (this == o) return true;
63 if (o == null || getClass() != o.getClass()) return false;
64 TaggingPresetMenu that = (TaggingPresetMenu) o;
65 return Objects.equals(getRawName(), that.getRawName());
66 }
67
68 @Override
69 public int hashCode() {
70 return Objects.hash(getRawName());
71 }
72
73 @Override
74 public void setDisplayName() {
75 putValue(Action.NAME, getName());
76 /* Tooltips should be shown for the toolbar buttons, but not in the menu. */
77 putValue(OPTIONAL_TOOLTIP_TEXT, group != null ?
78 tr("Preset group {1} / {0}", getLocaleName(), group.getName()) :
79 tr("Preset group {0}", getLocaleName()));
80 putValue("toolbar", "tagginggroup_" + getRawName());
81 }
82
83 private static Component copyMenuComponent(Component menuComponent) {
84 if (menuComponent instanceof JMenu) {
85 JMenu menu = (JMenu) menuComponent;
86 JMenu result = new JMenu(menu.getAction());
87 for (Component item:menu.getMenuComponents()) {
88 result.add(copyMenuComponent(item));
89 }
90 result.setText(menu.getText());
91 return result;
92 } else if (menuComponent instanceof JMenuItem) {
93 JMenuItem menuItem = (JMenuItem) menuComponent;
94 JMenuItem result = new JMenuItem(menuItem.getAction());
95 result.setText(menuItem.getText());
96 return result;
97 } else if (menuComponent instanceof JSeparator) {
98 return new JSeparator();
99 } else {
100 return menuComponent;
101 }
102 }
103
104 @Override
105 public void actionPerformed(ActionEvent e) {
106 Object s = e.getSource();
107 if (menu != null && s instanceof Component) {
108 JPopupMenu pm = new JPopupMenu(getName());
109 for (Component c : menu.getMenuComponents()) {
110 pm.add(copyMenuComponent(c));
111 }
112 try {
113 PointerInfo pointerInfo = MouseInfo.getPointerInfo();
114 if (pointerInfo != null) {
115 Point p = pointerInfo.getLocation();
116 MainFrame parent = MainApplication.getMainFrame();
117 if (parent.isShowing()) {
118 pm.show(parent, p.x-parent.getX(), p.y-parent.getY());
119 }
120 }
121 } catch (SecurityException ex) {
122 Logging.log(Logging.LEVEL_ERROR, "Unable to get mouse pointer info", ex);
123 }
124 }
125 }
126
127 /**
128 * Sorts the menu items using the translated item text
129 */
130 public void sortMenu() {
131 TaggingPresetMenu.sortMenu(this.menu);
132 }
133
134 /**
135 * Sorts the menu items using the translated item text
136 * @param menu menu to sort
137 */
138 public static void sortMenu(JMenu menu) {
139 Component[] items = menu.getMenuComponents();
140 PresetTextComparator comp = new PresetTextComparator();
141 List<JMenuItem> sortarray = new ArrayList<>();
142 int lastSeparator = 0;
143 for (int i = 0; i < items.length; i++) {
144 Object item = items[i];
145 if (item instanceof JMenu) {
146 sortMenu((JMenu) item);
147 }
148 if (item instanceof JMenuItem) {
149 sortarray.add((JMenuItem) item);
150 if (i == items.length-1) {
151 handleMenuItem(menu, comp, sortarray, lastSeparator);
152 sortarray = new ArrayList<>();
153 lastSeparator = 0;
154 }
155 } else if (item instanceof JSeparator) {
156 handleMenuItem(menu, comp, sortarray, lastSeparator);
157 sortarray = new ArrayList<>();
158 lastSeparator = i;
159 }
160 }
161 }
162
163 private static void handleMenuItem(JMenu menu, PresetTextComparator comp, List<JMenuItem> sortarray, int lastSeparator) {
164 sortarray.sort(comp);
165 int pos = 0;
166 for (JMenuItem menuItem : sortarray) {
167 int oldPos;
168 if (lastSeparator == 0) {
169 oldPos = pos;
170 } else {
171 oldPos = pos+lastSeparator+1;
172 }
173 menu.add(menuItem, oldPos);
174 pos++;
175 }
176 }
177}
Note: See TracBrowser for help on using the repository browser.