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