Statistics
| Revision:

root / trunk / frameworks / _fwAndami / src / com / iver / andami / iconthemes2 / IconThemeZip.java @ 15633

History | View | Annotate | Download (2.88 KB)

1
package com.iver.andami.iconthemes2;
2

    
3
import java.io.File;
4
import java.io.FilenameFilter;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.util.Enumeration;
8
import java.util.zip.ZipEntry;
9
import java.util.zip.ZipFile;
10

    
11
import javax.swing.ImageIcon;
12

    
13
/**
14
 * This class extends AbstractIconTheme and implements the abstract methods of this
15
 * class. This methods are <code>loadIcon</code> and <code>load</code>. This methods 
16
 * allows load one icon or all icons in the resource. 
17
 */
18
public class IconThemeZip extends AbstractIconTheme{
19
        
20
        /**
21
         * Constructor.Constructs an Icon Theme with a default one.
22
         * @param def. The default icon theme
23
         */
24
        public IconThemeZip(AbstractIconTheme def) {
25
                setDefault(def);
26
        }
27

    
28
        /**
29
         * Allows load a icon when this is inside in a Zip file. IconName is the name or 
30
         * key of the icon and "resource" 
31
         */
32
        @Override
33
        protected ImageIcon loadIcon(String iconName, Object resource) {
34
                if (resource instanceof ZipEntry && getResource() instanceof ZipFile) {
35
                        ZipEntry entry = (ZipEntry) resource;
36
                        ZipFile file = (ZipFile) getResource();
37
                        try {
38
                                InputStream is = file.getInputStream(entry);
39
                                int size=(int)entry.getSize();
40
                                if (size==-1) return null;
41
                                byte[] b=new byte[size];
42
                                int offset=0;
43
                                int chunk=0;
44
                                while ((size - offset) > 0) {
45
                                        chunk=is.read(b, offset, size - offset);
46
                                        if (chunk==-1) {
47
                                                break;
48
                                        }
49
                                        offset+=chunk;
50
                                }
51
                                return new ImageIcon(b);
52
                        } catch (IOException e) {
53
                        }
54
                }
55
                return null;
56
        }
57
        
58
        /**
59
         * Allows load all icons in the zip file.
60
         */
61
        @Override
62
        public void load() {
63
                if (getResource() instanceof ZipFile) {
64
                        ZipFile zipFile = (ZipFile) getResource();
65
                        ImageFileFilter filter = new ImageFileFilter();
66
                        Enumeration entries = zipFile.entries();
67
                        while (entries.hasMoreElements()) {
68
                                ZipEntry entry = (ZipEntry) entries.nextElement();
69
                                if (!entry.isDirectory() ) {
70
                                        if (filter.accept(new File(zipFile.getName()), entry.getName())) {
71
                                                register(computeKey(entry.getName()), entry);
72
                                        }
73
                                }
74
                        }
75

    
76
                }
77
        }
78

    
79
        /**
80
         * This class allows filter the images in the zip file. Allows to load the images
81
         * with a apropiate extension.
82
         */
83
        class ImageFileFilter implements FilenameFilter {
84
                public boolean accept(File dir, String fileName) {
85
                        String extension = "";
86
                        int pointPos = fileName.lastIndexOf(".");
87
                        if (pointPos>0 && pointPos < (fileName.length()-1) )
88
                                extension = fileName.substring(pointPos+1).toLowerCase();
89
                        if ( extension.equals("jpg") || extension.equals("jpeg")
90
                                        || extension.equals("png")
91
                                        || extension.equals("gif"))
92
                                return true;
93
                        else
94
                                return false;
95
                }
96
        }
97

    
98
        /**
99
         * Returns the key of the image without extension 
100
         * @param fileName
101
         * @return string
102
         */
103
        private String computeKey(String fileName) {
104
                int pointPos = fileName.lastIndexOf(".");
105
                if (pointPos!=-1)
106
                        return fileName.substring(0, pointPos);
107
                else
108
                        return fileName;
109
        }
110
}