Revision 138
org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.gui/src/main/java/es/unex/sextante/gui/core/GUIResources.java | ||
---|---|---|
1 | 1 |
package es.unex.sextante.gui.core; |
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 |
import java.net.MalformedURLException; |
|
4 | 5 |
import java.net.URL; |
5 | 6 |
import java.net.URLClassLoader; |
6 | 7 |
import java.net.URLDecoder; |
... | ... | |
13 | 14 |
import es.unex.sextante.core.Sextante; |
14 | 15 |
|
15 | 16 |
/** |
16 |
*
|
|
17 |
* |
|
17 | 18 |
* Methods in this class act as entry points for algorithms and resource files. |
18 |
*
|
|
19 |
* |
|
19 | 20 |
* Algorithms are loaded when the library is initialized. Adding new algorithms after calling {@link Sextante#initialize()} will |
20 | 21 |
* not add them to the list of currently available ones, so all additional algorithms should be added before initializing the |
21 | 22 |
* library |
22 |
*
|
|
23 |
* |
|
23 | 24 |
*/ |
24 | 25 |
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 | 26 |
|
27 |
private static ArrayList<Class> m_ParameterPanelClasses = new ArrayList<Class>(); |
|
28 |
private static ArrayList<Class> m_ModelerParameterPanelClasses = new ArrayList<Class>(); |
|
29 | 29 |
|
30 |
|
|
30 | 31 |
/** |
31 | 32 |
* Adds all sextante GUI resources located in the jar files in the classpath. |
32 | 33 |
*/ |
... | ... | |
41 | 42 |
final File classpathFile = new File(classpathElement); |
42 | 43 |
final String sFilename = classpathFile.getName(); |
43 | 44 |
if (classpathFile.exists() && classpathFile.canRead()) { |
44 |
if (sFilename.toLowerCase().endsWith(".jar") &&
|
|
45 |
if (sFilename.toLowerCase().endsWith(".jar") && |
|
45 | 46 |
sFilename.toLowerCase().startsWith(SextanteGUI.resourcePrefix)) { |
46 | 47 |
addResourcesFromJarFile(classpathElement); |
47 | 48 |
} |
... | ... | |
67 | 68 |
final File classpathFile = new File(URLDecoder.decode(url.getFile())); |
68 | 69 |
final String sFilename = classpathFile.getName(); |
69 | 70 |
if (classpathFile.exists() && classpathFile.canRead()) { |
70 |
if (sFilename.toLowerCase().endsWith(".jar") &&
|
|
71 |
if (sFilename.toLowerCase().endsWith(".jar") && |
|
71 | 72 |
sFilename.toLowerCase().startsWith(SextanteGUI.resourcePrefix)) { |
72 | 73 |
addResourcesFromJarFile(classpathFile.getAbsolutePath()); |
73 | 74 |
} |
... | ... | |
88 | 89 |
for (int i = 0; i < directoryFiles.length; i++) { |
89 | 90 |
if (!directoryFiles[i].isDirectory()) { |
90 | 91 |
final String sFilename = directoryFiles[i].getName(); |
91 |
if (sFilename.endsWith(".jar") &&
|
|
92 |
if (sFilename.endsWith(".jar") && |
|
92 | 93 |
sFilename.startsWith(SextanteGUI.resourcePrefix)) { |
93 | 94 |
addResourcesFromJarFile(directoryFiles[i].getAbsolutePath()); |
94 | 95 |
} |
... | ... | |
99 | 100 |
|
100 | 101 |
/** |
101 | 102 |
* Adds all sextante algorithms and properties files located in a given jar file |
102 |
*
|
|
103 |
* |
|
103 | 104 |
* @param sFilename |
104 | 105 |
* the filename of the jar file |
105 | 106 |
*/ |
106 |
private static void addResourcesFromJarFile(final String sFilename) { |
|
107 |
private static void addResourcesFromJarFile(final String sFilename) {
|
|
107 | 108 |
|
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 |
} |
|
109 |
try { |
|
110 |
File file = new File(sFilename); |
|
111 |
final ZipFile zip = new ZipFile(sFilename); |
|
112 |
final Enumeration entries = zip.entries(); |
|
113 |
while (entries.hasMoreElements()) { |
|
114 |
final ZipEntry entry = (ZipEntry) entries.nextElement(); |
|
115 |
final String sName = entry.getName(); |
|
116 |
if (!entry.isDirectory()) { |
|
117 |
if (sName.toLowerCase().endsWith( |
|
118 |
"modelerparameterspanel.class")) { |
|
119 |
final String sClassName = |
|
120 |
sName.substring(0, sName.lastIndexOf('.')).replace( |
|
121 |
'/', '.'); |
|
122 |
Class<?> loadedClassFromFile = loadClassFromFile(file, sClassName); |
|
123 |
if(loadedClassFromFile!=null){ |
|
124 |
m_ModelerParameterPanelClasses.add(loadedClassFromFile); |
|
125 |
} |
|
126 |
} else if (sName.toLowerCase().endsWith("parameterspanel.class")) { |
|
127 |
final String sClassName = |
|
128 |
sName.substring(0, sName.lastIndexOf('.')).replace('/', '.'); |
|
129 |
Class<?> loadedClassFromFile = loadClassFromFile(file, sClassName); |
|
130 |
if (loadedClassFromFile != null) { |
|
131 |
m_ParameterPanelClasses.add(loadedClassFromFile); |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
123 | 135 |
} |
124 |
} |
|
125 |
} |
|
126 |
catch (final Exception e) {/*ignore file*/} |
|
136 |
} catch (final Exception e) {/* ignore file */ |
|
137 |
} |
|
127 | 138 |
|
128 |
} |
|
139 |
}
|
|
129 | 140 |
|
130 | 141 |
|
142 |
private static Class loadClassFromFile(File jar, String className) { |
|
143 |
Class cls = null; |
|
144 |
try { |
|
145 |
// Convert File to a URL |
|
146 |
URL url = jar.toURL(); |
|
147 |
URL[] urls = new URL[] { url }; |
|
148 |
|
|
149 |
// Create a new class loader with the directory |
|
150 |
ClassLoader cl = new URLClassLoader(urls, GUIResources.class.getClassLoader()); |
|
151 |
|
|
152 |
cls = cl.loadClass(className); |
|
153 |
} catch (MalformedURLException e) { |
|
154 |
} catch (ClassNotFoundException e) { |
|
155 |
} |
|
156 |
return cls; |
|
157 |
} |
|
158 |
|
|
131 | 159 |
/** |
132 | 160 |
* Returns an array of class names corresponding to custom parameter panels found in jar files |
133 |
*
|
|
161 |
* |
|
134 | 162 |
* @return an array of class names corresponding to custom parameter panels found in jar files |
135 | 163 |
*/ |
136 |
public static String[] getParameterPanelClassNames() { |
|
164 |
public static String[] getParameterPanelClassNames() {
|
|
137 | 165 |
|
138 |
return m_ParameterPanelNames.toArray(new String[0]); |
|
166 |
String[] names = new String[m_ParameterPanelClasses.size()]; |
|
167 |
for (int i = 0; i < names.length; i++) { |
|
168 |
names[i] = m_ParameterPanelClasses.get(i).getName(); |
|
139 | 169 |
|
140 |
} |
|
170 |
} |
|
171 |
return names; |
|
172 |
} |
|
141 | 173 |
|
174 |
public static Class[] getParameterPanelClasses() { |
|
142 | 175 |
|
176 |
return m_ParameterPanelClasses.toArray(new Class[0]); |
|
177 |
|
|
178 |
} |
|
179 |
|
|
143 | 180 |
/** |
144 | 181 |
* Returns an array of class names corresponding to custom modeler parameter panels found in jar files |
145 |
*
|
|
182 |
* |
|
146 | 183 |
* @return an array of class names corresponding to custom modeler parameter panels found in jar files |
147 | 184 |
*/ |
148 |
public static String[] getModelerParameterPanelClassNames() { |
|
185 |
public static String[] getModelerParameterPanelClassNames() {
|
|
149 | 186 |
|
150 |
return m_ModelerParameterPanelNames.toArray(new String[0]); |
|
187 |
String[] names = new String[m_ModelerParameterPanelClasses.size()]; |
|
188 |
for (int i = 0; i < names.length; i++) { |
|
189 |
names[i] = m_ModelerParameterPanelClasses.get(i).getName(); |
|
151 | 190 |
|
152 |
} |
|
191 |
} |
|
192 |
return names; |
|
193 |
} |
|
153 | 194 |
|
195 |
public static Class[] getModelerParameterPanelClasses() { |
|
196 |
|
|
197 |
return m_ModelerParameterPanelClasses.toArray(new Class[0]); |
|
198 |
|
|
199 |
} |
|
200 |
|
|
154 | 201 |
} |
org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.gui/src/main/java/es/unex/sextante/gui/core/SextanteGUI.java | ||
---|---|---|
3 | 3 |
package es.unex.sextante.gui.core; |
4 | 4 |
|
5 | 5 |
import java.awt.Color; |
6 |
import java.awt.Container; |
|
6 | 7 |
import java.awt.Frame; |
7 | 8 |
import java.io.BufferedReader; |
8 | 9 |
import java.io.BufferedWriter; |
... | ... | |
65 | 66 |
private static String m_sSextanteHomePath; |
66 | 67 |
private static String m_sSextanteInstallPath; |
67 | 68 |
|
69 |
private static ArrayList<String> foldersWithResources = new ArrayList<String>(); |
|
70 |
private static boolean initialized = false; |
|
68 | 71 |
|
72 |
|
|
69 | 73 |
private final static HashMap<String, Class> m_ParametersPanel = new HashMap<String, Class>(); |
70 | 74 |
private final static HashMap<String, Class> m_ModelerParametersPanel = new HashMap<String, Class>(); |
71 | 75 |
|
... | ... | |
472 | 476 |
|
473 | 477 |
} |
474 | 478 |
|
479 |
public static void addResourcesFromFolder(final String sFolder) { |
|
480 |
if (initialized) { |
|
481 |
GUIResources.addResourcesFromFolder(sFolder); |
|
482 |
loadResources(); |
|
483 |
} else { |
|
484 |
foldersWithResources.add(sFolder); |
|
485 |
} |
|
486 |
} |
|
475 | 487 |
|
476 |
private static void _initialize() { |
|
488 |
private static void _initialize() {
|
|
477 | 489 |
|
478 |
setDefaultSettings(); |
|
479 |
readConfigFile(); |
|
490 |
setDefaultSettings();
|
|
491 |
readConfigFile();
|
|
480 | 492 |
|
481 |
checkPortableFolders(); |
|
493 |
checkPortableFolders();
|
|
482 | 494 |
|
483 |
//this loads built-in SEXTANTE algorithms and resources |
|
484 |
loadResources(); |
|
485 | 495 |
|
486 |
//and this loads additional algorithms from algorithm providers |
|
487 |
for (int i = 0; i < m_AlgorithmProviders.size(); i++) { |
|
488 |
final IAlgorithmProvider provider = m_AlgorithmProviders.get(i); |
|
489 |
provider.initialize(); |
|
490 |
if (provider.getAlgorithms().size() > 0) { |
|
491 |
Sextante.addAlgorithmsFromProvider(provider.getName(), provider.getAlgorithms()); |
|
492 |
addCustomModelerParametersPanel(provider.getCustomModelerParameterPanels()); |
|
493 |
addCustomParametersPanel(provider.getCustomParameterPanels()); |
|
494 |
} |
|
495 |
} |
|
496 |
for (Iterator iterator = foldersWithResources.iterator(); iterator |
|
497 |
.hasNext();) { |
|
498 |
String sFolder = (String) iterator.next(); |
|
499 |
GUIResources.addResourcesFromFolder(sFolder); |
|
500 |
} |
|
496 | 501 |
|
497 |
} |
|
502 |
// this loads built-in SEXTANTE algorithms and resources |
|
503 |
loadResources(); |
|
498 | 504 |
|
505 |
// and this loads additional algorithms from algorithm providers |
|
506 |
for (int i = 0; i < m_AlgorithmProviders.size(); i++) { |
|
507 |
final IAlgorithmProvider provider = m_AlgorithmProviders.get(i); |
|
508 |
provider.initialize(); |
|
509 |
if (provider.getAlgorithms().size() > 0) { |
|
510 |
Sextante.addAlgorithmsFromProvider(provider.getName(), |
|
511 |
provider.getAlgorithms()); |
|
512 |
addCustomModelerParametersPanel(provider |
|
513 |
.getCustomModelerParameterPanels()); |
|
514 |
addCustomParametersPanel(provider.getCustomParameterPanels()); |
|
515 |
} |
|
516 |
} |
|
499 | 517 |
|
518 |
initialized = true; |
|
519 |
|
|
520 |
} |
|
521 |
|
|
522 |
|
|
500 | 523 |
private static void setDefaultSettings() { |
501 | 524 |
|
502 | 525 |
final Setting[] baseSettings = new Setting[] { new SextanteGeneralSettings() }; |
... | ... | |
534 | 557 |
|
535 | 558 |
|
536 | 559 |
private static void loadResources() { |
560 |
final Class[] sPanelClass = GUIResources.getParameterPanelClasses(); |
|
561 |
for (final Class element : sPanelClass) { |
|
562 |
try { |
|
563 |
final Class panelClass = element; |
|
564 |
final String sAlgClassName = element.getName().substring(0, element.getName().indexOf("ParametersPanel")) + "Algorithm"; |
|
565 |
final Class algClass = panelClass.getClassLoader().loadClass(sAlgClassName); |
|
566 |
final GeoAlgorithm alg = (GeoAlgorithm) algClass.newInstance(); |
|
567 |
m_ParametersPanel.put(alg.getCommandLineName(), panelClass); |
|
568 |
} |
|
569 |
catch (final Exception e) {/*ignore it if there are problems*/ |
|
570 |
} |
|
571 |
} |
|
537 | 572 |
|
538 |
final String[] sPanelClassName = GUIResources.getParameterPanelClassNames(); |
|
539 |
for (final String element : sPanelClassName) { |
|
540 |
try { |
|
541 |
final Class panelClass = Class.forName(element); |
|
542 |
final String sAlgClassName = element.substring(0, element.indexOf("ParametersPanel")) + "Algorithm"; |
|
543 |
final Class algClass = Class.forName(sAlgClassName); |
|
544 |
final GeoAlgorithm alg = (GeoAlgorithm) algClass.newInstance(); |
|
545 |
m_ParametersPanel.put(alg.getCommandLineName(), panelClass); |
|
546 |
} |
|
547 |
catch (final Exception e) {/*ignore it if there are problems*/ |
|
548 |
} |
|
549 |
} |
|
550 |
|
|
551 |
final String[] sModelerPanelClassName = GUIResources.getModelerParameterPanelClassNames(); |
|
552 |
for (final String element : sModelerPanelClassName) { |
|
553 |
try { |
|
554 |
final Class panelClass = Class.forName(element); |
|
555 |
final String sAlgClassName = element.substring(0, element.indexOf("ModelerParametersPanel")) + "Algorithm"; |
|
556 |
final Class algClass = Class.forName(sAlgClassName); |
|
557 |
final GeoAlgorithm alg = (GeoAlgorithm) algClass.newInstance(); |
|
558 |
m_ModelerParametersPanel.put(alg.getCommandLineName(), panelClass); |
|
559 |
} |
|
560 |
catch (final Exception e) {/*ignore it if there are problems*/ |
|
561 |
} |
|
562 |
} |
|
563 |
|
|
564 |
|
|
573 |
final Class[] sModelerPanelClass = GUIResources.getModelerParameterPanelClasses(); |
|
574 |
for (final Class element : sModelerPanelClass) { |
|
575 |
try { |
|
576 |
final Class panelClass = element; |
|
577 |
final String sAlgClassName = element.getName().substring(0, element.getName().indexOf("ModelerParametersPanel")) + "Algorithm"; |
|
578 |
final Class algClass = panelClass.getClassLoader().loadClass(sAlgClassName); |
|
579 |
final GeoAlgorithm alg = (GeoAlgorithm) algClass.newInstance(); |
|
580 |
m_ModelerParametersPanel.put(alg.getCommandLineName(), panelClass); |
|
581 |
} |
|
582 |
catch (final Exception e) {/*ignore it if there are problems*/ |
|
583 |
} |
|
584 |
} |
|
565 | 585 |
} |
566 | 586 |
|
567 | 587 |
|
Also available in: Unified diff