Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_892 / extensions / extScripting / src / org / gvsig / scripting / ScriptPanel.java @ 10278

History | View | Annotate | Download (5.6 KB)

1 5554 jmvivo
package org.gvsig.scripting;
2
3
import java.awt.GridLayout;
4
import java.io.File;
5 6679 jmvivo
import java.io.FileReader;
6 5554 jmvivo
import java.io.IOException;
7 5565 jmvivo
import java.net.MalformedURLException;
8 5554 jmvivo
import java.net.URI;
9
import java.net.URISyntaxException;
10
import java.net.URL;
11
import java.util.Map;
12
13
import javax.swing.JPanel;
14
15 6679 jmvivo
import org.apache.bsf.BSFEngine;
16 5554 jmvivo
import org.apache.bsf.BSFException;
17
18
import thinlet.script.ScriptableThinlet;
19
20 5565 jmvivo
import com.iver.andami.PluginServices;
21 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
22 6880 cesar
import com.iver.andami.ui.mdiManager.WindowInfo;
23 5554 jmvivo
24 6877 cesar
public class ScriptPanel extends JPanel implements IWindow {
25 5554 jmvivo
26 5741 jmvivo
    public static final String JAVASCRIPT = ScriptingExtension.JAVASCRIPT;
27
    public static final String BEANSHELL = ScriptingExtension.BEANSHELL;
28
    public static final String JYTHON = ScriptingExtension.JYTHON;
29
    public static final String GROOVY = ScriptingExtension.GROOVY;
30 5554 jmvivo
31 6880 cesar
        private WindowInfo viewInfo;
32 5554 jmvivo
        private String xmlFileName;
33 5741 jmvivo
        private String language;
34 5554 jmvivo
        private String title = null;
35 6880 cesar
        private int viewType = WindowInfo.RESIZABLE;
36 5782 jmvivo
        //private int viewType = ViewInfo.MODALDIALOG;
37 5554 jmvivo
        private int top = 0;
38
        private int left = 0;
39
        private int width = -1;
40
        private int height = -1;
41 5741 jmvivo
        private Map params;
42 5554 jmvivo
43 6770 jmvivo
        private DefaultThinlet thinlet = null;
44 5741 jmvivo
45 5554 jmvivo
        public ScriptPanel(Map params) throws BSFException, IOException {
46 5782 jmvivo
                super();
47
                this.params = params;
48 5554 jmvivo
                if (!params.containsKey("fileName")) {
49
                        throw new IOException(); //FIXME !!!!
50
                }
51
                this.xmlFileName = (String)params.get("fileName");
52
                if (!params.containsKey("language")) {
53
                        throw new IOException(); //FIXME !!!!
54
                }
55
                this.language = (String)params.get("language");
56
57
                this.title =(String)params.get("title");
58
59
                this.viewType = params.containsKey("viewType") ? ((Integer)params.get("viewType")).intValue(): this.viewType;
60
61
                this.top = params.containsKey("top") ? ((Integer)params.get("top")).intValue(): this.top;
62
                this.left = params.containsKey("left") ? ((Integer)params.get("left")).intValue(): this.left;
63
                this.width = params.containsKey("width") ? ((Integer)params.get("width")).intValue(): this.width;
64
                this.height = params.containsKey("height") ? ((Integer)params.get("height")).intValue(): this.height;
65
                this.initilize();
66
        }
67
68 5741 jmvivo
        /*
69 5554 jmvivo
        public ScriptPanel(String xmlFileName, String language) throws BSFException, IOException {
70
                super();
71
                this.xmlFileName = xmlFileName;
72
                this.language = language;
73
                this.initilize();
74
        }
75

76
        public ScriptPanel(String xmlFileName, String language, String title ) throws BSFException, IOException  {
77
                super();
78
                this.xmlFileName = xmlFileName;
79
                this.language = language;
80
                this.title = title;
81
                this.initilize();
82
        }
83

84
        public ScriptPanel(String xmlFileName, String language, String title,int viewType ) throws BSFException, IOException  {
85
                super();
86
                this.xmlFileName = xmlFileName;
87
                this.language = language;
88
                this.title = title;
89
                this.viewType = viewType;
90
                this.initilize();
91
        }
92 5741 jmvivo
        */
93 5554 jmvivo
94
        private void initilize() throws BSFException, IOException {
95
                this.setLayout(new GridLayout());
96
97
                /*
98
                if (this.top > 0 && this.left > 0) {
99
                        this.setLocation(this.top,this.left);
100
                }
101 5565 jmvivo
                */
102 5596 jmvivo
                File file = new File(this.xmlFileName);
103
                if (!file.exists()) {
104
                        throw new IOException("File "+ file.getAbsolutePath()+ " not found.");
105 5554 jmvivo
                }
106
                if (this.title == null) {
107 5596 jmvivo
                        this.title = this.xmlFileName;
108 5554 jmvivo
                }
109 6770 jmvivo
                this.thinlet = ScriptPanel.getNewScriptableThinletInstance(this.language);
110 5554 jmvivo
111 6679 jmvivo
                ScriptingExtension.getBSFManager().declareBean("params",this.params, this.params.getClass());
112
113
                BSFEngine bsfEngine = ScriptingExtension.getBSFManager().loadScriptingEngine(this.language);
114
115
                if (this.language.equals(JYTHON)) {
116
                        this.runStartupScripJython(bsfEngine);
117
                }
118
119 6770 jmvivo
                this.thinlet.add(this.thinlet.parse(file));
120 5741 jmvivo
121 5554 jmvivo
122 6770 jmvivo
                this.add(this.thinlet);
123 5554 jmvivo
124 6770 jmvivo
                this.thinlet.setVisible(true);
125 5554 jmvivo
                this.doLayout();
126 6204 jmvivo
                this.setVisible(true);
127
128
                /*
129 5554 jmvivo
                if (this.width > 0 && this.height > 0) {
130
                        this.setSize(this.width, this.height);
131
                }else {
132
                        this.setSize(thinlet.getWidth() ,thinlet.getHeight()+2);
133 5565 jmvivo
                }
134 6204 jmvivo
                */
135
136
137 5554 jmvivo
        }
138 6770 jmvivo
139
        public DefaultThinlet getThinlet() {
140
                return this.thinlet;
141
        }
142 5554 jmvivo
143 6880 cesar
        public WindowInfo getWindowInfo() {
144 5554 jmvivo
        if (viewInfo == null) {
145 6880 cesar
            viewInfo=new WindowInfo(this.viewType);
146 6204 jmvivo
            viewInfo.setTitle(this.title);
147
            viewInfo.setWidth(this.width);
148
            viewInfo.setHeight(this.height);
149 5554 jmvivo
        }
150
        return viewInfo;
151
        }
152 5565 jmvivo
153 5596 jmvivo
        protected static DefaultThinlet getNewScriptableThinletInstance(String language) throws BSFException {
154
                DefaultThinlet thinlet = new DefaultThinlet(language);
155 5741 jmvivo
                //thinlet.getBSFManager().setClassLoader(PluginServices.getPluginServices("com.iver.cit.gvsig").getClassLoader());
156 5565 jmvivo
                //thinlet.getBSFManager().setClassLoader(PluginServices.ge);
157
                return thinlet;
158
        }
159 5554 jmvivo
160 6679 jmvivo
        private void runStartupScripJython(BSFEngine bsfEngine) throws IOException, BSFException {
161
                String startUpFileName = ScriptingExtension.getScriptingAppAccesor().getScriptsDirectory();
162
                startUpFileName = startUpFileName + File.separatorChar+ "jython"+ File.separatorChar+"startup.py";
163
                File startupFile = new File(startUpFileName);
164
165
                if (!startupFile.exists()) {
166
                        throw new IOException("Startup File "+startUpFileName+" not found");
167
                        //return;
168
                }
169
                String startupCode = this.readFile(startupFile);
170
                bsfEngine.exec(startUpFileName, -1, -1, startupCode);
171
        }
172
173
        private String readFile(File aFile) throws IOException {
174
                StringBuffer fileContents = new StringBuffer();
175
                FileReader fileReader = new FileReader(aFile);
176
                int c;
177
                while ((c = fileReader.read()) > -1) {
178
                        fileContents.append((char)c);
179
                }
180
                fileReader.close();
181
                return fileContents.toString();
182
        }
183 6770 jmvivo
184
        public void close() {
185 6880 cesar
                PluginServices.getMDIManager().closeWindow(this);
186 6770 jmvivo
        }
187 6679 jmvivo
188 5554 jmvivo
}