1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Font;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.net.URLClassLoader;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.SortedMap;
|
---|
18 | import java.util.TreeMap;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 |
|
---|
21 | import javax.swing.AbstractAction;
|
---|
22 | import javax.swing.BorderFactory;
|
---|
23 | import javax.swing.Box;
|
---|
24 | import javax.swing.JButton;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.JScrollPane;
|
---|
29 | import javax.swing.JTextArea;
|
---|
30 | import javax.swing.UIManager;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
34 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
35 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
36 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
37 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
---|
38 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
39 | import org.openstreetmap.josm.tools.GBC;
|
---|
40 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
41 |
|
---|
42 | public class PluginHandler {
|
---|
43 | /**
|
---|
44 | * All installed and loaded plugins (resp. their main classes)
|
---|
45 | */
|
---|
46 | public final static Collection<PluginProxy> pluginList = new LinkedList<PluginProxy>();
|
---|
47 | /**
|
---|
48 | * Load all plugins specified in preferences. If the parameter is
|
---|
49 | * <code>true</code>, all early plugins are loaded (before constructor).
|
---|
50 | */
|
---|
51 | public static void loadPlugins(boolean early) {
|
---|
52 | List<String> plugins = new LinkedList<String>();
|
---|
53 | Collection<String> cp = Main.pref.getCollection("plugins", null);
|
---|
54 | if (cp != null) {
|
---|
55 | plugins.addAll(cp);
|
---|
56 | }
|
---|
57 | if (System.getProperty("josm.plugins") != null) {
|
---|
58 | plugins.addAll(Arrays.asList(System.getProperty("josm.plugins").split(",")));
|
---|
59 | }
|
---|
60 |
|
---|
61 | String [] oldplugins = new String[] {"mappaint", "unglueplugin",
|
---|
62 | "lang-de", "lang-en_GB", "lang-fr", "lang-it", "lang-pl", "lang-ro",
|
---|
63 | "lang-ru", "ewmsplugin", "ywms", "tways-0.2", "geotagged", "landsat",
|
---|
64 | "namefinder", "waypoints", "slippy_map_chooser", "tcx-support"};
|
---|
65 | for (String p : oldplugins) {
|
---|
66 | if (plugins.contains(p)) {
|
---|
67 | plugins.remove(p);
|
---|
68 | Main.pref.removeFromCollection("plugins", p);
|
---|
69 | OptionPaneUtil.showMessageDialog(
|
---|
70 | Main.parent,
|
---|
71 | tr("Loading of {0} plugin was requested. This plugin is no longer required.", p),
|
---|
72 | tr("Warning"),
|
---|
73 | JOptionPane.WARNING_MESSAGE
|
---|
74 | );
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (plugins.isEmpty())
|
---|
79 | return;
|
---|
80 |
|
---|
81 | SortedMap<Integer, Collection<PluginInformation>> p = new TreeMap<Integer, Collection<PluginInformation>>();
|
---|
82 | for (String pluginName : plugins) {
|
---|
83 | PluginInformation info = PluginInformation.findPlugin(pluginName);
|
---|
84 | if (info != null) {
|
---|
85 | if (info.early != early) {
|
---|
86 | continue;
|
---|
87 | }
|
---|
88 | if (info.mainversion > AboutAction.getVersionNumber()) {
|
---|
89 | OptionPaneUtil.showMessageDialog(
|
---|
90 | Main.parent,
|
---|
91 | tr("Plugin {0} requires JOSM update to version {1}.", pluginName,
|
---|
92 | info.mainversion),
|
---|
93 | tr("Warning"),
|
---|
94 | JOptionPane.WARNING_MESSAGE
|
---|
95 | );
|
---|
96 | continue;
|
---|
97 | }
|
---|
98 | if(info.requires != null)
|
---|
99 | {
|
---|
100 | String warn = null;
|
---|
101 | for(String n : info.requires.split(";"))
|
---|
102 | {
|
---|
103 | if(!plugins.contains(n))
|
---|
104 | { warn = n; break; }
|
---|
105 | }
|
---|
106 | if(warn != null)
|
---|
107 | {
|
---|
108 | OptionPaneUtil.showMessageDialog(Main.parent,
|
---|
109 | tr("Plugin {0} is required by plugin {1} but was not found.",
|
---|
110 | warn, pluginName),
|
---|
111 | tr("Error"),
|
---|
112 | JOptionPane.ERROR_MESSAGE
|
---|
113 | );
|
---|
114 | continue;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | if (!p.containsKey(info.stage)) {
|
---|
118 | p.put(info.stage, new LinkedList<PluginInformation>());
|
---|
119 | }
|
---|
120 | p.get(info.stage).add(info);
|
---|
121 | } else if(early) {
|
---|
122 | OptionPaneUtil.showMessageDialog(
|
---|
123 | Main.parent,
|
---|
124 | tr("Plugin not found: {0}.", pluginName),
|
---|
125 | tr("Error"),
|
---|
126 | JOptionPane.ERROR_MESSAGE
|
---|
127 | );
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (!early) {
|
---|
132 | long tim = System.currentTimeMillis();
|
---|
133 | long last = Main.pref.getLong("pluginmanager.lastupdate", 0);
|
---|
134 | Integer maxTime = Main.pref.getInteger("pluginmanager.warntime", 30);
|
---|
135 | long d = (tim - last)/(24*60*60*1000l);
|
---|
136 | if ((last <= 0) || (maxTime <= 0)) {
|
---|
137 | Main.pref.put("pluginmanager.lastupdate",Long.toString(tim));
|
---|
138 | } else if (d > maxTime) {
|
---|
139 | OptionPaneUtil.showMessageDialog(Main.parent,
|
---|
140 | "<html>" +
|
---|
141 | tr("Last plugin update more than {0} days ago.", d) +
|
---|
142 | "<br><em>" +
|
---|
143 | tr("(You can change the number of days after which this warning appears<br>by setting the config option 'pluginmanager.warntime'.)") +
|
---|
144 | "</html>",
|
---|
145 | tr("Warning"),
|
---|
146 | JOptionPane.WARNING_MESSAGE
|
---|
147 | );
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | // iterate all plugins and collect all libraries of all plugins:
|
---|
152 | List<URL> allPluginLibraries = new ArrayList<URL>();
|
---|
153 | for (Collection<PluginInformation> c : p.values()) {
|
---|
154 | for (PluginInformation info : c) {
|
---|
155 | allPluginLibraries.addAll(info.libraries);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | // create a classloader for all plugins:
|
---|
159 | URL[] jarUrls = new URL[allPluginLibraries.size()];
|
---|
160 | jarUrls = allPluginLibraries.toArray(jarUrls);
|
---|
161 | URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader());
|
---|
162 | ImageProvider.sources.add(0, pluginClassLoader);
|
---|
163 |
|
---|
164 | for (Collection<PluginInformation> c : p.values()) {
|
---|
165 | for (PluginInformation info : c) {
|
---|
166 | try {
|
---|
167 | Class<?> klass = info.loadClass(pluginClassLoader);
|
---|
168 | if (klass != null) {
|
---|
169 | System.out.println("loading "+info.name);
|
---|
170 | pluginList.add(info.load(klass));
|
---|
171 | }
|
---|
172 | } catch (Throwable e) {
|
---|
173 | e.printStackTrace();
|
---|
174 |
|
---|
175 | int result = new ExtendedDialog(Main.parent,
|
---|
176 | tr("Disable plugin"),
|
---|
177 | tr("Could not load plugin {0}. Delete from preferences?", info.name),
|
---|
178 | new String[] {tr("Disable plugin"), tr("Keep plugin")},
|
---|
179 | new String[] {"dialogs/delete.png", "cancel.png"}).getValue();
|
---|
180 |
|
---|
181 | if(result == 1)
|
---|
182 | {
|
---|
183 | plugins.remove(info.name);
|
---|
184 | Main.pref.removeFromCollection("plugins", info.name);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | public static void setMapFrame(MapFrame old, MapFrame map) {
|
---|
191 | for (PluginProxy plugin : pluginList) {
|
---|
192 | plugin.mapFrameInitialized(old, map);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | public static Object getPlugin(String name) {
|
---|
197 | for (PluginProxy plugin : pluginList)
|
---|
198 | if(plugin.info.name.equals(name))
|
---|
199 | return plugin.plugin;
|
---|
200 | return null;
|
---|
201 | }
|
---|
202 |
|
---|
203 | public static void addDownloadSelection(List<DownloadSelection> downloadSelections)
|
---|
204 | {
|
---|
205 | for (PluginProxy p : pluginList) {
|
---|
206 | p.addDownloadSelection(downloadSelections);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | public static void getPreferenceSetting(Collection<PreferenceSettingFactory> settings)
|
---|
210 | {
|
---|
211 | for (PluginProxy plugin : pluginList) {
|
---|
212 | settings.add(new PluginPreferenceFactory(plugin));
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | public static void earlyCleanup()
|
---|
217 | {
|
---|
218 | if (!PluginDownloader.moveUpdatedPlugins()) {
|
---|
219 | OptionPaneUtil.showMessageDialog(
|
---|
220 | Main.parent,
|
---|
221 | tr("Activating the updated plugins failed. Check if JOSM has the permission to overwrite the existing ones."),
|
---|
222 | tr("Plugins"), JOptionPane.ERROR_MESSAGE);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | public static Boolean checkException(Throwable e)
|
---|
226 | {
|
---|
227 | PluginProxy plugin = null;
|
---|
228 |
|
---|
229 | // Check for an explicit problem when calling a plugin function
|
---|
230 | if (e instanceof PluginException) {
|
---|
231 | plugin = ((PluginException)e).plugin;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (plugin == null)
|
---|
235 | {
|
---|
236 | String name = null;
|
---|
237 | /**
|
---|
238 | * Analyze the stack of the argument and find a name of a plugin, if
|
---|
239 | * some known problem pattern has been found.
|
---|
240 | *
|
---|
241 | * Note: This heuristic is not meant as discrimination against specific
|
---|
242 | * plugins, but only to stop the flood of similar bug reports about plugins.
|
---|
243 | * Of course, plugin writers are free to install their own version of
|
---|
244 | * an exception handler with their email address listed to receive
|
---|
245 | * bug reports ;-).
|
---|
246 | */
|
---|
247 | for (StackTraceElement element : e.getStackTrace()) {
|
---|
248 | String c = element.getClassName();
|
---|
249 |
|
---|
250 | if (c.contains("wmsplugin.") || c.contains(".WMSLayer")) {
|
---|
251 | name = "wmsplugin";
|
---|
252 | }
|
---|
253 | if (c.contains("livegps.")) {
|
---|
254 | name = "livegps";
|
---|
255 | }
|
---|
256 | if (c.startsWith("UtilsPlugin.")) {
|
---|
257 | name = "UtilsPlugin";
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (c.startsWith("org.openstreetmap.josm.plugins.")) {
|
---|
261 | String p = c.substring("org.openstreetmap.josm.plugins.".length());
|
---|
262 | if (p.indexOf('.') != -1 && p.matches("[a-z].*")) {
|
---|
263 | name = p.substring(0,p.indexOf('.'));
|
---|
264 | }
|
---|
265 | }
|
---|
266 | if(name != null) {
|
---|
267 | break;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | for (PluginProxy p : pluginList)
|
---|
271 | {
|
---|
272 | if (p.info.name.equals(name))
|
---|
273 | {
|
---|
274 | plugin = p;
|
---|
275 | break;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (plugin != null) {
|
---|
281 | int answer = new ExtendedDialog(Main.parent,
|
---|
282 | tr("Disable plugin"),
|
---|
283 | tr("An unexpected exception occurred that may have come from the ''{0}'' plugin.", plugin.info.name)
|
---|
284 | + "\n"
|
---|
285 | + (plugin.info.author != null
|
---|
286 | ? tr("According to the information within the plugin, the author is {0}.", plugin.info.author)
|
---|
287 | : "")
|
---|
288 | + "\n"
|
---|
289 | + tr("Try updating to the newest version of this plugin before reporting a bug.")
|
---|
290 | + "\n"
|
---|
291 | + tr("Should the plugin be disabled?"),
|
---|
292 | new String[] {tr("Disable plugin"), tr("Cancel")},
|
---|
293 | new String[] {"dialogs/delete.png", "cancel.png"}).getValue();
|
---|
294 | if (answer == 1) {
|
---|
295 | List<String> plugins = new ArrayList<String>(Main.pref.getCollection("plugins", Collections.<String>emptyList()));
|
---|
296 | if (plugins.contains(plugin.info.name)) {
|
---|
297 | while (plugins.remove(plugin.info.name)) {}
|
---|
298 | Main.pref.putCollection("plugins", plugins);
|
---|
299 | OptionPaneUtil.showMessageDialog(Main.parent,
|
---|
300 | tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."),
|
---|
301 | tr("Information"),
|
---|
302 | JOptionPane.INFORMATION_MESSAGE);
|
---|
303 | } else {
|
---|
304 | OptionPaneUtil.showMessageDialog(
|
---|
305 | Main.parent,
|
---|
306 | tr("The plugin could not be removed. Probably it was already disabled"),
|
---|
307 | tr("Error"),
|
---|
308 | JOptionPane.ERROR_MESSAGE
|
---|
309 | );
|
---|
310 | }
|
---|
311 | return true;
|
---|
312 | }
|
---|
313 | }
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 | public static String getBugReportText()
|
---|
317 | {
|
---|
318 | String text = "";
|
---|
319 | String pl = Main.pref.get("plugins");
|
---|
320 | if(pl != null && pl.length() != 0) {
|
---|
321 | text += "Plugins: "+pl+"\n";
|
---|
322 | }
|
---|
323 | for (final PluginProxy pp : pluginList) {
|
---|
324 | text += "Plugin " + pp.info.name + (pp.info.version != null && !pp.info.version.equals("") ? " Version: "+pp.info.version+"\n" : "\n");
|
---|
325 | }
|
---|
326 | return text;
|
---|
327 | }
|
---|
328 | public static JPanel getInfoPanel()
|
---|
329 | {
|
---|
330 | JPanel pluginTab = new JPanel(new GridBagLayout());
|
---|
331 | for (final PluginProxy p : pluginList) {
|
---|
332 | String name = p.info.name + (p.info.version != null && !p.info.version.equals("") ? " Version: "+p.info.version : "");
|
---|
333 | pluginTab.add(new JLabel(name), GBC.std());
|
---|
334 | pluginTab.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
335 | pluginTab.add(new JButton(new AbstractAction(tr("Information")){
|
---|
336 | public void actionPerformed(ActionEvent event) {
|
---|
337 | StringBuilder b = new StringBuilder();
|
---|
338 | for (Entry<String,String> e : p.info.attr.entrySet()) {
|
---|
339 | b.append(e.getKey());
|
---|
340 | b.append(": ");
|
---|
341 | b.append(e.getValue());
|
---|
342 | b.append("\n");
|
---|
343 | }
|
---|
344 | JTextArea a = new JTextArea(10,40);
|
---|
345 | a.setEditable(false);
|
---|
346 | a.setText(b.toString());
|
---|
347 | OptionPaneUtil.showMessageDialog(
|
---|
348 | Main.parent,
|
---|
349 | new JScrollPane(a),
|
---|
350 | tr("Plugin information"),
|
---|
351 | JOptionPane.INFORMATION_MESSAGE
|
---|
352 | );
|
---|
353 | }
|
---|
354 | }), GBC.eol());
|
---|
355 |
|
---|
356 | JTextArea description = new JTextArea((p.info.description==null? tr("no description available"):p.info.description));
|
---|
357 | description.setEditable(false);
|
---|
358 | description.setFont(new JLabel().getFont().deriveFont(Font.ITALIC));
|
---|
359 | description.setLineWrap(true);
|
---|
360 | description.setWrapStyleWord(true);
|
---|
361 | description.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
|
---|
362 | description.setBackground(UIManager.getColor("Panel.background"));
|
---|
363 |
|
---|
364 | pluginTab.add(description, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
365 | }
|
---|
366 | return pluginTab;
|
---|
367 | }
|
---|
368 | }
|
---|