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