1 | package org.openstreetmap.josm.gui.annotation;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.Image;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.io.BufferedReader;
|
---|
10 | import java.io.FileInputStream;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.io.InputStreamReader;
|
---|
14 | import java.io.UnsupportedEncodingException;
|
---|
15 | import java.net.URL;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.StringTokenizer;
|
---|
20 |
|
---|
21 | import javax.swing.AbstractAction;
|
---|
22 | import javax.swing.Action;
|
---|
23 | import javax.swing.ImageIcon;
|
---|
24 | import javax.swing.JCheckBox;
|
---|
25 | import javax.swing.JComboBox;
|
---|
26 | import javax.swing.JLabel;
|
---|
27 | import javax.swing.JOptionPane;
|
---|
28 | import javax.swing.JPanel;
|
---|
29 | import javax.swing.JTextField;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
33 | import org.openstreetmap.josm.command.Command;
|
---|
34 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
35 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
36 | import org.openstreetmap.josm.tools.GBC;
|
---|
37 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
38 | import org.xml.sax.SAXException;
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * This class read encapsulate one annotation preset. A class method can
|
---|
43 | * read in all predefined presets, either shipped with JOSM or that are
|
---|
44 | * in the config directory.
|
---|
45 | *
|
---|
46 | * It is also able to construct dialogs out of preset definitions.
|
---|
47 | */
|
---|
48 | public class AnnotationPreset extends AbstractAction {
|
---|
49 |
|
---|
50 | private static interface Item {
|
---|
51 | void addToPanel(JPanel p);
|
---|
52 | void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds);
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static class Text implements Item {
|
---|
56 | public String key;
|
---|
57 | public String text;
|
---|
58 | public String default_;
|
---|
59 | public boolean delete_if_empty = false;
|
---|
60 |
|
---|
61 | private JTextField value = new JTextField();
|
---|
62 |
|
---|
63 | public void addToPanel(JPanel p) {
|
---|
64 | value.setText(default_ == null ? "" : default_);
|
---|
65 | p.add(new JLabel(text), GBC.std().insets(0,0,10,0));
|
---|
66 | p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
67 | }
|
---|
68 | public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
69 | String v = value.getText();
|
---|
70 | if (delete_if_empty && v.length() == 0)
|
---|
71 | v = null;
|
---|
72 | cmds.add(new ChangePropertyCommand(sel, key, v));
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static class Check implements Item {
|
---|
77 | public String key;
|
---|
78 | public String text;
|
---|
79 | public boolean default_ = false;
|
---|
80 |
|
---|
81 | private JCheckBox check = new JCheckBox();
|
---|
82 |
|
---|
83 | public void addToPanel(JPanel p) {
|
---|
84 | check.setSelected(default_);
|
---|
85 | check.setText(text);
|
---|
86 | p.add(check, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
87 | }
|
---|
88 | public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
89 | cmds.add(new ChangePropertyCommand(sel, key, check.isSelected() ? "true" : null));
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public static class Combo implements Item {
|
---|
94 | public String key;
|
---|
95 | public String text;
|
---|
96 | public String values;
|
---|
97 | public String display_values;
|
---|
98 | public String default_;
|
---|
99 | public boolean delete_if_empty = false;
|
---|
100 | public boolean editable = true;
|
---|
101 |
|
---|
102 | private JComboBox combo;
|
---|
103 |
|
---|
104 | public void addToPanel(JPanel p) {
|
---|
105 | combo = new JComboBox((display_values != null ? display_values : values).split(","));
|
---|
106 | combo.setEditable(editable);
|
---|
107 | combo.setSelectedItem(default_);
|
---|
108 | p.add(new JLabel(text), GBC.std().insets(0,0,10,0));
|
---|
109 | p.add(combo, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
110 | }
|
---|
111 | public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
112 | String v = combo.getSelectedIndex() == -1 ? null : values.split(",")[combo.getSelectedIndex()];
|
---|
113 | String str = combo.isEditable()?combo.getEditor().getItem().toString() : v;
|
---|
114 | if (delete_if_empty && str != null && str.length() == 0)
|
---|
115 | str = null;
|
---|
116 | cmds.add(new ChangePropertyCommand(sel, key, str));
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static class Label implements Item {
|
---|
121 | public String text;
|
---|
122 |
|
---|
123 | public void addToPanel(JPanel p) {
|
---|
124 | p.add(new JLabel(text), GBC.eol());
|
---|
125 | }
|
---|
126 | public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {}
|
---|
127 | }
|
---|
128 |
|
---|
129 | public static class Key implements Item {
|
---|
130 | public String key;
|
---|
131 | public String value;
|
---|
132 |
|
---|
133 | public void addToPanel(JPanel p) {}
|
---|
134 | public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
135 | cmds.add(new ChangePropertyCommand(sel, key, value != null && !value.equals("") ? value : null));
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * The types as preparsed collection.
|
---|
141 | */
|
---|
142 | public Collection<Class<?>> types;
|
---|
143 | private List<Item> data = new LinkedList<Item>();
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Create an empty annotation preset. This will not have any items and
|
---|
147 | * will be an empty string as text. createPanel will return null.
|
---|
148 | * Use this as default item for "do not select anything".
|
---|
149 | */
|
---|
150 | public AnnotationPreset() {}
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Called from the XML parser to set the name of the annotation preset
|
---|
154 | */
|
---|
155 | public void setName(String name) {
|
---|
156 | putValue(Action.NAME, name);
|
---|
157 | putValue("toolbar", "annotation_"+name);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Called from the XML parser to set the icon
|
---|
162 | */
|
---|
163 | public void setIcon(String icon) {
|
---|
164 | putValue(Action.SMALL_ICON, new ImageIcon(new ImageIcon(icon).getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH)));
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Called from the XML parser to set the types, this preset affects
|
---|
169 | */
|
---|
170 | public void setType(String types) throws SAXException {
|
---|
171 | try {
|
---|
172 | for (String type : types.split(",")) {
|
---|
173 | type = Character.toUpperCase(type.charAt(0))+type.substring(1);
|
---|
174 | if (this.types == null)
|
---|
175 | this.types = new LinkedList<Class<?>>();
|
---|
176 | this.types.add(Class.forName("org.openstreetmap.josm.data.osm."+type));
|
---|
177 | }
|
---|
178 | } catch (ClassNotFoundException e) {
|
---|
179 | e.printStackTrace();
|
---|
180 | throw new SAXException(tr("Unknown type"));
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | public static List<AnnotationPreset> readAll(InputStream inStream) throws SAXException {
|
---|
185 | BufferedReader in = null;
|
---|
186 | try {
|
---|
187 | in = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
|
---|
188 | } catch (UnsupportedEncodingException e) {
|
---|
189 | e.printStackTrace();
|
---|
190 | in = new BufferedReader(new InputStreamReader(inStream));
|
---|
191 | }
|
---|
192 | XmlObjectParser parser = new XmlObjectParser();
|
---|
193 | parser.mapOnStart("item", AnnotationPreset.class);
|
---|
194 | parser.map("text", Text.class);
|
---|
195 | parser.map("check", Check.class);
|
---|
196 | parser.map("combo", Combo.class);
|
---|
197 | parser.map("label", Label.class);
|
---|
198 | parser.map("key", Key.class);
|
---|
199 | LinkedList<AnnotationPreset> all = new LinkedList<AnnotationPreset>();
|
---|
200 | parser.start(in);
|
---|
201 | while(parser.hasNext()) {
|
---|
202 | Object o = parser.next();
|
---|
203 | if (o instanceof AnnotationPreset) {
|
---|
204 | all.add((AnnotationPreset)o);
|
---|
205 | Main.toolbar.register((AnnotationPreset)o);
|
---|
206 | } else
|
---|
207 | all.getLast().data.add((Item)o);
|
---|
208 | }
|
---|
209 | return all;
|
---|
210 | }
|
---|
211 |
|
---|
212 | public static Collection<AnnotationPreset> readFromPreferences() {
|
---|
213 | LinkedList<AnnotationPreset> allPresets = new LinkedList<AnnotationPreset>();
|
---|
214 | String allAnnotations = Main.pref.get("annotation.sources");
|
---|
215 | StringTokenizer st = new StringTokenizer(allAnnotations, ";");
|
---|
216 | while (st.hasMoreTokens()) {
|
---|
217 | InputStream in = null;
|
---|
218 | String source = st.nextToken();
|
---|
219 | try {
|
---|
220 | if (source.startsWith("http") || source.startsWith("ftp") || source.startsWith("file"))
|
---|
221 | in = new URL(source).openStream();
|
---|
222 | else if (source.startsWith("resource://"))
|
---|
223 | in = Main.class.getResourceAsStream(source.substring("resource:/".length()));
|
---|
224 | else
|
---|
225 | in = new FileInputStream(source);
|
---|
226 | allPresets.addAll(AnnotationPreset.readAll(in));
|
---|
227 | in.close();
|
---|
228 | } catch (IOException e) {
|
---|
229 | e.printStackTrace();
|
---|
230 | JOptionPane.showMessageDialog(Main.parent, tr("Could not read annotation preset source: {0}",source));
|
---|
231 | } catch (SAXException e) {
|
---|
232 | e.printStackTrace();
|
---|
233 | JOptionPane.showMessageDialog(Main.parent, tr("Error parsing {0}: ", source)+e.getMessage());
|
---|
234 | }
|
---|
235 | }
|
---|
236 | return allPresets;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | public JPanel createPanel() {
|
---|
241 | if (data == null)
|
---|
242 | return null;
|
---|
243 | JPanel p = new JPanel(new GridBagLayout());
|
---|
244 | for (Item i : data)
|
---|
245 | i.addToPanel(p);
|
---|
246 | return p;
|
---|
247 | }
|
---|
248 |
|
---|
249 | public void actionPerformed(ActionEvent e) {
|
---|
250 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
251 | JPanel p = createPanel();
|
---|
252 | if (p == null)
|
---|
253 | return;
|
---|
254 | int answer;
|
---|
255 | if (p.getComponentCount() == 0)
|
---|
256 | answer = JOptionPane.OK_OPTION;
|
---|
257 | else
|
---|
258 | answer = JOptionPane.showConfirmDialog(Main.parent, p, trn("Change {0} object", "Change {0} objects", sel.size(), sel.size()), JOptionPane.OK_CANCEL_OPTION);
|
---|
259 | if (answer == JOptionPane.OK_OPTION) {
|
---|
260 | Command cmd = createCommand(Main.ds.getSelected());
|
---|
261 | if (cmd != null)
|
---|
262 | Main.main.editLayer().add(cmd);
|
---|
263 | }
|
---|
264 | Main.ds.setSelected(Main.ds.getSelected()); // force update
|
---|
265 | }
|
---|
266 |
|
---|
267 | private Command createCommand(Collection<OsmPrimitive> participants) {
|
---|
268 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
269 | for (OsmPrimitive osm : participants)
|
---|
270 | if (types == null || types.contains(osm.getClass()))
|
---|
271 | sel.add(osm);
|
---|
272 | if (sel.isEmpty())
|
---|
273 | return null;
|
---|
274 |
|
---|
275 | List<Command> cmds = new LinkedList<Command>();
|
---|
276 | for (Item i : data)
|
---|
277 | i.addCommands(sel, cmds);
|
---|
278 | if (cmds.size() == 0)
|
---|
279 | return null;
|
---|
280 | else if (cmds.size() == 1)
|
---|
281 | return cmds.get(0);
|
---|
282 | else
|
---|
283 | return new SequenceCommand(tr("Change Properties"), cmds);
|
---|
284 | }
|
---|
285 | }
|
---|