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 @ 44008
History | View | Annotate | Download (28.7 KB)
1 | 43939 | jjdelcerro | package org.gvsig.expressionevaluator.swing.impl; |
---|---|---|---|
2 | |||
3 | 43983 | jjdelcerro | import java.awt.BorderLayout; |
4 | import java.awt.Dimension; |
||
5 | 43939 | jjdelcerro | import java.awt.event.ActionEvent; |
6 | import java.awt.event.ActionListener; |
||
7 | 43987 | jjdelcerro | import java.awt.event.KeyAdapter; |
8 | 43983 | jjdelcerro | import java.awt.event.KeyEvent; |
9 | 43987 | jjdelcerro | import java.awt.event.MouseAdapter; |
10 | import java.awt.event.MouseEvent; |
||
11 | 43983 | jjdelcerro | import java.net.URL; |
12 | import java.util.ArrayList; |
||
13 | import java.util.Collection; |
||
14 | import java.util.Date; |
||
15 | 43987 | jjdelcerro | import java.util.HashSet; |
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 | 44006 | jjdelcerro | import javax.swing.JEditorPane; |
22 | 43983 | jjdelcerro | import javax.swing.JLabel; |
23 | import javax.swing.JList; |
||
24 | 43987 | jjdelcerro | import javax.swing.JOptionPane; |
25 | 44008 | jjdelcerro | import javax.swing.JScrollPane; |
26 | 43983 | jjdelcerro | import javax.swing.JTree; |
27 | 43939 | jjdelcerro | import javax.swing.SwingUtilities; |
28 | import javax.swing.event.ListSelectionEvent; |
||
29 | import javax.swing.event.ListSelectionListener; |
||
30 | 43987 | jjdelcerro | import javax.swing.event.TreeModelEvent; |
31 | 43939 | jjdelcerro | import javax.swing.event.TreeModelListener; |
32 | import javax.swing.event.TreeSelectionEvent; |
||
33 | import javax.swing.event.TreeSelectionListener; |
||
34 | 43983 | jjdelcerro | import javax.swing.tree.DefaultTreeCellRenderer; |
35 | 43939 | jjdelcerro | import javax.swing.tree.TreeModel; |
36 | import javax.swing.tree.TreePath; |
||
37 | 43983 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
38 | 43939 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
39 | 43983 | jjdelcerro | import org.apache.commons.lang.StringEscapeUtils; |
40 | import org.gvsig.expressionevaluator.Expression; |
||
41 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
42 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
43 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
44 | import org.gvsig.expressionevaluator.swing.Element; |
||
45 | 44006 | jjdelcerro | import org.gvsig.expressionevaluator.swing.Element.CalculatedElement; |
46 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
47 | import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
||
48 | import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
||
49 | import org.gvsig.expressionevaluator.swing.Element.SimpleElement; |
||
50 | import org.gvsig.expressionevaluator.swing.Element.VariableElement; |
||
51 | import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
||
52 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
53 | import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
||
54 | 43983 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
55 | import org.gvsig.tools.script.Script; |
||
56 | 44006 | jjdelcerro | import org.gvsig.tools.swing.api.Component; |
57 | 43939 | jjdelcerro | import org.gvsig.tools.swing.api.ListElement; |
58 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
59 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
60 | 43983 | jjdelcerro | import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
61 | import org.gvsig.tools.swing.api.script.ScriptSwingManager; |
||
62 | 43987 | jjdelcerro | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
63 | 43983 | jjdelcerro | import org.gvsig.tools.swing.icontheme.IconTheme; |
64 | 43939 | jjdelcerro | |
65 | /**
|
||
66 | *
|
||
67 | * @author jjdelcerro
|
||
68 | */
|
||
69 | public class DefaultJExpressionBuilder |
||
70 | extends DefaultJExpressionBuilderView
|
||
71 | implements JExpressionBuilder {
|
||
72 | |||
73 | private final ExpressionEvaluatorSwingManager manager; |
||
74 | private GroupElement currentGroup;
|
||
75 | 43983 | jjdelcerro | private PickerController<List<Script>> scriptPicker; |
76 | private ScriptSwingManager.ScriptEditor scriptEditor;
|
||
77 | 43939 | jjdelcerro | |
78 | 43984 | jjdelcerro | private final ExpressionBuilderConfig config; |
79 | 43987 | jjdelcerro | private ElementsTreeModel treeModel;
|
80 | 44006 | jjdelcerro | private java.awt.Component infoPanel = null; |
81 | private JEditorPane txtDescription; |
||
82 | private ImageIcon defaultIconGroup; |
||
83 | private ImageIcon defaultIconElement; |
||
84 | 44008 | jjdelcerro | private JScrollPane scrDescription; |
85 | 43983 | jjdelcerro | |
86 | 43939 | jjdelcerro | private class ElementsTreeModel implements TreeModel { |
87 | |||
88 | 43970 | jjdelcerro | private final GroupElement root; |
89 | 43987 | jjdelcerro | private final HashSet<TreeModelListener> listeners; |
90 | 43983 | jjdelcerro | |
91 | 43970 | jjdelcerro | public ElementsTreeModel(GroupElement root) {
|
92 | 43939 | jjdelcerro | this.root = root;
|
93 | 43987 | jjdelcerro | this.listeners = new HashSet<>(); |
94 | 43939 | jjdelcerro | } |
95 | 43983 | jjdelcerro | |
96 | 43939 | jjdelcerro | @Override
|
97 | public Object getRoot() { |
||
98 | return this.root; |
||
99 | } |
||
100 | |||
101 | 43987 | jjdelcerro | public void reload() { |
102 | this.reload(this.root); |
||
103 | this.fireTreeChanged();
|
||
104 | } |
||
105 | |||
106 | private void reload(Element element) { |
||
107 | if (element instanceof Element) { |
||
108 | if (element instanceof GroupElement) { |
||
109 | reload(element); |
||
110 | } |
||
111 | element.reload(); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | 43970 | jjdelcerro | private List<Element> getChilds(final GroupElement node) { |
116 | 43983 | jjdelcerro | List<Element> x = new ArrayList<>(); |
117 | for (Element element : node.getElements()) { |
||
118 | if (element instanceof GroupElement) { |
||
119 | x.add(element); |
||
120 | } |
||
121 | } |
||
122 | return x; //node.getElements(); |
||
123 | 43939 | jjdelcerro | } |
124 | 43983 | jjdelcerro | |
125 | 43939 | jjdelcerro | @Override
|
126 | public Object getChild(Object parent, int index) { |
||
127 | 43983 | jjdelcerro | List<Element> childs = getChilds((GroupElement) parent); |
128 | Element child = childs.get(index);
|
||
129 | return child;
|
||
130 | 43939 | jjdelcerro | } |
131 | |||
132 | @Override
|
||
133 | public int getChildCount(Object parent) { |
||
134 | 43983 | jjdelcerro | List<Element> childs = getChilds((GroupElement) parent); |
135 | 43939 | jjdelcerro | return childs.size();
|
136 | } |
||
137 | |||
138 | @Override
|
||
139 | public boolean isLeaf(Object node) { |
||
140 | 43983 | jjdelcerro | if( node instanceof GroupElement ) { |
141 | return !((GroupElement) node).hasSubgroups();
|
||
142 | } |
||
143 | return false; |
||
144 | 43939 | jjdelcerro | } |
145 | |||
146 | @Override
|
||
147 | public void valueForPathChanged(TreePath path, Object newValue) { |
||
148 | } |
||
149 | |||
150 | @Override
|
||
151 | public int getIndexOfChild(Object parent, Object child) { |
||
152 | 43983 | jjdelcerro | List<Element> childs = getChilds((GroupElement) parent); |
153 | 43939 | jjdelcerro | int n = childs.indexOf(child);
|
154 | return n;
|
||
155 | } |
||
156 | |||
157 | @Override
|
||
158 | public void addTreeModelListener(TreeModelListener l) { |
||
159 | 43987 | jjdelcerro | this.listeners.add(l);
|
160 | 43939 | jjdelcerro | } |
161 | |||
162 | @Override
|
||
163 | public void removeTreeModelListener(TreeModelListener l) { |
||
164 | 43987 | jjdelcerro | this.listeners.remove(l);
|
165 | 43939 | jjdelcerro | } |
166 | 43983 | jjdelcerro | |
167 | 43987 | jjdelcerro | protected void fireTreeChanged() { |
168 | for (TreeModelListener listener : listeners) { |
||
169 | TreeModelEvent e = new TreeModelEvent(this, new Object[] {this.root}); |
||
170 | listener.treeNodesChanged(e); |
||
171 | } |
||
172 | } |
||
173 | 43939 | jjdelcerro | } |
174 | 43983 | jjdelcerro | |
175 | public DefaultJExpressionBuilder(ExpressionEvaluatorSwingManager manager, ExpressionBuilderConfig config) {
|
||
176 | 43939 | jjdelcerro | this.manager = manager;
|
177 | 43989 | jjdelcerro | if( config == null ) { |
178 | this.config = new DefaultExpressionBuilderConfig(manager); |
||
179 | } else {
|
||
180 | this.config = config;
|
||
181 | } |
||
182 | 43939 | jjdelcerro | this.initComponents();
|
183 | } |
||
184 | 43983 | jjdelcerro | |
185 | @Override
|
||
186 | public ImageIcon loadImage(String imageName) { |
||
187 | String name = FilenameUtils.getBaseName(imageName);
|
||
188 | IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
||
189 | if (theme.exists(name)) {
|
||
190 | return theme.get(name);
|
||
191 | } |
||
192 | URL url = this.getClass().getResource(name + ".png"); |
||
193 | if (url == null) { |
||
194 | return null; |
||
195 | } |
||
196 | return new ImageIcon(url); |
||
197 | } |
||
198 | 43939 | jjdelcerro | |
199 | private void initComponents() { |
||
200 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
201 | 43983 | jjdelcerro | final IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
|
202 | 43939 | jjdelcerro | |
203 | 44006 | jjdelcerro | this.defaultIconGroup = theme.get("expressionbuilder-element-group"); |
204 | this.defaultIconElement = theme.get("expressionbuilder-element"); |
||
205 | |||
206 | this.txtDescription = new JEditorPane(); |
||
207 | 44008 | jjdelcerro | this.scrDescription = new JScrollPane(this.txtDescription); |
208 | 44006 | jjdelcerro | |
209 | 43939 | jjdelcerro | toolsSwingManager.setDefaultPopupMenu(this.txtExpression);
|
210 | toolsSwingManager.setDefaultPopupMenu(this.txtGroupElement);
|
||
211 | toolsSwingManager.setDefaultPopupMenu(this.txtSimpleElementFilter);
|
||
212 | toolsSwingManager.setDefaultPopupMenu(this.txtDescription);
|
||
213 | 43983 | jjdelcerro | toolsSwingManager.addClearButton(this.txtExpression);
|
214 | toolsSwingManager.addClearButton(this.txtSimpleElementFilter, new ActionListener() { |
||
215 | @Override
|
||
216 | public void actionPerformed(ActionEvent e) { |
||
217 | txtSimpleElementFilter.setText("");
|
||
218 | doFilter(); |
||
219 | } |
||
220 | }); |
||
221 | 44006 | jjdelcerro | this.pnlDescription.setLayout(new BorderLayout()); |
222 | this.pnlDescription.setBackground(this.txtDescription.getBackground()); |
||
223 | 43939 | jjdelcerro | |
224 | 43983 | jjdelcerro | this.treeElements.setCellRenderer(new DefaultTreeCellRenderer() { |
225 | @Override
|
||
226 | 44006 | jjdelcerro | public java.awt.Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
227 | 43983 | jjdelcerro | JLabel label = (JLabel) super.getTreeCellRendererComponent(tree,value, selected, expanded, leaf, row, hasFocus); |
228 | if( value instanceof Element ) { |
||
229 | Element element = (Element) value; |
||
230 | 44006 | jjdelcerro | if( element.getIconName()==null ) { |
231 | if( element instanceof GroupElement ) { |
||
232 | label.setIcon(defaultIconGroup); |
||
233 | } else {
|
||
234 | label.setIcon(defaultIconElement); |
||
235 | } |
||
236 | } else {
|
||
237 | label.setIcon(element.getIcon()); |
||
238 | } |
||
239 | 43983 | jjdelcerro | label.setText(element.getName()); |
240 | } else {
|
||
241 | label.setIcon(null);
|
||
242 | label.setText(Objects.toString(value, ""));
|
||
243 | } |
||
244 | return label;
|
||
245 | } |
||
246 | }); |
||
247 | |||
248 | 43939 | jjdelcerro | this.treeElements.addTreeSelectionListener(new TreeSelectionListener() { |
249 | @Override
|
||
250 | public void valueChanged(TreeSelectionEvent e) { |
||
251 | TreePath path = e.getPath();
|
||
252 | Element element = (Element) path.getLastPathComponent(); |
||
253 | 43983 | jjdelcerro | if (element instanceof GroupElement) { |
254 | updateSimpleElementsList((GroupElement) element); |
||
255 | 43939 | jjdelcerro | } |
256 | } |
||
257 | }); |
||
258 | this.btnGroupElementInsert.addActionListener(new ActionListener() { |
||
259 | @Override
|
||
260 | public void actionPerformed(ActionEvent e) { |
||
261 | doInsert(txtGroupElement.getText()); |
||
262 | } |
||
263 | }); |
||
264 | this.btnSimpleElementInsert.addActionListener(new ActionListener() { |
||
265 | @Override
|
||
266 | public void actionPerformed(ActionEvent e) { |
||
267 | 43983 | jjdelcerro | ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
268 | if (element != null) { |
||
269 | doInsert(element.getValue()); |
||
270 | } |
||
271 | 43939 | jjdelcerro | } |
272 | }); |
||
273 | this.btnSimpleElementFilter.addActionListener(new ActionListener() { |
||
274 | @Override
|
||
275 | public void actionPerformed(ActionEvent e) { |
||
276 | doFilter(); |
||
277 | } |
||
278 | }); |
||
279 | 43989 | jjdelcerro | |
280 | this.lstSimpleElement.setModel(new FilteredListModel()); |
||
281 | 43983 | jjdelcerro | this.lstSimpleElement.setCellRenderer(new DefaultListCellRenderer() { |
282 | @Override
|
||
283 | 44006 | jjdelcerro | public java.awt.Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
284 | 43983 | jjdelcerro | JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
285 | Element element = null; |
||
286 | if( value instanceof ListElement ) { |
||
287 | if( ((ListElement) value).getValue() instanceof Element ) { |
||
288 | element = (Element) ((ListElement) value).getValue();
|
||
289 | } |
||
290 | } |
||
291 | if( element !=null ) { |
||
292 | 44006 | jjdelcerro | if( element.getIconName()==null ) { |
293 | if( element instanceof GroupElement ) { |
||
294 | label.setIcon(defaultIconGroup); |
||
295 | } else {
|
||
296 | label.setIcon(defaultIconElement); |
||
297 | } |
||
298 | } else {
|
||
299 | label.setIcon(element.getIcon()); |
||
300 | } |
||
301 | 43983 | jjdelcerro | label.setText(element.getName()); |
302 | } else {
|
||
303 | label.setIcon(null);
|
||
304 | label.setText(Objects.toString(value, ""));
|
||
305 | } |
||
306 | return label;
|
||
307 | } |
||
308 | }); |
||
309 | 43939 | jjdelcerro | this.lstSimpleElement.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
310 | @Override
|
||
311 | public void valueChanged(ListSelectionEvent e) { |
||
312 | 43983 | jjdelcerro | ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
313 | if (element != null) { |
||
314 | doSelectElement(element.getValue()); |
||
315 | } |
||
316 | 43939 | jjdelcerro | } |
317 | }); |
||
318 | 43987 | jjdelcerro | this.lstSimpleElement.addMouseListener(new MouseAdapter() { |
319 | @Override
|
||
320 | public void mouseClicked(MouseEvent e) { |
||
321 | if( e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2 ) { |
||
322 | ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
||
323 | if (element != null) { |
||
324 | doSelectElement(element.getValue()); |
||
325 | doInsert(element.getValue()); |
||
326 | } |
||
327 | } |
||
328 | } |
||
329 | }); |
||
330 | this.lstSimpleElement.addKeyListener(new KeyAdapter() { |
||
331 | @Override
|
||
332 | public void keyReleased(KeyEvent e) { |
||
333 | if( e.getKeyCode()==KeyEvent.VK_ENTER ) { |
||
334 | ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
||
335 | if (element != null) { |
||
336 | doSelectElement(element.getValue()); |
||
337 | doInsert(element.getValue()); |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | }); |
||
342 | 43989 | jjdelcerro | |
343 | 43987 | jjdelcerro | this.btnSimpleElementTimeLimit.addActionListener(new ActionListener() { |
344 | @Override
|
||
345 | public void actionPerformed(ActionEvent e) { |
||
346 | doSetTimeLimit(); |
||
347 | } |
||
348 | }); |
||
349 | 43939 | jjdelcerro | this.btnSimpleElementSortDown.addActionListener(new ActionListener() { |
350 | @Override
|
||
351 | public void actionPerformed(ActionEvent e) { |
||
352 | FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
||
353 | 43983 | jjdelcerro | model.sort(false);
|
354 | 43939 | jjdelcerro | } |
355 | }); |
||
356 | this.btnSimpleElementSortUp.addActionListener(new ActionListener() { |
||
357 | @Override
|
||
358 | public void actionPerformed(ActionEvent e) { |
||
359 | FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
||
360 | 43983 | jjdelcerro | model.sort(true);
|
361 | 43939 | jjdelcerro | } |
362 | }); |
||
363 | 43987 | jjdelcerro | this.txtSimpleElementFilter.addKeyListener(new KeyAdapter() { |
364 | 43983 | jjdelcerro | @Override
|
365 | public void keyTyped(KeyEvent e) { |
||
366 | if (e.getKeyChar() == '\n') { |
||
367 | doFilter(); |
||
368 | } |
||
369 | } |
||
370 | }); |
||
371 | this.btnAdd.addActionListener(new ActionListener() { |
||
372 | @Override
|
||
373 | public void actionPerformed(ActionEvent e) { |
||
374 | doInsert(" + ");
|
||
375 | } |
||
376 | }); |
||
377 | this.btnDiv.addActionListener(new ActionListener() { |
||
378 | @Override
|
||
379 | public void actionPerformed(ActionEvent e) { |
||
380 | doInsert(" / ");
|
||
381 | } |
||
382 | }); |
||
383 | this.btnEq.addActionListener(new ActionListener() { |
||
384 | @Override
|
||
385 | public void actionPerformed(ActionEvent e) { |
||
386 | doInsert(" = ");
|
||
387 | } |
||
388 | }); |
||
389 | this.btnMult.addActionListener(new ActionListener() { |
||
390 | @Override
|
||
391 | public void actionPerformed(ActionEvent e) { |
||
392 | doInsert(" * ");
|
||
393 | } |
||
394 | }); |
||
395 | this.btnNeq.addActionListener(new ActionListener() { |
||
396 | @Override
|
||
397 | public void actionPerformed(ActionEvent e) { |
||
398 | doInsert(" <> ");
|
||
399 | } |
||
400 | }); |
||
401 | this.btnParentClose.addActionListener(new ActionListener() { |
||
402 | @Override
|
||
403 | public void actionPerformed(ActionEvent e) { |
||
404 | doInsert(" ) ");
|
||
405 | } |
||
406 | }); |
||
407 | this.btnParentOpen.addActionListener(new ActionListener() { |
||
408 | @Override
|
||
409 | public void actionPerformed(ActionEvent e) { |
||
410 | doInsert(" ( ");
|
||
411 | } |
||
412 | }); |
||
413 | this.btnSubst.addActionListener(new ActionListener() { |
||
414 | @Override
|
||
415 | public void actionPerformed(ActionEvent e) { |
||
416 | doInsert(" - ");
|
||
417 | } |
||
418 | }); |
||
419 | ScriptSwingManager scriptSwingManager = ToolsSwingLocator.getScriptSwingManager(); |
||
420 | this.scriptPicker = scriptSwingManager.createListScriptPickerController(
|
||
421 | cboPickerScripts, |
||
422 | btnPickerRemove, |
||
423 | btnPickerSelectScript |
||
424 | ); |
||
425 | this.scriptEditor = scriptSwingManager.createScriptEditor();
|
||
426 | this.pnlScriptEditorContainer.setLayout(new BorderLayout()); |
||
427 | this.pnlScriptEditorContainer.add(this.scriptEditor.asJComponent(), BorderLayout.CENTER); |
||
428 | |||
429 | 43984 | jjdelcerro | this.tabExpressionBuilder.setEnabledAt(0, this.config.getScriptsEnabled()); |
430 | 43987 | jjdelcerro | this.setPreferredSize(new Dimension(650, 350)); |
431 | this.txtDescription.setPreferredSize(new Dimension(200,200)); |
||
432 | 43939 | jjdelcerro | } |
433 | 43983 | jjdelcerro | |
434 | private String removeCursorMark(String s) { |
||
435 | s = s.replace("{{", ""); |
||
436 | s = s.replace("}}", ""); |
||
437 | return s;
|
||
438 | } |
||
439 | |||
440 | 43939 | jjdelcerro | private void doInsert(String s) { |
441 | 43983 | jjdelcerro | this.txtExpression.replaceSelection(s);
|
442 | String ss = this.txtExpression.getText(); |
||
443 | int start = ss.indexOf("{{"); |
||
444 | int end = ss.indexOf("}}"); |
||
445 | if (start > 0 && end > 0) { |
||
446 | this.txtExpression.replaceRange("", end, end + 2); |
||
447 | this.txtExpression.replaceRange("", start, start + 2); |
||
448 | this.txtExpression.setSelectionStart(start);
|
||
449 | this.txtExpression.setSelectionEnd(end - 2); |
||
450 | } |
||
451 | this.txtExpression.requestFocusInWindow();
|
||
452 | 43939 | jjdelcerro | } |
453 | 43983 | jjdelcerro | |
454 | 43939 | jjdelcerro | private void doInsert(SimpleElement element) { |
455 | 43983 | jjdelcerro | if( element instanceof FunctionElement ) { |
456 | Function function = ((FunctionElement)element).getFunction(); |
||
457 | if (function == null) { |
||
458 | doInsert(element.getName()); |
||
459 | } else {
|
||
460 | doInsert(function.template() == null ? function.name() + "({{}})" : function.template()); |
||
461 | } |
||
462 | 44006 | jjdelcerro | } else if( element instanceof CalculatedElement) { |
463 | Object value = ((CalculatedElement) element).getValue();
|
||
464 | if( value != null ) { |
||
465 | doInsert(" "+value.toString()+" "); |
||
466 | } |
||
467 | 43983 | jjdelcerro | } else if( element instanceof ConstantElement) { |
468 | 44006 | jjdelcerro | Object value = ((ConstantElement) element).getValue();
|
469 | doInsert(" "+repr(value)+" "); |
||
470 | 43939 | jjdelcerro | } else {
|
471 | 43983 | jjdelcerro | doInsert(" "+element.getName()+" "); |
472 | 43939 | jjdelcerro | } |
473 | } |
||
474 | 43983 | jjdelcerro | |
475 | 43939 | jjdelcerro | private void doFilter() { |
476 | FilteredListModel model = (FilteredListModel) this.lstSimpleElement.getModel();
|
||
477 | model.setFilter(this.txtSimpleElementFilter.getText());
|
||
478 | } |
||
479 | 43983 | jjdelcerro | |
480 | 43939 | jjdelcerro | private void doSelectElement(SimpleElement element) { |
481 | 44006 | jjdelcerro | Component aditionalPanel = element.getAditionalPanel();
|
482 | java.awt.Component newInfoPanel = null;
|
||
483 | if( aditionalPanel == null ) { |
||
484 | StringBuilder html = new StringBuilder(); |
||
485 | html.append("<html>\n");
|
||
486 | if( element instanceof ConstantElement ) { |
||
487 | Object value = ((ConstantElement)element).getValue();
|
||
488 | if( value == null ) { |
||
489 | html.append("<b>Constant</b><br>\n<br>\n");
|
||
490 | html.append("<b>Value</b> NULL<br>\n");
|
||
491 | } else {
|
||
492 | html.append("<b>Constant</b><br>\n<br>\n");
|
||
493 | html.append("<b>Value</b> ").append(repr(value)).append("<br>\n"); |
||
494 | html.append("<b>Type</b> ").append(value.getClass().getSimpleName()).append("<br>\n"); |
||
495 | } |
||
496 | 43983 | jjdelcerro | |
497 | 44006 | jjdelcerro | } else if( element instanceof VariableElement ) { |
498 | VariableElement var = (VariableElement)element; |
||
499 | html.append("<b>").append(var.getLabel()).append("</b><br>\n<br>\n"); |
||
500 | html.append("<b>Source</b> ").append(var.getSource()).append("<br>\n"); |
||
501 | html.append("<b>Name</b> ").append(var.getName()).append("<br>\n"); |
||
502 | html.append("<b>Type</b> ").append(var.getTypeName()).append("<br>\n"); |
||
503 | |||
504 | } else if( element instanceof FunctionElement ) { |
||
505 | Function function = ((FunctionElement)element).getFunction(); |
||
506 | if (function == null) { |
||
507 | html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
||
508 | html.append("<b>Return</b> Object<br>\n<br>\n");
|
||
509 | html.append("<b>Template:</b> ").append(element.getName()).append("()<br>\n"); |
||
510 | } else {
|
||
511 | html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
||
512 | html.append("<b>Return</b> ");
|
||
513 | html.append(StringUtils.isEmpty(function.returnType())?"Objec": function.returnType());
|
||
514 | html.append("<br>\n");
|
||
515 | html.append("<b>Template</b> ");
|
||
516 | html.append(StringUtils.isEmpty(function.template())?element.getName()+"()": removeCursorMark(function.template()));
|
||
517 | html.append("<br>\n");
|
||
518 | |||
519 | String[] args = function.descriptionArgs(); |
||
520 | if (args != null) { |
||
521 | html.append("<b>Arguments:</b> ").append("<ul>\n"); |
||
522 | for (String arg : args) { |
||
523 | html.append("<li>").append(arg).append("</li>\n"); |
||
524 | } |
||
525 | html.append("</ul>\n");
|
||
526 | html.append("<br>\n");
|
||
527 | 43939 | jjdelcerro | } |
528 | } |
||
529 | 44006 | jjdelcerro | } else {
|
530 | html.append("<b>Generic element<br>\n<br>\n");
|
||
531 | html.append("<b>Name</b> ").append(element.getName()).append("</br>\n"); |
||
532 | |||
533 | 43983 | jjdelcerro | } |
534 | 44006 | jjdelcerro | |
535 | if( element.getDescription()!=null ) { |
||
536 | html.append("<b>Description</b><br>\n");
|
||
537 | html.append(element.getDescription().replace("\n", "<br>")).append("<br>\n"); |
||
538 | } |
||
539 | html.append("</html>\n");
|
||
540 | this.txtDescription.setContentType("text/html"); |
||
541 | this.txtDescription.setText(html.toString());
|
||
542 | this.txtDescription.setCaretPosition(0); |
||
543 | 44008 | jjdelcerro | newInfoPanel = this.scrDescription;
|
544 | 43983 | jjdelcerro | } else {
|
545 | 44006 | jjdelcerro | newInfoPanel = aditionalPanel.asJComponent(); |
546 | 43939 | jjdelcerro | } |
547 | 44006 | jjdelcerro | |
548 | if( this.infoPanel != newInfoPanel ) { |
||
549 | this.pnlDescription.removeAll();
|
||
550 | this.pnlDescription.add(newInfoPanel, BorderLayout.CENTER); |
||
551 | this.pnlDescription.revalidate();
|
||
552 | this.pnlDescription.repaint();
|
||
553 | this.infoPanel = newInfoPanel;
|
||
554 | 43983 | jjdelcerro | } |
555 | 44006 | jjdelcerro | |
556 | 43939 | jjdelcerro | } |
557 | 43983 | jjdelcerro | |
558 | 43939 | jjdelcerro | private void updateSimpleElementsList(GroupElement group) { |
559 | FilteredListModel model = new FilteredListModel();
|
||
560 | model.addElement("Loading...");
|
||
561 | this.currentGroup = group;
|
||
562 | this.txtGroupElement.setText(group.getName());
|
||
563 | 43983 | jjdelcerro | this.txtSimpleElementFilter.setText(""); |
564 | this.txtDescription.setText(""); |
||
565 | 44008 | jjdelcerro | if( this.infoPanel != this.scrDescription ) { |
566 | 44006 | jjdelcerro | this.pnlDescription.removeAll();
|
567 | 44008 | jjdelcerro | this.pnlDescription.add(this.scrDescription, BorderLayout.CENTER); |
568 | this.pnlDescription.revalidate();
|
||
569 | 44006 | jjdelcerro | this.pnlDescription.repaint();
|
570 | 44008 | jjdelcerro | this.infoPanel = this.scrDescription; |
571 | 44006 | jjdelcerro | } |
572 | 43983 | jjdelcerro | if (group instanceof SimpleElement) { |
573 | 43939 | jjdelcerro | this.btnGroupElementInsert.setEnabled(true); |
574 | } else {
|
||
575 | this.btnGroupElementInsert.setEnabled(false); |
||
576 | } |
||
577 | this.lstSimpleElement.setEnabled(false); |
||
578 | this.lstSimpleElement.setModel(model);
|
||
579 | 43983 | jjdelcerro | |
580 | 43939 | jjdelcerro | Thread th = new Thread(new Runnable() { |
581 | @Override
|
||
582 | public void run() { |
||
583 | simpleElementsUpdater(); |
||
584 | } |
||
585 | }, "ExpressionBuilderSimpleElementsUpdater");
|
||
586 | th.start(); |
||
587 | } |
||
588 | |||
589 | private void simpleElementsUpdater() { |
||
590 | final GroupElement group = this.currentGroup; |
||
591 | 43983 | jjdelcerro | final FilteredListModel model = new FilteredListModel(); |
592 | 43939 | jjdelcerro | for (Element child : group) { |
593 | 43983 | jjdelcerro | if (group != this.currentGroup) { |
594 | 43939 | jjdelcerro | return;
|
595 | } |
||
596 | 43983 | jjdelcerro | if (child instanceof SimpleElement) { |
597 | String label = child.getName();
|
||
598 | Function function = child instanceof FunctionElement? ((FunctionElement)child).getFunction(): null; |
||
599 | if (function != null && !StringUtils.isEmpty(function.returnType())) { |
||
600 | label = "<html><b>" + label + "</b> return " + function.returnType() + "</html>"; |
||
601 | // label = label + " return " + function.returnType();
|
||
602 | 43939 | jjdelcerro | } |
603 | 43983 | jjdelcerro | model.addElement(new ListElement<>(label, child));
|
604 | 43939 | jjdelcerro | } |
605 | } |
||
606 | 43983 | jjdelcerro | model.sort(true);
|
607 | 43939 | jjdelcerro | SwingUtilities.invokeLater(new Runnable() { |
608 | @Override
|
||
609 | public void run() { |
||
610 | 43983 | jjdelcerro | if (group != currentGroup) {
|
611 | 43939 | jjdelcerro | return;
|
612 | } |
||
613 | 43983 | jjdelcerro | if( group.hasMoreElements() ) {
|
614 | lblSimpleElementsMsg.setText("More elements...");
|
||
615 | } else {
|
||
616 | lblSimpleElementsMsg.setText("");
|
||
617 | } |
||
618 | 43939 | jjdelcerro | lstSimpleElement.setEnabled(true);
|
619 | lstSimpleElement.setModel(model); |
||
620 | } |
||
621 | }); |
||
622 | 43983 | jjdelcerro | |
623 | 43939 | jjdelcerro | } |
624 | 43983 | jjdelcerro | |
625 | 43939 | jjdelcerro | @Override
|
626 | 43983 | jjdelcerro | public Expression getExpression() { |
627 | String phrase = this.txtExpression.getText(); |
||
628 | if (StringUtils.isEmpty(phrase)) {
|
||
629 | 43939 | jjdelcerro | return null; |
630 | } |
||
631 | 43983 | jjdelcerro | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
632 | expression.setPhrase(phrase); |
||
633 | Script script = ToolsLocator.getScriptManager().createScript("UserScript", "", "python"); |
||
634 | this.scriptEditor.fetch(script);
|
||
635 | if( !StringUtils.isEmpty(script.getCode()) ) {
|
||
636 | expression.setUserScript(script); |
||
637 | } |
||
638 | 43939 | jjdelcerro | return expression;
|
639 | } |
||
640 | |||
641 | @Override
|
||
642 | 43983 | jjdelcerro | public void setExpression(Expression expression) { |
643 | this.txtExpression.setText(expression.getPhrase());
|
||
644 | if( expression.getUserScript()!=null ) { |
||
645 | this.scriptEditor.set(expression.getUserScript());
|
||
646 | 43939 | jjdelcerro | } |
647 | 43983 | jjdelcerro | if( expression.getScripts()!=null ) { |
648 | this.scriptPicker.set(expression.getScripts().toList());
|
||
649 | } |
||
650 | 43939 | jjdelcerro | } |
651 | |||
652 | @Override
|
||
653 | 43983 | jjdelcerro | public int getSimpleElementsLimit() { |
654 | return this.config.getSimpleElementsLimit(); |
||
655 | } |
||
656 | |||
657 | @Override
|
||
658 | public void setSimpleElementsLimit(int limit) { |
||
659 | this.config.setSimpleElementsLimit(limit);
|
||
660 | } |
||
661 | |||
662 | @Override
|
||
663 | public List<Element> getElements() { |
||
664 | return this.config.getElements(); |
||
665 | } |
||
666 | |||
667 | @Override
|
||
668 | public Collection<SymbolTable> getSymbolTables() { |
||
669 | return this.config.getSymbolTables(); |
||
670 | } |
||
671 | |||
672 | @Override
|
||
673 | 43987 | jjdelcerro | public void addSymbolTable(String name) { |
674 | this.config.addSymbolTable(name);
|
||
675 | } |
||
676 | |||
677 | @Override
|
||
678 | 43983 | jjdelcerro | public void addSymbolTable(SymbolTable symbolTable) { |
679 | this.config.addSymbolTable(symbolTable);
|
||
680 | } |
||
681 | |||
682 | @Override
|
||
683 | public void removeAllSymbolTables() { |
||
684 | this.config.removeAllSymbolTables();
|
||
685 | } |
||
686 | |||
687 | 43984 | jjdelcerro | @Override
|
688 | public void removeSymbolTable(String name) { |
||
689 | this.config.removeSymbolTable(name);
|
||
690 | } |
||
691 | |||
692 | @Override
|
||
693 | public void setScriptsEnabled(boolean enabled) { |
||
694 | this.config.setScriptsEnabled(enabled);
|
||
695 | this.tabExpressionBuilder.setEnabledAt(0, this.config.getScriptsEnabled()); |
||
696 | } |
||
697 | |||
698 | @Override
|
||
699 | public boolean getScriptsEnabled() { |
||
700 | return this.config.getScriptsEnabled(); |
||
701 | } |
||
702 | 43983 | jjdelcerro | |
703 | @Override
|
||
704 | 43939 | jjdelcerro | public JComponent asJComponent() { |
705 | 43987 | jjdelcerro | if( this.treeModel == null ) { |
706 | this.treeModel = new ElementsTreeModel( |
||
707 | this.manager.createGroupElement("_Elements", this.getElements()) |
||
708 | ); |
||
709 | this.treeElements.setModel(this.treeModel); |
||
710 | } |
||
711 | 43939 | jjdelcerro | return this; |
712 | } |
||
713 | 43983 | jjdelcerro | |
714 | private String repr(Object value) { |
||
715 | if (value == null) { |
||
716 | return "NULL"; |
||
717 | } else if (value instanceof Date) { |
||
718 | return "DATE('" + value + "')"; |
||
719 | } else if (value instanceof Boolean) { |
||
720 | return value.toString();
|
||
721 | } else if (value instanceof String) { |
||
722 | return "'" + StringEscapeUtils.escapeSql((String) value) + "'"; |
||
723 | } else {
|
||
724 | return StringEscapeUtils.escapeSql(value.toString());
|
||
725 | } |
||
726 | |||
727 | } |
||
728 | 43939 | jjdelcerro | |
729 | 43987 | jjdelcerro | private void doSetTimeLimit() { |
730 | ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
731 | String s = dialogs.inputDialog(
|
||
732 | "Indicate the time limit in seconds for calculating the elements",
|
||
733 | "Time limit",
|
||
734 | JOptionPane.QUESTION_MESSAGE,
|
||
735 | String.valueOf(this.config.getSimpleElementsLimit()) |
||
736 | ); |
||
737 | if( StringUtils.isBlank(s) ) {
|
||
738 | return;
|
||
739 | } |
||
740 | try {
|
||
741 | int n = Integer.parseInt(s); |
||
742 | this.config.setSimpleElementsLimit(n);
|
||
743 | this.treeModel.reload();
|
||
744 | } catch(Exception ex) { |
||
745 | dialogs.messageDialog( |
||
746 | "Invalid time limit '"+s+"'.", |
||
747 | "Time limit",
|
||
748 | JOptionPane.WARNING_MESSAGE
|
||
749 | ); |
||
750 | } |
||
751 | } |
||
752 | 43983 | jjdelcerro | } |