gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / icontheme / FolderIconTheme.java @ 1845
History | View | Annotate | Download (4.32 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.swing.impl.icontheme; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.FilenameFilter; |
28 |
import java.net.MalformedURLException; |
29 |
import java.util.HashMap; |
30 |
|
31 |
import org.gvsig.installer.lib.api.PackageInfo; |
32 |
import org.gvsig.tools.ToolsLocator; |
33 |
import org.gvsig.tools.packageutils.PackageManager; |
34 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
35 |
|
36 |
|
37 |
/**
|
38 |
* This class extends AbstractIconTheme and implements the abstract methods of this
|
39 |
* class. This methods are <code>loadIcon</code> and <code>load</code>. This methods
|
40 |
* allows load one icon or all icons in the resource.
|
41 |
*/
|
42 |
public class FolderIconTheme extends BaseIconTheme{ |
43 |
|
44 |
protected File resource = null; |
45 |
protected PackageInfo packageInfo = null; |
46 |
|
47 |
/**
|
48 |
* Constructor.Constructs an Icon Theme with a default one.
|
49 |
* @param def. The default icon theme
|
50 |
*/
|
51 |
public FolderIconTheme(IconTheme def) {
|
52 |
super(def);
|
53 |
} |
54 |
|
55 |
public void load(Object resource) throws IllegalArgumentException { |
56 |
if( !(resource instanceof File) ) { |
57 |
throw new IllegalArgumentException(); |
58 |
} |
59 |
File res = (File) resource; |
60 |
if( !res.isDirectory() ) {
|
61 |
throw new IllegalArgumentException(); |
62 |
} |
63 |
this.resource = res;
|
64 |
this.iconList = null; |
65 |
|
66 |
this.loadPackageInfo();
|
67 |
this.setID(this.packageInfo.getID()); |
68 |
this.setName(this.packageInfo.getName()); |
69 |
this.setDescription(this.packageInfo.getDescription()); |
70 |
} |
71 |
|
72 |
private void loadPackageInfo() { |
73 |
File pkgfile = new File(this.resource,"package.info"); |
74 |
PackageManager manager = ToolsLocator.getPackageManager(); |
75 |
try {
|
76 |
this.packageInfo = manager.createPackageInfo(pkgfile.toURI().toURL().openStream());
|
77 |
} catch (Exception e) { |
78 |
throw new IllegalArgumentException(e); |
79 |
} |
80 |
|
81 |
} |
82 |
|
83 |
protected void deferredLoad() { |
84 |
if( this.iconList != null ) { |
85 |
return;
|
86 |
} |
87 |
iconList = new HashMap<String, IconTheme.Icon>(); |
88 |
|
89 |
load(resource, "");
|
90 |
|
91 |
File[] files = resource.listFiles(); |
92 |
for (int i=files.length-1; i>=0; i--) { |
93 |
if( files[i].isDirectory() ) {
|
94 |
load(files[i],files[i].getName()); |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
private void load(File folder, String group) { |
100 |
File[] imagefiles = folder.listFiles(new ImageFileFilter()); |
101 |
String name;
|
102 |
for (int i=imagefiles.length-1; i>=0; i--) { |
103 |
name = computeKey(imagefiles[i].getName()); |
104 |
try {
|
105 |
this.register(null, group, name, null, imagefiles[i].toURI().toURL()); |
106 |
} catch (MalformedURLException e) { |
107 |
logger.info("Can't load icon", e);
|
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
|
113 |
|
114 |
/**
|
115 |
* This class allows filter the images in the directory. Allows to load the images
|
116 |
* with a apropiate extension.
|
117 |
*/
|
118 |
private class ImageFileFilter implements FilenameFilter { |
119 |
public boolean accept(File dir, String fileName) { |
120 |
String extension = ""; |
121 |
int pointPos = fileName.lastIndexOf("."); |
122 |
if (pointPos>0 && pointPos < (fileName.length()-1) ) |
123 |
extension = fileName.substring(pointPos+1).toLowerCase();
|
124 |
if ( extension.equals("jpg") || extension.equals("jpeg") |
125 |
|| extension.equals("png")
|
126 |
|| extension.equals("gif")
|
127 |
|
128 |
) |
129 |
return true; |
130 |
else
|
131 |
return false; |
132 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Returns the key of the image without extension
|
137 |
* @param fileName
|
138 |
* @return string
|
139 |
*/
|
140 |
private String computeKey(String fileName) { |
141 |
int pointPos = fileName.lastIndexOf("."); |
142 |
if (pointPos!=-1) |
143 |
return fileName.substring(0, pointPos); |
144 |
else
|
145 |
return fileName;
|
146 |
} |
147 |
|
148 |
} |
149 |
|