Changeset 3330 in josm for trunk


Ignore:
Timestamp:
2010-06-14T18:57:24+02:00 (14 years ago)
Author:
bastiK
Message:

added icons for plugin list

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginListPanel.java

    r3113 r3330  
    1313
    1414import javax.swing.JCheckBox;
     15import javax.swing.JLabel;
     16import javax.swing.SwingConstants;
    1517import javax.swing.event.HyperlinkEvent;
    1618import javax.swing.event.HyperlinkListener;
     
    105107            String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
    106108
    107             final JCheckBox cbPlugin = new JCheckBox(
    108                     tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion)
    109             );
     109            final JCheckBox cbPlugin = new JCheckBox();
    110110            cbPlugin.setSelected(selected);
    111111            cbPlugin.setToolTipText(formatCheckboxTooltipText(pi));
     
    115115                }
    116116            });
     117            JLabel lblPlugin = new JLabel(
     118                    tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion),
     119                    pi.getScaledIcon(),
     120                    SwingConstants.LEFT);
     121
     122            gbc.gridx = 0;
    117123            gbc.gridy = ++row;
    118124            gbc.insets = new Insets(5,5,0,5);
    119125            gbc.weighty = 0.0;
     126            gbc.weightx = 0.0;
    120127            add(cbPlugin, gbc);
     128
     129            gbc.gridx = 1;
     130            gbc.weightx = 1.0;
     131            add(lblPlugin, gbc);
    121132
    122133            HtmlPanel description = new HtmlPanel();
     
    130141            });
    131142
     143            gbc.gridx = 1;
    132144            gbc.gridy = ++row;
    133145            gbc.insets = new Insets(3,25,5,5);
  • trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java

    r3287 r3330  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Image;
    67import java.io.File;
    78import java.io.FileInputStream;
     
    2122import java.util.jar.JarInputStream;
    2223import java.util.jar.Manifest;
     24import javax.swing.ImageIcon;
    2325
    2426import org.openstreetmap.josm.Main;
    2527import org.openstreetmap.josm.data.Version;
     28import org.openstreetmap.josm.tools.ImageProvider;
    2629import org.openstreetmap.josm.tools.LanguageInfo;
    2730
     
    4750    public String localversion = null;
    4851    public String downloadlink = null;
     52    public String iconPath;
     53    public ImageIcon icon;
    4954    public List<URL> libraries = new LinkedList<URL>();
    5055    public final Map<String, String> attr = new TreeMap<String, String>();
     
    135140        this.version = other.version;
    136141        this.downloadlink = other.downloadlink;
     142        this.icon = other.icon;
     143        this.iconPath = other.iconPath;
    137144        this.libraries = other.libraries;
    138145        this.attr.clear();
     
    166173        catch(NumberFormatException e) {}
    167174        author = attr.getValue("Author");
     175        iconPath = attr.getValue("Plugin-Icon");
     176        if (iconPath != null) {
     177            // extract icon from the plugin jar file
     178            icon = ImageProvider.getIfAvailable(null, null, null, iconPath, file);
     179        }
    168180        if(oldcheck && mainversion > Version.getInstance().getVersion())
    169181        {
     
    413425    }
    414426
     427    public ImageIcon getScaledIcon() {
     428        if (icon == null)
     429            return null;
     430        return new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH));
     431    }
    415432}
  • trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java

    r3287 r3330  
    1414import java.util.Map;
    1515
    16 import org.openstreetmap.josm.Main;
    1716import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    1817import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1918import org.openstreetmap.josm.io.OsmTransferException;
     19import org.openstreetmap.josm.tools.ImageProvider;
    2020import org.xml.sax.SAXException;
    2121
     
    6666        } else {
    6767            availablePlugins.get(info.getName()).localversion = info.version;
     68            if (info.icon != null) {
     69                availablePlugins.get(info.getName()).icon = info.icon;
     70            }
    6871        }
    6972    }
     
    8992                System.err.println(tr("Warning: Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
    9093                e.printStackTrace();
     94            }
     95            monitor.worked(1);
     96        }
     97    }
     98
     99    protected void scanIconCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
     100        System.err.println("scanIconCacheFiles");
     101        File[] siteCacheFiles = pluginsDirectory.listFiles(
     102                new FilenameFilter() {
     103                    public boolean accept(File dir, String name) {
     104                        return name.matches("^([0-9]+-)?site.*plugin-icons\\.zip$");
     105                    }
     106                }
     107        );
     108        if (siteCacheFiles == null || siteCacheFiles.length == 0)
     109            return;
     110        monitor.subTask(tr("Processing plugin site cache icon files..."));
     111        monitor.setTicksCount(siteCacheFiles.length);
     112        for (File f: siteCacheFiles) {
     113            String fname = f.getName();
     114            monitor.setCustomText(tr("Processing file ''{0}''", fname));
     115            for (PluginInformation pi : availablePlugins.values()) {
     116                if (pi.icon == null && pi.iconPath != null) {
     117                    pi.icon = ImageProvider.getIfAvailable(null, null, null, pi.name+".jar/"+pi.iconPath, f);
     118                }
    91119            }
    92120            monitor.worked(1);
     
    130158            monitor.beginTask("");
    131159            scanSiteCacheFiles(monitor, pluginsDirectory);
     160            scanIconCacheFiles(monitor, pluginsDirectory);
    132161            scanPluginFiles(monitor, pluginsDirectory);
    133162        } finally {
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r3318 r3330  
    1212import java.io.InputStream;
    1313import java.io.InputStreamReader;
     14import java.io.OutputStream;
    1415import java.io.OutputStreamWriter;
    1516import java.io.PrintWriter;
     
    3233import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3334import org.openstreetmap.josm.io.OsmTransferException;
     35import org.openstreetmap.josm.tools.ImageProvider;
    3436import org.xml.sax.SAXException;
    3537
     
    4648    private List<PluginInformation> availablePlugins;
    4749
     50    protected enum CacheType {PLUGIN_LIST, ICON_LIST};
     51
    4852    protected void init(Collection<String> sites){
    4953        this.sites = sites;
     
    9094
    9195    /**
    92      * Creates the file name for the cached plugin list.
     96     * Creates the file name for the cached plugin list and the icon cache
     97     * file.
    9398     *
    9499     * @param site the name of the site
     100     * @param type icon cache or plugin list cache
    95101     * @return the file name for the cache file
    96102     */
    97     protected File createSiteCacheFile(File pluginDir, String site) {
     103    protected File createSiteCacheFile(File pluginDir, String site, CacheType type) {
    98104        String name;
    99105        try {
     
    114120                }
    115121            }
    116             sb.append(".txt");
     122            switch (type) {
     123                case PLUGIN_LIST:
     124                    sb.append(".txt");
     125                    break;
     126                case ICON_LIST:
     127                    sb.append("-icons.zip");
     128                    break;
     129            }
    117130            name = sb.toString();
    118131        } catch(MalformedURLException e) {
     
    175188
    176189    /**
     190     * Downloads the icon archive from a remote location
     191     *
     192     * @param site the site URL
     193     * @param monitor a progress monitor
     194     */
     195    protected void downloadPluginIcons(String site, File destFile, ProgressMonitor monitor) {
     196        InputStream in = null;
     197        OutputStream out = null;
     198        System.err.println("icons site: "+site);
     199        try {
     200            monitor.beginTask("");
     201            monitor.indeterminateSubTask(tr("Downloading plugin list from ''{0}''", site));
     202
     203            URL url = new URL(site);
     204            synchronized(this) {
     205                connection = (HttpURLConnection)url.openConnection();
     206                connection.setRequestProperty("Cache-Control", "no-cache");
     207                connection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());
     208                connection.setRequestProperty("Host", url.getHost());
     209            }
     210            in = connection.getInputStream();
     211            out = new FileOutputStream(destFile);
     212            byte[] buffer = new byte[8192];
     213            for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
     214                out.write(buffer, 0, read);
     215            }
     216            out.close();
     217            in.close();
     218        } catch(MalformedURLException e) {
     219            if (canceled) return;
     220            e.printStackTrace();
     221            return;
     222        } catch(IOException e) {
     223            if (canceled) return;
     224            e.printStackTrace();
     225            return;
     226        } finally {
     227            synchronized(this) {
     228                if (connection != null) {
     229                    connection.disconnect();
     230                }
     231                connection = null;
     232            }
     233            if (in != null) {
     234                try {
     235                    in.close();
     236                } catch(IOException e){/* ignore */}
     237            }
     238            monitor.finishTask();
     239        }
     240        for (PluginInformation pi : availablePlugins) {
     241            if (pi.icon == null && pi.iconPath != null) {
     242                pi.icon = ImageProvider.getIfAvailable(null, null, null, pi.name+".jar/"+pi.iconPath, destFile);
     243            }
     244        }
     245    }
     246
     247    /**
    177248     * Writes the list of plugins to a cache file
    178249     *
     
    189260                }
    190261            }
    191             File cacheFile = createSiteCacheFile(pluginDir, site);
     262            File cacheFile = createSiteCacheFile(pluginDir, site, CacheType.PLUGIN_LIST);
    192263            getProgressMonitor().subTask(tr("Writing plugin list to local cache ''{0}''", cacheFile.toString()));
    193264            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), "utf-8"));
     
    249320        getProgressMonitor().setTicksCount(sites.size() * 3);
    250321        File pluginDir = Main.pref.getPluginsDirectory();
     322
     323        // collect old cache files and remove if no longer in use
    251324        List<File> siteCacheFiles = new LinkedList<File>();
    252325        for (String location : PluginInformation.getPluginLocations()) {
     
    254327                new FilenameFilter() {
    255328                    public boolean accept(File dir, String name) {
    256                         return name.matches("^([0-9]+-)?site.*\\.txt$");
     329                        return name.matches("^([0-9]+-)?site.*\\.txt$") ||
     330                               name.matches("^([0-9]+-)?site.*-icons\\.zip$");
    257331                    }
    258332                }
     
    266340            String list = downloadPluginList(site, getProgressMonitor().createSubTaskMonitor(0, false));
    267341            if (canceled) return;
    268             siteCacheFiles.remove(createSiteCacheFile(pluginDir, site));
     342            siteCacheFiles.remove(createSiteCacheFile(pluginDir, site, CacheType.PLUGIN_LIST));
     343            siteCacheFiles.remove(createSiteCacheFile(pluginDir, site, CacheType.ICON_LIST));
    269344            if(list != null)
    270345            {
     
    278353                if (canceled) return;
    279354            }
     355            downloadPluginIcons(site+"-icons.zip", createSiteCacheFile(pluginDir, site, CacheType.ICON_LIST), getProgressMonitor().createSubTaskMonitor(0, false));
    280356        }
    281357        for (File file: siteCacheFiles) /* remove old stuff or whole update process is broken */
Note: See TracChangeset for help on using the changeset viewer.