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 @ 44216
History | View | Annotate | Download (34.6 KB)
1 |
package org.gvsig.expressionevaluator.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.Toolkit; |
6 |
import java.awt.datatransfer.Clipboard; |
7 |
import java.awt.datatransfer.StringSelection; |
8 |
import java.awt.event.ActionEvent; |
9 |
import java.awt.event.ActionListener; |
10 |
import java.awt.event.KeyAdapter; |
11 |
import java.awt.event.KeyEvent; |
12 |
import java.awt.event.MouseAdapter; |
13 |
import java.awt.event.MouseEvent; |
14 |
import java.io.InputStream; |
15 |
import java.net.URL; |
16 |
import java.util.ArrayList; |
17 |
import java.util.Collection; |
18 |
import java.util.HashSet; |
19 |
import java.util.List; |
20 |
import java.util.Locale; |
21 |
import java.util.Map; |
22 |
import java.util.Objects; |
23 |
import javax.swing.DefaultListCellRenderer; |
24 |
import javax.swing.ImageIcon; |
25 |
import javax.swing.JComponent; |
26 |
import javax.swing.JEditorPane; |
27 |
import javax.swing.JLabel; |
28 |
import javax.swing.JList; |
29 |
import javax.swing.JMenuItem; |
30 |
import javax.swing.JOptionPane; |
31 |
import javax.swing.JPanel; |
32 |
import javax.swing.JPopupMenu; |
33 |
import javax.swing.JScrollPane; |
34 |
import javax.swing.JTabbedPane; |
35 |
import javax.swing.JTree; |
36 |
import javax.swing.SwingUtilities; |
37 |
import javax.swing.event.CaretEvent; |
38 |
import javax.swing.event.CaretListener; |
39 |
import javax.swing.event.ListSelectionEvent; |
40 |
import javax.swing.event.ListSelectionListener; |
41 |
import javax.swing.event.TreeModelEvent; |
42 |
import javax.swing.event.TreeModelListener; |
43 |
import javax.swing.event.TreeSelectionEvent; |
44 |
import javax.swing.event.TreeSelectionListener; |
45 |
import javax.swing.tree.DefaultTreeCellRenderer; |
46 |
import javax.swing.tree.TreeModel; |
47 |
import javax.swing.tree.TreePath; |
48 |
import org.apache.commons.io.FilenameUtils; |
49 |
import org.apache.commons.io.IOUtils; |
50 |
import org.apache.commons.lang3.StringUtils; |
51 |
import org.gvsig.expressionevaluator.Expression; |
52 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
53 |
import org.gvsig.expressionevaluator.Function; |
54 |
import org.gvsig.expressionevaluator.SymbolTable; |
55 |
import org.gvsig.expressionevaluator.swing.Element; |
56 |
import org.gvsig.expressionevaluator.swing.Element.CalculatedElement; |
57 |
import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
58 |
import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
59 |
import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
60 |
import org.gvsig.expressionevaluator.swing.Element.SimpleElement; |
61 |
import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
62 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
63 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
64 |
import org.gvsig.tools.ToolsLocator; |
65 |
import org.gvsig.tools.i18n.I18nManager; |
66 |
import org.gvsig.tools.script.Script; |
67 |
import org.gvsig.tools.swing.api.Component; |
68 |
import org.gvsig.tools.swing.api.ListElement; |
69 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
70 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
71 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
72 |
import org.gvsig.tools.swing.api.script.ScriptSwingManager; |
73 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
74 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
75 |
|
76 |
/**
|
77 |
*
|
78 |
* @author jjdelcerro
|
79 |
*/
|
80 |
@SuppressWarnings("UseSpecificCatch") |
81 |
public class DefaultJExpressionBuilder |
82 |
extends DefaultJExpressionBuilderView
|
83 |
implements JExpressionBuilder {
|
84 |
|
85 |
private final ExpressionEvaluatorSwingManager manager; |
86 |
private GroupElement currentGroup;
|
87 |
private PickerController<List<Script>> scriptPicker; |
88 |
private ScriptSwingManager.ScriptEditor scriptEditor;
|
89 |
|
90 |
private final ExpressionBuilderConfig config; |
91 |
private ElementsTreeModel treeModel;
|
92 |
private ImageIcon defaultIconGroup; |
93 |
private ImageIcon defaultIconElement; |
94 |
private InfoPanel infoPanel;
|
95 |
private AbstractAutomaticExpressionChecker automaticExpressionChecker;
|
96 |
|
97 |
private static class InfoPanel implements Component { |
98 |
|
99 |
private final JEditorPane txtDescription; |
100 |
private final JScrollPane scrDescription; |
101 |
private final JTabbedPane tab; |
102 |
|
103 |
private final String labelDescription; |
104 |
private final String labelAdditionalPanel; |
105 |
private final JPanel pnlContainer; |
106 |
|
107 |
private final String defautltDescription; |
108 |
|
109 |
private String description; |
110 |
private Component additionalPanel; |
111 |
|
112 |
public InfoPanel(JPanel pnlContainer, String defautltDescription) { |
113 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
114 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
115 |
|
116 |
this.defautltDescription = defautltDescription;
|
117 |
this.pnlContainer = pnlContainer;
|
118 |
this.labelDescription = i18n.getTranslation("_Description"); |
119 |
this.labelAdditionalPanel = i18n.getTranslation("_Assistant"); |
120 |
|
121 |
this.txtDescription = new JEditorPane(); |
122 |
this.scrDescription = new JScrollPane(this.txtDescription); |
123 |
this.tab = new JTabbedPane(); |
124 |
|
125 |
toolsSwingManager.setDefaultPopupMenu(this.txtDescription);
|
126 |
this.txtDescription.setPreferredSize(new Dimension(200,200)); |
127 |
} |
128 |
|
129 |
public void clean() { |
130 |
this.description = null; |
131 |
this.additionalPanel = null; |
132 |
} |
133 |
|
134 |
public void setDescription(String description) { |
135 |
this.description = description;
|
136 |
} |
137 |
|
138 |
public void setAdditionalPanel(Component panel) { |
139 |
this.additionalPanel = panel;
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public JComponent asJComponent() { |
144 |
this.txtDescription.setContentType("text/html"); |
145 |
this.txtDescription.setText(this.description); |
146 |
this.txtDescription.setCaretPosition(0); |
147 |
|
148 |
if( StringUtils.isBlank(this.description) ) { |
149 |
if( this.additionalPanel==null ) { |
150 |
// Sin descripcion ni panel adicional, mostramos
|
151 |
// la descripcion por defecto.
|
152 |
this.txtDescription.setText(this.defautltDescription); |
153 |
this.txtDescription.setCaretPosition(0); |
154 |
return this.scrDescription; |
155 |
} |
156 |
// Sin descripcion pero con panel adicional, mostramos el
|
157 |
// panel adicional.
|
158 |
return this.additionalPanel.asJComponent(); |
159 |
} |
160 |
if( this.additionalPanel==null ) { |
161 |
// Con descripcion y sin panel adicional, mostramos
|
162 |
// la descripcion.
|
163 |
return this.scrDescription; |
164 |
} |
165 |
// Con descripcion y panel adicional, mostramos un tab con los dos.
|
166 |
this.tab.removeAll();
|
167 |
this.tab.add(
|
168 |
this.labelDescription,
|
169 |
this.scrDescription
|
170 |
); |
171 |
this.tab.add(
|
172 |
this.labelAdditionalPanel,
|
173 |
this.additionalPanel.asJComponent()
|
174 |
); |
175 |
this.tab.setSelectedIndex(1); |
176 |
return this.tab; |
177 |
} |
178 |
|
179 |
public void repaint() { |
180 |
this.pnlContainer.removeAll();
|
181 |
this.pnlContainer.add(this.asJComponent(), BorderLayout.CENTER); |
182 |
this.pnlContainer.revalidate();
|
183 |
this.pnlContainer.repaint();
|
184 |
} |
185 |
} |
186 |
|
187 |
private class ElementsTreeModel implements TreeModel { |
188 |
|
189 |
private final GroupElement root; |
190 |
private final HashSet<TreeModelListener> listeners; |
191 |
|
192 |
public ElementsTreeModel(GroupElement root) {
|
193 |
this.root = root;
|
194 |
this.listeners = new HashSet<>(); |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public Object getRoot() { |
199 |
return this.root; |
200 |
} |
201 |
|
202 |
public void reload() { |
203 |
this.reload(this.root); |
204 |
this.fireTreeChanged();
|
205 |
} |
206 |
|
207 |
private void reload(Element element) { |
208 |
if (element instanceof Element) { |
209 |
if (element instanceof GroupElement) { |
210 |
reload(element); |
211 |
} |
212 |
element.reload(); |
213 |
} |
214 |
} |
215 |
|
216 |
private List<Element> getChilds(final GroupElement node) { |
217 |
List<Element> x = new ArrayList<>(); |
218 |
for (Element element : node.getElements()) { |
219 |
if (element instanceof GroupElement) { |
220 |
x.add(element); |
221 |
} |
222 |
} |
223 |
return x; //node.getElements(); |
224 |
} |
225 |
|
226 |
@Override
|
227 |
public Object getChild(Object parent, int index) { |
228 |
List<Element> childs = getChilds((GroupElement) parent); |
229 |
Element child = childs.get(index);
|
230 |
return child;
|
231 |
} |
232 |
|
233 |
@Override
|
234 |
public int getChildCount(Object parent) { |
235 |
List<Element> childs = getChilds((GroupElement) parent); |
236 |
return childs.size();
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
public boolean isLeaf(Object node) { |
241 |
if( node instanceof GroupElement ) { |
242 |
return !((GroupElement) node).hasSubgroups();
|
243 |
} |
244 |
return false; |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public void valueForPathChanged(TreePath path, Object newValue) { |
249 |
} |
250 |
|
251 |
@Override
|
252 |
public int getIndexOfChild(Object parent, Object child) { |
253 |
List<Element> childs = getChilds((GroupElement) parent); |
254 |
int n = childs.indexOf(child);
|
255 |
return n;
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
public void addTreeModelListener(TreeModelListener l) { |
260 |
this.listeners.add(l);
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public void removeTreeModelListener(TreeModelListener l) { |
265 |
this.listeners.remove(l);
|
266 |
} |
267 |
|
268 |
protected void fireTreeChanged() { |
269 |
for (TreeModelListener listener : listeners) { |
270 |
TreeModelEvent e = new TreeModelEvent(this, new Object[] {this.root}); |
271 |
listener.treeNodesChanged(e); |
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
277 |
public DefaultJExpressionBuilder(ExpressionEvaluatorSwingManager manager, ExpressionBuilderConfig config) {
|
278 |
this.manager = manager;
|
279 |
if( config == null ) { |
280 |
this.config = new DefaultExpressionBuilderConfig(manager); |
281 |
} else {
|
282 |
this.config = config;
|
283 |
} |
284 |
this.initComponents();
|
285 |
} |
286 |
|
287 |
@Override
|
288 |
public ImageIcon loadImage(String imageName) { |
289 |
String name = FilenameUtils.getBaseName(imageName);
|
290 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
291 |
if (theme.exists(name)) {
|
292 |
return theme.get(name);
|
293 |
} |
294 |
URL url = this.getClass().getResource(name + ".png"); |
295 |
if (url == null) { |
296 |
return null; |
297 |
} |
298 |
return new ImageIcon(url); |
299 |
} |
300 |
|
301 |
private void initComponents() { |
302 |
final ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
|
303 |
final IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
|
304 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
305 |
|
306 |
this.automaticExpressionChecker = new AbstractAutomaticExpressionChecker( |
307 |
config, txtExpression |
308 |
) { |
309 |
@Override
|
310 |
protected Expression getExpression() { |
311 |
return DefaultJExpressionBuilder.this.getExpression();
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
protected void setMessage(int mode, String text) { |
316 |
super.setMessage(mode, text);
|
317 |
lblMsg.setText("<html>"+StringUtils.abbreviate(text,100)+"</html>"); |
318 |
} |
319 |
|
320 |
@Override
|
321 |
protected void setPreview(Object value) { |
322 |
super.setPreview(value);
|
323 |
lblMsg.setText("<html>"+StringUtils.abbreviate(this.getPreview(),100)+"</html>"); |
324 |
} |
325 |
|
326 |
@Override
|
327 |
protected void setTip(String theTip) { |
328 |
super.setTip(theTip);
|
329 |
btnTip.setToolTipText(this.getTip());
|
330 |
btnTip.setVisible(this.isTipEnabled());
|
331 |
} |
332 |
|
333 |
}; |
334 |
this.infoPanel = new InfoPanel(this.pnlDescription, load_description_from_resource()); |
335 |
|
336 |
this.btnTip.setVisible(false); |
337 |
this.btnTip.addActionListener(new ActionListener() { |
338 |
@Override
|
339 |
public void actionPerformed(ActionEvent e) { |
340 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
341 |
dialogs.messageDialog( |
342 |
btnTip.getToolTipText(), |
343 |
"Tip",
|
344 |
JOptionPane.INFORMATION_MESSAGE
|
345 |
); |
346 |
} |
347 |
}); |
348 |
|
349 |
this.defaultIconGroup = theme.get("expressionbuilder-element-group"); |
350 |
this.defaultIconElement = theme.get("expressionbuilder-element"); |
351 |
|
352 |
toolsSwingManager.translate(this.tabExpressionBuilder);
|
353 |
toolsSwingManager.setDefaultPopupMenu(this.txtGroupElement);
|
354 |
toolsSwingManager.setDefaultPopupMenu(this.txtSimpleElementFilter);
|
355 |
toolsSwingManager.addClearButton(this.txtSimpleElementFilter, new ActionListener() { |
356 |
@Override
|
357 |
public void actionPerformed(ActionEvent e) { |
358 |
txtSimpleElementFilter.setText("");
|
359 |
doFilter(); |
360 |
} |
361 |
}); |
362 |
JMenuItem msgMenuTextEditor = new JMenuItem(i18n.getTranslation("text_editor")); |
363 |
msgMenuTextEditor.addActionListener(new ActionListener() { |
364 |
@Override
|
365 |
public void actionPerformed(ActionEvent e) { |
366 |
automaticExpressionChecker.showMessageDialog(); |
367 |
} |
368 |
}); |
369 |
JMenuItem msgMenuCopy = new JMenuItem(i18n.getTranslation("copy")); |
370 |
msgMenuTextEditor.addActionListener(new ActionListener() { |
371 |
@Override
|
372 |
public void actionPerformed(ActionEvent e) { |
373 |
StringSelection selection = new StringSelection(automaticExpressionChecker.getMessage()); |
374 |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
375 |
clipboard.setContents(selection, selection); |
376 |
} |
377 |
}); |
378 |
|
379 |
JPopupMenu msgMenu = new JPopupMenu(); |
380 |
msgMenu.add(msgMenuTextEditor); |
381 |
msgMenu.addSeparator(); |
382 |
msgMenu.add(msgMenuCopy); |
383 |
this.lblMsg.setComponentPopupMenu(msgMenu);
|
384 |
|
385 |
this.pnlDescription.setLayout(new BorderLayout()); |
386 |
// this.pnlDescription.setBackground(this.txtDescription.getBackground());
|
387 |
this.txtExpression.addCaretListener(new CaretListener() { |
388 |
@Override
|
389 |
public void caretUpdate(CaretEvent e) { |
390 |
lblColumn.setText(Integer.toString(e.getDot()));
|
391 |
} |
392 |
}); |
393 |
this.treeElements.setCellRenderer(new DefaultTreeCellRenderer() { |
394 |
@Override
|
395 |
public java.awt.Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
396 |
JLabel label = (JLabel) super.getTreeCellRendererComponent(tree,value, selected, expanded, leaf, row, hasFocus); |
397 |
if( value instanceof Element ) { |
398 |
Element element = (Element) value; |
399 |
if( element.getIconName()==null ) { |
400 |
if( element instanceof GroupElement ) { |
401 |
label.setIcon(defaultIconGroup); |
402 |
} else {
|
403 |
label.setIcon(defaultIconElement); |
404 |
} |
405 |
} else {
|
406 |
label.setIcon(element.getIcon()); |
407 |
} |
408 |
label.setText(element.getName()); |
409 |
label.setEnabled(element.isEnabled()); |
410 |
} else {
|
411 |
label.setIcon(null);
|
412 |
label.setText(Objects.toString(value, ""));
|
413 |
} |
414 |
return label;
|
415 |
} |
416 |
}); |
417 |
|
418 |
this.treeElements.addTreeSelectionListener(new TreeSelectionListener() { |
419 |
@Override
|
420 |
public void valueChanged(TreeSelectionEvent e) { |
421 |
TreePath path = e.getPath();
|
422 |
Element element = (Element) path.getLastPathComponent(); |
423 |
if (element instanceof GroupElement) { |
424 |
updateSimpleElementsList((GroupElement) element); |
425 |
} |
426 |
} |
427 |
}); |
428 |
this.treeElements.addMouseListener(new MouseAdapter() { |
429 |
@Override
|
430 |
public void mouseClicked(MouseEvent e) { |
431 |
if( e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2 ) { |
432 |
TreePath path = treeElements.getSelectionPath();
|
433 |
if( path!=null ) { |
434 |
Element element = (Element) path.getLastPathComponent(); |
435 |
if( element instanceof SimpleElement ) { |
436 |
doInsert((SimpleElement) element); |
437 |
} |
438 |
} |
439 |
} |
440 |
} |
441 |
}); |
442 |
this.btnGroupElementInsert.addActionListener(new ActionListener() { |
443 |
@Override
|
444 |
public void actionPerformed(ActionEvent e) { |
445 |
doInsert(txtGroupElement.getText()); |
446 |
} |
447 |
}); |
448 |
this.btnSimpleElementInsert.addActionListener(new ActionListener() { |
449 |
@Override
|
450 |
public void actionPerformed(ActionEvent e) { |
451 |
ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
452 |
if (element != null) { |
453 |
doInsert(element.getValue()); |
454 |
} |
455 |
} |
456 |
}); |
457 |
this.btnSimpleElementFilter.addActionListener(new ActionListener() { |
458 |
@Override
|
459 |
public void actionPerformed(ActionEvent e) { |
460 |
doFilter(); |
461 |
} |
462 |
}); |
463 |
|
464 |
this.lstSimpleElement.setModel(new FilteredListModel()); |
465 |
this.lstSimpleElement.setCellRenderer(new DefaultListCellRenderer() { |
466 |
@Override
|
467 |
public java.awt.Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
468 |
JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
469 |
Element element = null; |
470 |
if( value instanceof ListElement ) { |
471 |
if( ((ListElement) value).getValue() instanceof Element ) { |
472 |
element = (Element) ((ListElement) value).getValue();
|
473 |
} |
474 |
} |
475 |
if( element !=null ) { |
476 |
if( element.getIconName()==null ) { |
477 |
if( element instanceof GroupElement ) { |
478 |
label.setIcon(defaultIconGroup); |
479 |
} else {
|
480 |
label.setIcon(defaultIconElement); |
481 |
} |
482 |
} else {
|
483 |
label.setIcon(element.getIcon()); |
484 |
} |
485 |
label.setText(element.getName()); |
486 |
label.setEnabled(element.isEnabled()); |
487 |
} else {
|
488 |
label.setIcon(null);
|
489 |
label.setText(Objects.toString(value, ""));
|
490 |
} |
491 |
return label;
|
492 |
} |
493 |
}); |
494 |
this.lstSimpleElement.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
495 |
@Override
|
496 |
public void valueChanged(ListSelectionEvent e) { |
497 |
ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
498 |
if (element != null) { |
499 |
doSelectElement(element.getValue()); |
500 |
} |
501 |
} |
502 |
}); |
503 |
this.lstSimpleElement.addMouseListener(new MouseAdapter() { |
504 |
@Override
|
505 |
public void mouseClicked(MouseEvent e) { |
506 |
if( e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2 ) { |
507 |
ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
508 |
if (element != null) { |
509 |
doSelectElement(element.getValue()); |
510 |
doInsert(element.getValue()); |
511 |
} |
512 |
} |
513 |
} |
514 |
}); |
515 |
this.lstSimpleElement.addKeyListener(new KeyAdapter() { |
516 |
@Override
|
517 |
public void keyReleased(KeyEvent e) { |
518 |
if( e.getKeyCode()==KeyEvent.VK_ENTER ) { |
519 |
ListElement<SimpleElement> element = (ListElement<SimpleElement>) lstSimpleElement.getSelectedValue(); |
520 |
if (element != null) { |
521 |
doSelectElement(element.getValue()); |
522 |
doInsert(element.getValue()); |
523 |
} |
524 |
} |
525 |
} |
526 |
}); |
527 |
|
528 |
this.btnSimpleElementTimeLimit.addActionListener(new ActionListener() { |
529 |
@Override
|
530 |
public void actionPerformed(ActionEvent e) { |
531 |
doSetTimeLimit(); |
532 |
} |
533 |
}); |
534 |
this.btnSimpleElementSortDown.addActionListener(new ActionListener() { |
535 |
@Override
|
536 |
public void actionPerformed(ActionEvent e) { |
537 |
FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
538 |
model.sort(false);
|
539 |
} |
540 |
}); |
541 |
this.btnSimpleElementSortUp.addActionListener(new ActionListener() { |
542 |
@Override
|
543 |
public void actionPerformed(ActionEvent e) { |
544 |
FilteredListModel model = (FilteredListModel) lstSimpleElement.getModel(); |
545 |
model.sort(true);
|
546 |
} |
547 |
}); |
548 |
this.txtSimpleElementFilter.addKeyListener(new KeyAdapter() { |
549 |
@Override
|
550 |
public void keyTyped(KeyEvent e) { |
551 |
if (e.getKeyChar() == '\n') { |
552 |
doFilter(); |
553 |
} |
554 |
} |
555 |
}); |
556 |
this.btnAdd.addActionListener(new ActionListener() { |
557 |
@Override
|
558 |
public void actionPerformed(ActionEvent e) { |
559 |
doInsert(" + ");
|
560 |
} |
561 |
}); |
562 |
this.btnDiv.addActionListener(new ActionListener() { |
563 |
@Override
|
564 |
public void actionPerformed(ActionEvent e) { |
565 |
doInsert(" / ");
|
566 |
} |
567 |
}); |
568 |
this.btnEq.addActionListener(new ActionListener() { |
569 |
@Override
|
570 |
public void actionPerformed(ActionEvent e) { |
571 |
doInsert(" = ");
|
572 |
} |
573 |
}); |
574 |
this.btnMult.addActionListener(new ActionListener() { |
575 |
@Override
|
576 |
public void actionPerformed(ActionEvent e) { |
577 |
doInsert(" * ");
|
578 |
} |
579 |
}); |
580 |
this.btnNeq.addActionListener(new ActionListener() { |
581 |
@Override
|
582 |
public void actionPerformed(ActionEvent e) { |
583 |
doInsert(" <> ");
|
584 |
} |
585 |
}); |
586 |
this.btnParentClose.addActionListener(new ActionListener() { |
587 |
@Override
|
588 |
public void actionPerformed(ActionEvent e) { |
589 |
doInsert(" ) ");
|
590 |
} |
591 |
}); |
592 |
this.btnParentOpen.addActionListener(new ActionListener() { |
593 |
@Override
|
594 |
public void actionPerformed(ActionEvent e) { |
595 |
doInsert(" ( ");
|
596 |
} |
597 |
}); |
598 |
this.btnSubst.addActionListener(new ActionListener() { |
599 |
@Override
|
600 |
public void actionPerformed(ActionEvent e) { |
601 |
doInsert(" - ");
|
602 |
} |
603 |
}); |
604 |
ScriptSwingManager scriptSwingManager = ToolsSwingLocator.getScriptSwingManager(); |
605 |
this.scriptPicker = scriptSwingManager.createListScriptPickerController(
|
606 |
cboPickerScripts, |
607 |
btnPickerRemove, |
608 |
btnPickerSelectScript |
609 |
); |
610 |
this.scriptEditor = scriptSwingManager.createScriptEditor();
|
611 |
this.pnlScriptEditorContainer.setLayout(new BorderLayout()); |
612 |
this.pnlScriptEditorContainer.add(this.scriptEditor.asJComponent(), BorderLayout.CENTER); |
613 |
|
614 |
this.tabExpressionBuilder.setEnabledAt(0, this.config.getScriptsEnabled()); |
615 |
|
616 |
this.infoPanel.repaint();
|
617 |
|
618 |
this.setPreferredSize(new Dimension(650, 350)); |
619 |
} |
620 |
|
621 |
private String removeCursorMark(String s) { |
622 |
if( s == null ) { |
623 |
return null; |
624 |
} |
625 |
s = s.replace("{{", ""); |
626 |
s = s.replace("}}", ""); |
627 |
return s;
|
628 |
} |
629 |
|
630 |
@Override
|
631 |
public void insertText(String text) { |
632 |
doInsert(text); |
633 |
} |
634 |
|
635 |
@Override
|
636 |
public String getText() { |
637 |
return this.txtExpression.getText(); |
638 |
} |
639 |
|
640 |
public void setText(String text) { |
641 |
this.txtExpression.setText(text);
|
642 |
} |
643 |
|
644 |
private void doInsert(String s) { |
645 |
this.txtExpression.replaceSelection(s);
|
646 |
String ss = this.txtExpression.getText(); |
647 |
int start = ss.indexOf("{{"); |
648 |
int end = ss.indexOf("}}"); |
649 |
if (start > 0 && end > 0) { |
650 |
this.txtExpression.replaceRange("", end, end + 2); |
651 |
this.txtExpression.replaceRange("", start, start + 2); |
652 |
this.txtExpression.setSelectionStart(start);
|
653 |
this.txtExpression.setSelectionEnd(end - 2); |
654 |
} |
655 |
this.txtExpression.requestFocusInWindow();
|
656 |
} |
657 |
|
658 |
private void doInsert(SimpleElement element) { |
659 |
if( element instanceof FunctionElement ) { |
660 |
Function function = ((FunctionElement)element).getFunction(); |
661 |
if (function == null) { |
662 |
doInsert(element.getName()); |
663 |
} else {
|
664 |
doInsert(function.template() == null ? function.name() + "({{}})" : function.template()); |
665 |
} |
666 |
} else if( element instanceof CalculatedElement) { |
667 |
Object value = ((CalculatedElement) element).getValue();
|
668 |
if( value != null ) { |
669 |
doInsert(" "+value.toString()+" "); |
670 |
} |
671 |
} else if( element instanceof ConstantElement) { |
672 |
Object value = ((ConstantElement) element).getValue();
|
673 |
doInsert(" "+Utils.repr(value)+" "); |
674 |
} else {
|
675 |
doInsert(" "+element.getName()+" "); |
676 |
} |
677 |
} |
678 |
|
679 |
private void doFilter() { |
680 |
FilteredListModel model = (FilteredListModel) this.lstSimpleElement.getModel();
|
681 |
model.setFilter(this.txtSimpleElementFilter.getText());
|
682 |
} |
683 |
|
684 |
private void doSelectElement(SimpleElement element) { |
685 |
// if( !element.isEnabled() ) {
|
686 |
// this.infoPanel.clean();
|
687 |
// } else {
|
688 |
this.infoPanel.setDescription(removeCursorMark(element.getDescription()));
|
689 |
this.infoPanel.setAdditionalPanel(element.getAditionalPanel(this)); |
690 |
this.infoPanel.repaint();
|
691 |
// }
|
692 |
} |
693 |
|
694 |
private void updateSimpleElementsList(GroupElement group) { |
695 |
FilteredListModel model = new FilteredListModel();
|
696 |
model.addElement("Loading...");
|
697 |
this.currentGroup = group;
|
698 |
this.txtGroupElement.setText(group.getName());
|
699 |
this.txtSimpleElementFilter.setText(""); |
700 |
this.infoPanel.clean();
|
701 |
this.infoPanel.repaint();
|
702 |
if (group instanceof SimpleElement) { |
703 |
this.btnGroupElementInsert.setEnabled(true); |
704 |
} else {
|
705 |
this.btnGroupElementInsert.setEnabled(false); |
706 |
} |
707 |
this.lstSimpleElement.setEnabled(false); |
708 |
this.lstSimpleElement.setModel(model);
|
709 |
|
710 |
Thread th = new Thread(new Runnable() { |
711 |
@Override
|
712 |
public void run() { |
713 |
simpleElementsUpdater(); |
714 |
} |
715 |
}, "ExpressionBuilderSimpleElementsUpdater");
|
716 |
th.start(); |
717 |
} |
718 |
|
719 |
private void simpleElementsUpdater() { |
720 |
final GroupElement group = this.currentGroup; |
721 |
final FilteredListModel model = new FilteredListModel(); |
722 |
for (Element child : group) { |
723 |
if (group != this.currentGroup) { |
724 |
return;
|
725 |
} |
726 |
if (child instanceof SimpleElement) { |
727 |
String label = child.getName();
|
728 |
Function function = child instanceof FunctionElement? ((FunctionElement)child).getFunction(): null; |
729 |
if (function != null && !StringUtils.isEmpty(function.returnType())) { |
730 |
label = "<html><b>" + label + "</b> return " + function.returnType() + "</html>"; |
731 |
// label = label + " return " + function.returnType();
|
732 |
} |
733 |
model.addElement(new ListElement<>(label, child));
|
734 |
} |
735 |
} |
736 |
model.sort(true);
|
737 |
SwingUtilities.invokeLater(new Runnable() { |
738 |
@Override
|
739 |
public void run() { |
740 |
if (group != currentGroup) {
|
741 |
return;
|
742 |
} |
743 |
if( group.hasMoreElements() ) {
|
744 |
lblSimpleElementsMsg.setText("More elements...");
|
745 |
} else {
|
746 |
lblSimpleElementsMsg.setText("");
|
747 |
} |
748 |
lstSimpleElement.setEnabled(true);
|
749 |
lstSimpleElement.setModel(model); |
750 |
} |
751 |
}); |
752 |
|
753 |
} |
754 |
|
755 |
@Override
|
756 |
public Expression getExpression() { |
757 |
String phrase = this.txtExpression.getText(); |
758 |
if (StringUtils.isEmpty(phrase)) {
|
759 |
return null; |
760 |
} |
761 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
762 |
expression.setPhrase(phrase); |
763 |
Script script = ToolsLocator.getScriptManager().createScript("UserScript", "", "python"); |
764 |
this.scriptEditor.fetch(script);
|
765 |
if( !StringUtils.isEmpty(script.getCode()) ) {
|
766 |
expression.setUserScript(script); |
767 |
} |
768 |
return expression;
|
769 |
} |
770 |
|
771 |
@Override
|
772 |
public void setExpression(Expression expression) { |
773 |
if( expression==null ) { |
774 |
this.txtExpression.setText(""); |
775 |
try { this.scriptEditor.set(null); } catch(Exception ex) {} |
776 |
this.scriptPicker.set(null); |
777 |
} else {
|
778 |
this.txtExpression.setText(expression.getPhrase());
|
779 |
if( expression.getUserScript()!=null ) { |
780 |
this.scriptEditor.set(expression.getUserScript());
|
781 |
} |
782 |
if( expression.getScripts()!=null ) { |
783 |
this.scriptPicker.set(expression.getScripts().toList());
|
784 |
} |
785 |
} |
786 |
} |
787 |
|
788 |
@Override
|
789 |
public int getSimpleElementsLimit() { |
790 |
return this.config.getSimpleElementsLimit(); |
791 |
} |
792 |
|
793 |
@Override
|
794 |
public void setSimpleElementsLimit(int limit) { |
795 |
this.config.setSimpleElementsLimit(limit);
|
796 |
} |
797 |
|
798 |
@Override
|
799 |
public List<Element> getElements() { |
800 |
return this.config.getElements(); |
801 |
} |
802 |
|
803 |
@Override
|
804 |
public void addElement(Element element) { |
805 |
this.config.addElement(element);
|
806 |
} |
807 |
|
808 |
@Override
|
809 |
public Collection<SymbolTable> getSymbolTables() { |
810 |
return this.config.getSymbolTables(); |
811 |
} |
812 |
|
813 |
@Override
|
814 |
public void addSymbolTable(String name) { |
815 |
this.config.addSymbolTable(name);
|
816 |
} |
817 |
|
818 |
@Override
|
819 |
public void addSymbolTable(SymbolTable symbolTable) { |
820 |
this.config.addSymbolTable(symbolTable);
|
821 |
} |
822 |
|
823 |
@Override
|
824 |
public void removeAllSymbolTables() { |
825 |
this.config.removeAllSymbolTables();
|
826 |
} |
827 |
|
828 |
@Override
|
829 |
public void removeSymbolTable(String name) { |
830 |
this.config.removeSymbolTable(name);
|
831 |
} |
832 |
|
833 |
@Override
|
834 |
public SymbolTable getPreviewSymbolTable() {
|
835 |
return this.config.getPreviewSymbolTable(); |
836 |
} |
837 |
|
838 |
@Override
|
839 |
public void setPreviewSymbolTable(SymbolTable symbolTable) { |
840 |
this.config.setPreviewSymbolTable(symbolTable);
|
841 |
} |
842 |
|
843 |
@Override
|
844 |
public void setScriptsEnabled(boolean enabled) { |
845 |
this.config.setScriptsEnabled(enabled);
|
846 |
this.tabExpressionBuilder.setEnabledAt(0, this.config.getScriptsEnabled()); |
847 |
} |
848 |
|
849 |
@Override
|
850 |
public boolean getScriptsEnabled() { |
851 |
return this.config.getScriptsEnabled(); |
852 |
} |
853 |
|
854 |
@Override
|
855 |
public void setSQLCompatible(boolean sqlCompatible) { |
856 |
this.config.setSQLCompatible(sqlCompatible);
|
857 |
} |
858 |
|
859 |
@Override
|
860 |
public boolean isSQLCompatible() { |
861 |
return this.config.isSQLCompatible(); |
862 |
} |
863 |
|
864 |
@Override
|
865 |
public Object getProperty(String string) { |
866 |
return this.config.getProperty(string); |
867 |
} |
868 |
|
869 |
@Override
|
870 |
public void setProperty(String string, Object o) { |
871 |
this.config.setProperty(string, o);
|
872 |
} |
873 |
|
874 |
@Override
|
875 |
public Map<String, Object> getProperties() { |
876 |
return this.config.getProperties(); |
877 |
} |
878 |
|
879 |
@Override
|
880 |
public JComponent asJComponent() { |
881 |
this.initializeTree();
|
882 |
return this; |
883 |
} |
884 |
|
885 |
private void initializeTree() { |
886 |
if( this.treeModel == null ) { |
887 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
888 |
this.treeModel = new ElementsTreeModel( |
889 |
this.manager.createGroupElement(
|
890 |
this.config,
|
891 |
i18n.getTranslation("_Elements"),
|
892 |
this.getElements()
|
893 |
) |
894 |
); |
895 |
this.treeElements.setModel(this.treeModel); |
896 |
} |
897 |
} |
898 |
|
899 |
private void doSetTimeLimit() { |
900 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
901 |
String s = dialogs.inputDialog(
|
902 |
"Indicate the time limit in seconds for calculating the elements",
|
903 |
"Time limit",
|
904 |
JOptionPane.QUESTION_MESSAGE,
|
905 |
String.valueOf(this.config.getSimpleElementsLimit()) |
906 |
); |
907 |
if( StringUtils.isBlank(s) ) {
|
908 |
return;
|
909 |
} |
910 |
try {
|
911 |
int n = Integer.parseInt(s); |
912 |
this.config.setSimpleElementsLimit(n);
|
913 |
this.treeModel.reload();
|
914 |
} catch(Exception ex) { |
915 |
dialogs.messageDialog( |
916 |
"Invalid time limit '"+s+"'.", |
917 |
"Time limit",
|
918 |
JOptionPane.WARNING_MESSAGE
|
919 |
); |
920 |
} |
921 |
} |
922 |
|
923 |
private String load_description_from_resource() { |
924 |
String lang = Locale.getDefault().getLanguage(); |
925 |
URL url = this.getClass().getResource("/org/gvsig/expressionevaluator/swing/jexpressionbuilder/"+lang+"/description.html"); |
926 |
if( url == null ) { |
927 |
url = this.getClass().getResource("/org/gvsig/expressionevaluator/swing/jexpressionbuilder/en/description.html"); |
928 |
if( url == null ) { |
929 |
return null; |
930 |
} |
931 |
} |
932 |
InputStream is = null; |
933 |
try {
|
934 |
is = url.openStream(); |
935 |
List<String> lines = IOUtils.readLines(is); |
936 |
return StringUtils.join(lines, "\n"); |
937 |
} catch (Exception ex) { |
938 |
return null; |
939 |
} finally {
|
940 |
IOUtils.closeQuietly(is); |
941 |
} |
942 |
} |
943 |
|
944 |
@Override
|
945 |
public void expandElement(Element element) { |
946 |
this.initializeTree();
|
947 |
for (int i = 0; i < this.treeElements.getRowCount(); i++) { |
948 |
TreePath path = this.treeElements.getPathForRow(i); |
949 |
if( path.getLastPathComponent()==element ) {
|
950 |
// FIXME: habria que expandir los padres hasta llegar a este.
|
951 |
this.treeElements.expandPath(path);
|
952 |
return;
|
953 |
} |
954 |
} |
955 |
} |
956 |
} |