Statistics
| Revision:

root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / tools / swing / icontheme / impl / FolderIconTheme.java @ 38564

History | View | Annotate | Download (3.34 KB)

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

    
3
import java.io.File;
4
import java.io.FilenameFilter;
5
import java.net.MalformedURLException;
6
import java.util.HashMap;
7

    
8
import org.gvsig.installer.lib.api.InstallerLocator;
9
import org.gvsig.installer.lib.api.InstallerManager;
10
import org.gvsig.installer.lib.api.PackageInfo;
11
import org.gvsig.tools.swing.icontheme.IconTheme;
12

    
13

    
14
/**
15
 * This class extends AbstractIconTheme and implements the abstract methods of this
16
 * class. This methods are <code>loadIcon</code> and <code>load</code>. This methods
17
 * allows load one icon or all icons in the resource.
18
 */
19
public class FolderIconTheme extends BaseIconTheme{
20

    
21
        protected File resource = null; 
22
        protected PackageInfo packageInfo = null;
23
        
24
        /**
25
         * Constructor.Constructs an Icon Theme with a default one.
26
         * @param def. The default icon theme
27
         */
28
        public FolderIconTheme(IconTheme def) {
29
                super(def);
30
        }
31

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

    
43
                this.loadPackageInfo();
44
                this.setName(this.packageInfo.getName());
45
                this.setDescription(this.packageInfo.getDescription());
46
        }
47
        
48
        private void loadPackageInfo() {
49
                File pkgfile = new File(this.resource,"package.info"); 
50
                InstallerManager manager = InstallerLocator.getInstallerManager();
51
                try {
52
                        this.packageInfo = manager.createPackageInfo(pkgfile.toURI().toURL().openStream());
53
                } catch (Exception e) {
54
                        throw new IllegalArgumentException(e);
55
                }
56
                
57
        }
58

    
59
        protected void deferredLoad() {
60
                if( this.iconList != null ) {
61
                        return;
62
                }
63
                iconList = new HashMap<String, IconTheme.Icon>();
64
                
65
                load(resource, "");
66
                
67
                File[] files = resource.listFiles();
68
                for (int i=files.length-1; i>=0; i--) {
69
                        if( files[i].isDirectory() ) {
70
                                load(files[i],files[i].getName());
71
                        }
72
                }
73
        }
74

    
75
        private void load(File folder, String group) {
76
                File[] imagefiles = folder.listFiles(new ImageFileFilter());
77
                String name;
78
                for (int i=imagefiles.length-1; i>=0; i--) {
79
                        name = computeKey(imagefiles[i].getName());
80
                        try {
81
                                this.register(null, group, name, null, imagefiles[i].toURI().toURL());
82
                        } catch (MalformedURLException e) {
83
                                logger.info("Can't load icon", e);
84
                        }
85
                }
86
        }
87

    
88
        
89
        
90
        /**
91
         * This class allows filter the images in the directory. Allows to load the images
92
         * with a apropiate extension.
93
         */
94
        private class ImageFileFilter implements FilenameFilter {
95
                public boolean accept(File dir, String fileName) {
96
                        String extension = "";
97
                        int pointPos = fileName.lastIndexOf(".");
98
                        if (pointPos>0 && pointPos < (fileName.length()-1) )
99
                                extension = fileName.substring(pointPos+1).toLowerCase();
100
                        if ( extension.equals("jpg") || extension.equals("jpeg")
101
                                        || extension.equals("png")
102
                                        || extension.equals("gif")
103

    
104
                                        )
105
                                return true;
106
                        else
107
                                return false;
108
                }
109
        }
110

    
111
        /**
112
         * Returns the key of the image without extension
113
         * @param fileName
114
         * @return string
115
         */
116
        private String computeKey(String fileName) {
117
                int pointPos = fileName.lastIndexOf(".");
118
                if (pointPos!=-1)
119
                        return fileName.substring(0, pointPos);
120
                else
121
                        return fileName;
122
        }
123

    
124
}
125