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