Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / java / org / gvsig / scripting / app / extension / ScriptingExtension.java @ 465

History | View | Annotate | Download (9.82 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.scripting.app.extension;
23

    
24
import java.io.File;
25
import java.io.IOException;
26
import java.nio.file.FileVisitResult;
27
import java.nio.file.Files;
28
import java.nio.file.Path;
29
import java.nio.file.Paths;
30
import java.nio.file.SimpleFileVisitor;
31
import java.nio.file.attribute.BasicFileAttributes;
32
import java.util.ArrayList;
33
import java.util.Arrays;
34
import java.util.List;
35

    
36
import javax.swing.JOptionPane;
37
import org.apache.commons.io.FilenameUtils;
38

    
39
import org.gvsig.andami.IconThemeHelper;
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.andami.PluginsLocator;
42
import org.gvsig.andami.PluginsManager;
43
import org.gvsig.andami.plugins.Extension;
44
import org.gvsig.app.ApplicationLocator;
45
import org.gvsig.app.ApplicationManager;
46
import org.gvsig.scripting.ScriptingBaseScript;
47
import org.gvsig.scripting.ScriptingFolder;
48
import org.gvsig.scripting.ScriptingHelpManager;
49
import org.gvsig.scripting.ScriptingLocator;
50
import org.gvsig.scripting.ScriptingManager;
51
import org.gvsig.scripting.swing.api.JScriptingComposer;
52
import org.gvsig.scripting.swing.api.ScriptingSwingLocator;
53
import org.gvsig.scripting.swing.api.ScriptingUIManager;
54
import org.gvsig.tools.dynobject.DynObject;
55
import org.gvsig.tools.swing.api.ToolsSwingLocator;
56
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
57
import org.gvsig.tools.swing.impl.windowmanager.DefaultWindowManager;
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

    
61
public class ScriptingExtension extends Extension {
62

    
63
    private static final Logger logger = LoggerFactory.getLogger(ScriptingExtension.class);
64

    
65
    public void execute(String actionCommand) {
66
        this.execute(actionCommand, null);
67
    }
68

    
69
    @Override
70
    public void execute(String command, Object[] args) {
71
        ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager();
72
        WindowManager winmanager = ToolsSwingLocator.getWindowManager();
73

    
74
        if ("tools-scripting-launcher".equalsIgnoreCase(command)) {
75
            winmanager.showWindow(uimanager.createLauncher(), uimanager.getTranslation("Scripting_Launcher"), WindowManager.MODE.TOOL);
76

    
77
        } else if ("tools-scripting-composer".equalsIgnoreCase(command)) {
78
            DynObject preferences = this.getPlugin().getPluginProperties();
79
            Boolean composerUseHisWindowManager = (Boolean) preferences.getDynValue("ComposerUseHisWindowManager");
80
            JScriptingComposer composer = uimanager.createComposer();
81
            if (composerUseHisWindowManager.booleanValue()) {
82
                winmanager = new DefaultWindowManager();
83
                composer.setWindowManager(winmanager);
84
                winmanager.showWindow(composer, uimanager.getTranslation("Scripting_Composer"), WindowManager.MODE.WINDOW);
85
            } else {
86
                winmanager.showWindow(composer, uimanager.getTranslation("Scripting_Composer"), WindowManager.MODE.WINDOW);
87
            }
88

    
89
        } else {
90
            ScriptingBaseScript script = uimanager.getManager().getScript(command);
91
            if (script != null) {
92
                script.run(args);
93
            } else {
94
                ApplicationManager application = ApplicationLocator.getManager();
95
                application.messageDialog("Can't locate script '" + command + "'.", "ScriptLaunch", JOptionPane.OK_OPTION);
96
            }
97
        }
98
    }
99

    
100
    public void initialize() {
101
        IconThemeHelper.registerIcon("action", "tools-scripting-launcher", this);
102
        IconThemeHelper.registerIcon("action", "tools-scripting-composer", this);
103
        IconThemeHelper.registerIcon("action", "tools-scripting-console-jython", this);
104
    }
105

    
106
    @Override
107
    public void postInitialize() {
108
        super.postInitialize();
109
        ScriptingManager manager = ScriptingLocator.getManager();
110
        PluginsManager pluginManager = PluginsLocator.getManager();
111

    
112
        File home = this.getPlugin().getPluginHomeFolder();
113
        manager.setHomeFolder(home);
114
        File home_1_0 = new File(home.getParentFile(),"org.gvsig.scripting.app.extension");
115
        File home_scripts_1_0 = new File(home_1_0,"scripts");
116
        manager.addAlternativeUserFolder(home_scripts_1_0, "Usuario (1.0)", "Scripts de usuario (1.0)");
117

    
118
        List<File> pluginsFolders = new ArrayList<File>();
119
        for (File f : pluginManager.getPluginsFolders()) {
120
            pluginsFolders.addAll(Arrays.asList(f.listFiles()));
121
        }
122

    
123
        for (File pluginFolder : pluginsFolders) {
124
            File scriptsFolder = new File(pluginFolder, "scripting/scripts");
125
            if (scriptsFolder.exists()) {
126
                manager.registerSystemFolder(pluginFolder.getName(), scriptsFolder);
127
            }
128
            File libFolder = new File(pluginFolder, "scripting/lib");
129
            if (libFolder.exists()) {
130
                manager.addLibFolder(libFolder);
131
            }
132
        }
133
        pluginManager.addStartupTask(
134
                "ExecuteAutorunScripts", 
135
                new ExecuteAutorunScriptsOnStartup(),
136
                true, 
137
                600
138
        );
139

    
140
        /*
141
         * 
142
         ProviderManager providerManager = InstallerProviderLocator.getProviderManager();
143
         providerManager.addProviderFactory(new ScriptingInstallerProviderFactory());
144
         */
145
    }
146

    
147
    public boolean isEnabled() {
148
        return true;
149
    }
150

    
151
    public boolean isVisible() {
152
        return true;
153
    }
154
    private static class ExecuteAutorunScriptsOnStartup implements Runnable {
155

    
156
        public void run() {
157
            Thread th = new Thread(new ExecuteAutorunScripts());
158
            th.start();
159
        }
160
    }
161
    
162
    private static class ExecuteAutorunScripts implements Runnable {
163

    
164
        public void run() {
165
            final ScriptingManager manager = ScriptingLocator.getManager();
166
            PluginsManager pluginManager = PluginsLocator.getManager();
167

    
168
            try {
169
                ScriptingHelpManager helpmanager = manager.getHelpManager();
170
                PluginServices plugin = pluginManager.getPlugin(this);
171
                File scriptingfolder = new File(plugin.getPluginDirectory(),"scripting");
172
                File helpfolder = new File(scriptingfolder,"help");
173
                for (File folder : helpfolder.listFiles() ) {
174
                    File helpindex = new File(folder, "IdeHelpIndex.xml");
175
                    try {
176
                        helpmanager.addMethods(helpindex.toURI().toURL());
177
                    } catch(Exception e) {
178
                        logger.info("Can't add metothods from '"+helpindex.getAbsolutePath()+"'.",e);
179
                    }
180
                }
181
            } catch(Exception e) {
182
               logger.info("Can't add metothods.",e);
183
            }
184
            
185
            List<File> pluginsFolders = new ArrayList<File>();
186
            for (File f : pluginManager.getPluginsFolders()) {
187
                pluginsFolders.addAll(Arrays.asList(f.listFiles()));
188
            }
189

    
190
            File autorun_file = null;
191
            ScriptingBaseScript autorun = null;
192
            for (File pluginFolder : pluginsFolders) {
193
                autorun_file = new File(pluginFolder, "scripting/scripts/autorun.inf");
194
                if (autorun_file.exists()) {
195
                    logger.info("running autorun script '"+autorun_file.getAbsolutePath()+"'.");
196
                    autorun = manager.getScript(autorun_file);
197
                    try {
198
                        autorun.run();
199
                    } catch (Exception ex) {
200
                        logger.warn("Can't execute autorun from '" + autorun_file.getAbsolutePath() + "'.", ex);
201
                    }
202
                }
203
            }
204

    
205
            SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
206
                @Override
207
                public FileVisitResult visitFile(Path path, BasicFileAttributes bfa) throws IOException {
208
                    File file = path.toFile();
209
                    if( "autorun.inf".equalsIgnoreCase(file.getName()) ) {
210
                        if (file.exists()) {
211
                            logger.info("running autorun script '"+file.getAbsolutePath()+"'.");
212
                            ScriptingBaseScript autorun = manager.getScript(file);
213
                            try {
214
                                autorun.run();
215
                            } catch (Exception ex) {
216
                                logger.warn("Can't execute autorun from '" + file.getAbsolutePath() + "'.", ex);
217
                            }
218
                        }
219
                    }
220
                    return FileVisitResult.CONTINUE;
221
                }
222
            };
223
            try {
224
                Files.walkFileTree(Paths.get(manager.getRootUserFolder().toURI()), visitor);
225
                List<ScriptingFolder> folders = manager.getAlternativeUserFolders();
226
                for (ScriptingFolder folder : folders) {
227
                    Files.walkFileTree(Paths.get(folder.getFile().toURI()), visitor);
228
                }
229
            } catch (Exception ex) {
230
                logger.warn("Can't execute autoruns in home.", ex);
231
            }
232
            logger.info("Running autorun scripts terminated.");
233

    
234
        }
235

    
236
    }
237
}