svn-gvsig-desktop / trunk / extensions / extScripting / src / org / gvsig / scripting / ScriptPanel.java @ 6880
History | View | Annotate | Download (5.6 KB)
1 |
package org.gvsig.scripting; |
---|---|
2 |
|
3 |
import java.awt.GridLayout; |
4 |
import java.io.File; |
5 |
import java.io.FileReader; |
6 |
import java.io.IOException; |
7 |
import java.net.MalformedURLException; |
8 |
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 |
import org.apache.bsf.BSFEngine; |
16 |
import org.apache.bsf.BSFException; |
17 |
|
18 |
import thinlet.script.ScriptableThinlet; |
19 |
|
20 |
import com.iver.andami.PluginServices; |
21 |
import com.iver.andami.ui.mdiManager.IWindow; |
22 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
23 |
|
24 |
public class ScriptPanel extends JPanel implements IWindow { |
25 |
|
26 |
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 |
|
31 |
private WindowInfo viewInfo;
|
32 |
private String xmlFileName; |
33 |
private String language; |
34 |
private String title = null; |
35 |
private int viewType = WindowInfo.RESIZABLE; |
36 |
//private int viewType = ViewInfo.MODALDIALOG;
|
37 |
private int top = 0; |
38 |
private int left = 0; |
39 |
private int width = -1; |
40 |
private int height = -1; |
41 |
private Map params; |
42 |
|
43 |
private DefaultThinlet thinlet = null; |
44 |
|
45 |
public ScriptPanel(Map params) throws BSFException, IOException { |
46 |
super();
|
47 |
this.params = params;
|
48 |
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 |
/*
|
69 |
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 |
*/
|
93 |
|
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 |
*/
|
102 |
File file = new File(this.xmlFileName); |
103 |
if (!file.exists()) {
|
104 |
throw new IOException("File "+ file.getAbsolutePath()+ " not found."); |
105 |
} |
106 |
if (this.title == null) { |
107 |
this.title = this.xmlFileName; |
108 |
} |
109 |
this.thinlet = ScriptPanel.getNewScriptableThinletInstance(this.language); |
110 |
|
111 |
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 |
this.thinlet.add(this.thinlet.parse(file)); |
120 |
|
121 |
|
122 |
this.add(this.thinlet); |
123 |
|
124 |
this.thinlet.setVisible(true); |
125 |
this.doLayout();
|
126 |
this.setVisible(true); |
127 |
|
128 |
/*
|
129 |
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 |
}
|
134 |
*/
|
135 |
|
136 |
|
137 |
} |
138 |
|
139 |
public DefaultThinlet getThinlet() {
|
140 |
return this.thinlet; |
141 |
} |
142 |
|
143 |
public WindowInfo getWindowInfo() {
|
144 |
if (viewInfo == null) { |
145 |
viewInfo=new WindowInfo(this.viewType); |
146 |
viewInfo.setTitle(this.title);
|
147 |
viewInfo.setWidth(this.width);
|
148 |
viewInfo.setHeight(this.height);
|
149 |
} |
150 |
return viewInfo;
|
151 |
} |
152 |
|
153 |
protected static DefaultThinlet getNewScriptableThinletInstance(String language) throws BSFException { |
154 |
DefaultThinlet thinlet = new DefaultThinlet(language);
|
155 |
//thinlet.getBSFManager().setClassLoader(PluginServices.getPluginServices("com.iver.cit.gvsig").getClassLoader());
|
156 |
//thinlet.getBSFManager().setClassLoader(PluginServices.ge);
|
157 |
return thinlet;
|
158 |
} |
159 |
|
160 |
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 |
|
184 |
public void close() { |
185 |
PluginServices.getMDIManager().closeWindow(this);
|
186 |
} |
187 |
|
188 |
} |