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