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 | @Override
|
---|
38 | public int compare(JMenuItem o1, JMenuItem o2) {
|
---|
39 | if (MainApplication.getMenu().presetSearchAction.equals(o1.getAction()))
|
---|
40 | return -1;
|
---|
41 | else if (MainApplication.getMenu().presetSearchAction.equals(o2.getAction()))
|
---|
42 | return 1;
|
---|
43 | else
|
---|
44 | return AlphanumComparator.getInstance().compare(o1.getText(), o2.getText());
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * {@code TaggingPresetMenu} are considered equivalent if (and only if) their {@link #getRawName()} match.
|
---|
50 | */
|
---|
51 | @Override
|
---|
52 | public boolean equals(Object o) {
|
---|
53 | if (this == o) return true;
|
---|
54 | if (o == null || getClass() != o.getClass()) return false;
|
---|
55 | TaggingPresetMenu that = (TaggingPresetMenu) o;
|
---|
56 | return Objects.equals(getRawName(), that.getRawName());
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public int hashCode() {
|
---|
61 | return Objects.hash(getRawName());
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void setDisplayName() {
|
---|
66 | putValue(Action.NAME, getName());
|
---|
67 | /** Tooltips should be shown for the toolbar buttons, but not in the menu. */
|
---|
68 | putValue(OPTIONAL_TOOLTIP_TEXT, group != null ?
|
---|
69 | tr("Preset group {1} / {0}", getLocaleName(), group.getName()) :
|
---|
70 | tr("Preset group {0}", getLocaleName()));
|
---|
71 | putValue("toolbar", "tagginggroup_" + getRawName());
|
---|
72 | }
|
---|
73 |
|
---|
74 | private static Component copyMenuComponent(Component menuComponent) {
|
---|
75 | if (menuComponent instanceof JMenu) {
|
---|
76 | JMenu menu = (JMenu) menuComponent;
|
---|
77 | JMenu result = new JMenu(menu.getAction());
|
---|
78 | for (Component item:menu.getMenuComponents()) {
|
---|
79 | result.add(copyMenuComponent(item));
|
---|
80 | }
|
---|
81 | result.setText(menu.getText());
|
---|
82 | return result;
|
---|
83 | } else if (menuComponent instanceof JMenuItem) {
|
---|
84 | JMenuItem menuItem = (JMenuItem) menuComponent;
|
---|
85 | JMenuItem result = new JMenuItem(menuItem.getAction());
|
---|
86 | result.setText(menuItem.getText());
|
---|
87 | return result;
|
---|
88 | } else if (menuComponent instanceof JSeparator) {
|
---|
89 | return new JSeparator();
|
---|
90 | } else {
|
---|
91 | return menuComponent;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public void actionPerformed(ActionEvent e) {
|
---|
97 | Object s = e.getSource();
|
---|
98 | if (menu != null && s instanceof Component) {
|
---|
99 | JPopupMenu pm = new JPopupMenu(getName());
|
---|
100 | for (Component c : menu.getMenuComponents()) {
|
---|
101 | pm.add(copyMenuComponent(c));
|
---|
102 | }
|
---|
103 | try {
|
---|
104 | PointerInfo pointerInfo = MouseInfo.getPointerInfo();
|
---|
105 | if (pointerInfo != null) {
|
---|
106 | Point p = pointerInfo.getLocation();
|
---|
107 | MainFrame parent = MainApplication.getMainFrame();
|
---|
108 | pm.show(parent, p.x-parent.getX(), p.y-parent.getY());
|
---|
109 | }
|
---|
110 | } catch (SecurityException ex) {
|
---|
111 | Logging.log(Logging.LEVEL_ERROR, "Unable to get mouse pointer info", ex);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Sorts the menu items using the translated item text
|
---|
118 | */
|
---|
119 | public void sortMenu() {
|
---|
120 | TaggingPresetMenu.sortMenu(this.menu);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Sorts the menu items using the translated item text
|
---|
125 | * @param menu menu to sort
|
---|
126 | */
|
---|
127 | public static void sortMenu(JMenu menu) {
|
---|
128 | Component[] items = menu.getMenuComponents();
|
---|
129 | PresetTextComparator comp = new PresetTextComparator();
|
---|
130 | List<JMenuItem> sortarray = new ArrayList<>();
|
---|
131 | int lastSeparator = 0;
|
---|
132 | for (int i = 0; i < items.length; i++) {
|
---|
133 | Object item = items[i];
|
---|
134 | if (item instanceof JMenu) {
|
---|
135 | sortMenu((JMenu) item);
|
---|
136 | }
|
---|
137 | if (item instanceof JMenuItem) {
|
---|
138 | sortarray.add((JMenuItem) item);
|
---|
139 | if (i == items.length-1) {
|
---|
140 | handleMenuItem(menu, comp, sortarray, lastSeparator);
|
---|
141 | sortarray = new ArrayList<>();
|
---|
142 | lastSeparator = 0;
|
---|
143 | }
|
---|
144 | } else if (item instanceof JSeparator) {
|
---|
145 | handleMenuItem(menu, comp, sortarray, lastSeparator);
|
---|
146 | sortarray = new ArrayList<>();
|
---|
147 | lastSeparator = i;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | private static void handleMenuItem(JMenu menu, PresetTextComparator comp, List<JMenuItem> sortarray, int lastSeparator) {
|
---|
153 | sortarray.sort(comp);
|
---|
154 | int pos = 0;
|
---|
155 | for (JMenuItem menuItem : sortarray) {
|
---|
156 | int oldPos;
|
---|
157 | if (lastSeparator == 0) {
|
---|
158 | oldPos = pos;
|
---|
159 | } else {
|
---|
160 | oldPos = pos+lastSeparator+1;
|
---|
161 | }
|
---|
162 | menu.add(menuItem, oldPos);
|
---|
163 | pos++;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|