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