1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.Font;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 | import java.awt.event.MouseListener;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
19 | import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
|
---|
20 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
21 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.Check;
|
---|
22 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.Combo;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.Text;
|
---|
25 | import org.openstreetmap.josm.tools.GBC;
|
---|
26 |
|
---|
27 | public class PresetListPanel extends JPanel {
|
---|
28 |
|
---|
29 | public PresetListPanel() {
|
---|
30 | super(new GridBagLayout());
|
---|
31 | }
|
---|
32 |
|
---|
33 | public interface PresetHandler {
|
---|
34 | Collection<OsmPrimitive> getSelection();
|
---|
35 | void updateTags(List<Tag> tags);
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Small helper class that manages the highlighting of the label on hover as well as opening
|
---|
40 | * the corresponding preset when clicked
|
---|
41 | */
|
---|
42 | private static class PresetLabelML implements MouseListener {
|
---|
43 | final JLabel label;
|
---|
44 | final Font bold;
|
---|
45 | final Font normal;
|
---|
46 | final TaggingPreset tag;
|
---|
47 | final PresetHandler presetHandler;
|
---|
48 |
|
---|
49 | PresetLabelML(JLabel lbl, TaggingPreset t, PresetHandler presetHandler) {
|
---|
50 | super();
|
---|
51 | label = lbl;
|
---|
52 | lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
---|
53 | normal = label.getFont();
|
---|
54 | bold = normal.deriveFont(normal.getStyle() ^ Font.BOLD);
|
---|
55 | tag = t;
|
---|
56 | this.presetHandler = presetHandler;
|
---|
57 | }
|
---|
58 | public void mouseClicked(MouseEvent arg0) {
|
---|
59 | Collection<OsmPrimitive> selection = presetHandler.getSelection();
|
---|
60 | if (selection == null || selection.isEmpty())
|
---|
61 | return;
|
---|
62 | int answer = tag.showDialog(selection, false);
|
---|
63 |
|
---|
64 | if (answer == TaggingPreset.DIALOG_ANSWER_APPLY) {
|
---|
65 | presetHandler.updateTags(tag.getChangedTags());
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 | public void mouseEntered(MouseEvent arg0) {
|
---|
70 | label.setFont(bold);
|
---|
71 | }
|
---|
72 | public void mouseExited(MouseEvent arg0) {
|
---|
73 | label.setFont(normal);
|
---|
74 | }
|
---|
75 | public void mousePressed(MouseEvent arg0) {}
|
---|
76 | public void mouseReleased(MouseEvent arg0) {}
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void updatePresets(int nodes, int ways, int relations, int closedways, Map<String, Map<String, Integer>> valueCount, PresetHandler presetHandler) {
|
---|
80 |
|
---|
81 | removeAll();
|
---|
82 | int total = nodes+ways+relations+closedways;
|
---|
83 | if(total == 0) {
|
---|
84 | setVisible(false);
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | for(TaggingPreset t : TaggingPresetPreference.taggingPresets) {
|
---|
89 | if(
|
---|
90 | ( t.types == null
|
---|
91 | || (relations > 0 && t.types.contains(PresetType.RELATION))
|
---|
92 | || (nodes > 0 && t.types.contains(PresetType.NODE))
|
---|
93 | || (ways+closedways > 0 && t.types.contains(PresetType.WAY))
|
---|
94 | || (closedways > 0 && t.types.contains(PresetType.CLOSEDWAY))
|
---|
95 | )
|
---|
96 | && t.isShowable())
|
---|
97 | {
|
---|
98 | int found = 0;
|
---|
99 | for(TaggingPreset.Item i : t.data) {
|
---|
100 | if(i instanceof TaggingPreset.Key) {
|
---|
101 | String val = ((TaggingPreset.Key)i).value;
|
---|
102 | String key = ((TaggingPreset.Key)i).key;
|
---|
103 | // we subtract 100 if not found and add 1 if found
|
---|
104 | found -= 100;
|
---|
105 | if(key == null || !valueCount.containsKey(key)) {
|
---|
106 | continue;
|
---|
107 | }
|
---|
108 |
|
---|
109 | Map<String, Integer> v = valueCount.get(key);
|
---|
110 | if(v.size() == 1 && val != null && v.containsKey(val) && v.get(val) == total) {
|
---|
111 | found += 101;
|
---|
112 | }
|
---|
113 | } else {
|
---|
114 | String key = null;
|
---|
115 | if ((i instanceof Text) && ((Text)i).required) {
|
---|
116 | key = ((Text)i).key;
|
---|
117 | } else if ((i instanceof Combo) && ((Combo)i).required) {
|
---|
118 | key = ((Combo)i).key;
|
---|
119 | } else if ((i instanceof Check) && ((Check)i).required) {
|
---|
120 | key = ((Check)i).key;
|
---|
121 | }
|
---|
122 | if (key != null) {
|
---|
123 | if (valueCount.get(key) != null) {
|
---|
124 | found += 1;
|
---|
125 | } else {
|
---|
126 | found -= 100;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | if(found <= 0) {
|
---|
133 | continue;
|
---|
134 | }
|
---|
135 |
|
---|
136 | JLabel lbl = new JLabel(t.getName());
|
---|
137 | lbl.addMouseListener(new PresetLabelML(lbl, t, presetHandler));
|
---|
138 | add(lbl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | if(getComponentCount() > 0) {
|
---|
143 | setVisible(true);
|
---|
144 | // This ensures the presets are exactly as high as needed.
|
---|
145 | int height = getComponentCount() * getComponent(0).getHeight();
|
---|
146 | Dimension size = new Dimension(getWidth(), height);
|
---|
147 | setMaximumSize(size);
|
---|
148 | setMinimumSize(size);
|
---|
149 | } else {
|
---|
150 | setVisible(false);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | }
|
---|