root / org.gvsig.expressionfield / trunk / org.gvsig.expressionfield / src / main / java / org / gvsig / expressionfield / project / documents / table / gui / AdvancedPanel.java @ 43
History | View | Annotate | Download (6.58 KB)
1 |
package org.gvsig.expressionfield.project.documents.table.gui; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.io.File; |
7 |
import java.io.IOException; |
8 |
import javax.swing.JFileChooser; |
9 |
import javax.swing.JOptionPane; |
10 |
import org.apache.bsf.BSFException; |
11 |
import org.apache.bsf.BSFManager; |
12 |
import org.apache.commons.io.FileUtils; |
13 |
import org.apache.commons.lang.StringUtils; |
14 |
import org.gvsig.app.ApplicationLocator; |
15 |
import org.gvsig.app.ApplicationManager; |
16 |
import org.gvsig.expressionfield.ExpressionFieldExtension; |
17 |
import org.gvsig.tools.ToolsLocator; |
18 |
import org.gvsig.tools.i18n.I18nManager; |
19 |
import org.gvsig.utils.GenericFileFilter; |
20 |
import org.slf4j.Logger; |
21 |
import org.slf4j.LoggerFactory; |
22 |
import org.gvsig.expressionfield.project.documents.table.gui.syntaxhighlight.JRSyntaxTextArea; |
23 |
import org.gvsig.expressionfield.project.documents.table.gui.syntaxhighlight.SyntaxtHighlightTextComponent; |
24 |
|
25 |
public class AdvancedPanel extends AdvancedPanelLayout { |
26 |
|
27 |
private static Logger logger = LoggerFactory.getLogger(AdvancedPanel.class); |
28 |
|
29 |
private BSFManager interpreter = null; |
30 |
private SyntaxtHighlightTextComponent textArea = null; |
31 |
|
32 |
public AdvancedPanel(BSFManager interpreter) {
|
33 |
super();
|
34 |
this.interpreter = interpreter;
|
35 |
initComponents(); |
36 |
translate(); |
37 |
} |
38 |
|
39 |
private void translate() { |
40 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
41 |
this.butLoad.setToolTipText(i18nManager.getTranslation("_Load_script_from_file")); |
42 |
this.butSave.setToolTipText(i18nManager.getTranslation("_Save_script_to_file")); |
43 |
this.butTest.setToolTipText(i18nManager.getTranslation("_Run_script_for_testing")); |
44 |
} |
45 |
|
46 |
private void initComponents() { |
47 |
// this.butLoad.setIcon(...);
|
48 |
// this.butSave.setIcon(...);
|
49 |
// this.butTest.setIcon(...);
|
50 |
this.editorContainer.setLayout(new BorderLayout()); |
51 |
this.textArea = new JRSyntaxTextArea(); |
52 |
this.editorContainer.add(textArea.getJTextComponent(), BorderLayout.CENTER); |
53 |
|
54 |
this.butLoad.addActionListener(new ActionListener() { |
55 |
public void actionPerformed(ActionEvent ae) { |
56 |
doLoad(); |
57 |
} |
58 |
}); |
59 |
this.butSave.addActionListener(new ActionListener() { |
60 |
public void actionPerformed(ActionEvent ae) { |
61 |
doSave(); |
62 |
} |
63 |
}); |
64 |
this.butTest.addActionListener(new ActionListener() { |
65 |
public void actionPerformed(ActionEvent ae) { |
66 |
doTest(); |
67 |
} |
68 |
}); |
69 |
} |
70 |
|
71 |
private void doLoad() { |
72 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
73 |
ApplicationManager application = ApplicationLocator.getManager(); |
74 |
File files[] = application.showChooserDialog( |
75 |
i18nManager.getTranslation("_Seleccione_el_script_a_cargar"),
|
76 |
JFileChooser.OPEN_DIALOG,
|
77 |
JFileChooser.FILES_ONLY,
|
78 |
false,
|
79 |
null,
|
80 |
new GenericFileFilter(new String[]{"py", "txt"}, i18nManager.getTranslation("python")), |
81 |
false
|
82 |
); |
83 |
if (files != null && files.length >= 1) { |
84 |
String code;
|
85 |
try {
|
86 |
code = FileUtils.readFileToString(files[0]);
|
87 |
} catch (IOException ex) { |
88 |
logger.warn("Can't read file '" + files[0].getAbsolutePath() + "'.", ex); |
89 |
application.messageDialog( |
90 |
i18nManager.getTranslation("_No_se_puede_cargar_el_fichero_seleccionado"),
|
91 |
i18nManager.getTranslation("_Atencion"),
|
92 |
JOptionPane.WARNING_MESSAGE);
|
93 |
return;
|
94 |
} |
95 |
this.textArea.setText(code);
|
96 |
} |
97 |
} |
98 |
|
99 |
private void doSave() { |
100 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
101 |
ApplicationManager application = ApplicationLocator.getManager(); |
102 |
String code = this.getCode(); |
103 |
if (code == null) { |
104 |
application.messageDialog( |
105 |
i18nManager.getTranslation("_No_hay_ningun_script_cargado"),
|
106 |
i18nManager.getTranslation("_Informacion"),
|
107 |
JOptionPane.INFORMATION_MESSAGE);
|
108 |
return;
|
109 |
} |
110 |
File files[] = application.showChooserDialog( |
111 |
i18nManager.getTranslation("_Seleccione_el_script_a_cargar"),
|
112 |
JFileChooser.SAVE_DIALOG,
|
113 |
JFileChooser.FILES_ONLY,
|
114 |
false,
|
115 |
null,
|
116 |
new GenericFileFilter(new String[]{"py", "txt"}, i18nManager.getTranslation("python")), |
117 |
false
|
118 |
); |
119 |
if (files != null && files.length >= 1) { |
120 |
try {
|
121 |
FileUtils.writeStringToFile(files[0], code);
|
122 |
} catch (IOException ex) { |
123 |
logger.warn("Can't write file '" + files[0].getAbsolutePath() + "'.", ex); |
124 |
application.messageDialog( |
125 |
i18nManager.getTranslation("_No_se_puede_guardar_en_el_fichero_seleccionado"),
|
126 |
i18nManager.getTranslation("_Atencion"),
|
127 |
JOptionPane.WARNING_MESSAGE);
|
128 |
return;
|
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
private void doTest() { |
134 |
final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
135 |
final ApplicationManager application = ApplicationLocator.getManager();
|
136 |
String code = this.getCode(); |
137 |
if (code == null) { |
138 |
application.messageDialog( |
139 |
i18nManager.getTranslation("_No_hay_ningun_script_cargado"),
|
140 |
i18nManager.getTranslation("_Informacion"),
|
141 |
JOptionPane.INFORMATION_MESSAGE);
|
142 |
return;
|
143 |
} |
144 |
try {
|
145 |
interpreter.exec(ExpressionFieldExtension.JYTHON, null, -1, -1, code); |
146 |
} catch (BSFException ex) {
|
147 |
logger.warn("Can't execute script:\n" + code + "\n", ex); |
148 |
application.messageDialog( |
149 |
i18nManager.getTranslation("_Se_han_producido_errores") + "\n\n" + ex.getMessage(), |
150 |
i18nManager.getTranslation("_Atencion"),
|
151 |
JOptionPane.WARNING_MESSAGE);
|
152 |
return;
|
153 |
} |
154 |
application.messageDialog( |
155 |
i18nManager.getTranslation("_Ejecucion_correcta"),
|
156 |
i18nManager.getTranslation("_Informacion"),
|
157 |
JOptionPane.INFORMATION_MESSAGE);
|
158 |
} |
159 |
|
160 |
public String getCode() { |
161 |
String code = this.textArea.getText(); |
162 |
code = code.trim(); |
163 |
if (StringUtils.isBlank(code)) {
|
164 |
return null; |
165 |
} |
166 |
return code;
|
167 |
} |
168 |
|
169 |
} |