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