Changeset 277 in josm


Ignore:
Timestamp:
2007-07-06T00:38:51+02:00 (17 years ago)
Author:
imi
Message:
  • added Plugin Manager to download new plugins from within JOSM
Location:
src/org/openstreetmap/josm
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/gui/preferences/PluginPreference.java

    r243 r277  
    22
    33import static org.openstreetmap.josm.tools.I18n.tr;
    4 
     4import static org.openstreetmap.josm.tools.I18n.trn;
     5
     6import java.awt.Dimension;
    57import java.awt.event.ActionEvent;
    68import java.awt.event.ActionListener;
    79import java.io.File;
     10import java.io.FileReader;
    811import java.util.Arrays;
    912import java.util.Collection;
    10 import java.util.Collections;
    1113import java.util.Comparator;
    1214import java.util.HashMap;
    1315import java.util.LinkedList;
    14 import java.util.List;
    1516import java.util.Map;
     17import java.util.SortedMap;
     18import java.util.TreeMap;
    1619import java.util.Map.Entry;
    1720
    1821import javax.swing.BorderFactory;
    1922import javax.swing.Box;
     23import javax.swing.JButton;
    2024import javax.swing.JCheckBox;
    2125import javax.swing.JLabel;
     26import javax.swing.JOptionPane;
    2227import javax.swing.JPanel;
    2328import javax.swing.JScrollPane;
    2429
    2530import org.openstreetmap.josm.Main;
     31import org.openstreetmap.josm.plugins.PluginDownloader;
    2632import org.openstreetmap.josm.plugins.PluginException;
    2733import org.openstreetmap.josm.plugins.PluginInformation;
     34import org.openstreetmap.josm.plugins.PluginProxy;
    2835import org.openstreetmap.josm.tools.GBC;
    29 import org.openstreetmap.josm.tools.UrlLabel;
     36import org.openstreetmap.josm.tools.XmlObjectParser.Uniform;
    3037
    3138public class PluginPreference implements PreferenceSetting {
    3239
    33         private Map<PluginInformation, Boolean> pluginMap;
     40        /**
     41         * Only the plugin name, it's jar location and the description.
     42         * In other words, this is the minimal requirement the plugin preference page
     43         * needs to show the plugin as available
     44         *
     45         * @author imi
     46         */
     47        public static class PluginDescription {
     48                public String name;
     49                public String description;
     50                public String resource;
     51                public PluginDescription(String name, String description, String resource) {
     52                        this.name = name;
     53                        this.description = description;
     54                        this.resource = resource;
     55        }
     56                public PluginDescription() {
     57        }
     58        }
     59       
     60        private Map<PluginDescription, Boolean> pluginMap;
     61        private Box pluginPanel = Box.createVerticalBox();
     62        private JPanel plugin;
    3463
    3564        public void addGui(final PreferenceDialog gui) {
    36                 pluginMap = new HashMap<PluginInformation, Boolean>();
    37                 Box pluginPanel = Box.createVerticalBox();
    38                 List<PluginInformation> availablePlugins = new LinkedList<PluginInformation>();
    39                 for (String location : PluginInformation.getPluginLocations()) {
    40                         File[] pluginFiles = new File(location).listFiles();
    41                         if (pluginFiles != null) {
    42                                 Arrays.sort(pluginFiles);
    43                                 for (File f : pluginFiles) {
    44                                         if (f.isFile() && f.getName().endsWith(".jar")) {
    45                                                 try {
    46                                     availablePlugins.add(new PluginInformation(f));
    47                             } catch (PluginException x) {
    48                             }
    49                                         }
    50                                 }
    51                         }
    52                 }
    53                 Collections.sort(availablePlugins, new Comparator<PluginInformation>(){
    54                         public int compare(PluginInformation o1, PluginInformation o2) {
    55                     return o1.name.compareTo(o2.name);
    56             }
    57                 });
    58 
     65                plugin = gui.createPreferenceTab("plugin", tr("Plugins"), tr("Configure available Plugins."));
     66                JScrollPane pluginPane = new JScrollPane(pluginPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
     67                pluginPane.setBorder(null);
     68                plugin.add(pluginPane, GBC.eol().fill(GBC.BOTH));
     69                plugin.add(GBC.glue(0,10), GBC.eol());
     70                JButton morePlugins = new JButton(tr("Get more plugins"));
     71                morePlugins.addActionListener(new ActionListener(){
     72                        public void actionPerformed(ActionEvent e) {
     73                                int count = PluginDownloader.downloadDescription();
     74                        if (count > 0)
     75                                JOptionPane.showMessageDialog(Main.parent,
     76                                                trn("Downloaded plugin information from {0} site",
     77                                                                "Downloaded plugin information from {0} sites", count, count));
     78                        else
     79                                JOptionPane.showMessageDialog(Main.parent, tr("No plugin information found."));
     80                        refreshPluginPanel(gui);
     81            }
     82                });
     83                plugin.add(morePlugins, GBC.std().insets(0,0,10,0));
     84               
     85                JButton update = new JButton(tr("Update current"));
     86                update.addActionListener(new ActionListener(){
     87                        public void actionPerformed(ActionEvent e) {
     88                                JOptionPane.showMessageDialog(Main.parent, tr("Not implemented yet."));
     89            }
     90                });
     91                plugin.add(update, GBC.std().insets(0,0,10,0));
     92
     93                JButton configureSites = new JButton(tr("Configure Plugin Sites"));
     94                configureSites.addActionListener(new ActionListener(){
     95                        public void actionPerformed(ActionEvent e) {
     96                                JOptionPane.showMessageDialog(Main.parent, tr("Not implemented yet."));
     97            }
     98                });
     99                plugin.add(configureSites, GBC.std());
     100
     101                refreshPluginPanel(gui);
     102        }
     103
     104        private void refreshPluginPanel(final PreferenceDialog gui) {
     105            Collection<PluginDescription> availablePlugins = getAvailablePlugins();
     106            pluginMap = new HashMap<PluginDescription, Boolean>();
     107            pluginPanel.removeAll();
    59108                Collection<String> enabledPlugins = Arrays.asList(Main.pref.get("plugins").split(","));
    60                 for (final PluginInformation plugin : availablePlugins) {
     109                for (final PluginDescription plugin : availablePlugins) {
    61110                        boolean enabled = enabledPlugins.contains(plugin.name);
    62111                        final JCheckBox pluginCheck = new JCheckBox(plugin.name, enabled);
    63112                        pluginPanel.add(pluginCheck);
    64113
    65                         pluginCheck.setToolTipText(plugin.file != null ? plugin.file.getAbsolutePath() : tr("Plugin bundled with JOSM"));
     114                        pluginCheck.setToolTipText(plugin.resource != null ? plugin.resource : tr("Plugin bundled with JOSM"));
    66115                        JLabel label = new JLabel("<html><i>"+(plugin.description==null?"no description available":plugin.description)+"</i></html>");
    67116                        label.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
     117                        label.setMaximumSize(new Dimension(450,1000));
    68118                        pluginPanel.add(label);
    69119                        pluginPanel.add(Box.createVerticalStrut(5));
     
    77127                        });
    78128                }
    79                 JScrollPane pluginPane = new JScrollPane(pluginPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    80                 pluginPane.setBorder(null);
    81 
    82                 JPanel plugin = gui.createPreferenceTab("plugin", tr("Plugins"), tr("Configure available Plugins."));
    83                 plugin.add(pluginPane, GBC.eol().fill(GBC.BOTH));
    84                 plugin.add(GBC.glue(0,10), GBC.eol());
    85                 plugin.add(new UrlLabel("http://josm.eigenheimstrasse.de/wiki/Plugins", tr("Get more plugins")), GBC.std().fill(GBC.HORIZONTAL));
    86         }
     129                plugin.updateUI();
     130    }
     131
     132        private Collection<PluginDescription> getAvailablePlugins() {
     133                SortedMap<String, PluginDescription> availablePlugins = new TreeMap<String, PluginDescription>(new Comparator<String>(){
     134                        public int compare(String o1, String o2) {
     135                    return o1.compareToIgnoreCase(o2);
     136            }
     137                });
     138                for (String location : PluginInformation.getPluginLocations()) {
     139                        File[] pluginFiles = new File(location).listFiles();
     140                        if (pluginFiles != null) {
     141                                for (File f : pluginFiles) {
     142                                        if (!f.isFile())
     143                                                continue;
     144                                        if (f.getName().endsWith(".jar")) {
     145                                                try {
     146                                                        PluginInformation info = new PluginInformation(f);
     147                                    availablePlugins.put(info.name, new PluginDescription(info.name, info.description, PluginInformation.getURLString(f.getPath())));
     148                            } catch (PluginException x) {
     149                            }
     150                                        } else if (f.getName().endsWith(".xml")) {
     151                                                try {
     152                                Uniform<PluginDescription> parser = new Uniform<PluginDescription>(new FileReader(f), "plugin", PluginDescription.class);
     153                                for (PluginDescription pd : parser)
     154                                        availablePlugins.put(pd.name, pd);
     155                        } catch (Exception e) {
     156                                e.printStackTrace();
     157                                JOptionPane.showMessageDialog(Main.parent, tr("Error reading plugin information file: {0}", f.getName()));
     158                        }
     159                                        }
     160                                }
     161                        }
     162                }
     163                for (PluginProxy proxy : Main.plugins)
     164                        if (!availablePlugins.containsKey(proxy.info.name))
     165                                availablePlugins.put(proxy.info.name, new PluginDescription(
     166                                                proxy.info.name,
     167                                                proxy.info.description,
     168                                                PluginInformation.getURLString(proxy.info.file.getPath())));
     169            return availablePlugins.values();
     170    }
    87171
    88172        public void ok() {
     173                Collection<PluginDescription> toDownload = new LinkedList<PluginDescription>();
     174                String msg = "";
     175                for (Entry<PluginDescription, Boolean> entry : pluginMap.entrySet()) {
     176                        if (entry.getValue() && PluginInformation.findPlugin(entry.getKey().name) == null) {
     177                                toDownload.add(entry.getKey());
     178                                msg += entry.getKey().name+"\n";
     179                        }
     180                }
     181                if (!toDownload.isEmpty()) {
     182                        int answer = JOptionPane.showConfirmDialog(Main.parent,
     183                                        tr("Download the following plugins?\n\n{0}", msg),
     184                                        tr("Download missing plugins"),
     185                                        JOptionPane.YES_NO_OPTION);
     186                        if (answer != JOptionPane.OK_OPTION)
     187                                for (PluginDescription pd : toDownload)
     188                                        pluginMap.put(pd, false);
     189                        else
     190                                for (PluginDescription pd : toDownload)
     191                                        PluginDownloader.downloadPlugin(pd);
     192                }
     193
    89194                String plugins = "";
    90                 for (Entry<PluginInformation, Boolean> entry : pluginMap.entrySet())
    91                         if (entry.getValue())
     195                for (Entry<PluginDescription, Boolean> entry : pluginMap.entrySet()) {
     196                        if (entry.getValue()) {
    92197                                plugins += entry.getKey().name + ",";
     198                                if (PluginInformation.findPlugin(entry.getKey().name) == null)
     199                                        toDownload.add(entry.getKey());
     200                        }
     201                }
    93202                if (plugins.endsWith(","))
    94203                        plugins = plugins.substring(0, plugins.length()-1);
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

    r267 r277  
    3232        public final String author;
    3333        public final int stage;
     34        public final String version;
    3435        public final List<URL> libraries = new ArrayList<URL>();
    3536
     
    6465                        String stageStr = attr.getValue("Plugin-Stage");
    6566                        stage = stageStr == null ? 50 : Integer.parseInt(stageStr);
     67                        version = attr.getValue("Plugin-Version");
    6668                        author = attr.getValue("Author");
    6769                        if (file != null)
     
    118120        }
    119121
    120         private String getURLString(String fileName) {
     122        public static String getURLString(String fileName) {
    121123                if (System.getProperty("os.name").startsWith("Windows"))
    122124                        return "file:/"+fileName;
  • src/org/openstreetmap/josm/tools/XmlObjectParser.java

    r215 r277  
    6666                        if (mapping.containsKey(qname) && !mapping.get(qname).onStart)
    6767                                report();
    68                         else if (characters != null && !current.isEmpty())
     68                        else if (characters != null && !current.isEmpty()) {
    6969                                setValue(qname, characters);
     70                                characters = "";
     71                        }
    7072                }
    7173                @Override public void characters(char[] ch, int start, int length) {
Note: See TracChangeset for help on using the changeset viewer.