Ticket #6964: 6964-from-zip.patch

File 6964-from-zip.patch, 3.4 KB (added by simon04, 14 years ago)
  • src/org/openstreetmap/josm/tools/ImageProvider.java

    diff --git a/src/org/openstreetmap/josm/tools/ImageProvider.java b/src/org/openstreetmap/josm/tools/ImageProvider.java
    index 39678d6..f7e6046 100644
    a b  
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    22package org.openstreetmap.josm.tools;
    33
     4import java.util.Enumeration;
     5import java.util.logging.Level;
     6import java.util.logging.Logger;
    47import static org.openstreetmap.josm.tools.I18n.tr;
    58
    69import java.awt.Component;
    import org.xml.sax.helpers.XMLReaderFactory;  
    4851import com.kitfox.svg.SVGDiagram;
    4952import com.kitfox.svg.SVGException;
    5053import com.kitfox.svg.SVGUniverse;
     54import java.io.BufferedInputStream;
     55import java.io.FileInputStream;
     56import java.util.zip.ZipInputStream;
    5157
    5258/**
    5359 * Helper class to support the application with images.
    public class ImageProvider {  
    147153     *                  around certain issues.
    148154     */
    149155    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) {
     156        ImageIcon fromZip = zipImages.get("images/" + name);
     157        if (fromZip != null) {
     158            return fromZip;
     159        }
     160
    150161        ImageResource ir = getIfAvailableImpl(dirs, id, subdir, name, archive);
    151162        if (ir == null)
    152163            return null;
    153164        return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize);
    154165    }
    155166
     167    private static class ZipImages {
     168        private String filename;
     169        private Map<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
     170
     171        public ZipImages(String filename) {
     172            this.filename = filename;
     173            try {
     174                init();
     175            } catch (IOException ex) {
     176                ex.printStackTrace();
     177            }
     178            System.out.println(icons.keySet());
     179        }
     180
     181        private void init() throws IOException {
     182            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(filename)));
     183            ZipEntry entry;
     184            while ((entry = zis.getNextEntry()) != null) {
     185                if (entry.isDirectory()) {
     186                    continue;
     187                }
     188                String name = entry.getName();
     189                int size = (int) entry.getSize();
     190                byte[] b = new byte[size];
     191                int rb = 0;
     192                int chunk = 0;
     193                while ((size - rb) > 0) {
     194                    chunk = zis.read(b, rb, size - rb);
     195                    if (chunk == -1) {
     196                        break;
     197                    }
     198                    rb += chunk;
     199                }
     200                ImageIcon icon = new ImageIcon(b);
     201                icons.put(name, icon);
     202            }
     203        }
     204
     205        public ImageIcon get(String name) {
     206            return icons.get(name);
     207        }
     208    }
     209
     210    private static final ZipImages zipImages = new ZipImages("/home/simon/src/josm.git/images.zip");
     211
    156212    private static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) {
    157213        if (name == null)
    158214            return null;
     215
    159216        ImageType type = name.toLowerCase().endsWith(".svg") ? ImageType.SVG : ImageType.OTHER;
    160217
    161218        if (name.startsWith("http://")) {