Changeset 2889 in josm for trunk


Ignore:
Timestamp:
2010-01-24T14:20:12+01:00 (14 years ago)
Author:
stoecker
Message:

close #4418 - support zip files for styles and presets

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r2839 r2889  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import java.io.File;
     6import java.io.InputStream;
    57import java.io.IOException;
    68import java.util.Collection;
     
    2325    private static ElemStyles styles = new ElemStyles();
    2426    private static Collection<String> iconDirs;
     27    private static File zipIcons;
    2528
    2629    public static ElemStyles getStyles()
     
    4649            }
    4750        }
    48         ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, name);
     51        ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, name, zipIcons);
    4952        if(i == null)
    5053        {
     
    8790                xmlReader.setErrorHandler(handler);
    8891                MirroredInputStream in = new MirroredInputStream(a[1]);
    89                 xmlReader.parse(new InputSource(in));
     92                InputStream zip = in.getZipEntry("xml","style");
     93                if(zip != null)
     94                {
     95                    zipIcons = in.getFile();
     96                    xmlReader.parse(new InputSource(zip));
     97                }
     98                else
     99                    xmlReader.parse(new InputSource(in));
    90100            } catch(IOException e) {
    91101                System.err.println(tr("Warning: failed to load Mappaint styles from ''{0}''. Exception was: {1}", a[1], e.toString()));
     
    97107        }
    98108        iconDirs = null;
     109        zipIcons = null;
    99110    }
    100111}
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r2813 r2889  
    1212import java.awt.event.ActionEvent;
    1313import java.io.BufferedReader;
     14import java.io.File;
    1415import java.io.IOException;
     16import java.io.InputStream;
    1517import java.io.InputStreamReader;
    1618import java.io.Reader;
     
    1820import java.util.Arrays;
    1921import java.util.Collection;
     22import java.util.Collections;
    2023import java.util.HashMap;
    2124import java.util.LinkedHashMap;
     
    7275    public String locale_name;
    7376    public final static String OPTIONAL_TOOLTIP_TEXT = "Optional tooltip text";
     77    private static File zipIcons = null;
    7478
    7579    public static abstract class Item {
     
    550554    public void setIcon(String iconName) {
    551555        Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null);
    552         ImageIcon icon = ImageProvider.getIfAvailable(s, "presets", null, iconName);
     556        ImageIcon icon = ImageProvider.getIfAvailable(s, "presets", null, iconName, zipIcons);
    553557        if (icon == null)
    554558        {
     
    636640            try {
    637641                MirroredInputStream s = new MirroredInputStream(source);
     642                InputStream zip = s.getZipEntry("xml","preset");
     643                if(zip != null)
     644                    zipIcons = s.getFile();
    638645                InputStreamReader r;
    639646                try
    640647                {
    641                     r = new InputStreamReader(s, "UTF-8");
     648                    r = new InputStreamReader(zip == null ? s : zip, "UTF-8");
    642649                }
    643650                catch (UnsupportedEncodingException e)
    644651                {
    645                     r = new InputStreamReader(s);
     652                    r = new InputStreamReader(zip == null ? s: zip);
    646653                }
    647654                allPresets.addAll(TaggingPreset.readAll(new BufferedReader(r)));
     
    663670                );
    664671            }
     672            zipIcons = null;
    665673        }
    666674        return allPresets;
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r2832 r2889  
    1313import java.net.URL;
    1414import java.net.URLConnection;
     15import java.util.Enumeration;
     16import java.util.zip.ZipEntry;
     17import java.util.zip.ZipFile;
    1518
    1619import org.openstreetmap.josm.Main;
     
    6265            throw new IOException();
    6366        fs = new FileInputStream(file);
     67    }
     68
     69    public InputStream getZipEntry(String extension, String namepart)
     70    {
     71        InputStream res = null;
     72        try {
     73            if(file != null && (file.getName().endsWith(".zip")
     74            || file.getName().endsWith(".ZIP")))
     75            {
     76                ZipFile zipFile = new ZipFile(file);
     77                ZipEntry resentry = null;
     78                Enumeration entries = zipFile.entries();
     79                while(entries.hasMoreElements()) {
     80                    ZipEntry entry = (ZipEntry)entries.nextElement();
     81                    if(entry.getName().endsWith("."+extension)) {
     82                        /* choose any file with correct extension. When more than
     83                        one file, prefer the one which matches namepart */
     84                        if(resentry == null || entry.getName().indexOf(namepart) >= 0) {
     85                            resentry = entry;
     86                        }
     87                    }
     88                }
     89                if(resentry != null) {
     90                    res = zipFile.getInputStream(resentry);
     91                }
     92                else {
     93                    zipFile.close();
     94                }
     95            }
     96        } catch (Exception e) {
     97            System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", file.getName(), e.toString()));
     98        }
     99        return res;
    64100    }
    65101
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r2853 r2889  
    1717import java.awt.image.BufferedImage;
    1818import java.io.File;
     19import java.io.InputStream;
    1920import java.io.IOException;
    2021import java.net.MalformedURLException;
     
    2627import java.util.List;
    2728import java.util.Map;
     29import java.util.zip.ZipEntry;
     30import java.util.zip.ZipFile;
    2831
    2932import javax.swing.Icon;
     
    8891     */
    8992    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) {
     93        return getIfAvailable(dirs, id, subdir, name, null);
     94    }
     95
     96    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) {
    9097        if (name == null)
    9198            return null;
     
    116123        if (dirs != null && dirs.size() > 0) {
    117124            cache_name = "id:" + id + ":" + full_name;
     125            if(archive != null)
     126                cache_name += ":" + archive.getName();
    118127        }
    119128
    120129        Image img = cache.get(cache_name);
    121130        if (img == null) {
     131            if(archive != null)
     132            {
     133                try
     134                {
     135                    ZipFile zipFile = new ZipFile(archive);
     136                    ZipEntry entry = zipFile.getEntry(full_name);
     137                    if(entry != null)
     138                    {
     139                        int size = (int)entry.getSize();
     140                        int offs = 0;
     141                        byte[] buf = new byte[size];
     142                        InputStream is = zipFile.getInputStream(entry);
     143                        while(size > 0)
     144                        {
     145                            int l = is.read(buf, offs, size);
     146                            offs += l;
     147                            size -= l;
     148                        }
     149                        img = Toolkit.getDefaultToolkit().createImage(buf);
     150                    }
     151                } catch (Exception e) {
     152                    System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", archive.getName(), e.toString()));
     153                }
     154            }
    122155            // getImageUrl() does a ton of "stat()" calls and gets expensive
    123156            // and redundant when you have a whole ton of objects. So,
     
    125158            // and don't bother to create a URL unless we're actually
    126159            // creating the image.
    127             URL path = getImageUrl(full_name, dirs);
    128             if (path == null)
    129                 return null;
    130             img = Toolkit.getDefaultToolkit().createImage(path);
     160            if(img == null)
     161            {
     162                URL path = getImageUrl(full_name, dirs);
     163                if (path == null)
     164                    return null;
     165                img = Toolkit.getDefaultToolkit().createImage(path);
     166            }
    131167            cache.put(cache_name, img);
    132168        }
Note: See TracChangeset for help on using the changeset viewer.