Revision 1077
org.gvsig.scripting/tags/org.gvsig.scripting-2.3.68/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/ScriptingUtils.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.scripting.app.extension; |
|
7 |
|
|
8 |
import java.io.File; |
|
9 |
import java.io.IOException; |
|
10 |
import java.nio.file.FileVisitOption; |
|
11 |
import static java.nio.file.FileVisitOption.FOLLOW_LINKS; |
|
12 |
import java.nio.file.FileVisitResult; |
|
13 |
import java.nio.file.Files; |
|
14 |
import java.nio.file.Path; |
|
15 |
import java.nio.file.Paths; |
|
16 |
import java.nio.file.SimpleFileVisitor; |
|
17 |
import java.nio.file.attribute.BasicFileAttributes; |
|
18 |
import java.text.MessageFormat; |
|
19 |
import java.util.ArrayList; |
|
20 |
import java.util.Arrays; |
|
21 |
import java.util.Collections; |
|
22 |
import java.util.Comparator; |
|
23 |
import java.util.EnumSet; |
|
24 |
import java.util.List; |
|
25 |
import java.util.function.Predicate; |
|
26 |
import javax.swing.JOptionPane; |
|
27 |
import org.apache.commons.io.FileUtils; |
|
28 |
import org.apache.commons.io.FilenameUtils; |
|
29 |
import org.apache.commons.io.IOCase; |
|
30 |
import org.apache.commons.lang3.BooleanUtils; |
|
31 |
import org.gvsig.scripting.ScriptingBaseScript; |
|
32 |
import org.gvsig.scripting.ScriptingFolder; |
|
33 |
import org.gvsig.scripting.ScriptingLocator; |
|
34 |
import org.gvsig.scripting.ScriptingManager; |
|
35 |
import org.gvsig.scripting.ScriptingScript; |
|
36 |
import org.gvsig.scripting.ScriptingUnit; |
|
37 |
import org.gvsig.scripting.app.extension.messagewait.MessageWait; |
|
38 |
import org.gvsig.scripting.swing.api.JScriptingComposer; |
|
39 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
|
40 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
|
41 |
import static org.gvsig.scripting.swing.api.ScriptingUIManager.SCRIPT_COMPOSER_AUTORUN; |
|
42 |
import org.gvsig.tools.ToolsLocator; |
|
43 |
import org.gvsig.tools.exception.BaseException; |
|
44 |
import org.gvsig.tools.i18n.I18nManager; |
|
45 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
46 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
|
47 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
|
48 |
import org.gvsig.tools.task.SimpleTaskStatus; |
|
49 |
import org.gvsig.tools.visitor.VisitCanceledException; |
|
50 |
import org.gvsig.tools.visitor.Visitor; |
|
51 |
import org.slf4j.Logger; |
|
52 |
import org.slf4j.LoggerFactory; |
|
53 |
|
|
54 |
/** |
|
55 |
* |
|
56 |
* @author jjdelcerro |
|
57 |
*/ |
|
58 |
public class ScriptingUtils { |
|
59 |
|
|
60 |
public static final String SKIP_AUTORUNS = "skipAutoruns"; |
|
61 |
|
|
62 |
private static final Logger logger = LoggerFactory.getLogger(ScriptingExtension.class); |
|
63 |
|
|
64 |
private boolean executingAutorunScripts = false; |
|
65 |
private static boolean composer_initialized = false; |
|
66 |
private final MessageWait message; |
|
67 |
|
|
68 |
/* |
|
69 |
* la funcion log y las constantes estan pensadas para usarlas desde los scripts. |
|
70 |
*/ |
|
71 |
public static final int INFO = 0; |
|
72 |
public static final int TRACE = 1; |
|
73 |
public static final int WARN = 2; |
|
74 |
public static final int ERROR = 3; |
|
75 |
|
|
76 |
public static void log(String message) { |
|
77 |
log(INFO, message, null); |
|
78 |
} |
|
79 |
|
|
80 |
public static void log(int level, String message) { |
|
81 |
log(level, message, null); |
|
82 |
} |
|
83 |
|
|
84 |
public static void log(int level, String message, Throwable th) { |
|
85 |
switch( level ) { |
|
86 |
case TRACE: |
|
87 |
logger.trace(message, th); |
|
88 |
break; |
|
89 |
case ERROR: |
|
90 |
logger.error(message, th); |
|
91 |
break; |
|
92 |
case WARN: |
|
93 |
logger.warn(message, th); |
|
94 |
break; |
|
95 |
default: |
|
96 |
case INFO: |
|
97 |
logger.info(message, th); |
|
98 |
break; |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
public ScriptingUtils() { |
|
103 |
message = new MessageWait(); |
|
104 |
} |
|
105 |
|
|
106 |
public static File getScriptsHomeFolder(File pluginHomeFolder, String appversion) { |
|
107 |
File scriptsHomeFolder; |
|
108 |
File f = FileUtils.getFile(pluginHomeFolder, "scripts-folder.txt"); |
|
109 |
if( f.exists() ) { |
|
110 |
try { |
|
111 |
List<String> lines = FileUtils.readLines(f); |
|
112 |
for( String line : lines ) { |
|
113 |
line = line.trim(); |
|
114 |
if( !line.startsWith("#") ) { |
|
115 |
scriptsHomeFolder = new File(line); |
|
116 |
if( !scriptsHomeFolder.isAbsolute() ) { |
|
117 |
scriptsHomeFolder = new File(pluginHomeFolder, line); |
|
118 |
} |
|
119 |
scriptsHomeFolder = new File( |
|
120 |
FilenameUtils.normalizeNoEndSeparator( |
|
121 |
scriptsHomeFolder.getAbsolutePath(), |
|
122 |
true |
|
123 |
) |
|
124 |
); |
|
125 |
if( scriptsHomeFolder.exists() ) { |
|
126 |
return scriptsHomeFolder; |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
} catch (IOException ex) { |
|
131 |
} |
|
132 |
} else { |
|
133 |
try { |
|
134 |
FileUtils.touch(f); |
|
135 |
} catch (IOException ex) { |
|
136 |
} |
|
137 |
} |
|
138 |
scriptsHomeFolder = FileUtils.getFile(pluginHomeFolder, appversion) ; |
|
139 |
return scriptsHomeFolder; |
|
140 |
} |
|
141 |
|
|
142 |
public void initializaPaths(List<File> pluginsFolder, File installFolder, File pluginHoneFolder, String appversion) { |
|
143 |
|
|
144 |
ScriptingManager manager = ScriptingLocator.getManager(); |
|
145 |
manager.setHomeFolder(getScriptsHomeFolder(pluginHoneFolder, appversion)); |
|
146 |
|
|
147 |
ScriptingFolder folder = manager.createLink( |
|
148 |
"Common", |
|
149 |
manager.getUserFolder(), |
|
150 |
"../../scripts" |
|
151 |
); |
|
152 |
folder.setProperty(ScriptingUtils.SKIP_AUTORUNS, BooleanUtils.toStringTrueFalse(true)); |
|
153 |
folder.save(); |
|
154 |
// manager.createLink( |
|
155 |
// "Previous version", |
|
156 |
// manager.getUserFolder(), |
|
157 |
// "../../../org.gvsig.scripting.app.extension/scripts" |
|
158 |
// ); |
|
159 |
|
|
160 |
List<File> pluginsFolders = new ArrayList<>(); |
|
161 |
for( File f : pluginsFolder ) { |
|
162 |
pluginsFolders.addAll(Arrays.asList(f.listFiles())); |
|
163 |
} |
|
164 |
|
|
165 |
for( File pluginFolder : pluginsFolders ) { |
|
166 |
File scriptsFolder = new File(pluginFolder, "scripting/scripts"); |
|
167 |
if( scriptsFolder.exists() ) { |
|
168 |
manager.registerSystemFolder(pluginFolder.getName(), scriptsFolder); |
|
169 |
} |
|
170 |
File libFolder = new File(pluginFolder, "scripting/lib"); |
|
171 |
if( libFolder.exists() ) { |
|
172 |
manager.addLibFolder(libFolder); |
|
173 |
} |
|
174 |
} |
|
175 |
manager.setPackagesFolder(installFolder); |
|
176 |
|
|
177 |
File localAddonRepositoryFolder = new File(manager.getRootUserFolder(), "addons"); |
|
178 |
if( !localAddonRepositoryFolder.exists() ) { |
|
179 |
try { |
|
180 |
FileUtils.forceMkdir(localAddonRepositoryFolder); |
|
181 |
} catch (IOException ex) { |
|
182 |
logger.info("Can't create addons folder in '" + localAddonRepositoryFolder.getAbsolutePath() + "'.", ex); |
|
183 |
} |
|
184 |
} |
|
185 |
File initAddonFile = new File(localAddonRepositoryFolder, "__init__.py"); |
|
186 |
if( !initAddonFile.exists() ) { |
|
187 |
try { |
|
188 |
FileUtils.touch(initAddonFile); |
|
189 |
} catch (IOException ex) { |
|
190 |
logger.info("Can't create addons __init__ file in '" + localAddonRepositoryFolder.getAbsolutePath() + "'.", ex); |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
|
|
196 |
public void runScript(String name, Object[] args) { |
|
197 |
ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
|
198 |
WindowManager winmanager = ToolsSwingLocator.getWindowManager(); |
|
199 |
|
|
200 |
ScriptingBaseScript script = uimanager.getManager().getScript(name); |
|
201 |
if( script != null ) { |
|
202 |
script.run(args); |
|
203 |
} else { |
|
204 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
|
205 |
dialogs.messageDialog( |
|
206 |
"Can't locate script '" + name + "'.", |
|
207 |
"ScriptLaunch", |
|
208 |
JOptionPane.OK_OPTION |
|
209 |
); |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
public void runLauncher() { |
|
214 |
ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
|
215 |
WindowManager winmanager = ToolsSwingLocator.getWindowManager(); |
|
216 |
winmanager.showWindow(uimanager.createLauncher().asJComponent(), uimanager.getTranslation("Scripting_Launcher"), WindowManager.MODE.TOOL); |
|
217 |
} |
|
218 |
|
|
219 |
public void runComposer() { |
|
220 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
221 |
message.showMessage( |
|
222 |
i18n.getTranslation("_notice"), |
|
223 |
i18n.getTranslation("_Initializing_the_scripting_plugin_Xhorizontal_ellipsisX") + "\n" |
|
224 |
+ i18n.getTranslation("_Waiting_to_terminate"), |
|
225 |
new MessageWait.WaitingCondition() { |
|
226 |
private Thread th = null; |
|
227 |
|
|
228 |
@Override |
|
229 |
public boolean stopWaiting() { |
|
230 |
if (executingAutorunScripts) { |
|
231 |
return false; |
|
232 |
} |
|
233 |
if (composer_initialized) { |
|
234 |
return true; |
|
235 |
} |
|
236 |
if (th == null) { |
|
237 |
th = new Thread(new ExecuteScriptsFromScriptingFolders(null, "scautorun", SCRIPT_COMPOSER_AUTORUN), "StartupScriptingComposer"); |
|
238 |
th.start(); |
|
239 |
} |
|
240 |
return false; |
|
241 |
} |
|
242 |
}, |
|
243 |
new Runnable() { |
|
244 |
@Override |
|
245 |
public void run() { |
|
246 |
showScriptingComposer(); |
|
247 |
} |
|
248 |
} |
|
249 |
); |
|
250 |
|
|
251 |
} |
|
252 |
|
|
253 |
public Runnable getAutorunScriptsOnStartup(List<File> pluginsFolders) { |
|
254 |
return new ExecuteAutorunScriptsOnStartup(pluginsFolders); |
|
255 |
} |
|
256 |
|
|
257 |
private void showScriptingComposer() { |
|
258 |
ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
|
259 |
JScriptingComposer composer = uimanager.createComposer(); |
|
260 |
uimanager.showWindow( |
|
261 |
composer.asJComponent(), |
|
262 |
uimanager.getTranslation("Scripting_Composer") |
|
263 |
); |
|
264 |
} |
|
265 |
|
|
266 |
private class ExecuteScriptsFromScriptingFolders extends AbstractExecuteScripts { |
|
267 |
|
|
268 |
public ExecuteScriptsFromScriptingFolders(List<File> pluginsFolders, String title, String patternName) { |
|
269 |
super(pluginsFolders, title, patternName); |
|
270 |
} |
|
271 |
|
|
272 |
@Override |
|
273 |
public void run() { |
|
274 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
|
275 |
SimpleTaskStatus status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Scripts startup"); |
|
276 |
status.setIndeterminate(); |
|
277 |
status.setAutoremove(true); |
|
278 |
status.add(); |
|
279 |
final I18nManager i18nManager = ToolsLocator.getI18nManager(); |
|
280 |
try { |
|
281 |
logger.info("Searching "+title+" scripts..."); |
|
282 |
dialogs.message( |
|
283 |
i18nManager.getTranslation("_Searching_autorun_scripts_Xhorizontal_ellipsisX"), |
|
284 |
JOptionPane.INFORMATION_MESSAGE |
|
285 |
); |
|
286 |
ScriptingManager manager = ScriptingLocator.getManager(); |
|
287 |
final List<ScriptingScript> scripts = new ArrayList<>(); |
|
288 |
|
|
289 |
Visitor visitor = new Visitor() { |
|
290 |
@Override |
|
291 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
|
292 |
ScriptingUnit unit = (ScriptingUnit) o; |
|
293 |
if( unit instanceof ScriptingScript && |
|
294 |
FilenameUtils.wildcardMatch(unit.getName(), patternName, IOCase.INSENSITIVE) ) { |
|
295 |
ScriptingScript script = (ScriptingScript) unit; |
|
296 |
if( script.isEnabled() ) { |
|
297 |
scripts.add(script); |
|
298 |
} else { |
|
299 |
logger.info("Skip "+title+" script '" + script.getFile().getAbsolutePath() + "'."); |
|
300 |
} |
|
301 |
} |
|
302 |
} |
|
303 |
}; |
|
304 |
Predicate<ScriptingUnit> includeFilter = new Predicate<ScriptingUnit>() { |
|
305 |
@Override |
|
306 |
public boolean test(ScriptingUnit unit) { |
|
307 |
if( unit instanceof ScriptingFolder ) { |
|
308 |
return !BooleanUtils.toBoolean(unit.getProperty(SKIP_AUTORUNS)); |
|
309 |
} |
|
310 |
return false; |
|
311 |
} |
|
312 |
}; |
|
313 |
manager.getSystemFolder().accept(visitor, includeFilter); |
|
314 |
manager.getUserFolder().accept(visitor, includeFilter); |
|
315 |
sortScripts(scripts); |
|
316 |
executeScripts(scripts, status); |
|
317 |
} catch(Throwable th) { |
|
318 |
|
|
319 |
} finally { |
|
320 |
composer_initialized = true; |
|
321 |
logger.info("Running "+title+" scripts terminated."); |
|
322 |
dialogs.message("", JOptionPane.INFORMATION_MESSAGE); |
|
323 |
status.terminate(); |
|
324 |
} |
|
325 |
} |
|
326 |
} |
|
327 |
|
|
328 |
public class ExecuteAutorunScriptsOnStartup implements Runnable { |
|
329 |
|
|
330 |
private final List<File> pluginsFolders; |
|
331 |
|
|
332 |
public ExecuteAutorunScriptsOnStartup(List<File> pluginsFolders) { |
|
333 |
this.pluginsFolders = pluginsFolders; |
|
334 |
} |
|
335 |
|
|
336 |
@Override |
|
337 |
public void run() { |
|
338 |
Thread th = new Thread(new ExecuteScriptsFromFilesystem( |
|
339 |
this.pluginsFolders, |
|
340 |
"autotun", |
|
341 |
"autorun.inf" |
|
342 |
) |
|
343 |
); |
|
344 |
th.start(); |
|
345 |
} |
|
346 |
} |
|
347 |
|
|
348 |
private abstract class AbstractExecuteScripts implements Runnable { |
|
349 |
|
|
350 |
protected String patternName; |
|
351 |
protected String title; |
|
352 |
private final List<File> pluginsFolders; |
|
353 |
|
|
354 |
public AbstractExecuteScripts(List<File> pluginsFolders, String title, String patternName) { |
|
355 |
if( pluginsFolders==null ) { |
|
356 |
this.pluginsFolders = Collections.EMPTY_LIST; |
|
357 |
} else { |
|
358 |
this.pluginsFolders = pluginsFolders; |
|
359 |
} |
|
360 |
this.patternName = patternName; |
|
361 |
this.title = title; |
|
362 |
} |
|
363 |
|
|
364 |
protected List<File> getPluginsFolders() { |
|
365 |
return this.pluginsFolders; |
|
366 |
} |
|
367 |
|
|
368 |
private String getScriptOrderKey(ScriptingBaseScript o) { |
|
369 |
int groupOrder = 500; |
|
370 |
String groupName = "default"; |
|
371 |
int scriptOrder = 500; |
|
372 |
String s = o.getProperty("autorun.group.order"); |
|
373 |
if (s != null) { |
|
374 |
try { |
|
375 |
groupOrder = Integer.parseInt(s); |
|
376 |
} catch (Exception ex) { |
|
377 |
// Do nothing. |
|
378 |
} |
|
379 |
} |
|
380 |
s = o.getProperty("autorun.group.name"); |
|
381 |
if (s != null) { |
|
382 |
groupName = s; |
|
383 |
} |
|
384 |
s = o.getProperty("autorun.order"); |
|
385 |
if (s != null) { |
|
386 |
try { |
|
387 |
scriptOrder = Integer.parseInt(s); |
|
388 |
} catch (Exception ex) { |
|
389 |
// Do nothing. |
|
390 |
} |
|
391 |
} |
|
392 |
String key = MessageFormat.format( |
|
393 |
"{0,number,000000}.{1}.{2,number,000000}", |
|
394 |
groupOrder, |
|
395 |
groupName, |
|
396 |
scriptOrder |
|
397 |
); |
|
398 |
return key; |
|
399 |
} |
|
400 |
|
|
401 |
protected void sortScripts(List<ScriptingScript> scripts) { |
|
402 |
Collections.sort(scripts, new Comparator<ScriptingBaseScript>() { |
|
403 |
|
|
404 |
@Override |
|
405 |
public int compare(ScriptingBaseScript o1, ScriptingBaseScript o2) { |
|
406 |
return getScriptOrderKey(o2).compareToIgnoreCase(getScriptOrderKey(o1)); |
|
407 |
} |
|
408 |
}); |
|
409 |
} |
|
410 |
|
|
411 |
protected void executeScripts(List<ScriptingScript> scripts, SimpleTaskStatus status) { |
|
412 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
|
413 |
final I18nManager i18nManager = ToolsLocator.getI18nManager(); |
|
414 |
if (scripts != null && !scripts.isEmpty()) { |
|
415 |
if (status != null) { |
|
416 |
status.setRangeOfValues(0, scripts.size()); |
|
417 |
} |
|
418 |
int n = 0; |
|
419 |
for (ScriptingBaseScript script : scripts) { |
|
420 |
if (status != null) { |
|
421 |
status.setCurValue(n++); |
|
422 |
} |
|
423 |
try { |
|
424 |
logger.info("running " + title + " script '" + script.getFile().getAbsolutePath() + "' (" + getScriptOrderKey(script) + ", " + script.getIsolationGroup() + ")."); |
|
425 |
dialogs.message( |
|
426 |
i18nManager.getTranslation( |
|
427 |
"_Running_autorun_script_from_XnameX", |
|
428 |
new String[]{ |
|
429 |
script.getFile().getParentFile().getName()} |
|
430 |
), |
|
431 |
JOptionPane.INFORMATION_MESSAGE |
|
432 |
); |
|
433 |
} catch (Exception ex) { |
|
434 |
// Ignore it |
|
435 |
} |
|
436 |
try { |
|
437 |
script.run(); |
|
438 |
} catch (Exception ex) { |
|
439 |
logger.warn("Can't execute " + title + " from '" + script.getFile().getAbsolutePath() + "'.", ex); |
|
440 |
} |
|
441 |
} |
|
442 |
} |
|
443 |
|
|
444 |
} |
|
445 |
} |
|
446 |
|
|
447 |
private class ExecuteScriptsFromFilesystem extends AbstractExecuteScripts { |
|
448 |
|
|
449 |
public ExecuteScriptsFromFilesystem(List<File> pluginsFolder, String title, String patternName) { |
|
450 |
super(pluginsFolder, title, patternName); |
|
451 |
} |
|
452 |
|
|
453 |
@Override |
|
454 |
public void run() { |
|
455 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
|
456 |
executingAutorunScripts = true; |
|
457 |
try { |
|
458 |
final ScriptingManager manager = ScriptingLocator.getManager(); |
|
459 |
manager.loadEngines(); |
|
460 |
|
|
461 |
// final PluginsManager pluginManager = PluginsLocator.getManager(); |
|
462 |
final I18nManager i18nManager = ToolsLocator.getI18nManager(); |
|
463 |
|
|
464 |
final List<ScriptingScript> scripts = new ArrayList<>(); |
|
465 |
|
|
466 |
List<File> pluginsFolders = new ArrayList<>(); |
|
467 |
for (File pluginsFolders2 : this.getPluginsFolders()) { |
|
468 |
for (File pluginFolder : pluginsFolders2.listFiles()) { |
|
469 |
File f = FileUtils.getFile(pluginFolder, "scripting", "scripts"); |
|
470 |
if (f.exists()) { |
|
471 |
pluginsFolders.add(f); |
|
472 |
} |
|
473 |
} |
|
474 |
} |
|
475 |
|
|
476 |
dialogs.message( |
|
477 |
i18nManager.getTranslation("_Searching_autorun_scripts_Xhorizontal_ellipsisX"), |
|
478 |
JOptionPane.INFORMATION_MESSAGE |
|
479 |
); |
|
480 |
|
|
481 |
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { |
|
482 |
@Override |
|
483 |
public FileVisitResult visitFile(Path path, BasicFileAttributes bfa) throws IOException { |
|
484 |
File file = path.toFile(); |
|
485 |
if (FilenameUtils.wildcardMatch(file.getName(), patternName, IOCase.INSENSITIVE)) { |
|
486 |
if (file.exists()) { |
|
487 |
ScriptingScript script = (ScriptingScript) manager.getScript(file); |
|
488 |
if (script.isEnabled()) { |
|
489 |
scripts.add(script); |
|
490 |
} else { |
|
491 |
logger.info("Skip " + title + " script '" + file.getAbsolutePath() + "'."); |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
return FileVisitResult.CONTINUE; |
|
496 |
} |
|
497 |
}; |
|
498 |
try { |
|
499 |
EnumSet<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); |
|
500 |
for (File pluginFolder : pluginsFolders) { |
|
501 |
Files.walkFileTree(Paths.get(pluginFolder.toURI()), opts, Integer.MAX_VALUE, visitor); |
|
502 |
} |
|
503 |
Files.walkFileTree(Paths.get(manager.getRootUserFolder().toURI()), opts, Integer.MAX_VALUE, visitor); |
|
504 |
List<ScriptingFolder> folders = manager.getAlternativeUserFolders(); |
|
505 |
for (ScriptingFolder folder : folders) { |
|
506 |
Files.walkFileTree(Paths.get(folder.getFile().toURI()), opts, Integer.MAX_VALUE, visitor); |
|
507 |
} |
|
508 |
} catch (Exception ex) { |
|
509 |
logger.warn("Can't execute " + title + " in home.", ex); |
|
510 |
} |
|
511 |
sortScripts(scripts); |
|
512 |
executeScripts(scripts, null); |
|
513 |
|
|
514 |
} finally { |
|
515 |
logger.info("Running " + title + " scripts terminated."); |
|
516 |
dialogs.message("", JOptionPane.INFORMATION_MESSAGE); |
|
517 |
executingAutorunScripts = false; |
|
518 |
} |
|
519 |
} |
|
520 |
|
|
521 |
} |
|
522 |
} |
org.gvsig.scripting/tags/org.gvsig.scripting-2.3.68/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/ScriptingMain.java | ||
---|---|---|
1 |
package org.gvsig.scripting.app.extension; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.net.URL; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.HashMap; |
|
7 |
import java.util.List; |
|
8 |
import java.util.Map; |
|
9 |
import javax.swing.UIManager; |
|
10 |
import org.apache.commons.cli.CommandLine; |
|
11 |
import org.apache.commons.cli.CommandLineParser; |
|
12 |
import org.apache.commons.cli.Options; |
|
13 |
import org.apache.commons.cli.PosixParser; |
|
14 |
import org.apache.commons.io.FileUtils; |
|
15 |
import org.gvsig.about.AboutLocator; |
|
16 |
import org.gvsig.about.AboutManager; |
|
17 |
import org.gvsig.scripting.ScriptingManager; |
|
18 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
|
19 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
|
20 |
import org.gvsig.tools.ToolsLocator; |
|
21 |
import org.gvsig.tools.exception.BaseException; |
|
22 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
|
23 |
import org.gvsig.tools.packageutils.PackageInfo; |
|
24 |
import org.gvsig.tools.packageutils.PackageManager; |
|
25 |
import org.gvsig.tools.packageutils.Version; |
|
26 |
|
|
27 |
/** |
|
28 |
* |
|
29 |
* @author jjdelcerro |
|
30 |
*/ |
|
31 |
public class ScriptingMain { |
|
32 |
|
|
33 |
private File applicationFolder = null; |
|
34 |
private Version applicacionVersion = null; |
|
35 |
private List<File> pluginsFolder = null; |
|
36 |
private File applicationHomeFolder; |
|
37 |
private File installFolder; |
|
38 |
private int applicacionBuildNumber; |
|
39 |
|
|
40 |
private ScriptingUtils utils; |
|
41 |
|
|
42 |
public static void main(String args[]) throws Exception { |
|
43 |
ScriptingMain main = new ScriptingMain(); |
|
44 |
main.doMain(args); |
|
45 |
} |
|
46 |
|
|
47 |
public File getApplicationFolder() { |
|
48 |
if (applicationFolder == null) { |
|
49 |
applicationFolder = new File(System.getProperty("user.dir")); |
|
50 |
} |
|
51 |
return applicationFolder; |
|
52 |
} |
|
53 |
|
|
54 |
public File getApplicationHomeFolder() { |
|
55 |
if (applicationHomeFolder == null) { |
|
56 |
applicationHomeFolder = new File(System.getProperty("user.home")); |
|
57 |
} |
|
58 |
return applicationHomeFolder; |
|
59 |
} |
|
60 |
|
|
61 |
public File getInstallFolder() { |
|
62 |
if (installFolder == null) { |
|
63 |
installFolder = FileUtils.getFile(getApplicationFolder(), "install"); |
|
64 |
} |
|
65 |
return this.installFolder; |
|
66 |
} |
|
67 |
|
|
68 |
public Version getApplicationVersion() throws BaseException { |
|
69 |
if (applicacionVersion == null) { |
|
70 |
PackageManager packageManager = ToolsLocator.getPackageManager(); |
|
71 |
PackageInfo packageInfo = packageManager.createPackageInfo( |
|
72 |
new File(this.applicationFolder, "package.info") |
|
73 |
); |
|
74 |
applicacionVersion = (Version) packageInfo.getVersion(); |
|
75 |
} |
|
76 |
return applicacionVersion; |
|
77 |
} |
|
78 |
|
|
79 |
public int getApplicationBuildNumber() throws BaseException { |
|
80 |
if (applicacionBuildNumber == 0) { |
|
81 |
applicacionBuildNumber = getApplicationVersion().getBuild(); |
|
82 |
} |
|
83 |
return this.applicacionBuildNumber; |
|
84 |
} |
|
85 |
|
|
86 |
public List<File> getPluginsFolder() { |
|
87 |
List<File> folders = new ArrayList<>(); |
|
88 |
folders.add(FileUtils.getFile(this.getApplicationFolder(), "gvSIG", "extensiones")); |
|
89 |
folders.add(FileUtils.getFile(this.getApplicationHomeFolder(), "gvSIG", "installation", "gvSIG", "extensiones")); |
|
90 |
return folders; |
|
91 |
} |
|
92 |
|
|
93 |
public File getPluginHomeFolder() { |
|
94 |
File f = this.getApplicationHomeFolder(); |
|
95 |
f = FileUtils.getFile(f, "gvSIG", "plugins", "org.gvsig.scripting.app.mainplugin"); |
|
96 |
return f; |
|
97 |
} |
|
98 |
|
|
99 |
private void initializeAbout() throws Exception { |
|
100 |
AboutManager aboutManager = AboutLocator.getManager(); |
|
101 |
|
|
102 |
String build = String.valueOf(this.getApplicationBuildNumber()); |
|
103 |
String version = this.getApplicationVersion().toString(); |
|
104 |
|
|
105 |
Map<String, String> gvsig_vars = new HashMap<>(); |
|
106 |
gvsig_vars.put("version", version); |
|
107 |
gvsig_vars.put("build", build); |
|
108 |
aboutManager.setProject( |
|
109 |
"gvSIG Desktop", |
|
110 |
getResource("gvsig.html"), |
|
111 |
null, |
|
112 |
gvsig_vars); |
|
113 |
aboutManager.getProject().set("build", build); |
|
114 |
|
|
115 |
aboutManager.addSponsor( |
|
116 |
"Generalitat Valenciana", |
|
117 |
getResource("gva.html"), |
|
118 |
1); |
|
119 |
|
|
120 |
aboutManager.addDeveloper( |
|
121 |
"gvSIG Asocicion", |
|
122 |
getResource("gvSIG-asociacion.html"), |
|
123 |
2); |
|
124 |
|
|
125 |
} |
|
126 |
|
|
127 |
public void doMain(String args[]) throws Exception { |
|
128 |
|
|
129 |
String lookAndFeel = "com.jgoodies.looks.plastic.PlasticXPLookAndFeel"; |
|
130 |
try { |
|
131 |
UIManager.setLookAndFeel(lookAndFeel); |
|
132 |
} catch(Throwable th) { |
|
133 |
|
|
134 |
} |
|
135 |
|
|
136 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
137 |
|
|
138 |
initializeAbout(); |
|
139 |
|
|
140 |
this.utils = new ScriptingUtils(); |
|
141 |
this.utils.initializaPaths( |
|
142 |
this.getPluginsFolder(), |
|
143 |
this.getInstallFolder(), |
|
144 |
this.getPluginHomeFolder(), |
|
145 |
this.getApplicationVersion().format("%M.%m.%r") |
|
146 |
); |
|
147 |
|
|
148 |
ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
|
149 |
ScriptingManager manager = uimanager.getManager(); |
|
150 |
|
|
151 |
Options options = new Options(); |
|
152 |
options.addOption("d", |
|
153 |
"debug", |
|
154 |
false, |
|
155 |
""); |
|
156 |
options.addOption("p", |
|
157 |
"pause", |
|
158 |
false, |
|
159 |
""); |
|
160 |
options.addOption("l", |
|
161 |
"launcher", |
|
162 |
false, |
|
163 |
"Show the launcher window"); |
|
164 |
options.addOption("c", |
|
165 |
"composer", |
|
166 |
false, |
|
167 |
"Show the composer window"); |
|
168 |
options.addOption("r", |
|
169 |
"run", |
|
170 |
true, |
|
171 |
"Run the specified script"); |
|
172 |
options.addOption("h", |
|
173 |
"home", |
|
174 |
true, |
|
175 |
"Specify for the home of the script framework"); |
|
176 |
CommandLineParser parser = new PosixParser(); |
|
177 |
CommandLine line = null; |
|
178 |
boolean runLauncherAtEnd = true; |
|
179 |
|
|
180 |
line = parser.parse(options, args); |
|
181 |
if (line.hasOption("home")) { |
|
182 |
manager.setHomeFolder(new File(line.getOptionValue("home"))); |
|
183 |
} |
|
184 |
if (line.hasOption("composer")) { |
|
185 |
runLauncherAtEnd = false; |
|
186 |
utils.runComposer(); |
|
187 |
} |
|
188 |
if (line.hasOption("run")) { |
|
189 |
runLauncherAtEnd = false; |
|
190 |
utils.runScript(line.getOptionValue("run"), line.getArgs()); |
|
191 |
} |
|
192 |
if( runLauncherAtEnd ) { |
|
193 |
utils.runLauncher(); |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
private URL getResource(String name) { |
|
198 |
URL resource = this.getClass().getResource(name); |
|
199 |
return resource; |
|
200 |
} |
|
201 |
|
|
202 |
} |
org.gvsig.scripting/tags/org.gvsig.scripting-2.3.68/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/ScriptsInstallerInitializer.java | ||
---|---|---|
1 |
package org.gvsig.scripting.app.extension; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import org.apache.commons.io.FileUtils; |
|
5 |
import org.gvsig.andami.PluginServices; |
|
6 |
import org.gvsig.andami.PluginsLocator; |
|
7 |
import org.gvsig.andami.PluginsManager; |
|
8 |
import org.gvsig.installer.lib.api.InstallerLocator; |
|
9 |
import org.gvsig.installer.lib.api.InstallerManager; |
|
10 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
|
11 |
import org.gvsig.scripting.ScriptingLocator; |
|
12 |
import org.gvsig.scripting.ScriptingManager; |
|
13 |
import org.gvsig.tools.service.spi.ProviderFactory; |
|
14 |
import org.gvsig.tools.service.spi.ProviderManager; |
|
15 |
import org.gvsig.tools.util.Invocable; |
|
16 |
import org.slf4j.Logger; |
|
17 |
import org.slf4j.LoggerFactory; |
|
18 |
|
|
19 |
public class ScriptsInstallerInitializer implements Invocable { |
|
20 |
|
|
21 |
public static final String VERSION_FORMAT = "%M.%m.%r"; |
|
22 |
|
|
23 |
private static final Logger LOG = LoggerFactory.getLogger(ScriptsInstallerInitializer.class); |
|
24 |
|
|
25 |
@Override |
|
26 |
public Object call(Object... args) { |
|
27 |
// Cuando se hace la llamada a este metodo, aun no esta inicializado gvSIG. |
|
28 |
// Los servicios que ofrece el PluginsManager no son completos, solo algunos |
|
29 |
// de ellos estan disponibles. La lista de plugins aun no se ha cargado. |
|
30 |
try { |
|
31 |
String pluginName = (String) args[0]; |
|
32 |
|
|
33 |
ProviderManager providerManager = InstallerProviderLocator.getProviderManager(); |
|
34 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
|
35 |
ScriptingManager manager = ScriptingLocator.getManager(); |
|
36 |
InstallerManager installerManager = InstallerLocator.getInstallerManager(); |
|
37 |
File pluginHomeFolder = pluginManager.getPluginHomeFolder(pluginName); |
|
38 |
|
|
39 |
String gvsigversion = pluginManager.getApplicationVersion().format(VERSION_FORMAT) ; |
|
40 |
File home = ScriptingUtils.getScriptsHomeFolder(pluginHomeFolder, gvsigversion); |
|
41 |
|
|
42 |
manager.setHomeFolder(home); |
|
43 |
manager.setPackagesFolder(pluginManager.getInstallFolder()); |
|
44 |
|
|
45 |
File localAddonRepositoryFolder = new File(manager.getRootUserFolder(), "addons"); |
|
46 |
|
|
47 |
ProviderFactory factory = manager.getInstallerFactory(); |
|
48 |
providerManager.addProviderFactory(factory); |
|
49 |
|
|
50 |
installerManager.setDefaultLocalAddonRepository( |
|
51 |
localAddonRepositoryFolder, |
|
52 |
factory.getName() |
|
53 |
); |
|
54 |
|
|
55 |
return true; |
|
56 |
|
|
57 |
} catch (Exception ex) { |
|
58 |
LOG.warn("Problems initializing scripts installer.",ex); |
|
59 |
return false; |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
} |
org.gvsig.scripting/tags/org.gvsig.scripting-2.3.68/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/messagewait/MessageWait.java | ||
---|---|---|
1 |
package org.gvsig.scripting.app.extension.messagewait; |
|
2 |
|
|
3 |
import java.awt.Dimension; |
|
4 |
import java.awt.event.ActionEvent; |
|
5 |
import java.awt.event.ActionListener; |
|
6 |
import java.net.URL; |
|
7 |
import javax.swing.ImageIcon; |
|
8 |
import javax.swing.SwingUtilities; |
|
9 |
import org.gvsig.tools.ToolsLocator; |
|
10 |
import org.gvsig.tools.i18n.I18nManager; |
|
11 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
12 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
|
13 |
|
|
14 |
|
|
15 |
public class MessageWait extends MessageWaitView { |
|
16 |
|
|
17 |
private static final long serialVersionUID = 6668980523307140920L; |
|
18 |
private WaitingCondition condition; |
|
19 |
private int seconds = 3; |
|
20 |
private boolean cancelled = false; |
|
21 |
private Runnable action; |
|
22 |
|
|
23 |
public interface WaitingCondition { |
|
24 |
|
|
25 |
public boolean stopWaiting(); |
|
26 |
} |
|
27 |
|
|
28 |
public MessageWait() { |
|
29 |
this.btnCancel.addActionListener(new ActionListener() { |
|
30 |
@Override |
|
31 |
public void actionPerformed(ActionEvent e) { |
|
32 |
cancelled = true; |
|
33 |
setVisible(false); |
|
34 |
} |
|
35 |
}); |
|
36 |
URL url = this.getClass().getResource("/images/spinner.gif"); |
|
37 |
ImageIcon icon = new ImageIcon(url); |
|
38 |
this.lblIcon.setIcon(icon); |
|
39 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
40 |
this.btnCancel.setText(i18n.getTranslation("Cancel")); |
|
41 |
} |
|
42 |
|
|
43 |
public void showMessage(String title, String msg, WaitingCondition condition, Runnable action) { |
|
44 |
this.showMessage(title, msg, condition, action, 300, -1, 3); |
|
45 |
} |
|
46 |
|
|
47 |
public void showMessage(String title, String msg, WaitingCondition condition, Runnable action, int width, int height, int seconds) { |
|
48 |
if( condition.stopWaiting() ) { |
|
49 |
SwingUtilities.invokeLater(action); |
|
50 |
return; |
|
51 |
} |
|
52 |
this.setVisible(true); |
|
53 |
this.condition = condition; |
|
54 |
this.seconds = seconds; |
|
55 |
this.action = action; |
|
56 |
this.cancelled = false; |
|
57 |
this.lblMsg.setText("<html>"+msg.replace("\n", "<br>\n")+"</html>"); |
|
58 |
Dimension d = this.getPreferredSize(); |
|
59 |
if( height<d.height ) { |
|
60 |
height = d.height; |
|
61 |
} |
|
62 |
if( width<d.width ) { |
|
63 |
width = d.width; |
|
64 |
} |
|
65 |
this.setPreferredSize(new Dimension(width, height)); |
|
66 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
|
67 |
winmgr.showWindow(this, title, WindowManager.MODE.TOOL); |
|
68 |
Thread th = new Thread(new Runnable() { |
|
69 |
@Override |
|
70 |
public void run() { |
|
71 |
doWait(); |
|
72 |
} |
|
73 |
}, "MessageWait"); |
|
74 |
th.start(); |
|
75 |
} |
|
76 |
|
|
77 |
private void doWait() { |
|
78 |
while( true ) { |
|
79 |
if( this.condition.stopWaiting() ) { |
|
80 |
break; |
|
81 |
} |
|
82 |
try { |
|
83 |
Thread.sleep(this.seconds*1000); |
|
84 |
} catch (InterruptedException ex) { |
|
85 |
|
|
86 |
} |
|
87 |
} |
|
88 |
if( this.cancelled ) { |
|
89 |
return; |
|
90 |
} |
|
91 |
SwingUtilities.invokeLater(new Runnable() { |
|
92 |
@Override |
|
93 |
public void run() { |
|
94 |
setVisible(false); |
|
95 |
action.run(); |
|
96 |
} |
|
97 |
}); |
|
98 |
} |
|
99 |
} |
org.gvsig.scripting/tags/org.gvsig.scripting-2.3.68/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/messagewait/MessageWaitView.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
|
|
3 |
<object classname="com.jeta.forms.store.memento.FormPackage"> |
|
4 |
<at name="fileversion"> |
|
5 |
<object classname="com.jeta.forms.store.memento.FormsVersion2"> |
|
6 |
<at name="major">2</at> |
|
7 |
<at name="minor">0</at> |
|
8 |
<at name="sub">0</at> |
|
9 |
</object> |
|
10 |
</at> |
|
11 |
<at name="form"> |
|
12 |
<object classname="com.jeta.forms.store.memento.FormMemento"> |
|
13 |
<super classname="com.jeta.forms.store.memento.ComponentMemento"> |
|
14 |
<at name="cellconstraints"> |
|
15 |
<object classname="com.jeta.forms.store.memento.CellConstraintsMemento"> |
|
16 |
<at name="column">1</at> |
|
17 |
<at name="row">1</at> |
|
18 |
<at name="colspan">1</at> |
|
19 |
<at name="rowspan">1</at> |
|
20 |
<at name="halign">default</at> |
|
21 |
<at name="valign">default</at> |
|
22 |
<at name="insets" object="insets">0,0,0,0</at> |
|
23 |
</object> |
|
24 |
</at> |
|
25 |
<at name="componentclass">com.jeta.forms.gui.form.FormComponent</at> |
|
26 |
</super> |
|
27 |
<at name="id">/home/jjdelcerro/datos/devel/org.gvsig.scripting/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/messagewait/MessageWaitView.xml</at> |
|
28 |
<at name="rowspecs">CENTER:4DLU:NONE,FILL:DEFAULT:GROW(1.0),CENTER:4DLU:NONE,CENTER:DEFAULT:NONE,CENTER:4DLU:NONE</at> |
|
29 |
<at name="colspecs">FILL:4DLU:NONE,FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:GROW(1.0),FILL:4DLU:NONE</at> |
|
30 |
<at name="components"> |
|
31 |
<object classname="java.util.LinkedList"> |
|
32 |
<item > |
|
33 |
<at name="value"> |
|
34 |
<object classname="com.jeta.forms.store.memento.BeanMemento"> |
|
35 |
<super classname="com.jeta.forms.store.memento.ComponentMemento"> |
|
36 |
<at name="cellconstraints"> |
|
37 |
<object classname="com.jeta.forms.store.memento.CellConstraintsMemento"> |
|
38 |
<at name="column">4</at> |
|
39 |
<at name="row">2</at> |
|
40 |
<at name="colspan">1</at> |
|
41 |
<at name="rowspan">1</at> |
|
42 |
<at name="halign">left</at> |
|
43 |
<at name="valign">top</at> |
|
44 |
<at name="insets" object="insets">0,0,0,0</at> |
|
45 |
</object> |
|
46 |
</at> |
|
47 |
<at name="componentclass">com.jeta.forms.gui.form.StandardComponent</at> |
|
48 |
</super> |
|
49 |
<at name="jetabeanclass">com.jeta.forms.gui.beans.JETABean</at> |
|
50 |
<at name="beanclass">com.jeta.forms.components.label.JETALabel</at> |
|
51 |
<at name="beanproperties"> |
|
52 |
<object classname="com.jeta.forms.store.memento.PropertiesMemento"> |
|
53 |
<at name="classname">com.jeta.forms.components.label.JETALabel</at> |
|
54 |
<at name="properties"> |
|
55 |
<object classname="com.jeta.forms.store.support.PropertyMap"> |
|
56 |
<at name="border"> |
|
57 |
<object classname="com.jeta.forms.store.properties.CompoundBorderProperty"> |
|
58 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
59 |
<at name="name">border</at> |
|
60 |
</super> |
|
61 |
<at name="borders"> |
|
62 |
<object classname="java.util.LinkedList"> |
|
63 |
<item > |
|
64 |
<at name="value"> |
|
65 |
<object classname="com.jeta.forms.store.properties.DefaultBorderProperty"> |
|
66 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
67 |
<at name="name">border</at> |
|
68 |
</super> |
|
69 |
</object> |
|
70 |
</at> |
|
71 |
</item> |
|
72 |
</object> |
|
73 |
</at> |
|
74 |
</object> |
|
75 |
</at> |
|
76 |
<at name="name">lblMsg</at> |
|
77 |
<at name="width">12</at> |
|
78 |
<at name="fill"> |
|
79 |
<object classname="com.jeta.forms.store.properties.effects.PaintProperty"> |
|
80 |
<at name="name">fill</at> |
|
81 |
</object> |
|
82 |
</at> |
|
83 |
<at name="height">12</at> |
|
84 |
</object> |
|
85 |
</at> |
|
86 |
</object> |
|
87 |
</at> |
|
88 |
</object> |
|
89 |
</at> |
|
90 |
</item> |
|
91 |
<item > |
|
92 |
<at name="value"> |
|
93 |
<object classname="com.jeta.forms.store.memento.BeanMemento"> |
|
94 |
<super classname="com.jeta.forms.store.memento.ComponentMemento"> |
|
95 |
<at name="cellconstraints"> |
|
96 |
<object classname="com.jeta.forms.store.memento.CellConstraintsMemento"> |
|
97 |
<at name="column">4</at> |
|
98 |
<at name="row">4</at> |
|
99 |
<at name="colspan">1</at> |
|
100 |
<at name="rowspan">1</at> |
|
101 |
<at name="halign">right</at> |
|
102 |
<at name="valign">default</at> |
|
103 |
<at name="insets" object="insets">0,0,0,0</at> |
|
104 |
</object> |
|
105 |
</at> |
|
106 |
<at name="componentclass">com.jeta.forms.gui.form.StandardComponent</at> |
|
107 |
</super> |
|
108 |
<at name="jetabeanclass">com.jeta.forms.gui.beans.JETABean</at> |
|
109 |
<at name="beanclass">javax.swing.JButton</at> |
|
110 |
<at name="beanproperties"> |
|
111 |
<object classname="com.jeta.forms.store.memento.PropertiesMemento"> |
|
112 |
<at name="classname">javax.swing.JButton</at> |
|
113 |
<at name="properties"> |
|
114 |
<object classname="com.jeta.forms.store.support.PropertyMap"> |
|
115 |
<at name="border"> |
|
116 |
<object classname="com.jeta.forms.store.properties.CompoundBorderProperty"> |
|
117 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
118 |
<at name="name">border</at> |
|
119 |
</super> |
|
120 |
<at name="borders"> |
|
121 |
<object classname="java.util.LinkedList"> |
|
122 |
<item > |
|
123 |
<at name="value"> |
|
124 |
<object classname="com.jeta.forms.store.properties.DefaultBorderProperty"> |
|
125 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
126 |
<at name="name">border</at> |
|
127 |
</super> |
|
128 |
</object> |
|
129 |
</at> |
|
130 |
</item> |
|
131 |
</object> |
|
132 |
</at> |
|
133 |
</object> |
|
134 |
</at> |
|
135 |
<at name="actionCommand">_Cancel</at> |
|
136 |
<at name="name">btnCancel</at> |
|
137 |
<at name="width">76</at> |
|
138 |
<at name="text">_Cancel</at> |
|
139 |
<at name="height">22</at> |
|
140 |
</object> |
|
141 |
</at> |
|
142 |
</object> |
|
143 |
</at> |
|
144 |
</object> |
|
145 |
</at> |
|
146 |
</item> |
|
147 |
<item > |
|
148 |
<at name="value"> |
|
149 |
<object classname="com.jeta.forms.store.memento.BeanMemento"> |
|
150 |
<super classname="com.jeta.forms.store.memento.ComponentMemento"> |
|
151 |
<at name="cellconstraints"> |
|
152 |
<object classname="com.jeta.forms.store.memento.CellConstraintsMemento"> |
|
153 |
<at name="column">2</at> |
|
154 |
<at name="row">2</at> |
|
155 |
<at name="colspan">1</at> |
|
156 |
<at name="rowspan">1</at> |
|
157 |
<at name="halign">center</at> |
|
158 |
<at name="valign">top</at> |
|
159 |
<at name="insets" object="insets">0,0,0,0</at> |
|
160 |
</object> |
|
161 |
</at> |
|
162 |
<at name="componentclass">com.jeta.forms.gui.form.StandardComponent</at> |
|
163 |
</super> |
|
164 |
<at name="jetabeanclass">com.jeta.forms.gui.beans.JETABean</at> |
|
165 |
<at name="beanclass">com.jeta.forms.components.label.JETALabel</at> |
|
166 |
<at name="beanproperties"> |
|
167 |
<object classname="com.jeta.forms.store.memento.PropertiesMemento"> |
|
168 |
<at name="classname">com.jeta.forms.components.label.JETALabel</at> |
|
169 |
<at name="properties"> |
|
170 |
<object classname="com.jeta.forms.store.support.PropertyMap"> |
|
171 |
<at name="border"> |
|
172 |
<object classname="com.jeta.forms.store.properties.CompoundBorderProperty"> |
|
173 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
174 |
<at name="name">border</at> |
|
175 |
</super> |
|
176 |
<at name="borders"> |
|
177 |
<object classname="java.util.LinkedList"> |
|
178 |
<item > |
|
179 |
<at name="value"> |
|
180 |
<object classname="com.jeta.forms.store.properties.DefaultBorderProperty"> |
|
181 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
182 |
<at name="name">border</at> |
|
183 |
</super> |
|
184 |
</object> |
|
185 |
</at> |
|
186 |
</item> |
|
187 |
</object> |
|
188 |
</at> |
|
189 |
</object> |
|
190 |
</at> |
|
191 |
<at name="name">lblIcon</at> |
|
192 |
<at name="width">12</at> |
|
193 |
<at name="fill"> |
|
194 |
<object classname="com.jeta.forms.store.properties.effects.PaintProperty"> |
|
195 |
<at name="name">fill</at> |
|
196 |
</object> |
|
197 |
</at> |
|
198 |
<at name="height">12</at> |
|
199 |
</object> |
|
200 |
</at> |
|
201 |
</object> |
|
202 |
</at> |
|
203 |
</object> |
|
204 |
</at> |
|
205 |
</item> |
|
206 |
</object> |
|
207 |
</at> |
|
208 |
<at name="properties"> |
|
209 |
<object classname="com.jeta.forms.store.memento.PropertiesMemento"> |
|
210 |
<at name="classname">com.jeta.forms.gui.form.GridView</at> |
|
211 |
<at name="properties"> |
|
212 |
<object classname="com.jeta.forms.store.support.PropertyMap"> |
|
213 |
<at name="border"> |
|
214 |
<object classname="com.jeta.forms.store.properties.CompoundBorderProperty"> |
|
215 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
216 |
<at name="name">border</at> |
|
217 |
</super> |
|
218 |
<at name="borders"> |
|
219 |
<object classname="java.util.LinkedList"/> |
|
220 |
</at> |
|
221 |
</object> |
|
222 |
</at> |
|
223 |
<at name="name"></at> |
|
224 |
<at name="fill"> |
|
225 |
<object classname="com.jeta.forms.store.properties.effects.PaintProperty"> |
|
226 |
<at name="name">fill</at> |
|
227 |
</object> |
|
228 |
</at> |
|
229 |
<at name="scollBars"> |
|
230 |
<object classname="com.jeta.forms.store.properties.ScrollBarsProperty"> |
|
231 |
<at name="name">scollBars</at> |
|
232 |
<at name="verticalpolicy">21</at> |
|
233 |
<at name="horizontalpolicy">31</at> |
|
234 |
<at name="border"> |
|
235 |
<object classname="com.jeta.forms.store.properties.CompoundBorderProperty"> |
|
236 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
237 |
<at name="name">border</at> |
|
238 |
</super> |
|
239 |
<at name="borders"> |
|
240 |
<object classname="java.util.LinkedList"> |
|
241 |
<item > |
|
242 |
<at name="value"> |
|
243 |
<object classname="com.jeta.forms.store.properties.DefaultBorderProperty"> |
|
244 |
<super classname="com.jeta.forms.store.properties.BorderProperty"> |
|
245 |
<at name="name">border</at> |
|
246 |
</super> |
|
247 |
</object> |
|
248 |
</at> |
|
249 |
</item> |
|
250 |
</object> |
|
251 |
</at> |
|
252 |
</object> |
|
253 |
</at> |
|
254 |
</object> |
|
255 |
</at> |
|
256 |
</object> |
|
257 |
</at> |
|
258 |
</object> |
|
259 |
</at> |
|
260 |
<at name="cellpainters"> |
|
261 |
<object classname="com.jeta.forms.store.support.Matrix"> |
|
262 |
<at name="rows"> |
|
263 |
<object classname="[Ljava.lang.Object;" size="5"> |
|
264 |
<at name="item" index="0"> |
|
265 |
<object classname="[Ljava.lang.Object;" size="5"/> |
|
266 |
</at> |
|
267 |
<at name="item" index="1"> |
|
268 |
<object classname="[Ljava.lang.Object;" size="5"/> |
|
269 |
</at> |
|
270 |
<at name="item" index="2"> |
|
271 |
<object classname="[Ljava.lang.Object;" size="5"/> |
|
272 |
</at> |
|
273 |
<at name="item" index="3"> |
|
274 |
<object classname="[Ljava.lang.Object;" size="5"/> |
|
275 |
</at> |
|
276 |
<at name="item" index="4"> |
|
277 |
<object classname="[Ljava.lang.Object;" size="5"/> |
|
278 |
</at> |
|
279 |
</object> |
|
280 |
</at> |
|
281 |
</object> |
|
282 |
</at> |
|
283 |
<at name="rowgroups"> |
|
284 |
<object classname="com.jeta.forms.store.memento.FormGroupSet"> |
|
285 |
<at name="groups"> |
|
286 |
<object classname="java.util.HashMap"/> |
|
287 |
</at> |
|
288 |
</object> |
|
289 |
</at> |
|
290 |
<at name="colgroups"> |
|
291 |
<object classname="com.jeta.forms.store.memento.FormGroupSet"> |
|
292 |
<at name="groups"> |
|
293 |
<object classname="java.util.HashMap"/> |
|
294 |
</at> |
|
295 |
</object> |
|
296 |
</at> |
|
297 |
</object> |
|
298 |
</at> |
|
299 |
</object> |
org.gvsig.scripting/tags/org.gvsig.scripting-2.3.68/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/messagewait/MessageWaitView.java | ||
---|---|---|
1 |
package org.gvsig.scripting.app.extension.messagewait; |
|
2 |
|
|
3 |
import com.jeta.open.i18n.I18NUtils; |
|
4 |
import com.jgoodies.forms.layout.CellConstraints; |
|
5 |
import com.jgoodies.forms.layout.FormLayout; |
|
6 |
import java.awt.BorderLayout; |
|
7 |
import java.awt.ComponentOrientation; |
|
8 |
import java.awt.Container; |
|
9 |
import java.awt.Dimension; |
|
10 |
import javax.swing.Box; |
|
11 |
import javax.swing.ImageIcon; |
|
12 |
import javax.swing.JButton; |
|
13 |
import javax.swing.JFrame; |
|
14 |
import javax.swing.JLabel; |
|
15 |
import javax.swing.JPanel; |
|
16 |
|
|
17 |
|
|
18 |
public class MessageWaitView extends JPanel |
|
19 |
{ |
|
20 |
JLabel lblMsg = new JLabel(); |
|
21 |
JButton btnCancel = new JButton(); |
|
22 |
JLabel lblIcon = new JLabel(); |
|
23 |
|
|
24 |
/** |
|
25 |
* Default constructor |
|
26 |
*/ |
|
27 |
public MessageWaitView() |
|
28 |
{ |
|
29 |
initializePanel(); |
|
30 |
} |
|
31 |
|
|
32 |
/** |
|
33 |
* Adds fill components to empty cells in the first row and first column of the grid. |
|
34 |
* This ensures that the grid spacing will be the same as shown in the designer. |
|
35 |
* @param cols an array of column indices in the first row where fill components should be added. |
|
36 |
* @param rows an array of row indices in the first column where fill components should be added. |
|
37 |
*/ |
|
38 |
void addFillComponents( Container panel, int[] cols, int[] rows ) |
|
39 |
{ |
|
40 |
Dimension filler = new Dimension(10,10); |
|
41 |
|
|
42 |
boolean filled_cell_11 = false; |
|
43 |
CellConstraints cc = new CellConstraints(); |
|
44 |
if ( cols.length > 0 && rows.length > 0 ) |
|
45 |
{ |
|
46 |
if ( cols[0] == 1 && rows[0] == 1 ) |
|
47 |
{ |
|
48 |
/** add a rigid area */ |
|
49 |
panel.add( Box.createRigidArea( filler ), cc.xy(1,1) ); |
|
50 |
filled_cell_11 = true; |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
for( int index = 0; index < cols.length; index++ ) |
|
55 |
{ |
|
56 |
if ( cols[index] == 1 && filled_cell_11 ) |
|
57 |
{ |
|
58 |
continue; |
|
59 |
} |
|
60 |
panel.add( Box.createRigidArea( filler ), cc.xy(cols[index],1) ); |
|
61 |
} |
|
62 |
|
|
63 |
for( int index = 0; index < rows.length; index++ ) |
|
64 |
{ |
|
65 |
if ( rows[index] == 1 && filled_cell_11 ) |
|
66 |
{ |
|
67 |
continue; |
|
68 |
} |
|
69 |
panel.add( Box.createRigidArea( filler ), cc.xy(1,rows[index]) ); |
|
70 |
} |
|
71 |
|
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* Helper method to load an image file from the CLASSPATH |
|
76 |
* @param imageName the package and name of the file to load relative to the CLASSPATH |
|
77 |
* @return an ImageIcon instance with the specified image file |
|
78 |
* @throws IllegalArgumentException if the image resource cannot be loaded. |
|
79 |
*/ |
|
80 |
public ImageIcon loadImage( String imageName ) |
|
81 |
{ |
|
82 |
try |
|
83 |
{ |
|
84 |
ClassLoader classloader = getClass().getClassLoader(); |
|
85 |
java.net.URL url = classloader.getResource( imageName ); |
|
86 |
if ( url != null ) |
|
87 |
{ |
|
88 |
ImageIcon icon = new ImageIcon( url ); |
|
89 |
return icon; |
|
90 |
} |
|
91 |
} |
|
92 |
catch( Exception e ) |
|
93 |
{ |
|
94 |
e.printStackTrace(); |
|
95 |
} |
|
96 |
throw new IllegalArgumentException( "Unable to load image: " + imageName ); |
|
97 |
} |
|
98 |
|
|
99 |
/** |
|
100 |
* Method for recalculating the component orientation for |
|
101 |
* right-to-left Locales. |
|
102 |
* @param orientation the component orientation to be applied |
|
103 |
*/ |
|
104 |
public void applyComponentOrientation( ComponentOrientation orientation ) |
|
105 |
{ |
|
106 |
// Not yet implemented... |
|
107 |
// I18NUtils.applyComponentOrientation(this, orientation); |
|
108 |
super.applyComponentOrientation(orientation); |
|
109 |
} |
|
110 |
|
|
111 |
public JPanel createPanel() |
|
112 |
{ |
|
113 |
JPanel jpanel1 = new JPanel(); |
|
114 |
FormLayout formlayout1 = new FormLayout("FILL:4DLU:NONE,FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:GROW(1.0),FILL:4DLU:NONE","CENTER:4DLU:NONE,FILL:DEFAULT:GROW(1.0),CENTER:4DLU:NONE,CENTER:DEFAULT:NONE,CENTER:4DLU:NONE"); |
|
115 |
CellConstraints cc = new CellConstraints(); |
|
116 |
jpanel1.setLayout(formlayout1); |
|
117 |
|
|
118 |
lblMsg.setName("lblMsg"); |
|
119 |
jpanel1.add(lblMsg,new CellConstraints(4,2,1,1,CellConstraints.LEFT,CellConstraints.TOP)); |
|
120 |
|
|
121 |
btnCancel.setActionCommand("_Cancel"); |
|
122 |
btnCancel.setName("btnCancel"); |
|
123 |
btnCancel.setText("_Cancel"); |
|
124 |
jpanel1.add(btnCancel,new CellConstraints(4,4,1,1,CellConstraints.RIGHT,CellConstraints.DEFAULT)); |
|
125 |
|
|
126 |
lblIcon.setName("lblIcon"); |
|
127 |
jpanel1.add(lblIcon,new CellConstraints(2,2,1,1,CellConstraints.CENTER,CellConstraints.TOP)); |
|
128 |
|
|
129 |
addFillComponents(jpanel1,new int[]{ 1,2,3,4,5 },new int[]{ 1,2,3,4,5 }); |
|
130 |
return jpanel1; |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* Initializer |
|
135 |
*/ |
|
136 |
protected void initializePanel() |
|
137 |
{ |
|
138 |
setLayout(new BorderLayout()); |
|
139 |
add(createPanel(), BorderLayout.CENTER); |
|
140 |
} |
|
141 |
|
|
142 |
|
|
143 |
} |
org.gvsig.scripting/tags/org.gvsig.scripting-2.3.68/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/java/org/gvsig/scripting/app/extension/ScriptLaunchExtension.java | ||
---|---|---|
1 |
package org.gvsig.scripting.app.extension; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.Arrays; |
|
6 |
import java.util.List; |
|
7 |
import javax.swing.JOptionPane; |
|
8 |
import org.gvsig.andami.PluginsLocator; |
|
9 |
import org.gvsig.andami.PluginsManager; |
|
10 |
import org.gvsig.andami.plugins.Extension; |
|
11 |
import org.gvsig.app.ApplicationLocator; |
|
12 |
import org.gvsig.app.ApplicationManager; |
|
13 |
import org.gvsig.scripting.ScriptingLocator; |
|
14 |
import org.gvsig.scripting.ScriptingManager; |
|
15 |
import org.gvsig.scripting.ScriptingScript; |
|
16 |
import org.slf4j.Logger; |
|
17 |
import org.slf4j.LoggerFactory; |
|
18 |
|
|
19 |
public class ScriptLaunchExtension extends Extension { |
|
20 |
|
|
21 |
private static final Logger logger = LoggerFactory.getLogger(ScriptLaunchExtension.class); |
|
22 |
|
|
23 |
@Override |
|
24 |
public void initialize() { |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public boolean isEnabled() { |
|
29 |
return true; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public boolean isVisible() { |
|
34 |
return true; |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public boolean canQueryByAction() { |
|
39 |
return true; |
|
40 |
} |
|
41 |
|
|
42 |
@Override |
|
43 |
public void execute(String action) { |
|
44 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
45 |
ScriptingScript script = getScript(action); |
|
46 |
if( script == null ) { |
|
47 |
application.messageDialog( |
|
48 |
"Can't locate script '"+action+"'", |
|
49 |
"Warning", |
|
50 |
JOptionPane.WARNING_MESSAGE |
|
51 |
); |
|
52 |
return; |
|
53 |
} |
|
54 |
try { |
|
55 |
script.runAsTask(null); |
|
56 |
} catch (Throwable th) { |
|
57 |
logger.warn("Can't execute '" + action + "' script.", th); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public void execute(String action, Object[] args) { |
|
63 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
64 |
ScriptingScript script = getScript(action); |
|
65 |
if( script == null ) { |
|
66 |
application.messageDialog( |
|
67 |
"Can't locate script '"+action+"'", |
|
68 |
"Warning", |
|
69 |
JOptionPane.WARNING_MESSAGE |
|
70 |
); |
|
71 |
return; |
|
72 |
} |
|
73 |
try { |
|
74 |
script.runAsTask(args); |
|
75 |
} catch (Throwable th) { |
|
76 |
logger.warn("Can't execute '" + action + "' script.", th); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public boolean isVisible(String action) { |
|
82 |
ScriptingScript script = getScript(action); |
|
83 |
try { |
|
84 |
Boolean value = (Boolean) script.invokeFunction("isVisible", null); |
|
85 |
return value; |
|
86 |
} catch (Throwable th) { |
|
87 |
logger.warn("Can't execute isVisible in '" + action + "' script.", th); |
|
88 |
} |
|
89 |
return false; |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public boolean isEnabled(String action) { |
|
94 |
ScriptingScript script = getScript(action); |
|
95 |
try { |
|
96 |
Boolean value = (Boolean) script.invokeFunction("isEnabled", null); |
|
97 |
return value; |
|
98 |
} catch (Throwable th) { |
|
99 |
logger.warn("Can't execute isEnabled in '" + action + "' script.", th); |
|
100 |
} |
|
101 |
return false; |
|
102 |
} |
|
103 |
|
|
104 |
private ScriptingScript getScript(String scriptpathname) { |
|
105 |
final ScriptingManager manager = ScriptingLocator.getManager(); |
|
106 |
final PluginsManager pluginManager = PluginsLocator.getManager(); |
|
107 |
|
|
108 |
File scriptfile; |
|
109 |
ScriptingScript script; |
|
110 |
|
|
111 |
String s = manager.getRootUserFolder().getAbsolutePath().replace("\\", "/"); |
|
112 |
scriptfile = new File(s+"/"+scriptpathname); |
|
113 |
if (scriptfile.exists()) { |
|
114 |
script = (ScriptingScript) manager.getScript(scriptfile); |
|
115 |
return script; |
|
116 |
} |
|
117 |
|
|
118 |
final List<File> pluginsFolders = new ArrayList<>(); |
|
119 |
for (File f : pluginManager.getPluginsFolders()) { |
|
120 |
pluginsFolders.addAll(Arrays.asList(f.listFiles())); |
|
121 |
} |
|
122 |
|
|
123 |
for (File pluginFolder : pluginsFolders) { |
|
124 |
s = pluginFolder.getAbsolutePath().replace("\\", "/"); |
|
125 |
scriptfile = new File(s+"/scripting/scripts/"+scriptpathname); |
|
126 |
if (scriptfile.exists()) { |
|
127 |
script = (ScriptingScript) manager.getScript(scriptfile); |
|
128 |
return script; |
|
129 |
} |
|
130 |
} |
|
131 |
return (ScriptingScript) manager.getScript(scriptpathname); |
|
132 |
} |
|
133 |
|
|
134 |
} |
Also available in: Unified diff