Revision 43983 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
DefaultJExpressionBuilder.java | ||
---|---|---|
1 |
|
|
2 | 1 |
package org.gvsig.expressionevaluator.swing.impl; |
3 | 2 |
|
3 |
import java.awt.BorderLayout; |
|
4 |
import java.awt.Component; |
|
5 |
import java.awt.Dimension; |
|
4 | 6 |
import java.awt.event.ActionEvent; |
5 | 7 |
import java.awt.event.ActionListener; |
8 |
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; |
|
6 | 16 |
import java.util.List; |
7 |
import javax.swing.DefaultListModel; |
|
17 |
import java.util.Objects; |
|
18 |
import javax.swing.DefaultListCellRenderer; |
|
19 |
import javax.swing.ImageIcon; |
|
8 | 20 |
import javax.swing.JComponent; |
21 |
import javax.swing.JLabel; |
|
22 |
import javax.swing.JList; |
|
23 |
import javax.swing.JTree; |
|
9 | 24 |
import javax.swing.SwingUtilities; |
10 | 25 |
import javax.swing.event.ListSelectionEvent; |
11 | 26 |
import javax.swing.event.ListSelectionListener; |
12 | 27 |
import javax.swing.event.TreeModelListener; |
13 | 28 |
import javax.swing.event.TreeSelectionEvent; |
14 | 29 |
import javax.swing.event.TreeSelectionListener; |
30 |
import javax.swing.tree.DefaultTreeCellRenderer; |
|
15 | 31 |
import javax.swing.tree.TreeModel; |
16 | 32 |
import javax.swing.tree.TreePath; |
33 |
import org.apache.commons.io.FilenameUtils; |
|
17 | 34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.apache.commons.lang.StringEscapeUtils; |
|
36 |
import org.gvsig.expressionevaluator.Expression; |
|
37 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
38 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
18 | 39 |
import org.gvsig.expressionevaluator.Function; |
40 |
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; |
|
19 | 48 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
20 | 49 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
50 |
import org.gvsig.tools.ToolsLocator; |
|
51 |
import org.gvsig.tools.script.Script; |
|
21 | 52 |
import org.gvsig.tools.swing.api.ListElement; |
22 | 53 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
23 | 54 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
55 |
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; |
|
24 | 58 |
|
25 | 59 |
/** |
26 | 60 |
* |
... | ... | |
31 | 65 |
implements JExpressionBuilder { |
32 | 66 |
|
33 | 67 |
private final ExpressionEvaluatorSwingManager manager; |
34 |
private List<Element> elements; |
|
35 | 68 |
private GroupElement currentGroup; |
69 |
private PickerController<List<Script>> scriptPicker; |
|
70 |
private ScriptSwingManager.ScriptEditor scriptEditor; |
|
36 | 71 |
|
72 |
private ExpressionBuilderConfig config; |
|
73 |
|
|
37 | 74 |
private class ElementsTreeModel implements TreeModel { |
38 | 75 |
|
39 | 76 |
private final GroupElement root; |
40 |
|
|
77 |
|
|
41 | 78 |
public ElementsTreeModel(GroupElement root) { |
42 | 79 |
this.root = root; |
43 | 80 |
} |
44 |
|
|
81 |
|
|
45 | 82 |
@Override |
46 | 83 |
public Object getRoot() { |
47 | 84 |
return this.root; |
48 | 85 |
} |
49 | 86 |
|
50 | 87 |
private List<Element> getChilds(final GroupElement node) { |
51 |
return node.getElements(); |
|
88 |
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(); |
|
52 | 95 |
} |
53 |
|
|
96 |
|
|
54 | 97 |
@Override |
55 | 98 |
public Object getChild(Object parent, int index) { |
56 |
List<Element> childs = getChilds((DefaultGroupElement)parent); |
|
57 |
return childs.get(index); |
|
99 |
List<Element> childs = getChilds((GroupElement) parent); |
|
100 |
Element child = childs.get(index); |
|
101 |
return child; |
|
58 | 102 |
} |
59 | 103 |
|
60 | 104 |
@Override |
61 | 105 |
public int getChildCount(Object parent) { |
62 |
List<Element> childs = getChilds((DefaultGroupElement)parent);
|
|
106 |
List<Element> childs = getChilds((GroupElement) parent);
|
|
63 | 107 |
return childs.size(); |
64 | 108 |
} |
65 | 109 |
|
66 | 110 |
@Override |
67 | 111 |
public boolean isLeaf(Object node) { |
68 |
boolean leaf = node instanceof SimpleElement; |
|
69 |
return leaf; |
|
112 |
if( node instanceof GroupElement ) { |
|
113 |
return !((GroupElement) node).hasSubgroups(); |
|
114 |
} |
|
115 |
return false; |
|
70 | 116 |
} |
71 | 117 |
|
72 | 118 |
@Override |
... | ... | |
75 | 121 |
|
76 | 122 |
@Override |
77 | 123 |
public int getIndexOfChild(Object parent, Object child) { |
78 |
List<Element> childs = getChilds((DefaultGroupElement)parent);
|
|
124 |
List<Element> childs = getChilds((GroupElement) parent);
|
|
79 | 125 |
int n = childs.indexOf(child); |
80 | 126 |
return n; |
81 | 127 |
} |
... | ... | |
87 | 133 |
@Override |
88 | 134 |
public void removeTreeModelListener(TreeModelListener l) { |
89 | 135 |
} |
90 |
|
|
136 |
|
|
91 | 137 |
} |
92 |
|
|
93 |
|
|
94 |
public DefaultJExpressionBuilder(ExpressionEvaluatorSwingManager manager) { |
|
138 |
|
|
139 |
public DefaultJExpressionBuilder(ExpressionEvaluatorSwingManager manager, ExpressionBuilderConfig config) { |
|
95 | 140 |
this.manager = manager; |
141 |
this.config = new DefaultExpressionBuilderConfig(manager); |
|
96 | 142 |
this.initComponents(); |
97 | 143 |
} |
144 |
|
|
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 |
} |
|
98 | 158 |
|
99 | 159 |
private void initComponents() { |
100 | 160 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
161 |
final IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
|
101 | 162 |
|
102 | 163 |
toolsSwingManager.setDefaultPopupMenu(this.txtExpression); |
103 | 164 |
toolsSwingManager.setDefaultPopupMenu(this.txtGroupElement); |
104 | 165 |
toolsSwingManager.setDefaultPopupMenu(this.txtSimpleElementFilter); |
105 | 166 |
toolsSwingManager.setDefaultPopupMenu(this.txtDescription); |
106 |
toolsSwingManager.addClearButton(this.txtSimpleElementFilter); |
|
167 |
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 |
}); |
|
107 | 175 |
|
108 |
TreeModel model = new ElementsTreeModel( |
|
109 |
this.manager.createGroup("Elements", this.getElements()) |
|
110 |
); |
|
111 |
this.treeElements.setModel(model); |
|
176 |
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 |
|
|
112 | 192 |
this.treeElements.addTreeSelectionListener(new TreeSelectionListener() { |
113 | 193 |
@Override |
114 | 194 |
public void valueChanged(TreeSelectionEvent e) { |
115 | 195 |
TreePath path = e.getPath(); |
116 | 196 |
Element element = (Element) path.getLastPathComponent(); |
117 |
if( element instanceof GroupElement) {
|
|
118 |
updateSimpleElementsList((GroupElement)element); |
|
197 |
if (element instanceof GroupElement) {
|
|
198 |
updateSimpleElementsList((GroupElement) element);
|
|
119 | 199 |
} |
120 | 200 |
} |
121 | 201 |
}); |
... | ... | |
128 | 208 |
this.btnSimpleElementInsert.addActionListener(new ActionListener() { |
129 | 209 |
@Override |
130 | 210 |
public void actionPerformed(ActionEvent e) { |
131 |
SimpleElement element = (SimpleElement) lstSimpleElement.getSelectedValue(); |
|
132 |
doInsert(element); |
|
211 |
ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
|
212 |
if (element != null) { |
|
213 |
doInsert(element.getValue()); |
|
214 |
} |
|
133 | 215 |
} |
134 | 216 |
}); |
135 | 217 |
this.btnSimpleElementFilter.addActionListener(new ActionListener() { |
... | ... | |
138 | 220 |
doFilter(); |
139 | 221 |
} |
140 | 222 |
}); |
223 |
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 |
}); |
|
141 | 243 |
this.lstSimpleElement.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
142 | 244 |
@Override |
143 | 245 |
public void valueChanged(ListSelectionEvent e) { |
144 |
SimpleElement element = (SimpleElement) lstSimpleElement.getSelectedValue(); |
|
145 |
doSelectElement(element); |
|
246 |
ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
|
247 |
if (element != null) { |
|
248 |
doSelectElement(element.getValue()); |
|
249 |
} |
|
146 | 250 |
} |
147 | 251 |
}); |
148 | 252 |
this.btnSimpleElementSortDown.addActionListener(new ActionListener() { |
149 | 253 |
@Override |
150 | 254 |
public void actionPerformed(ActionEvent e) { |
151 | 255 |
FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
152 |
model.sort(true);
|
|
256 |
model.sort(false);
|
|
153 | 257 |
} |
154 | 258 |
}); |
155 | 259 |
this.btnSimpleElementSortUp.addActionListener(new ActionListener() { |
156 | 260 |
@Override |
157 | 261 |
public void actionPerformed(ActionEvent e) { |
158 | 262 |
FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
159 |
model.sort(false);
|
|
263 |
model.sort(true);
|
|
160 | 264 |
} |
161 | 265 |
}); |
266 |
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)); |
|
162 | 341 |
} |
163 |
|
|
342 |
|
|
343 |
private String removeCursorMark(String s) { |
|
344 |
s = s.replace("{{", ""); |
|
345 |
s = s.replace("}}", ""); |
|
346 |
return s; |
|
347 |
} |
|
348 |
|
|
164 | 349 |
private void doInsert(String s) { |
165 |
this.txtExpression.insert(s, this.txtExpression.getCaretPosition()); |
|
350 |
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(); |
|
166 | 361 |
} |
167 |
|
|
362 |
|
|
168 | 363 |
private void doInsert(SimpleElement element) { |
169 |
Function function = element.getFunction(); |
|
170 |
if( function == null ) { |
|
171 |
doInsert(element.getName()); |
|
364 |
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())+" "); |
|
172 | 373 |
} else { |
173 |
doInsert(function.template());
|
|
374 |
doInsert(" "+element.getName()+" ");
|
|
174 | 375 |
} |
175 | 376 |
} |
176 |
|
|
377 |
|
|
177 | 378 |
private void doFilter() { |
178 | 379 |
FilteredListModel model = (FilteredListModel) this.lstSimpleElement.getModel(); |
179 | 380 |
model.setFilter(this.txtSimpleElementFilter.getText()); |
180 | 381 |
} |
181 |
|
|
382 |
|
|
182 | 383 |
private void doSelectElement(SimpleElement element) { |
183 | 384 |
StringBuilder html = new StringBuilder(); |
184 | 385 |
html.append("<html>\n"); |
185 |
html.append("<h1>").append(element.getName()).append("</h1>\n"); |
|
186 |
Function function = element.getFunction(); |
|
187 |
if( function != null ) { |
|
188 |
if( StringUtils.isEmpty(function.returnType()) ) { |
|
189 |
html.append("<b>return</b> ").append(function.returnType()).append("<br>\n"); |
|
190 |
} |
|
386 |
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 |
|
|
191 | 419 |
String[] args = function.descriptionArgs(); |
192 |
if( args!=null ) {
|
|
420 |
if (args != null) {
|
|
193 | 421 |
html.append("<b>Arguments:</b> ").append("<ul>\n"); |
194 | 422 |
for (String arg : args) { |
195 | 423 |
html.append("<li>").append(arg).append("</li>\n"); |
... | ... | |
197 | 425 |
html.append("</ul>\n"); |
198 | 426 |
html.append("<br>\n"); |
199 | 427 |
} |
200 |
if( StringUtils.isEmpty(function.template()) ) { |
|
201 |
html.append("<b>Template:</b> ").append(function.template()).append("<br>\n"); |
|
202 |
} |
|
428 |
} |
|
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 |
|
|
203 | 433 |
} |
204 |
html.append(element.getDescription().replace("\n", "<br>")).append("<br>\n"); |
|
434 |
|
|
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 |
} |
|
205 | 439 |
html.append("</html>\n"); |
440 |
this.txtDescription.setContentType("text/html"); |
|
206 | 441 |
this.txtDescription.setText(html.toString()); |
442 |
this.txtDescription.setCaretPosition(0); |
|
207 | 443 |
} |
208 |
|
|
444 |
|
|
209 | 445 |
private void updateSimpleElementsList(GroupElement group) { |
210 | 446 |
FilteredListModel model = new FilteredListModel(); |
211 | 447 |
model.addElement("Loading..."); |
212 | 448 |
this.currentGroup = group; |
213 | 449 |
this.txtGroupElement.setText(group.getName()); |
214 |
if( group instanceof SimpleElement ) { |
|
450 |
this.txtSimpleElementFilter.setText(""); |
|
451 |
this.txtDescription.setText(""); |
|
452 |
if (group instanceof SimpleElement) { |
|
215 | 453 |
this.btnGroupElementInsert.setEnabled(true); |
216 | 454 |
} else { |
217 | 455 |
this.btnGroupElementInsert.setEnabled(false); |
218 | 456 |
} |
219 | 457 |
this.lstSimpleElement.setEnabled(false); |
220 | 458 |
this.lstSimpleElement.setModel(model); |
221 |
|
|
459 |
|
|
222 | 460 |
Thread th = new Thread(new Runnable() { |
223 | 461 |
@Override |
224 | 462 |
public void run() { |
... | ... | |
230 | 468 |
|
231 | 469 |
private void simpleElementsUpdater() { |
232 | 470 |
final GroupElement group = this.currentGroup; |
233 |
final DefaultListModel model = new DefaultListModel();
|
|
471 |
final FilteredListModel model = new FilteredListModel();
|
|
234 | 472 |
for (Element child : group) { |
235 |
if( group != this.currentGroup ) {
|
|
473 |
if (group != this.currentGroup) {
|
|
236 | 474 |
return; |
237 | 475 |
} |
238 |
String label = child.getName(); |
|
239 |
if( child instanceof SimpleElement ) { |
|
240 |
Function function = ((SimpleElement) child).getFunction(); |
|
241 |
if( function != null && StringUtils.isEmpty(function.returnType()) ) { |
|
242 |
label = "<html><b>" + label + "</b> return " + function.returnType() + "</html>"; |
|
476 |
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(); |
|
243 | 482 |
} |
483 |
model.addElement(new ListElement<>(label, child)); |
|
244 | 484 |
} |
245 |
model.addElement(new ListElement<>(label,child)); |
|
246 | 485 |
} |
486 |
model.sort(true); |
|
247 | 487 |
SwingUtilities.invokeLater(new Runnable() { |
248 | 488 |
@Override |
249 | 489 |
public void run() { |
250 |
if( group != currentGroup ) {
|
|
490 |
if (group != currentGroup) {
|
|
251 | 491 |
return; |
252 | 492 |
} |
493 |
if( group.hasMoreElements() ) { |
|
494 |
lblSimpleElementsMsg.setText("More elements..."); |
|
495 |
} else { |
|
496 |
lblSimpleElementsMsg.setText(""); |
|
497 |
} |
|
253 | 498 |
lstSimpleElement.setEnabled(true); |
254 | 499 |
lstSimpleElement.setModel(model); |
255 | 500 |
} |
256 | 501 |
}); |
257 |
|
|
502 |
|
|
258 | 503 |
} |
259 |
|
|
504 |
|
|
260 | 505 |
@Override |
261 |
public String getExpression() {
|
|
262 |
String expression = this.txtExpression.getText();
|
|
263 |
if( StringUtils.isEmpty(expression) ) {
|
|
506 |
public Expression getExpression() {
|
|
507 |
String phrase = this.txtExpression.getText();
|
|
508 |
if (StringUtils.isEmpty(phrase)) {
|
|
264 | 509 |
return null; |
265 | 510 |
} |
511 |
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 |
} |
|
266 | 518 |
return expression; |
267 | 519 |
} |
268 | 520 |
|
269 | 521 |
@Override |
270 |
public List<Element> getElements() { |
|
271 |
if( this.elements == null ) { |
|
272 |
this.elements = manager.getElements(); |
|
522 |
public void setExpression(Expression expression) { |
|
523 |
this.txtExpression.setText(expression.getPhrase()); |
|
524 |
if( expression.getUserScript()!=null ) { |
|
525 |
this.scriptEditor.set(expression.getUserScript()); |
|
273 | 526 |
} |
274 |
return elements; |
|
527 |
if( expression.getScripts()!=null ) { |
|
528 |
this.scriptPicker.set(expression.getScripts().toList()); |
|
529 |
} |
|
275 | 530 |
} |
276 | 531 |
|
277 | 532 |
@Override |
533 |
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 |
|
278 | 564 |
public JComponent asJComponent() { |
565 |
TreeModel model = new ElementsTreeModel( |
|
566 |
this.manager.createGroupElement("Elements", this.getElements()) |
|
567 |
); |
|
568 |
this.treeElements.setModel(model); |
|
279 | 569 |
return this; |
280 | 570 |
} |
571 |
|
|
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 |
} |
|
281 | 586 |
|
282 |
} |
|
587 |
} |
Also available in: Unified diff