source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetMenu.java@ 8863

Last change on this file since 8863 was 8863, checked in by Don-vip, 9 years ago

major code cleanup/refactoring of tagging presets: slay the monster TaggingPresetItems (60 Kb, 1600 lines) and extract all its internal classes to a new package gui.tagging.presets.items

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.MouseInfo;
8import java.awt.Point;
9import java.awt.event.ActionEvent;
10import java.io.Serializable;
11import java.util.ArrayList;
12import java.util.Collections;
13import java.util.Comparator;
14import java.util.List;
15import java.util.Objects;
16
17import javax.swing.Action;
18import javax.swing.JMenu;
19import javax.swing.JMenuItem;
20import javax.swing.JPopupMenu;
21import javax.swing.JSeparator;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.tools.AlphanumComparator;
25
26public class TaggingPresetMenu extends TaggingPreset {
27 public JMenu menu; // set by TaggingPresets
28
29 private static class PresetTextComparator implements Comparator<JMenuItem>, Serializable {
30 @Override
31 public int compare(JMenuItem o1, JMenuItem o2) {
32 if (Main.main.menu.presetSearchAction.equals(o1.getAction()))
33 return -1;
34 else if (Main.main.menu.presetSearchAction.equals(o2.getAction()))
35 return 1;
36 else
37 return AlphanumComparator.getInstance().compare(o1.getText(), o2.getText());
38 }
39 }
40
41 /**
42 * {@code TaggingPresetMenu} are considered equivalent if (and only if) their {@link #getRawName()} match.
43 */
44 @Override
45 public boolean equals(Object o) {
46 if (this == o) return true;
47 if (o == null || getClass() != o.getClass()) return false;
48 TaggingPresetMenu that = (TaggingPresetMenu) o;
49 return Objects.equals(getRawName(), that.getRawName());
50 }
51
52 @Override
53 public int hashCode() {
54 return Objects.hash(getRawName());
55 }
56
57 @Override
58 public void setDisplayName() {
59 putValue(Action.NAME, getName());
60 /** Tooltips should be shown for the toolbar buttons, but not in the menu. */
61 putValue(OPTIONAL_TOOLTIP_TEXT, group != null ?
62 tr("Preset group {1} / {0}", getLocaleName(), group.getName()) :
63 tr("Preset group {0}", getLocaleName()));
64 putValue("toolbar", "tagginggroup_" + getRawName());
65 }
66
67 private Component copyMenuComponent(Component menuComponent) {
68 if (menuComponent instanceof JMenu) {
69 JMenu menu = (JMenu) menuComponent;
70 JMenu result = new JMenu(menu.getAction());
71 for (Component item:menu.getMenuComponents()) {
72 result.add(copyMenuComponent(item));
73 }
74 result.setText(menu.getText());
75 return result;
76 } else if (menuComponent instanceof JMenuItem) {
77 JMenuItem menuItem = (JMenuItem) menuComponent;
78 JMenuItem result = new JMenuItem(menuItem.getAction());
79 result.setText(menuItem.getText());
80 return result;
81 } else if (menuComponent instanceof JSeparator) {
82 return new JSeparator();
83 } else {
84 return menuComponent;
85 }
86 }
87
88 @Override
89 public void actionPerformed(ActionEvent e) {
90 Object s = e.getSource();
91 if (menu != null && s instanceof Component) {
92 JPopupMenu pm = new JPopupMenu(getName());
93 for (Component c : menu.getMenuComponents()) {
94 pm.add(copyMenuComponent(c));
95 }
96 Point p = MouseInfo.getPointerInfo().getLocation();
97 pm.show(Main.parent, p.x-Main.parent.getX(), p.y-Main.parent.getY());
98 }
99 }
100
101 /**
102 * Sorts the menu items using the translated item text
103 */
104 public void sortMenu() {
105 TaggingPresetMenu.sortMenu(this.menu);
106 }
107
108 /**
109 * Sorts the menu items using the translated item text
110 */
111 public static void sortMenu(JMenu menu) {
112 Component[] items = menu.getMenuComponents();
113 PresetTextComparator comp = new PresetTextComparator();
114 List<JMenuItem> sortarray = new ArrayList<>();
115 int lastSeparator = 0;
116 for (int i = 0; i < items.length; i++) {
117 Object item = items[i];
118 if (item instanceof JMenu) {
119 sortMenu((JMenu) item);
120 }
121 if (item instanceof JMenuItem) {
122 sortarray.add((JMenuItem) item);
123 if (i == items.length-1) {
124 Collections.sort(sortarray, comp);
125 int pos = 0;
126 for (JMenuItem menuItem : sortarray) {
127 int oldPos;
128 if (lastSeparator == 0) {
129 oldPos = pos;
130 } else {
131 oldPos = pos+lastSeparator+1;
132 }
133 menu.add(menuItem, oldPos);
134 pos++;
135 }
136 sortarray = new ArrayList<>();
137 lastSeparator = 0;
138 }
139 } else if (item instanceof JSeparator) {
140 Collections.sort(sortarray, comp);
141 int pos = 0;
142 for (JMenuItem menuItem : sortarray) {
143 int oldPos;
144 if (lastSeparator == 0) {
145 oldPos = pos;
146 } else {
147 oldPos = pos+lastSeparator+1;
148 }
149 menu.add(menuItem, oldPos);
150 pos++;
151 }
152 sortarray = new ArrayList<>();
153 lastSeparator = i;
154 }
155 }
156 }
157}
Note: See TracBrowser for help on using the repository browser.