Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / es / unex / sextante / gui / core / GUIResources.java @ 52

History | View | Annotate | Download (5.32 KB)

1
package es.unex.sextante.gui.core;
2

    
3
import java.io.File;
4
import java.net.URL;
5
import java.net.URLClassLoader;
6
import java.net.URLDecoder;
7
import java.util.ArrayList;
8
import java.util.Enumeration;
9
import java.util.StringTokenizer;
10
import java.util.zip.ZipEntry;
11
import java.util.zip.ZipFile;
12

    
13
import es.unex.sextante.core.Sextante;
14

    
15
/**
16
 * 
17
 * Methods in this class act as entry points for algorithms and resource files.
18
 * 
19
 * Algorithms are loaded when the library is initialized. Adding new algorithms after calling {@link Sextante#initialize()} will
20
 * not add them to the list of currently available ones, so all additional algorithms should be added before initializing the
21
 * library
22
 * 
23
 */
24
public class GUIResources {
25
   
26
   private static ArrayList<String> m_ParameterPanelNames        = new ArrayList<String>();
27
   private static ArrayList<String> m_ModelerParameterPanelNames = new ArrayList<String>();
28

    
29

    
30
   /**
31
    * Adds all sextante GUI resources located in the jar files in the classpath.
32
    */
33
   public static void addResourcesFromClasspath() {
34

    
35
      final StringTokenizer localTokenizer = new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator, false);
36

    
37
      //final Set<String> filenames = new LinkedHashSet();
38

    
39
      while (localTokenizer.hasMoreTokens()) {
40
         final String classpathElement = localTokenizer.nextToken();
41
         final File classpathFile = new File(classpathElement);
42
         final String sFilename = classpathFile.getName();
43
         if (classpathFile.exists() && classpathFile.canRead()) {
44
            if (sFilename.toLowerCase().endsWith(".jar") && 
45
                    sFilename.toLowerCase().startsWith(SextanteGUI.resourcePrefix)) {
46
               addResourcesFromJarFile(classpathElement);
47
            }
48

    
49
         }
50
      }
51

    
52
      final ClassLoader classloader = GUIResources.class.getClassLoader();
53
      URL[] webappClasses = new URL[0];
54
      if (classloader instanceof URLClassLoader) {
55
         final URLClassLoader urlClassLoader = (URLClassLoader) classloader;
56
         webappClasses = urlClassLoader.getURLs();
57
      }
58

    
59
      addResourcesFromURLs(webappClasses);
60

    
61
   }
62

    
63

    
64
   public static void addResourcesFromURLs(final URL[] urls) {
65

    
66
      for (final URL url : urls) {
67
         final File classpathFile = new File(URLDecoder.decode(url.getFile()));
68
         final String sFilename = classpathFile.getName();
69
         if (classpathFile.exists() && classpathFile.canRead()) {
70
            if (sFilename.toLowerCase().endsWith(".jar") && 
71
                    sFilename.toLowerCase().startsWith(SextanteGUI.resourcePrefix)) {
72
               addResourcesFromJarFile(classpathFile.getAbsolutePath());
73
            }
74
         }
75
      }
76

    
77

    
78
   }
79

    
80

    
81
   /**
82
    * Adds all sextante GUI resources from files located in the jar files of a given folder.
83
    */
84
   public static void addResourcesFromFolder(final String sFolder) {
85

    
86
      final File folder = new File(sFolder);
87
      final File[] directoryFiles = folder.listFiles();
88
      for (int i = 0; i < directoryFiles.length; i++) {
89
         if (!directoryFiles[i].isDirectory()) {
90
            final String sFilename = directoryFiles[i].getName();
91
            if (sFilename.endsWith(".jar") && 
92
                    sFilename.startsWith(SextanteGUI.resourcePrefix)) {
93
               addResourcesFromJarFile(directoryFiles[i].getAbsolutePath());
94
            }
95
         }
96
      }
97
   }
98

    
99

    
100
   /**
101
    * Adds all sextante algorithms and properties files located in a given jar file
102
    * 
103
    * @param sFilename
104
    *                the filename of the jar file
105
    */
106
   private static void addResourcesFromJarFile(final String sFilename) {
107

    
108
      try {
109
         final ZipFile zip = new ZipFile(sFilename);
110
         final Enumeration entries = zip.entries();
111
         while (entries.hasMoreElements()) {
112
            final ZipEntry entry = (ZipEntry) entries.nextElement();
113
            final String sName = entry.getName();
114
            if (!entry.isDirectory()) {
115
               if (sName.toLowerCase().endsWith("parameterspanel.class")) {
116
                  final String sClassName = sName.substring(0, sName.lastIndexOf('.')).replace('/', '.');
117
                  m_ParameterPanelNames.add(sClassName);
118
               }
119
               if (sName.toLowerCase().endsWith("modelerparameterspanel.class")) {
120
                  final String sClassName = sName.substring(0, sName.lastIndexOf('.')).replace('/', '.');
121
                  m_ModelerParameterPanelNames.add(sClassName);
122
               }
123
            }
124
         }
125
      }
126
      catch (final Exception e) {/*ignore file*/}
127

    
128
   }
129

    
130

    
131
   /**
132
    * Returns an array of class names corresponding to custom parameter panels found in jar files
133
    * 
134
    * @return an array of class names corresponding to custom parameter panels found in jar files
135
    */
136
   public static String[] getParameterPanelClassNames() {
137

    
138
      return m_ParameterPanelNames.toArray(new String[0]);
139

    
140
   }
141

    
142

    
143
   /**
144
    * Returns an array of class names corresponding to custom modeler parameter panels found in jar files
145
    * 
146
    * @return an array of class names corresponding to custom modeler parameter panels found in jar files
147
    */
148
   public static String[] getModelerParameterPanelClassNames() {
149

    
150
      return m_ModelerParameterPanelNames.toArray(new String[0]);
151

    
152
   }
153

    
154
}