Statistics
| Revision:

root / trunk / extensions / extScripting / src / org / gvsig / scripting / ScriptPanel.java @ 24989

History | View | Annotate | Download (5.52 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.util.Map;
8

    
9
import javax.swing.JPanel;
10

    
11
import org.apache.bsf.BSFEngine;
12
import org.apache.bsf.BSFException;
13

    
14
import com.iver.andami.PluginServices;
15
import com.iver.andami.ui.mdiManager.IWindow;
16
import com.iver.andami.ui.mdiManager.WindowInfo;
17

    
18
public class ScriptPanel extends JPanel implements IWindow {
19
        
20
    public static final String JAVASCRIPT = ScriptingExtension.JAVASCRIPT;
21
    public static final String BEANSHELL = ScriptingExtension.BEANSHELL;
22
    public static final String JYTHON = ScriptingExtension.JYTHON;
23
    public static final String GROOVY = ScriptingExtension.GROOVY;
24
    
25
        private WindowInfo viewInfo;        
26
        private String xmlFileName;
27
        private String language;        
28
        private String title = null;
29
        private int viewType = WindowInfo.RESIZABLE;        
30
        //private int viewType = ViewInfo.MODALDIALOG;
31
        private int top = 0;
32
        private int left = 0;
33
        private int width = -1;
34
        private int height = -1;
35
        private Map params;
36
        
37
        private DefaultThinlet thinlet = null;
38
        
39
        public ScriptPanel(Map params) throws BSFException, IOException {
40
                super();
41
                this.params = params;
42
                if (!params.containsKey("fileName")) {
43
                        throw new IOException(); //FIXME !!!!
44
                }
45
                this.xmlFileName = (String)params.get("fileName");
46
                if (!params.containsKey("language")) {
47
                        throw new IOException(); //FIXME !!!!
48
                }
49
                this.language = (String)params.get("language");
50
                
51
                this.title =(String)params.get("title");
52
                
53
                this.viewType = params.containsKey("viewType") ? ((Integer)params.get("viewType")).intValue(): this.viewType;
54
                
55
                this.top = params.containsKey("top") ? ((Integer)params.get("top")).intValue(): this.top;
56
                this.left = params.containsKey("left") ? ((Integer)params.get("left")).intValue(): this.left;
57
                this.width = params.containsKey("width") ? ((Integer)params.get("width")).intValue(): this.width;
58
                this.height = params.containsKey("height") ? ((Integer)params.get("height")).intValue(): this.height;                
59
                this.initilize();                
60
        }
61

    
62
        /*
63
        public ScriptPanel(String xmlFileName, String language) throws BSFException, IOException {
64
                super();
65
                this.xmlFileName = xmlFileName;
66
                this.language = language;                
67
                this.initilize();
68
        }
69

70
        public ScriptPanel(String xmlFileName, String language, String title ) throws BSFException, IOException  {
71
                super();
72
                this.xmlFileName = xmlFileName;
73
                this.language = language;
74
                this.title = title;                
75
                this.initilize();
76
        }
77

78
        public ScriptPanel(String xmlFileName, String language, String title,int viewType ) throws BSFException, IOException  {
79
                super();
80
                this.xmlFileName = xmlFileName;
81
                this.language = language;                
82
                this.title = title;
83
                this.viewType = viewType;                
84
                this.initilize();
85
        }
86
        */
87

    
88
        private void initilize() throws BSFException, IOException {
89
                this.setLayout(new GridLayout());
90

    
91
                /*
92
                if (this.top > 0 && this.left > 0) {
93
                        this.setLocation(this.top,this.left);
94
                }
95
                */
96
                File file = new File(this.xmlFileName);
97
                if (!file.exists()) {
98
                        throw new IOException("File "+ file.getAbsolutePath()+ " not found.");
99
                }
100
                if (this.title == null) {
101
                        this.title = this.xmlFileName;
102
                }
103
                this.thinlet = ScriptPanel.getNewScriptableThinletInstance(this.language);                
104
                
105
                ScriptingExtension.getBSFManager().declareBean("params",this.params, this.params.getClass());
106
                
107
                BSFEngine bsfEngine = ScriptingExtension.getBSFManager().loadScriptingEngine(this.language);
108
                
109
                if (this.language.equals(JYTHON)) {
110
                        this.runStartupScripJython(bsfEngine);
111
                }
112
                
113
                this.thinlet.add(this.thinlet.parse(file));
114
                
115
                                
116
                this.add(this.thinlet);
117
                
118
                this.thinlet.setVisible(true);
119
                this.doLayout();
120
                this.setVisible(true);                
121
                                
122
                /*
123
                if (this.width > 0 && this.height > 0) {
124
                        this.setSize(this.width, this.height);
125
                }else {
126
                        this.setSize(thinlet.getWidth() ,thinlet.getHeight()+2);
127
                }
128
                */
129

    
130
                
131
        }
132
        
133
        public DefaultThinlet getThinlet() {
134
                return this.thinlet;
135
        }
136

    
137
        public WindowInfo getWindowInfo() {
138
        if (viewInfo == null) {                
139
            viewInfo=new WindowInfo(this.viewType);
140
            viewInfo.setTitle(this.title);
141
            viewInfo.setWidth(this.width);
142
            viewInfo.setHeight(this.height);
143
        }
144
        return viewInfo;
145
        }
146
        
147
        protected static DefaultThinlet getNewScriptableThinletInstance(String language) throws BSFException {
148
                DefaultThinlet thinlet = new DefaultThinlet(language);
149
                //thinlet.getBSFManager().setClassLoader(PluginServices.getPluginServices("com.iver.cit.gvsig").getClassLoader());
150
                //thinlet.getBSFManager().setClassLoader(PluginServices.ge);
151
                return thinlet;
152
        }
153

    
154
        private void runStartupScripJython(BSFEngine bsfEngine) throws IOException, BSFException {
155
                String startUpFileName = ScriptingExtension.getScriptingAppAccesor().getScriptsDirectory();
156
                startUpFileName = startUpFileName + File.separatorChar+ "jython"+ File.separatorChar+"startup.py";
157
                File startupFile = new File(startUpFileName);
158
                
159
                if (!startupFile.exists()) {
160
                        throw new IOException("Startup File "+startUpFileName+" not found");
161
                        //return;
162
                }
163
                String startupCode = this.readFile(startupFile);
164
                bsfEngine.exec(startUpFileName, -1, -1, startupCode);
165
        }
166
        
167
        private String readFile(File aFile) throws IOException {
168
                StringBuffer fileContents = new StringBuffer();
169
                FileReader fileReader = new FileReader(aFile);
170
                int c;
171
                while ((c = fileReader.read()) > -1) {
172
                        fileContents.append((char)c);
173
                }
174
                fileReader.close();
175
                return fileContents.toString();
176
        }
177
        
178
        public void close() {
179
                PluginServices.getMDIManager().closeWindow(this);
180
        }
181

    
182
        @Override
183
        public Object getWindowProfile() {
184
                return WindowInfo.TOOL_PROFILE;
185
        }
186

    
187
}