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