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