-
Notifications
You must be signed in to change notification settings - Fork 65
/
RoadSignsPlugin.java
273 lines (244 loc) · 10.9 KB
/
RoadSignsPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.roadsigns;
import static org.openstreetmap.josm.tools.I18n.tr;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import org.openstreetmap.josm.actions.JosmAction;
import org.openstreetmap.josm.data.StructUtils;
import org.openstreetmap.josm.data.StructUtils.StructEntry;
import org.openstreetmap.josm.gui.ExtendedDialog;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
import org.openstreetmap.josm.io.CachedFile;
import org.openstreetmap.josm.plugins.Plugin;
import org.openstreetmap.josm.plugins.PluginInformation;
import org.openstreetmap.josm.plugins.roadsigns.RoadSignInputDialog.SettingsPanel;
import org.openstreetmap.josm.spi.preferences.Config;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Shortcut;
import org.openstreetmap.josm.tools.Utils;
import org.xml.sax.SAXException;
public class RoadSignsPlugin extends Plugin {
static PresetMetaData selectedPreset;
public static List<Sign> signs;
public static List<String> iconDirs;
public static RoadSignsPlugin plugin;
public static final PresetMetaData PRESET_BE = new PresetMetaData(
"BE", tr("Belgium"), "resource://data/roadsignpresetBE.xml", "resource://images/BE/");
public static final PresetMetaData PRESET_CZ = new PresetMetaData(
"CZ", tr("Czech Republic"), "resource://data/roadsignpresetCZ.xml", "resource://images/CZ/");
public static final PresetMetaData PRESET_ES = new PresetMetaData(
"ES", tr("Spain"), "resource://data/roadsignpresetES.xml", "resource://images/ES/");
public static final PresetMetaData PRESET_DE = new PresetMetaData(
"DE", tr("Germany"), "resource://data/roadsignpresetDE.xml", "resource://images/DE/");
public static final PresetMetaData PRESET_PL = new PresetMetaData(
"PL", tr("Poland"), "resource://data/roadsignpresetPL.xml", "resource://images/PL/");
public static final PresetMetaData PRESET_SK = new PresetMetaData(
"SK", tr("Slovakia"), "resource://data/roadsignpresetSK.xml", "resource://images/SK/");
public static final Collection<PresetMetaData> DEFAULT_PRESETS = Arrays.asList(
PRESET_BE, PRESET_CZ, PRESET_ES, PRESET_DE, PRESET_PL, PRESET_SK);
public RoadSignsPlugin(PluginInformation info) {
super(info);
plugin = this;
registerAction();
}
public static File pluginDir() {
File dir = plugin.getPluginDirs().getUserDataDirectory(false);
if (!dir.exists()) {
dir.mkdirs();
}
return dir;
}
private void registerAction() {
JButton btn = new JButton(new RoadSignAction());
btn.setText(null);
btn.setBorder(BorderFactory.createEmptyBorder());
btn.setPreferredSize(new Dimension(18, 18));
PropertiesDialog.pluginHook.add(btn);
PropertiesDialog.pluginHook.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
PropertiesDialog.pluginHook.revalidate();
}
private static class RoadSignAction extends JosmAction {
RoadSignAction() {
super(tr("Roadsign tagging"), "pref/roadsigns-small", tr("Add tags by clicking on road signs"),
Shortcut.registerShortcut("plugin:roadsigns:dialog", tr("Roadsigns plugin: open dialog"),
KeyEvent.VK_Q, Shortcut.ALT_SHIFT), false);
}
@Override
public void actionPerformed(ActionEvent e) {
String code = Config.getPref().get("plugin.roadsigns.preset.selection", null);
if (code == null) {
ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Settings"), tr("Ok"), tr("Cancel"));
ed.setButtonIcons("ok", "cancel");
SettingsPanel settings = new SettingsPanel(true, null);
ed.setContent(settings);
ed.showDialog();
if (ed.getValue() != 1) return;
try {
settings.apply();
} catch (IOException ex) {
return;
}
}
try {
loadSignPreset();
} catch (IOException ex) {
return;
}
RoadSignInputDialog input = new RoadSignInputDialog();
input.showDialog();
}
}
public static class PresetMetaData {
@StructEntry public String code;
@StructEntry public String display_name;
@StructEntry public String preset_path;
@StructEntry public String icon_path;
public PresetMetaData() {
}
public PresetMetaData(String country_code, String display_name, String preset_path, String icons_path) {
this.code = country_code;
this.display_name = display_name;
this.preset_path = preset_path;
this.icon_path = icons_path;
}
@Override
public String toString() {
return display_name;
}
}
public static void setSelectedPreset(PresetMetaData preset) throws IOException {
Config.getPref().put("plugin.roadsigns.preset.selection", preset.code);
loadSignPreset();
}
public static List<PresetMetaData> getAvailablePresetsMetaData() {
List<PresetMetaData> presetsData = StructUtils.getListOfStructs(
Config.getPref(), "plugin.roadsigns.presets", DEFAULT_PRESETS, PresetMetaData.class);
String customFile = Config.getPref().get("plugin.roadsigns.sources", null);
if (customFile == null) {
// for legacy reasons, try both string and collection preference type
List<String> customFiles = Config.getPref().getList("plugin.roadsigns.sources", null);
if (customFiles != null && !customFiles.isEmpty()) {
customFile = customFiles.iterator().next();
}
}
if (customFile != null) {
// first check, if custom file preference has changed. If yes,
// change the current preset selection to custom directly
String lastCustomFile = Config.getPref().get("plugin.roadsigns.sources.last", null);
if (!Objects.equals(customFile, lastCustomFile)) {
Config.getPref().put("plugin.roadsigns.sources.last", customFile);
Config.getPref().put("plugin.roadsigns.preset.selection", "custom");
}
String customIconDirsStr = Config.getPref().get("plugin.roadsigns.icon.sources", null);
List<String> customIconDirs = null;
if (customIconDirsStr != null) {
customIconDirs = new ArrayList<>(Arrays.asList(customIconDirsStr.split(",")));
} else {
customIconDirs = Config.getPref().getList("plugin.roadsigns.icon.sources", null);
}
if (customIconDirs != null) {
customIconDirs = new ArrayList<>(customIconDirs);
} else {
customIconDirs = new ArrayList<>();
}
// add icon directory relative to preset file
if (!customFile.startsWith("resource:")) {
String parentDir = null;
try {
URL url = new URL(customFile);
parentDir = url.getPath();
} catch (MalformedURLException ex) {
File f = new File(customFile);
parentDir = f.getParent();
}
if (parentDir != null && !parentDir.isEmpty()) {
customIconDirs.add(parentDir);
}
}
if (Config.getPref().getBoolean("plugin.roadsigns.use_default_icon_source", true)) {
customIconDirs.add("resource://images/");
}
presetsData.add(new PresetMetaData("custom", tr("custom"), customFile,
String.join(",", customIconDirs)));
} else {
Config.getPref().put("plugin.roadsigns.sources.last", null);
}
return presetsData;
}
protected static void loadSignPreset() throws IOException {
List<PresetMetaData> presetsData = getAvailablePresetsMetaData();
String code = Config.getPref().get("plugin.roadsigns.preset.selection", null);
for (PresetMetaData data : presetsData) {
if (data.code.equals(code)) {
selectedPreset = data;
break;
}
}
if (selectedPreset == null) {
if (!presetsData.isEmpty()) {
selectedPreset = presetsData.get(0);
} else {
selectedPreset = PRESET_DE;
}
}
iconDirs = Arrays.asList(selectedPreset.icon_path.split(","));
String source = selectedPreset.preset_path;
try {
InputStream in = getInputStream(source);
RoadSignsReader reader = new RoadSignsReader(in);
signs = reader.parse();
} catch (IOException ex) {
Logging.error(ex);
JOptionPane.showMessageDialog(
MainApplication.getMainFrame(),
tr("Could not read tagging preset source: ''{0}''", source),
tr("Error"),
JOptionPane.ERROR_MESSAGE
);
throw ex;
} catch (SAXException ex) {
Logging.error(ex);
JOptionPane.showMessageDialog(
MainApplication.getMainFrame(),
tr("Error parsing tagging preset from ''{0}'':\n", source)+ex.getMessage(),
tr("Error"),
JOptionPane.ERROR_MESSAGE
);
throw new IOException(ex);
}
}
/**
* Returns an inputstream from urls, files and classloaders, depending on the name.
*/
@SuppressWarnings("resource")
public static InputStream getInputStream(String source) throws IOException {
InputStream in = null;
if (source.startsWith("http://") || source.startsWith("https://") || source.startsWith("ftp://")) {
in = new CachedFile(source).getInputStream();
} else if (source.startsWith("file:")) {
in = new URL(source).openStream();
} else if (source.startsWith("resource://")) {
in = RoadSignsPlugin.class.getResourceAsStream(source.substring("resource:/".length()));
} else {
in = new FileInputStream(source);
}
return in;
}
}