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 | String n = getName();
|
---|
23 | putValue(Action.NAME, n);
|
---|
24 | putValue(SHORT_DESCRIPTION, tr("Preset group ''{0}''", n));
|
---|
25 | putValue("toolbar", "tagginggroup_" + getRawName());
|
---|
26 | }
|
---|
27 | public void setIcon(String iconName) {
|
---|
28 | super.setIcon(iconName);
|
---|
29 | }
|
---|
30 | public void actionPerformed(ActionEvent e) {
|
---|
31 | Object s = e.getSource();
|
---|
32 | if(menu != null && s instanceof Component)
|
---|
33 | {
|
---|
34 | Component co = (Component)s;
|
---|
35 | JPopupMenu pm = new JPopupMenu(getName());
|
---|
36 | for(Component c : menu.getMenuComponents())
|
---|
37 | {
|
---|
38 | if(c instanceof JMenuItem)
|
---|
39 | {
|
---|
40 | JMenuItem j = new JMenuItem(((JMenuItem)c).getAction());
|
---|
41 | j.setText(((JMenuItem)c).getText());
|
---|
42 | pm.add(j);
|
---|
43 | }
|
---|
44 | else if(c instanceof JSeparator)
|
---|
45 | pm.addSeparator();
|
---|
46 | }
|
---|
47 | pm.show(co, co.getWidth()/2, co.getHeight()/2);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | /**
|
---|
51 | * Sorts the menu items using the translated item text
|
---|
52 | */
|
---|
53 | public void sortMenu(){
|
---|
54 | TaggingPresetMenu.sortMenu(this.menu);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Sorts the menu items using the translated item text
|
---|
59 | */
|
---|
60 | public static void sortMenu(JMenu menu){
|
---|
61 | Component[] items = menu.getMenuComponents();
|
---|
62 | PresetTextComparator comp = new PresetTextComparator();
|
---|
63 | ArrayList<JMenuItem> sortarray = new ArrayList<JMenuItem>();
|
---|
64 | int lastSeperator = 0;
|
---|
65 | for (int i = 0; i < items.length; i++) {
|
---|
66 | Object item = items[i];
|
---|
67 | if (item instanceof JMenu){
|
---|
68 | sortMenu((JMenu)item);
|
---|
69 | }
|
---|
70 | if (item instanceof JMenuItem){
|
---|
71 | sortarray.add((JMenuItem)item);
|
---|
72 | if (i == items.length-1){
|
---|
73 | Collections.sort(sortarray, comp);
|
---|
74 | int pos = 0;
|
---|
75 | for (JMenuItem menuItem : sortarray) {
|
---|
76 | int oldPos;
|
---|
77 | if(lastSeperator == 0){
|
---|
78 | oldPos=pos;
|
---|
79 | }else {
|
---|
80 | oldPos = pos+lastSeperator+1;
|
---|
81 | }
|
---|
82 | menu.add(menuItem, oldPos);
|
---|
83 | pos++;
|
---|
84 | }
|
---|
85 | sortarray = new ArrayList<JMenuItem>();
|
---|
86 | lastSeperator = 0;
|
---|
87 | }
|
---|
88 | }else if (item instanceof JSeparator){
|
---|
89 | Collections.sort(sortarray, comp);
|
---|
90 | int pos = 0;
|
---|
91 | for (JMenuItem menuItem : sortarray) {
|
---|
92 | int oldPos;
|
---|
93 | if(lastSeperator == 0){
|
---|
94 | oldPos=pos;
|
---|
95 | }else {
|
---|
96 | oldPos = pos+lastSeperator+1;
|
---|
97 | }
|
---|
98 | menu.add(menuItem, oldPos);
|
---|
99 | pos++;
|
---|
100 | }
|
---|
101 | sortarray = new ArrayList<JMenuItem>();
|
---|
102 | lastSeperator = i;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|