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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.io.BufferedReader;
|
---|
12 | import java.io.ByteArrayInputStream;
|
---|
13 | import java.io.File;
|
---|
14 | import java.io.FileInputStream;
|
---|
15 | import java.io.InputStreamReader;
|
---|
16 | import java.util.Arrays;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.Comparator;
|
---|
20 | import java.util.HashMap;
|
---|
21 | import java.util.HashSet;
|
---|
22 | import java.util.LinkedList;
|
---|
23 | import java.util.Map;
|
---|
24 | import java.util.Set;
|
---|
25 | import java.util.TreeMap;
|
---|
26 | import java.util.Map.Entry;
|
---|
27 |
|
---|
28 | import javax.swing.BorderFactory;
|
---|
29 | import javax.swing.JCheckBox;
|
---|
30 | import javax.swing.JEditorPane;
|
---|
31 | import javax.swing.JOptionPane;
|
---|
32 | import javax.swing.JPanel;
|
---|
33 | import javax.swing.UIManager;
|
---|
34 | import javax.swing.event.HyperlinkEvent;
|
---|
35 | import javax.swing.event.HyperlinkListener;
|
---|
36 | import javax.swing.event.HyperlinkEvent.EventType;
|
---|
37 |
|
---|
38 | import org.openstreetmap.josm.Main;
|
---|
39 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
40 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
41 |
|
---|
42 | public class PluginSelection {
|
---|
43 |
|
---|
44 | private Map<String, Boolean> pluginMap;
|
---|
45 | private Map<String, PluginInformation> availablePlugins;
|
---|
46 | private Map<String, PluginInformation> localPlugins;
|
---|
47 |
|
---|
48 | public void updateDescription(JPanel pluginPanel) {
|
---|
49 | int count = PluginDownloader.downloadDescription();
|
---|
50 | if (count > 0)
|
---|
51 | JOptionPane.showMessageDialog(Main.parent,
|
---|
52 | trn("Downloaded plugin information from {0} site",
|
---|
53 | "Downloaded plugin information from {0} sites", count, count));
|
---|
54 | else
|
---|
55 | JOptionPane.showMessageDialog(Main.parent, tr("No plugin information found."));
|
---|
56 | drawPanel(pluginPanel);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void update(JPanel pluginPanel) {
|
---|
60 | // refresh description
|
---|
61 | int num = PluginDownloader.downloadDescription();
|
---|
62 | Boolean done = false;
|
---|
63 | drawPanel(pluginPanel);
|
---|
64 |
|
---|
65 | Set<PluginInformation> toUpdate = new HashSet<PluginInformation>();
|
---|
66 | StringBuilder toUpdateStr = new StringBuilder();
|
---|
67 | for (PluginProxy proxy : PluginHandler.pluginList) {
|
---|
68 | PluginInformation local = localPlugins.get(proxy.info.name);
|
---|
69 | PluginInformation description = availablePlugins.get(local.name);
|
---|
70 |
|
---|
71 | if (description.version != null && !description.version.equals(local.version)) {
|
---|
72 | toUpdate.add(description);
|
---|
73 | toUpdateStr.append(description.name+"\n");
|
---|
74 | }
|
---|
75 | }
|
---|
76 | if (toUpdate.isEmpty()) {
|
---|
77 | JOptionPane.showMessageDialog(Main.parent, tr("All installed plugins are up to date."));
|
---|
78 | done = true;
|
---|
79 | } else {
|
---|
80 | int answer = new ExtendedDialog(Main.parent,
|
---|
81 | tr("Update"),
|
---|
82 | tr("Update the following plugins:\n\n{0}", toUpdateStr.toString()),
|
---|
83 | new String[] {tr("Update Plugins"), tr("Cancel")},
|
---|
84 | new String[] {"dialogs/refresh.png", "cancel.png"}).getValue();
|
---|
85 | if (answer == 1) {
|
---|
86 | PluginDownloader.update(toUpdate);
|
---|
87 | done = true;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | if (done && num >= 1)
|
---|
91 | Main.pref.put("pluginmanager.lastupdate", Long.toString(System.currentTimeMillis()));
|
---|
92 | drawPanel(pluginPanel);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public boolean finish() {
|
---|
96 | Collection<PluginInformation> toDownload = new LinkedList<PluginInformation>();
|
---|
97 | Collection<String> installedPlugins = Main.pref.getCollection("plugins", Collections.<String>emptySet());
|
---|
98 |
|
---|
99 | String msg = "";
|
---|
100 | for (Entry<String, Boolean> entry : pluginMap.entrySet()) {
|
---|
101 | if(entry.getValue() && !installedPlugins.contains(entry.getKey()))
|
---|
102 | {
|
---|
103 | String name = entry.getKey();
|
---|
104 | PluginInformation ap = availablePlugins.get(name);
|
---|
105 | PluginInformation pi = localPlugins.get(name);
|
---|
106 | if(pi == null || (pi.version == null && ap.version != null)
|
---|
107 | || (pi.version != null && !pi.version.equals(ap.version)))
|
---|
108 | {
|
---|
109 | toDownload.add(ap);
|
---|
110 | msg += name + "\n";
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | if (!toDownload.isEmpty()) {
|
---|
115 | int answer = new ExtendedDialog(Main.parent,
|
---|
116 | tr("Download missing plugins"),
|
---|
117 | tr("Download the following plugins?\n\n{0}", msg),
|
---|
118 | new String[] {tr("Download Plugins"), tr("Cancel")},
|
---|
119 | new String[] {"download.png", "cancel.png"}).getValue();
|
---|
120 | Collection<PluginInformation> error =
|
---|
121 | (answer != 1 ? toDownload : new PluginDownloader().download(toDownload));
|
---|
122 | for (PluginInformation pd : error)
|
---|
123 | pluginMap.put(pd.name, false);
|
---|
124 |
|
---|
125 | }
|
---|
126 | LinkedList<String> plugins = new LinkedList<String>();
|
---|
127 | for (Map.Entry<String, Boolean> d : pluginMap.entrySet()) {
|
---|
128 | if (d.getValue())
|
---|
129 | plugins.add(d.getKey());
|
---|
130 | }
|
---|
131 |
|
---|
132 | Collections.sort(plugins);
|
---|
133 | return Main.pref.putCollection("plugins", plugins);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* return true when plugin list changed */
|
---|
137 | public void drawPanel(JPanel pluginPanel) {
|
---|
138 | loadPlugins();
|
---|
139 | Collection<String> enabledPlugins = Main.pref.getCollection("plugins", null);
|
---|
140 |
|
---|
141 | if (pluginMap == null)
|
---|
142 | pluginMap = new HashMap<String, Boolean>();
|
---|
143 | else {
|
---|
144 | // Keep the map in bounds; possibly slightly pointless.
|
---|
145 | Set<String> pluginsToRemove = new HashSet<String>();
|
---|
146 | for (final String pname : pluginMap.keySet()) {
|
---|
147 | if (availablePlugins.get(pname) == null) pluginsToRemove.add(pname);
|
---|
148 | }
|
---|
149 |
|
---|
150 | for (String pname : pluginsToRemove) {
|
---|
151 | pluginMap.remove(pname);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | pluginPanel.removeAll();
|
---|
156 |
|
---|
157 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
158 | gbc.gridx = 0;
|
---|
159 | gbc.anchor = GridBagConstraints.NORTHWEST;
|
---|
160 |
|
---|
161 | int row = 0;
|
---|
162 | for (final PluginInformation plugin : availablePlugins.values()) {
|
---|
163 | boolean enabled = (enabledPlugins != null) && enabledPlugins.contains(plugin.name);
|
---|
164 | if (pluginMap.get(plugin.name) == null)
|
---|
165 | pluginMap.put(plugin.name, enabled);
|
---|
166 |
|
---|
167 | String remoteversion = plugin.version;
|
---|
168 | if ((remoteversion == null) || remoteversion.equals(""))
|
---|
169 | remoteversion = tr("unknown");
|
---|
170 |
|
---|
171 | String localversion = "";
|
---|
172 | PluginInformation p = localPlugins.get(plugin.name);
|
---|
173 | if(p != null)
|
---|
174 | {
|
---|
175 | if (p.version != null && !p.version.equals(""))
|
---|
176 | localversion = p.version;
|
---|
177 | else
|
---|
178 | localversion = tr("unknown");
|
---|
179 | localversion = " (" + localversion + ")";
|
---|
180 | }
|
---|
181 |
|
---|
182 | final JCheckBox pluginCheck = new JCheckBox(
|
---|
183 | tr("{0}: Version {1}{2}", plugin.name, remoteversion, localversion),
|
---|
184 | pluginMap.get(plugin.name));
|
---|
185 | gbc.gridy = row++;
|
---|
186 | gbc.insets = new Insets(5,5,0,5);
|
---|
187 | gbc.weighty = 0.1;
|
---|
188 | gbc.fill = GridBagConstraints.NONE;
|
---|
189 | pluginPanel.add(pluginCheck, gbc);
|
---|
190 |
|
---|
191 | pluginCheck.setToolTipText(plugin.downloadlink != null ? ""+plugin.downloadlink : tr("Plugin bundled with JOSM"));
|
---|
192 |
|
---|
193 | JEditorPane description = new JEditorPane();
|
---|
194 | description.setContentType("text/html");
|
---|
195 | description.setEditable(false);
|
---|
196 | description.setText("<html><body bgcolor=\"#" + Integer.toHexString( UIManager.getColor("Panel.background").getRGB() & 0x00ffffff ) +"\"><i>"+plugin.getLinkDescription()+"</i></body></html>");
|
---|
197 | description.setBorder(BorderFactory.createEmptyBorder());
|
---|
198 | description.setBackground(UIManager.getColor("Panel.background"));
|
---|
199 | description.addHyperlinkListener(new HyperlinkListener() {
|
---|
200 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
201 | if(e.getEventType() == EventType.ACTIVATED) {
|
---|
202 | OpenBrowser.displayUrl(e.getURL().toString());
|
---|
203 | }
|
---|
204 | }
|
---|
205 | });
|
---|
206 |
|
---|
207 | gbc.gridy = row++;
|
---|
208 | gbc.insets = new Insets(3,25,5,5);
|
---|
209 | gbc.weighty = 0.9;
|
---|
210 | gbc.weightx = 1.0;
|
---|
211 | gbc.anchor = GridBagConstraints.WEST;
|
---|
212 | gbc.fill = GridBagConstraints.HORIZONTAL;
|
---|
213 | pluginPanel.add(description, gbc);
|
---|
214 |
|
---|
215 | pluginCheck.addActionListener(new ActionListener(){
|
---|
216 | public void actionPerformed(ActionEvent e) {
|
---|
217 | pluginMap.put(plugin.name, pluginCheck.isSelected());
|
---|
218 | }
|
---|
219 | });
|
---|
220 | }
|
---|
221 | pluginPanel.updateUI();
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void loadPlugins() {
|
---|
225 | availablePlugins = new TreeMap<String, PluginInformation>(new Comparator<String>(){
|
---|
226 | public int compare(String o1, String o2) {
|
---|
227 | return o1.compareToIgnoreCase(o2);
|
---|
228 | }
|
---|
229 | });
|
---|
230 | localPlugins = new TreeMap<String, PluginInformation>(new Comparator<String>(){
|
---|
231 | public int compare(String o1, String o2) {
|
---|
232 | return o1.compareToIgnoreCase(o2);
|
---|
233 | }
|
---|
234 | });
|
---|
235 | for (String location : PluginInformation.getPluginLocations()) {
|
---|
236 | File[] pluginFiles = new File(location).listFiles();
|
---|
237 | if (pluginFiles != null) {
|
---|
238 | Arrays.sort(pluginFiles);
|
---|
239 | for (File f : pluginFiles) {
|
---|
240 | if (!f.isFile())
|
---|
241 | continue;
|
---|
242 | String fname = f.getName();
|
---|
243 | if (fname.endsWith(".jar")) {
|
---|
244 | try {
|
---|
245 | PluginInformation info = new PluginInformation(f,fname.substring(0,fname.length()-4));
|
---|
246 | if (!availablePlugins.containsKey(info.name))
|
---|
247 | availablePlugins.put(info.name, info);
|
---|
248 | if (!localPlugins.containsKey(info.name))
|
---|
249 | localPlugins.put(info.name, info);
|
---|
250 | } catch (PluginException x) {
|
---|
251 | }
|
---|
252 | } else if (fname.endsWith(".jar.new")) {
|
---|
253 | try {
|
---|
254 | PluginInformation info = new PluginInformation(f,fname.substring(0,fname.length()-8));
|
---|
255 | availablePlugins.put(info.name, info);
|
---|
256 | localPlugins.put(info.name, info);
|
---|
257 | } catch (PluginException x) {
|
---|
258 | }
|
---|
259 | } else if (fname.matches("^[0-9]+-site.*\\.txt$")) {
|
---|
260 | int err = 0;
|
---|
261 | try {
|
---|
262 | BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(f), "utf-8"));
|
---|
263 | String name = null;
|
---|
264 | String url = null;
|
---|
265 | String manifest = null;
|
---|
266 | for (String line = r.readLine(); line != null; line = r.readLine())
|
---|
267 | {
|
---|
268 | if(line.startsWith("\t"))
|
---|
269 | {
|
---|
270 | line = line.substring(1);
|
---|
271 | if(line.length() > 70)
|
---|
272 | {
|
---|
273 | manifest += line.substring(0,70)+"\n";
|
---|
274 | line = " " + line.substring(70);
|
---|
275 | }
|
---|
276 | manifest += line+"\n";
|
---|
277 | }
|
---|
278 | else
|
---|
279 | {
|
---|
280 | if(name != null)
|
---|
281 | {
|
---|
282 | try
|
---|
283 | {
|
---|
284 | PluginInformation info = new PluginInformation(
|
---|
285 | new ByteArrayInputStream(manifest.getBytes("utf-8")),
|
---|
286 | name.substring(0,name.length()-4));
|
---|
287 | info.downloadlink = url;
|
---|
288 | if(!availablePlugins.containsKey(info.name))
|
---|
289 | availablePlugins.put(info.name, info);
|
---|
290 | }
|
---|
291 | catch (Exception e)
|
---|
292 | {
|
---|
293 | e.printStackTrace();
|
---|
294 | ++err;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | String x[] = line.split(";");
|
---|
298 | name = x[0];
|
---|
299 | url = x[1];
|
---|
300 | manifest = null;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | if(name != null)
|
---|
304 | {
|
---|
305 | PluginInformation info = new PluginInformation(
|
---|
306 | new ByteArrayInputStream(manifest.getBytes("utf-8")),
|
---|
307 | name.substring(0,name.length()-4));
|
---|
308 | info.downloadlink = url;
|
---|
309 | if(!availablePlugins.containsKey(info.name))
|
---|
310 | availablePlugins.put(info.name, info);
|
---|
311 | }
|
---|
312 | r.close();
|
---|
313 | } catch (Exception e) {
|
---|
314 | e.printStackTrace();
|
---|
315 | ++err;
|
---|
316 | }
|
---|
317 | if(err > 0)
|
---|
318 | {
|
---|
319 | JOptionPane.showMessageDialog(Main.parent, tr("Error reading plugin information file: {0}", f.getName()));
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 | for (PluginProxy proxy : PluginHandler.pluginList)
|
---|
326 | {
|
---|
327 | if (!availablePlugins.containsKey(proxy.info.name))
|
---|
328 | availablePlugins.put(proxy.info.name, proxy.info);
|
---|
329 | if (!localPlugins.containsKey(proxy.info.name))
|
---|
330 | localPlugins.put(proxy.info.name, proxy.info);
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|