svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.swing / org.gvsig.expressionevaluator.swing.impl / src / main / java / org / gvsig / expressionevaluator / swing / impl / DefaultJExpressionBuilder.java @ 43983
History | View | Annotate | Download (22 KB)
1 | 43939 | jjdelcerro | package org.gvsig.expressionevaluator.swing.impl; |
---|---|---|---|
2 | |||
3 | 43983 | jjdelcerro | import java.awt.BorderLayout; |
4 | import java.awt.Component; |
||
5 | import java.awt.Dimension; |
||
6 | 43939 | jjdelcerro | import java.awt.event.ActionEvent; |
7 | import java.awt.event.ActionListener; |
||
8 | 43983 | jjdelcerro | import java.awt.event.KeyEvent; |
9 | import java.awt.event.KeyListener; |
||
10 | import java.net.URL; |
||
11 | import java.util.ArrayList; |
||
12 | import java.util.Collection; |
||
13 | import java.util.Collections; |
||
14 | import java.util.Date; |
||
15 | import java.util.HashMap; |
||
16 | 43939 | jjdelcerro | import java.util.List; |
17 | 43983 | jjdelcerro | import java.util.Objects; |
18 | import javax.swing.DefaultListCellRenderer; |
||
19 | import javax.swing.ImageIcon; |
||
20 | 43939 | jjdelcerro | import javax.swing.JComponent; |
21 | 43983 | jjdelcerro | import javax.swing.JLabel; |
22 | import javax.swing.JList; |
||
23 | import javax.swing.JTree; |
||
24 | 43939 | jjdelcerro | import javax.swing.SwingUtilities; |
25 | import javax.swing.event.ListSelectionEvent; |
||
26 | import javax.swing.event.ListSelectionListener; |
||
27 | import javax.swing.event.TreeModelListener; |
||
28 | import javax.swing.event.TreeSelectionEvent; |
||
29 | import javax.swing.event.TreeSelectionListener; |
||
30 | 43983 | jjdelcerro | import javax.swing.tree.DefaultTreeCellRenderer; |
31 | 43939 | jjdelcerro | import javax.swing.tree.TreeModel; |
32 | import javax.swing.tree.TreePath; |
||
33 | 43983 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
34 | 43939 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
35 | 43983 | jjdelcerro | import org.apache.commons.lang.StringEscapeUtils; |
36 | import org.gvsig.expressionevaluator.Expression; |
||
37 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
38 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
39 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
40 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
41 | import org.gvsig.expressionevaluator.swing.Element; |
||
42 | import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
||
43 | import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
||
44 | import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
||
45 | import org.gvsig.expressionevaluator.swing.Element.SimpleElement; |
||
46 | import org.gvsig.expressionevaluator.swing.Element.VariableElement; |
||
47 | import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
||
48 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
49 | import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
||
50 | 43983 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
51 | import org.gvsig.tools.script.Script; |
||
52 | 43939 | jjdelcerro | import org.gvsig.tools.swing.api.ListElement; |
53 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
54 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
55 | 43983 | jjdelcerro | import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
56 | import org.gvsig.tools.swing.api.script.ScriptSwingManager; |
||
57 | import org.gvsig.tools.swing.icontheme.IconTheme; |
||
58 | 43939 | jjdelcerro | |
59 | /**
|
||
60 | *
|
||
61 | * @author jjdelcerro
|
||
62 | */
|
||
63 | public class DefaultJExpressionBuilder |
||
64 | extends DefaultJExpressionBuilderView
|
||
65 | implements JExpressionBuilder {
|
||
66 | |||
67 | private final ExpressionEvaluatorSwingManager manager; |
||
68 | private GroupElement currentGroup;
|
||
69 | 43983 | jjdelcerro | private PickerController<List<Script>> scriptPicker; |
70 | private ScriptSwingManager.ScriptEditor scriptEditor;
|
||
71 | 43939 | jjdelcerro | |
72 | 43983 | jjdelcerro | private ExpressionBuilderConfig config;
|
73 | |||
74 | 43939 | jjdelcerro | private class ElementsTreeModel implements TreeModel { |
75 | |||
76 | 43970 | jjdelcerro | private final GroupElement root; |
77 | 43983 | jjdelcerro | |
78 | 43970 | jjdelcerro | public ElementsTreeModel(GroupElement root) {
|
79 | 43939 | jjdelcerro | this.root = root;
|
80 | } |
||
81 | 43983 | jjdelcerro | |
82 | 43939 | jjdelcerro | @Override
|
83 | public Object getRoot() { |
||
84 | return this.root; |
||
85 | } |
||
86 | |||
87 | 43970 | jjdelcerro | private List<Element> getChilds(final GroupElement node) { |
88 | 43983 | jjdelcerro | List<Element> x = new ArrayList<>(); |
89 | for (Element element : node.getElements()) { |
||
90 | if (element instanceof GroupElement) { |
||
91 | x.add(element); |
||
92 | } |
||
93 | } |
||
94 | return x; //node.getElements(); |
||
95 | 43939 | jjdelcerro | } |
96 | 43983 | jjdelcerro | |
97 | 43939 | jjdelcerro | @Override
|
98 | public Object getChild(Object parent, int index) { |
||
99 | 43983 | jjdelcerro | List<Element> childs = getChilds((GroupElement) parent); |
100 | Element child = childs.get(index);
|
||
101 | return child;
|
||
102 | 43939 | jjdelcerro | } |
103 | |||
104 | @Override
|
||
105 | public int getChildCount(Object parent) { |
||
106 | 43983 | jjdelcerro | List<Element> childs = getChilds((GroupElement) parent); |
107 | 43939 | jjdelcerro | return childs.size();
|
108 | } |
||
109 | |||
110 | @Override
|
||
111 | public boolean isLeaf(Object node) { |
||
112 | 43983 | jjdelcerro | if( node instanceof GroupElement ) { |
113 | return !((GroupElement) node).hasSubgroups();
|
||
114 | } |
||
115 | return false; |
||
116 | 43939 | jjdelcerro | } |
117 | |||
118 | @Override
|
||
119 | public void valueForPathChanged(TreePath path, Object newValue) { |
||
120 | } |
||
121 | |||
122 | @Override
|
||
123 | public int getIndexOfChild(Object parent, Object child) { |
||
124 | 43983 | jjdelcerro | List<Element> childs = getChilds((GroupElement) parent); |
125 | 43939 | jjdelcerro | int n = childs.indexOf(child);
|
126 | return n;
|
||
127 | } |
||
128 | |||
129 | @Override
|
||
130 | public void addTreeModelListener(TreeModelListener l) { |
||
131 | } |
||
132 | |||
133 | @Override
|
||
134 | public void removeTreeModelListener(TreeModelListener l) { |
||
135 | } |
||
136 | 43983 | jjdelcerro | |
137 | 43939 | jjdelcerro | } |
138 | 43983 | jjdelcerro | |
139 | public DefaultJExpressionBuilder(ExpressionEvaluatorSwingManager manager, ExpressionBuilderConfig config) {
|
||
140 | 43939 | jjdelcerro | this.manager = manager;
|
141 | 43983 | jjdelcerro | this.config = new DefaultExpressionBuilderConfig(manager); |
142 | 43939 | jjdelcerro | this.initComponents();
|
143 | } |
||
144 | 43983 | jjdelcerro | |
145 | @Override
|
||
146 | public ImageIcon loadImage(String imageName) { |
||
147 | String name = FilenameUtils.getBaseName(imageName);
|
||
148 | IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
||
149 | if (theme.exists(name)) {
|
||
150 | return theme.get(name);
|
||
151 | } |
||
152 | URL url = this.getClass().getResource(name + ".png"); |
||
153 | if (url == null) { |
||
154 | return null; |
||
155 | } |
||
156 | return new ImageIcon(url); |
||
157 | } |
||
158 | 43939 | jjdelcerro | |
159 | private void initComponents() { |
||
160 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
161 | 43983 | jjdelcerro | final IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
|
162 | 43939 | jjdelcerro | |
163 | toolsSwingManager.setDefaultPopupMenu(this.txtExpression);
|
||
164 | toolsSwingManager.setDefaultPopupMenu(this.txtGroupElement);
|
||
165 | toolsSwingManager.setDefaultPopupMenu(this.txtSimpleElementFilter);
|
||
166 | toolsSwingManager.setDefaultPopupMenu(this.txtDescription);
|
||
167 | 43983 | jjdelcerro | toolsSwingManager.addClearButton(this.txtExpression);
|
168 | toolsSwingManager.addClearButton(this.txtSimpleElementFilter, new ActionListener() { |
||
169 | @Override
|
||
170 | public void actionPerformed(ActionEvent e) { |
||
171 | txtSimpleElementFilter.setText("");
|
||
172 | doFilter(); |
||
173 | } |
||
174 | }); |
||
175 | 43939 | jjdelcerro | |
176 | 43983 | jjdelcerro | this.treeElements.setCellRenderer(new DefaultTreeCellRenderer() { |
177 | @Override
|
||
178 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
||
179 | JLabel label = (JLabel) super.getTreeCellRendererComponent(tree,value, selected, expanded, leaf, row, hasFocus); |
||
180 | if( value instanceof Element ) { |
||
181 | Element element = (Element) value; |
||
182 | label.setIcon(element.getIcon()); |
||
183 | label.setText(element.getName()); |
||
184 | } else {
|
||
185 | label.setIcon(null);
|
||
186 | label.setText(Objects.toString(value, ""));
|
||
187 | } |
||
188 | return label;
|
||
189 | } |
||
190 | }); |
||
191 | |||
192 | 43939 | jjdelcerro | this.treeElements.addTreeSelectionListener(new TreeSelectionListener() { |
193 | @Override
|
||
194 | public void valueChanged(TreeSelectionEvent e) { |
||
195 | TreePath path = e.getPath();
|
||
196 | Element element = (Element) path.getLastPathComponent(); |
||
197 | 43983 | jjdelcerro | if (element instanceof GroupElement) { |
198 | updateSimpleElementsList((GroupElement) element); |
||
199 | 43939 | jjdelcerro | } |
200 | } |
||
201 | }); |
||
202 | this.btnGroupElementInsert.addActionListener(new ActionListener() { |
||
203 | @Override
|
||
204 | public void actionPerformed(ActionEvent e) { |
||
205 | doInsert(txtGroupElement.getText()); |
||
206 | } |
||
207 | }); |
||
208 | this.btnSimpleElementInsert.addActionListener(new ActionListener() { |
||
209 | @Override
|
||
210 | public void actionPerformed(ActionEvent e) { |
||
211 | 43983 | jjdelcerro | ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
212 | if (element != null) { |
||
213 | doInsert(element.getValue()); |
||
214 | } |
||
215 | 43939 | jjdelcerro | } |
216 | }); |
||
217 | this.btnSimpleElementFilter.addActionListener(new ActionListener() { |
||
218 | @Override
|
||
219 | public void actionPerformed(ActionEvent e) { |
||
220 | doFilter(); |
||
221 | } |
||
222 | }); |
||
223 | 43983 | jjdelcerro | this.lstSimpleElement.setCellRenderer(new DefaultListCellRenderer() { |
224 | @Override
|
||
225 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
||
226 | JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
||
227 | Element element = null; |
||
228 | if( value instanceof ListElement ) { |
||
229 | if( ((ListElement) value).getValue() instanceof Element ) { |
||
230 | element = (Element) ((ListElement) value).getValue();
|
||
231 | } |
||
232 | } |
||
233 | if( element !=null ) { |
||
234 | label.setIcon(element.getIcon()); |
||
235 | label.setText(element.getName()); |
||
236 | } else {
|
||
237 | label.setIcon(null);
|
||
238 | label.setText(Objects.toString(value, ""));
|
||
239 | } |
||
240 | return label;
|
||
241 | } |
||
242 | }); |
||
243 | 43939 | jjdelcerro | this.lstSimpleElement.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
244 | @Override
|
||
245 | public void valueChanged(ListSelectionEvent e) { |
||
246 | 43983 | jjdelcerro | ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
247 | if (element != null) { |
||
248 | doSelectElement(element.getValue()); |
||
249 | } |
||
250 | 43939 | jjdelcerro | } |
251 | }); |
||
252 | this.btnSimpleElementSortDown.addActionListener(new ActionListener() { |
||
253 | @Override
|
||
254 | public void actionPerformed(ActionEvent e) { |
||
255 | FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
||
256 | 43983 | jjdelcerro | model.sort(false);
|
257 | 43939 | jjdelcerro | } |
258 | }); |
||
259 | this.btnSimpleElementSortUp.addActionListener(new ActionListener() { |
||
260 | @Override
|
||
261 | public void actionPerformed(ActionEvent e) { |
||
262 | FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
||
263 | 43983 | jjdelcerro | model.sort(true);
|
264 | 43939 | jjdelcerro | } |
265 | }); |
||
266 | 43983 | jjdelcerro | this.txtSimpleElementFilter.addKeyListener(new KeyListener() { |
267 | @Override
|
||
268 | public void keyTyped(KeyEvent e) { |
||
269 | if (e.getKeyChar() == '\n') { |
||
270 | doFilter(); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | @Override
|
||
275 | public void keyPressed(KeyEvent e) { |
||
276 | } |
||
277 | |||
278 | @Override
|
||
279 | public void keyReleased(KeyEvent e) { |
||
280 | } |
||
281 | }); |
||
282 | this.btnAdd.addActionListener(new ActionListener() { |
||
283 | @Override
|
||
284 | public void actionPerformed(ActionEvent e) { |
||
285 | doInsert(" + ");
|
||
286 | } |
||
287 | }); |
||
288 | this.btnDiv.addActionListener(new ActionListener() { |
||
289 | @Override
|
||
290 | public void actionPerformed(ActionEvent e) { |
||
291 | doInsert(" / ");
|
||
292 | } |
||
293 | }); |
||
294 | this.btnEq.addActionListener(new ActionListener() { |
||
295 | @Override
|
||
296 | public void actionPerformed(ActionEvent e) { |
||
297 | doInsert(" = ");
|
||
298 | } |
||
299 | }); |
||
300 | this.btnMult.addActionListener(new ActionListener() { |
||
301 | @Override
|
||
302 | public void actionPerformed(ActionEvent e) { |
||
303 | doInsert(" * ");
|
||
304 | } |
||
305 | }); |
||
306 | this.btnNeq.addActionListener(new ActionListener() { |
||
307 | @Override
|
||
308 | public void actionPerformed(ActionEvent e) { |
||
309 | doInsert(" <> ");
|
||
310 | } |
||
311 | }); |
||
312 | this.btnParentClose.addActionListener(new ActionListener() { |
||
313 | @Override
|
||
314 | public void actionPerformed(ActionEvent e) { |
||
315 | doInsert(" ) ");
|
||
316 | } |
||
317 | }); |
||
318 | this.btnParentOpen.addActionListener(new ActionListener() { |
||
319 | @Override
|
||
320 | public void actionPerformed(ActionEvent e) { |
||
321 | doInsert(" ( ");
|
||
322 | } |
||
323 | }); |
||
324 | this.btnSubst.addActionListener(new ActionListener() { |
||
325 | @Override
|
||
326 | public void actionPerformed(ActionEvent e) { |
||
327 | doInsert(" - ");
|
||
328 | } |
||
329 | }); |
||
330 | ScriptSwingManager scriptSwingManager = ToolsSwingLocator.getScriptSwingManager(); |
||
331 | this.scriptPicker = scriptSwingManager.createListScriptPickerController(
|
||
332 | cboPickerScripts, |
||
333 | btnPickerRemove, |
||
334 | btnPickerSelectScript |
||
335 | ); |
||
336 | this.scriptEditor = scriptSwingManager.createScriptEditor();
|
||
337 | this.pnlScriptEditorContainer.setLayout(new BorderLayout()); |
||
338 | this.pnlScriptEditorContainer.add(this.scriptEditor.asJComponent(), BorderLayout.CENTER); |
||
339 | |||
340 | this.setPreferredSize(new Dimension(750, 500)); |
||
341 | 43939 | jjdelcerro | } |
342 | 43983 | jjdelcerro | |
343 | private String removeCursorMark(String s) { |
||
344 | s = s.replace("{{", ""); |
||
345 | s = s.replace("}}", ""); |
||
346 | return s;
|
||
347 | } |
||
348 | |||
349 | 43939 | jjdelcerro | private void doInsert(String s) { |
350 | 43983 | jjdelcerro | this.txtExpression.replaceSelection(s);
|
351 | String ss = this.txtExpression.getText(); |
||
352 | int start = ss.indexOf("{{"); |
||
353 | int end = ss.indexOf("}}"); |
||
354 | if (start > 0 && end > 0) { |
||
355 | this.txtExpression.replaceRange("", end, end + 2); |
||
356 | this.txtExpression.replaceRange("", start, start + 2); |
||
357 | this.txtExpression.setSelectionStart(start);
|
||
358 | this.txtExpression.setSelectionEnd(end - 2); |
||
359 | } |
||
360 | this.txtExpression.requestFocusInWindow();
|
||
361 | 43939 | jjdelcerro | } |
362 | 43983 | jjdelcerro | |
363 | 43939 | jjdelcerro | private void doInsert(SimpleElement element) { |
364 | 43983 | jjdelcerro | if( element instanceof FunctionElement ) { |
365 | Function function = ((FunctionElement)element).getFunction(); |
||
366 | if (function == null) { |
||
367 | doInsert(element.getName()); |
||
368 | } else {
|
||
369 | doInsert(function.template() == null ? function.name() + "({{}})" : function.template()); |
||
370 | } |
||
371 | } else if( element instanceof ConstantElement) { |
||
372 | doInsert(" "+repr(((ConstantElement) element).getValue())+" "); |
||
373 | 43939 | jjdelcerro | } else {
|
374 | 43983 | jjdelcerro | doInsert(" "+element.getName()+" "); |
375 | 43939 | jjdelcerro | } |
376 | } |
||
377 | 43983 | jjdelcerro | |
378 | 43939 | jjdelcerro | private void doFilter() { |
379 | FilteredListModel model = (FilteredListModel) this.lstSimpleElement.getModel();
|
||
380 | model.setFilter(this.txtSimpleElementFilter.getText());
|
||
381 | } |
||
382 | 43983 | jjdelcerro | |
383 | 43939 | jjdelcerro | private void doSelectElement(SimpleElement element) { |
384 | StringBuilder html = new StringBuilder(); |
||
385 | html.append("<html>\n");
|
||
386 | 43983 | jjdelcerro | if( element instanceof ConstantElement ) { |
387 | Object value = ((ConstantElement)element).getValue();
|
||
388 | if( value == null ) { |
||
389 | html.append("<b>Constant</b><br>\n<br>\n");
|
||
390 | html.append("<b>Value</b> NULL<br>\n");
|
||
391 | } else {
|
||
392 | html.append("<b>Constant</b><br>\n<br>\n");
|
||
393 | html.append("<b>Value</b> ").append(repr(value)).append("<br>\n<br>\n"); |
||
394 | html.append("<b>Type</b> ").append(value.getClass().getSimpleName()).append("<br>\n<br>\n"); |
||
395 | } |
||
396 | |||
397 | } else if( element instanceof VariableElement ) { |
||
398 | VariableElement var = (VariableElement)element; |
||
399 | html.append("<b>").append(var.getLabel()).append("</b><br>\n<br>\n\n"); |
||
400 | html.append("<b>Source</b> ").append(var.getSource()).append("<br>\n<br>\n"); |
||
401 | html.append("<b>Name</b> ").append(var.getName()).append("<br>\n<br>\n"); |
||
402 | html.append("<b>Type</b> ").append(var.getTypeName()).append("<br>\n<br>\n"); |
||
403 | |||
404 | } else if( element instanceof FunctionElement ) { |
||
405 | Function function = ((FunctionElement)element).getFunction(); |
||
406 | if (function == null) { |
||
407 | html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
||
408 | html.append("<b>Return</b> Object<br>\n<br>\n");
|
||
409 | html.append("<b>Template:</b> ").append(element.getName()).append("()<br>\n<br>\n"); |
||
410 | } else {
|
||
411 | html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
||
412 | html.append("<b>Return</b> ");
|
||
413 | html.append(StringUtils.isEmpty(function.returnType())?"Objec": function.returnType());
|
||
414 | html.append("<br>\n<br>\n");
|
||
415 | html.append("<b>Template</b> ");
|
||
416 | html.append(StringUtils.isEmpty(function.template())?element.getName()+"()": removeCursorMark(function.template()));
|
||
417 | html.append("<br>\n<br>\n");
|
||
418 | |||
419 | 43939 | jjdelcerro | String[] args = function.descriptionArgs(); |
420 | 43983 | jjdelcerro | if (args != null) { |
421 | 43939 | jjdelcerro | html.append("<b>Arguments:</b> ").append("<ul>\n"); |
422 | for (String arg : args) { |
||
423 | html.append("<li>").append(arg).append("</li>\n"); |
||
424 | } |
||
425 | html.append("</ul>\n");
|
||
426 | html.append("<br>\n");
|
||
427 | } |
||
428 | 43983 | jjdelcerro | } |
429 | } else {
|
||
430 | html.append("<b>Generic element<br>\n<br>\n");
|
||
431 | html.append("<b>Name</b> ").append(element.getName()).append("</br>\n</br>\n"); |
||
432 | |||
433 | 43939 | jjdelcerro | } |
434 | 43983 | jjdelcerro | |
435 | if( element.getDescription()!=null ) { |
||
436 | html.append("<b>Description</b><br>\n");
|
||
437 | html.append(element.getDescription().replace("\n", "<br>")).append("<br>\n"); |
||
438 | } |
||
439 | 43939 | jjdelcerro | html.append("</html>\n");
|
440 | 43983 | jjdelcerro | this.txtDescription.setContentType("text/html"); |
441 | 43939 | jjdelcerro | this.txtDescription.setText(html.toString());
|
442 | 43983 | jjdelcerro | this.txtDescription.setCaretPosition(0); |
443 | 43939 | jjdelcerro | } |
444 | 43983 | jjdelcerro | |
445 | 43939 | jjdelcerro | private void updateSimpleElementsList(GroupElement group) { |
446 | FilteredListModel model = new FilteredListModel();
|
||
447 | model.addElement("Loading...");
|
||
448 | this.currentGroup = group;
|
||
449 | this.txtGroupElement.setText(group.getName());
|
||
450 | 43983 | jjdelcerro | this.txtSimpleElementFilter.setText(""); |
451 | this.txtDescription.setText(""); |
||
452 | if (group instanceof SimpleElement) { |
||
453 | 43939 | jjdelcerro | this.btnGroupElementInsert.setEnabled(true); |
454 | } else {
|
||
455 | this.btnGroupElementInsert.setEnabled(false); |
||
456 | } |
||
457 | this.lstSimpleElement.setEnabled(false); |
||
458 | this.lstSimpleElement.setModel(model);
|
||
459 | 43983 | jjdelcerro | |
460 | 43939 | jjdelcerro | Thread th = new Thread(new Runnable() { |
461 | @Override
|
||
462 | public void run() { |
||
463 | simpleElementsUpdater(); |
||
464 | } |
||
465 | }, "ExpressionBuilderSimpleElementsUpdater");
|
||
466 | th.start(); |
||
467 | } |
||
468 | |||
469 | private void simpleElementsUpdater() { |
||
470 | final GroupElement group = this.currentGroup; |
||
471 | 43983 | jjdelcerro | final FilteredListModel model = new FilteredListModel(); |
472 | 43939 | jjdelcerro | for (Element child : group) { |
473 | 43983 | jjdelcerro | if (group != this.currentGroup) { |
474 | 43939 | jjdelcerro | return;
|
475 | } |
||
476 | 43983 | jjdelcerro | if (child instanceof SimpleElement) { |
477 | String label = child.getName();
|
||
478 | Function function = child instanceof FunctionElement? ((FunctionElement)child).getFunction(): null; |
||
479 | if (function != null && !StringUtils.isEmpty(function.returnType())) { |
||
480 | label = "<html><b>" + label + "</b> return " + function.returnType() + "</html>"; |
||
481 | // label = label + " return " + function.returnType();
|
||
482 | 43939 | jjdelcerro | } |
483 | 43983 | jjdelcerro | model.addElement(new ListElement<>(label, child));
|
484 | 43939 | jjdelcerro | } |
485 | } |
||
486 | 43983 | jjdelcerro | model.sort(true);
|
487 | 43939 | jjdelcerro | SwingUtilities.invokeLater(new Runnable() { |
488 | @Override
|
||
489 | public void run() { |
||
490 | 43983 | jjdelcerro | if (group != currentGroup) {
|
491 | 43939 | jjdelcerro | return;
|
492 | } |
||
493 | 43983 | jjdelcerro | if( group.hasMoreElements() ) {
|
494 | lblSimpleElementsMsg.setText("More elements...");
|
||
495 | } else {
|
||
496 | lblSimpleElementsMsg.setText("");
|
||
497 | } |
||
498 | 43939 | jjdelcerro | lstSimpleElement.setEnabled(true);
|
499 | lstSimpleElement.setModel(model); |
||
500 | } |
||
501 | }); |
||
502 | 43983 | jjdelcerro | |
503 | 43939 | jjdelcerro | } |
504 | 43983 | jjdelcerro | |
505 | 43939 | jjdelcerro | @Override
|
506 | 43983 | jjdelcerro | public Expression getExpression() { |
507 | String phrase = this.txtExpression.getText(); |
||
508 | if (StringUtils.isEmpty(phrase)) {
|
||
509 | 43939 | jjdelcerro | return null; |
510 | } |
||
511 | 43983 | jjdelcerro | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
512 | expression.setPhrase(phrase); |
||
513 | Script script = ToolsLocator.getScriptManager().createScript("UserScript", "", "python"); |
||
514 | this.scriptEditor.fetch(script);
|
||
515 | if( !StringUtils.isEmpty(script.getCode()) ) {
|
||
516 | expression.setUserScript(script); |
||
517 | } |
||
518 | 43939 | jjdelcerro | return expression;
|
519 | } |
||
520 | |||
521 | @Override
|
||
522 | 43983 | jjdelcerro | public void setExpression(Expression expression) { |
523 | this.txtExpression.setText(expression.getPhrase());
|
||
524 | if( expression.getUserScript()!=null ) { |
||
525 | this.scriptEditor.set(expression.getUserScript());
|
||
526 | 43939 | jjdelcerro | } |
527 | 43983 | jjdelcerro | if( expression.getScripts()!=null ) { |
528 | this.scriptPicker.set(expression.getScripts().toList());
|
||
529 | } |
||
530 | 43939 | jjdelcerro | } |
531 | |||
532 | @Override
|
||
533 | 43983 | jjdelcerro | public int getSimpleElementsLimit() { |
534 | return this.config.getSimpleElementsLimit(); |
||
535 | } |
||
536 | |||
537 | @Override
|
||
538 | public void setSimpleElementsLimit(int limit) { |
||
539 | this.config.setSimpleElementsLimit(limit);
|
||
540 | } |
||
541 | |||
542 | @Override
|
||
543 | public List<Element> getElements() { |
||
544 | return this.config.getElements(); |
||
545 | } |
||
546 | |||
547 | @Override
|
||
548 | public Collection<SymbolTable> getSymbolTables() { |
||
549 | return this.config.getSymbolTables(); |
||
550 | } |
||
551 | |||
552 | @Override
|
||
553 | public void addSymbolTable(SymbolTable symbolTable) { |
||
554 | this.config.addSymbolTable(symbolTable);
|
||
555 | } |
||
556 | |||
557 | @Override
|
||
558 | public void removeAllSymbolTables() { |
||
559 | this.config.removeAllSymbolTables();
|
||
560 | } |
||
561 | |||
562 | |||
563 | @Override
|
||
564 | 43939 | jjdelcerro | public JComponent asJComponent() { |
565 | 43983 | jjdelcerro | TreeModel model = new ElementsTreeModel( |
566 | this.manager.createGroupElement("Elements", this.getElements()) |
||
567 | ); |
||
568 | this.treeElements.setModel(model);
|
||
569 | 43939 | jjdelcerro | return this; |
570 | } |
||
571 | 43983 | jjdelcerro | |
572 | private String repr(Object value) { |
||
573 | if (value == null) { |
||
574 | return "NULL"; |
||
575 | } else if (value instanceof Date) { |
||
576 | return "DATE('" + value + "')"; |
||
577 | } else if (value instanceof Boolean) { |
||
578 | return value.toString();
|
||
579 | } else if (value instanceof String) { |
||
580 | return "'" + StringEscapeUtils.escapeSql((String) value) + "'"; |
||
581 | } else {
|
||
582 | return StringEscapeUtils.escapeSql(value.toString());
|
||
583 | } |
||
584 | |||
585 | } |
||
586 | 43939 | jjdelcerro | |
587 | 43983 | jjdelcerro | } |