svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / frameworks / _fwAndami / src / com / iver / andami / iconthemes / IconThemeDir.java @ 19520
History | View | Annotate | Download (2.8 KB)
1 |
package com.iver.andami.iconthemes; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FilenameFilter; |
5 |
import java.net.MalformedURLException; |
6 |
import java.net.URL; |
7 |
|
8 |
import javax.swing.ImageIcon; |
9 |
|
10 |
|
11 |
/**
|
12 |
* This class extends AbstractIconTheme and implements the abstract methods of this
|
13 |
* class. This methods are <code>loadIcon</code> and <code>load</code>. This methods
|
14 |
* allows load one icon or all icons in the resource.
|
15 |
*/
|
16 |
public class IconThemeDir extends AbstractIconTheme{ |
17 |
|
18 |
/**
|
19 |
* Constructor.Constructs an Icon Theme with a default one.
|
20 |
* @param def. The default icon theme
|
21 |
*/
|
22 |
public IconThemeDir(IIconTheme def) {
|
23 |
setDefault(def); |
24 |
} |
25 |
|
26 |
/**
|
27 |
* Allows load a icon when this is inside in a directoyr. IconName is the name or
|
28 |
* key of the icon and "resource" is the URL or the icon
|
29 |
*/
|
30 |
// @Override
|
31 |
protected ImageIcon loadIcon(String iconName,Object resource) { |
32 |
if (resource instanceof URL) { |
33 |
// ok, we got an imagePath, let's see if it's valid
|
34 |
ImageIcon icon = new ImageIcon((URL)resource); |
35 |
if (icon.getImage()!=null) { |
36 |
// the icon was successfully created from the imagePath
|
37 |
return icon;
|
38 |
} |
39 |
else {
|
40 |
log().error("Registered icon does not map to a valid image -- key: "+iconName+" -- URL: "+resource.toString()); |
41 |
return null; |
42 |
} |
43 |
|
44 |
} else if (resource instanceof ImageIcon) { |
45 |
return (ImageIcon)resource; |
46 |
|
47 |
} |
48 |
return null; |
49 |
} |
50 |
|
51 |
/**
|
52 |
* Allows to load all icons in the directory.
|
53 |
*/
|
54 |
// @Override
|
55 |
public void load() { |
56 |
if (getResource() instanceof File) { |
57 |
File basedir = (File) getResource(); |
58 |
if (basedir.isDirectory()) {
|
59 |
File[] imageList = basedir.listFiles(new ImageFileFilter()); |
60 |
String name;
|
61 |
for (int i=imageList.length-1; i>=0; i--) { |
62 |
name = computeKey(imageList[i].getName()); |
63 |
try {
|
64 |
register(name, imageList[i].toURL()); |
65 |
} catch (MalformedURLException e) {} |
66 |
} |
67 |
} |
68 |
|
69 |
} |
70 |
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* This class allows filter the images in the directory. Allows to load the images
|
75 |
* with a apropiate extension.
|
76 |
*/
|
77 |
class ImageFileFilter implements FilenameFilter { |
78 |
public boolean accept(File dir, String fileName) { |
79 |
String extension = ""; |
80 |
int pointPos = fileName.lastIndexOf("."); |
81 |
if (pointPos>0 && pointPos < (fileName.length()-1) ) |
82 |
extension = fileName.substring(pointPos+1).toLowerCase();
|
83 |
if ( extension.equals("jpg") || extension.equals("jpeg") |
84 |
|| extension.equals("png")
|
85 |
|| extension.equals("gif")
|
86 |
|
87 |
) |
88 |
return true; |
89 |
else
|
90 |
return false; |
91 |
} |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Returns the key of the image without extension
|
96 |
* @param fileName
|
97 |
* @return string
|
98 |
*/
|
99 |
private String computeKey(String fileName) { |
100 |
int pointPos = fileName.lastIndexOf("."); |
101 |
if (pointPos!=-1) |
102 |
return fileName.substring(0, pointPos); |
103 |
else
|
104 |
return fileName;
|
105 |
} |
106 |
|
107 |
} |
108 |
|