source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PresetListPanel.java@ 4968

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

fix #7386 - Major rework of preferences GUI settings in order to speed up preferences dialog startup time. The building of each preferences tab is delayed until this tab is selected. Plugins that use preferences will need to make some (minor) changes.

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