root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / cmd / ScriptEditingPanel.java @ 124
History | View | Annotate | Download (5.56 KB)
1 |
package es.unex.sextante.gui.cmd; |
---|---|
2 |
|
3 |
import info.clearthought.layout.TableLayout; |
4 |
import info.clearthought.layout.TableLayoutConstants; |
5 |
|
6 |
import java.awt.Dimension; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.io.BufferedReader; |
10 |
import java.io.BufferedWriter; |
11 |
import java.io.File; |
12 |
import java.io.FileNotFoundException; |
13 |
import java.io.FileReader; |
14 |
import java.io.FileWriter; |
15 |
import java.io.IOException; |
16 |
import java.io.Writer; |
17 |
|
18 |
import javax.swing.BorderFactory; |
19 |
import javax.swing.JButton; |
20 |
import javax.swing.JFileChooser; |
21 |
import javax.swing.JPanel; |
22 |
import javax.swing.JScrollPane; |
23 |
import javax.swing.JTextArea; |
24 |
import javax.swing.ScrollPaneConstants; |
25 |
import javax.swing.border.BevelBorder; |
26 |
|
27 |
import es.unex.sextante.core.Sextante; |
28 |
import es.unex.sextante.gui.algorithm.GenericFileFilter; |
29 |
import es.unex.sextante.gui.core.SextanteGUI; |
30 |
import es.unex.sextante.gui.settings.SextanteScriptsSettings; |
31 |
|
32 |
public class ScriptEditingPanel |
33 |
extends
|
34 |
JPanel {
|
35 |
|
36 |
private String m_sFilename; |
37 |
private JButton jButtonOpen; |
38 |
private JTextArea jScriptTextArea; |
39 |
private JScrollPane jScrollPaneTextArea; |
40 |
private JButton jButtonSave; |
41 |
|
42 |
|
43 |
public ScriptEditingPanel() {
|
44 |
|
45 |
super();
|
46 |
|
47 |
initGUI(); |
48 |
|
49 |
} |
50 |
|
51 |
|
52 |
public ScriptEditingPanel(final String sScriptFilename) { |
53 |
|
54 |
super();
|
55 |
|
56 |
m_sFilename = sScriptFilename; |
57 |
|
58 |
initGUI(); |
59 |
|
60 |
openFromFile(new File(sScriptFilename)); |
61 |
|
62 |
} |
63 |
|
64 |
|
65 |
private void initGUI() { |
66 |
|
67 |
final TableLayout layout = new TableLayout(new double[][] { { TableLayoutConstants.FILL, 70.0, 5.0, 70.0, 5 }, |
68 |
{ TableLayoutConstants.FILL, 5.0, 25.0, 5.0 } }); |
69 |
layout.setHGap(5);
|
70 |
layout.setVGap(5);
|
71 |
|
72 |
this.setLayout(layout);
|
73 |
|
74 |
this.setPreferredSize(new Dimension(600, 500)); |
75 |
this.setSize(new Dimension(600, 500)); |
76 |
jScriptTextArea = new JTextArea(); |
77 |
jScriptTextArea.setEditable(true);
|
78 |
jScriptTextArea.setLineWrap(false);
|
79 |
jScrollPaneTextArea = new JScrollPane(jScriptTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, |
80 |
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
81 |
this.add(jScrollPaneTextArea, "0,0,4,0"); |
82 |
jScriptTextArea.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
83 |
|
84 |
jButtonOpen = new JButton(Sextante.getText("Open")); |
85 |
this.add(jButtonOpen, "1, 2"); |
86 |
jButtonOpen.addActionListener(new ActionListener() { |
87 |
public void actionPerformed(final ActionEvent e) { |
88 |
open(); |
89 |
} |
90 |
}); |
91 |
|
92 |
jButtonSave = new JButton(Sextante.getText("Save")); |
93 |
this.add(jButtonSave, "3, 2"); |
94 |
jButtonSave.addActionListener(new ActionListener() { |
95 |
public void actionPerformed(final ActionEvent e) { |
96 |
save(); |
97 |
} |
98 |
}); |
99 |
|
100 |
|
101 |
} |
102 |
|
103 |
|
104 |
protected void save() { |
105 |
|
106 |
final JFileChooser fc = new JFileChooser(); |
107 |
final GenericFileFilter filter = new GenericFileFilter("bsh", Sextante.getText("BeanShell_script")); |
108 |
fc.setFileFilter(filter); |
109 |
if (m_sFilename != null) { |
110 |
File file = null; |
111 |
if(m_sFilename.endsWith(".bsh")){ |
112 |
file = new File(m_sFilename); |
113 |
} else {
|
114 |
file = new File(m_sFilename+".bsh"); |
115 |
} |
116 |
fc.setSelectedFile(file); |
117 |
} |
118 |
else {
|
119 |
final String sFolder = SextanteGUI.getSettingParameterValue(SextanteScriptsSettings.SCRIPTS_FOLDER); |
120 |
fc.setCurrentDirectory(new File(sFolder)); |
121 |
} |
122 |
final int returnVal = fc.showSaveDialog(this); |
123 |
|
124 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
125 |
final File file = fc.getSelectedFile(); |
126 |
m_sFilename = file.getAbsolutePath(); |
127 |
if(!m_sFilename.endsWith(".bsh")){ |
128 |
m_sFilename = m_sFilename+".bsh";
|
129 |
} |
130 |
saveToFile(); |
131 |
SextanteGUI.updateAlgorithmProvider(ScriptAlgorithmProvider.class); |
132 |
SextanteGUI.getGUIFactory().updateToolbox(); |
133 |
} |
134 |
|
135 |
} |
136 |
|
137 |
|
138 |
private void saveToFile() { |
139 |
|
140 |
Writer output = null; |
141 |
try {
|
142 |
output = new BufferedWriter(new FileWriter(m_sFilename)); |
143 |
output.write(jScriptTextArea.getText()); |
144 |
|
145 |
} |
146 |
catch (final IOException e) { |
147 |
Sextante.addErrorToLog(e); |
148 |
} |
149 |
finally {
|
150 |
if (output != null) { |
151 |
try {
|
152 |
output.close(); |
153 |
} |
154 |
catch (final IOException e) { |
155 |
Sextante.addErrorToLog(e); |
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
|
161 |
} |
162 |
|
163 |
|
164 |
protected void open() { |
165 |
|
166 |
final JFileChooser fc = new JFileChooser(); |
167 |
|
168 |
final String sFolder = SextanteGUI.getSettingParameterValue(SextanteScriptsSettings.SCRIPTS_FOLDER); |
169 |
fc.setFileFilter(new GenericFileFilter("bsh", Sextante.getText("BeanShell_script"))); |
170 |
fc.setCurrentDirectory(new File(sFolder)); |
171 |
final int returnVal = fc.showOpenDialog(this); |
172 |
|
173 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
174 |
final File file = fc.getSelectedFile(); |
175 |
openFromFile(file); |
176 |
|
177 |
} |
178 |
|
179 |
|
180 |
} |
181 |
|
182 |
|
183 |
private void openFromFile(final File file) { |
184 |
|
185 |
|
186 |
BufferedReader input = null; |
187 |
try {
|
188 |
input = new BufferedReader(new FileReader(file)); |
189 |
final StringBuffer sText = new StringBuffer(); |
190 |
String sLine = null; |
191 |
while ((sLine = input.readLine()) != null) { |
192 |
sText.append(sLine + "\n");
|
193 |
} |
194 |
jScriptTextArea.setText(sText.toString()); |
195 |
} |
196 |
catch (final FileNotFoundException e) {} |
197 |
catch (final IOException e) {} |
198 |
finally {
|
199 |
try {
|
200 |
if (input != null) { |
201 |
input.close(); |
202 |
} |
203 |
} |
204 |
catch (final IOException e) { |
205 |
Sextante.addErrorToLog(e); |
206 |
} |
207 |
} |
208 |
|
209 |
|
210 |
} |
211 |
|
212 |
} |