Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / tools / swing / icontheme / impl / IconThemeZip.java @ 38564

History | View | Annotate | Download (3.28 KB)

1
package org.gvsig.tools.swing.icontheme.impl;
2

    
3
import java.io.File;
4
import java.io.InputStream;
5
import java.net.MalformedURLException;
6
import java.net.URI;
7
import java.net.URISyntaxException;
8
import java.net.URL;
9
import java.util.Enumeration;
10
import java.util.HashMap;
11
import java.util.zip.ZipEntry;
12
import java.util.zip.ZipFile;
13

    
14
import org.gvsig.installer.lib.api.InstallerLocator;
15
import org.gvsig.installer.lib.api.InstallerManager;
16
import org.gvsig.installer.lib.api.PackageInfo;
17
import org.gvsig.tools.swing.icontheme.IconTheme;
18

    
19
/**
20
 * Extends BaseIconTheme to add support for themes stored in zip files.
21
 * 
22
 * TODO: This class is under construction.
23
 */
24
public class IconThemeZip extends BaseIconTheme{
25

    
26
        protected File resource = null; 
27
        protected PackageInfo packageInfo = null;
28

    
29
        public IconThemeZip(IconTheme def) {
30
                super(def);
31
        }
32

    
33
        public void load(Object resource) {
34
                if( !(resource instanceof File) ) {
35
                        throw new IllegalArgumentException();
36
                }
37
                File res = (File) resource;
38
                if( !res.isDirectory() ) {
39
                        throw new IllegalArgumentException();
40
                }
41
                this.resource = res;
42
                this.iconList = null;
43

    
44
                this.loadPackageInfo();
45
                this.setName(this.packageInfo .getName());
46
                this.setDescription(this.packageInfo.getDescription());
47
        }
48

    
49
        private void loadPackageInfo() {
50
                InstallerManager manager = InstallerLocator.getInstallerManager();
51
                ZipFile zipFile = null;
52
                try {
53
                        zipFile = new ZipFile(resource);
54
                        InputStream pkgstream = zipFile.getInputStream( zipFile.getEntry("packages.zip") );
55
                        this.packageInfo = manager.createPackageInfo(pkgstream);
56
                } catch (Exception e) {
57
                        throw new IllegalArgumentException(e);
58
                }
59
        }
60

    
61
        protected void deferredLoad() {
62
                if (this.iconList != null) {
63
                        return;
64
                }
65
                iconList = new HashMap<String, IconTheme.Icon>();
66

    
67
                ZipFile zipFile = null;
68
                try {
69
                        zipFile = new ZipFile(resource);
70
                } catch (Exception e) {
71
                        throw new IllegalArgumentException();
72
                }
73

    
74
                Enumeration<?> entries = zipFile.entries();
75
                while (entries.hasMoreElements()) {
76
                        ZipEntry entry = (ZipEntry) entries.nextElement();
77
                        if (!entry.isDirectory()) {
78
                                try {
79
                                        if (isImage(entry.getName())) {
80
                                                String name = getNameOfEntry(entry);
81
                                                String group = getGroupOfEntry(entry);
82
                                                URL resource = getURLOfEntry(this.resource, entry);
83
                                                this.register(null, group, name, null, resource);
84
                                        }
85
                                } catch (Exception e) {
86
                                        logger.info("Can't register icon. Entry = " +entry.toString(), e);
87
                                }
88
                        }
89
                }
90

    
91
        }
92

    
93
        private URL getURLOfEntry(File zip, ZipEntry entry) throws MalformedURLException, URISyntaxException {
94
                String s = "jar:" + zip.toString() + "!" + entry.getName();
95
                return new URI(s).toURL();
96
        }
97

    
98
        private String getGroupOfEntry(ZipEntry entry) {
99
                // FIXME:
100
                return "";
101
        }
102

    
103
        private String getNameOfEntry(ZipEntry entry) {
104
                // FIXME: 
105
                return entry.getName();
106
        }
107

    
108
        private boolean isImage(String fileName) {
109
                String extension = "";
110
                int pointPos = fileName.lastIndexOf(".");
111
                if (pointPos>0 && pointPos < (fileName.length()-1) )
112
                        extension = fileName.substring(pointPos+1).toLowerCase();
113
                if ( extension.equals("jpg") || extension.equals("jpeg")
114
                                || extension.equals("png")
115
                                || extension.equals("gif"))
116
                        return true;
117
                else
118
                        return false;
119
        }
120

    
121
}