Revision 44006 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 | 1 |
package org.gvsig.expressionevaluator.swing.impl; |
2 | 2 |
|
3 | 3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
|
5 | 4 |
import java.awt.Dimension; |
6 | 5 |
import java.awt.event.ActionEvent; |
7 | 6 |
import java.awt.event.ActionListener; |
... | ... | |
19 | 18 |
import javax.swing.DefaultListCellRenderer; |
20 | 19 |
import javax.swing.ImageIcon; |
21 | 20 |
import javax.swing.JComponent; |
21 |
import javax.swing.JEditorPane; |
|
22 | 22 |
import javax.swing.JLabel; |
23 | 23 |
import javax.swing.JList; |
24 | 24 |
import javax.swing.JOptionPane; |
... | ... | |
41 | 41 |
import org.gvsig.expressionevaluator.Function; |
42 | 42 |
import org.gvsig.expressionevaluator.SymbolTable; |
43 | 43 |
import org.gvsig.expressionevaluator.swing.Element; |
44 |
import org.gvsig.expressionevaluator.swing.Element.CalculatedElement; |
|
44 | 45 |
import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
45 | 46 |
import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
46 | 47 |
import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
... | ... | |
51 | 52 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
52 | 53 |
import org.gvsig.tools.ToolsLocator; |
53 | 54 |
import org.gvsig.tools.script.Script; |
55 |
import org.gvsig.tools.swing.api.Component; |
|
54 | 56 |
import org.gvsig.tools.swing.api.ListElement; |
55 | 57 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
56 | 58 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
... | ... | |
74 | 76 |
|
75 | 77 |
private final ExpressionBuilderConfig config; |
76 | 78 |
private ElementsTreeModel treeModel; |
79 |
private java.awt.Component infoPanel = null; |
|
80 |
private JEditorPane txtDescription; |
|
81 |
private ImageIcon defaultIconGroup; |
|
82 |
private ImageIcon defaultIconElement; |
|
77 | 83 |
|
78 | 84 |
private class ElementsTreeModel implements TreeModel { |
79 | 85 |
|
... | ... | |
192 | 198 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
193 | 199 |
final IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
194 | 200 |
|
201 |
this.defaultIconGroup = theme.get("expressionbuilder-element-group"); |
|
202 |
this.defaultIconElement = theme.get("expressionbuilder-element"); |
|
203 |
|
|
204 |
this.txtDescription = new JEditorPane(); |
|
205 |
|
|
195 | 206 |
toolsSwingManager.setDefaultPopupMenu(this.txtExpression); |
196 | 207 |
toolsSwingManager.setDefaultPopupMenu(this.txtGroupElement); |
197 | 208 |
toolsSwingManager.setDefaultPopupMenu(this.txtSimpleElementFilter); |
... | ... | |
204 | 215 |
doFilter(); |
205 | 216 |
} |
206 | 217 |
}); |
218 |
this.pnlDescription.setLayout(new BorderLayout()); |
|
219 |
this.pnlDescription.setBackground(this.txtDescription.getBackground()); |
|
207 | 220 |
|
208 | 221 |
this.treeElements.setCellRenderer(new DefaultTreeCellRenderer() { |
209 | 222 |
@Override |
210 |
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
|
223 |
public java.awt.Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
|
|
211 | 224 |
JLabel label = (JLabel) super.getTreeCellRendererComponent(tree,value, selected, expanded, leaf, row, hasFocus); |
212 | 225 |
if( value instanceof Element ) { |
213 | 226 |
Element element = (Element) value; |
214 |
label.setIcon(element.getIcon()); |
|
227 |
if( element.getIconName()==null ) { |
|
228 |
if( element instanceof GroupElement ) { |
|
229 |
label.setIcon(defaultIconGroup); |
|
230 |
} else { |
|
231 |
label.setIcon(defaultIconElement); |
|
232 |
} |
|
233 |
} else { |
|
234 |
label.setIcon(element.getIcon()); |
|
235 |
} |
|
215 | 236 |
label.setText(element.getName()); |
216 | 237 |
} else { |
217 | 238 |
label.setIcon(null); |
... | ... | |
256 | 277 |
this.lstSimpleElement.setModel(new FilteredListModel()); |
257 | 278 |
this.lstSimpleElement.setCellRenderer(new DefaultListCellRenderer() { |
258 | 279 |
@Override |
259 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
|
280 |
public java.awt.Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
|
260 | 281 |
JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
261 | 282 |
Element element = null; |
262 | 283 |
if( value instanceof ListElement ) { |
... | ... | |
265 | 286 |
} |
266 | 287 |
} |
267 | 288 |
if( element !=null ) { |
268 |
label.setIcon(element.getIcon()); |
|
289 |
if( element.getIconName()==null ) { |
|
290 |
if( element instanceof GroupElement ) { |
|
291 |
label.setIcon(defaultIconGroup); |
|
292 |
} else { |
|
293 |
label.setIcon(defaultIconElement); |
|
294 |
} |
|
295 |
} else { |
|
296 |
label.setIcon(element.getIcon()); |
|
297 |
} |
|
269 | 298 |
label.setText(element.getName()); |
270 | 299 |
} else { |
271 | 300 |
label.setIcon(null); |
... | ... | |
427 | 456 |
} else { |
428 | 457 |
doInsert(function.template() == null ? function.name() + "({{}})" : function.template()); |
429 | 458 |
} |
459 |
} else if( element instanceof CalculatedElement) { |
|
460 |
Object value = ((CalculatedElement) element).getValue(); |
|
461 |
if( value != null ) { |
|
462 |
doInsert(" "+value.toString()+" "); |
|
463 |
} |
|
430 | 464 |
} else if( element instanceof ConstantElement) { |
431 |
doInsert(" "+repr(((ConstantElement) element).getValue())+" "); |
|
465 |
Object value = ((ConstantElement) element).getValue(); |
|
466 |
doInsert(" "+repr(value)+" "); |
|
432 | 467 |
} else { |
433 | 468 |
doInsert(" "+element.getName()+" "); |
434 | 469 |
} |
... | ... | |
440 | 475 |
} |
441 | 476 |
|
442 | 477 |
private void doSelectElement(SimpleElement element) { |
443 |
StringBuilder html = new StringBuilder(); |
|
444 |
html.append("<html>\n"); |
|
445 |
if( element instanceof ConstantElement ) { |
|
446 |
Object value = ((ConstantElement)element).getValue(); |
|
447 |
if( value == null ) { |
|
448 |
html.append("<b>Constant</b><br>\n<br>\n"); |
|
449 |
html.append("<b>Value</b> NULL<br>\n"); |
|
450 |
} else { |
|
451 |
html.append("<b>Constant</b><br>\n<br>\n"); |
|
452 |
html.append("<b>Value</b> ").append(repr(value)).append("<br>\n"); |
|
453 |
html.append("<b>Type</b> ").append(value.getClass().getSimpleName()).append("<br>\n"); |
|
454 |
} |
|
455 |
|
|
456 |
} else if( element instanceof VariableElement ) { |
|
457 |
VariableElement var = (VariableElement)element; |
|
458 |
html.append("<b>").append(var.getLabel()).append("</b><br>\n<br>\n"); |
|
459 |
html.append("<b>Source</b> ").append(var.getSource()).append("<br>\n"); |
|
460 |
html.append("<b>Name</b> ").append(var.getName()).append("<br>\n"); |
|
461 |
html.append("<b>Type</b> ").append(var.getTypeName()).append("<br>\n"); |
|
462 |
|
|
463 |
} else if( element instanceof FunctionElement ) { |
|
464 |
Function function = ((FunctionElement)element).getFunction(); |
|
465 |
if (function == null) { |
|
466 |
html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
|
467 |
html.append("<b>Return</b> Object<br>\n<br>\n"); |
|
468 |
html.append("<b>Template:</b> ").append(element.getName()).append("()<br>\n"); |
|
469 |
} else { |
|
470 |
html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
|
471 |
html.append("<b>Return</b> "); |
|
472 |
html.append(StringUtils.isEmpty(function.returnType())?"Objec": function.returnType()); |
|
473 |
html.append("<br>\n"); |
|
474 |
html.append("<b>Template</b> "); |
|
475 |
html.append(StringUtils.isEmpty(function.template())?element.getName()+"()": removeCursorMark(function.template())); |
|
476 |
html.append("<br>\n"); |
|
478 |
Component aditionalPanel = element.getAditionalPanel(); |
|
479 |
java.awt.Component newInfoPanel = null; |
|
480 |
if( aditionalPanel == null ) { |
|
481 |
StringBuilder html = new StringBuilder(); |
|
482 |
html.append("<html>\n"); |
|
483 |
if( element instanceof ConstantElement ) { |
|
484 |
Object value = ((ConstantElement)element).getValue(); |
|
485 |
if( value == null ) { |
|
486 |
html.append("<b>Constant</b><br>\n<br>\n"); |
|
487 |
html.append("<b>Value</b> NULL<br>\n"); |
|
488 |
} else { |
|
489 |
html.append("<b>Constant</b><br>\n<br>\n"); |
|
490 |
html.append("<b>Value</b> ").append(repr(value)).append("<br>\n"); |
|
491 |
html.append("<b>Type</b> ").append(value.getClass().getSimpleName()).append("<br>\n"); |
|
492 |
} |
|
477 | 493 |
|
478 |
String[] args = function.descriptionArgs(); |
|
479 |
if (args != null) { |
|
480 |
html.append("<b>Arguments:</b> ").append("<ul>\n"); |
|
481 |
for (String arg : args) { |
|
482 |
html.append("<li>").append(arg).append("</li>\n"); |
|
494 |
} else if( element instanceof VariableElement ) { |
|
495 |
VariableElement var = (VariableElement)element; |
|
496 |
html.append("<b>").append(var.getLabel()).append("</b><br>\n<br>\n"); |
|
497 |
html.append("<b>Source</b> ").append(var.getSource()).append("<br>\n"); |
|
498 |
html.append("<b>Name</b> ").append(var.getName()).append("<br>\n"); |
|
499 |
html.append("<b>Type</b> ").append(var.getTypeName()).append("<br>\n"); |
|
500 |
|
|
501 |
} else if( element instanceof FunctionElement ) { |
|
502 |
Function function = ((FunctionElement)element).getFunction(); |
|
503 |
if (function == null) { |
|
504 |
html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
|
505 |
html.append("<b>Return</b> Object<br>\n<br>\n"); |
|
506 |
html.append("<b>Template:</b> ").append(element.getName()).append("()<br>\n"); |
|
507 |
} else { |
|
508 |
html.append("<b>Function</b> ").append(element.getName()).append("<br>\n<br>\n"); |
|
509 |
html.append("<b>Return</b> "); |
|
510 |
html.append(StringUtils.isEmpty(function.returnType())?"Objec": function.returnType()); |
|
511 |
html.append("<br>\n"); |
|
512 |
html.append("<b>Template</b> "); |
|
513 |
html.append(StringUtils.isEmpty(function.template())?element.getName()+"()": removeCursorMark(function.template())); |
|
514 |
html.append("<br>\n"); |
|
515 |
|
|
516 |
String[] args = function.descriptionArgs(); |
|
517 |
if (args != null) { |
|
518 |
html.append("<b>Arguments:</b> ").append("<ul>\n"); |
|
519 |
for (String arg : args) { |
|
520 |
html.append("<li>").append(arg).append("</li>\n"); |
|
521 |
} |
|
522 |
html.append("</ul>\n"); |
|
523 |
html.append("<br>\n"); |
|
483 | 524 |
} |
484 |
html.append("</ul>\n"); |
|
485 |
html.append("<br>\n"); |
|
486 | 525 |
} |
526 |
} else { |
|
527 |
html.append("<b>Generic element<br>\n<br>\n"); |
|
528 |
html.append("<b>Name</b> ").append(element.getName()).append("</br>\n"); |
|
529 |
|
|
487 | 530 |
} |
531 |
|
|
532 |
if( element.getDescription()!=null ) { |
|
533 |
html.append("<b>Description</b><br>\n"); |
|
534 |
html.append(element.getDescription().replace("\n", "<br>")).append("<br>\n"); |
|
535 |
} |
|
536 |
html.append("</html>\n"); |
|
537 |
this.txtDescription.setContentType("text/html"); |
|
538 |
this.txtDescription.setText(html.toString()); |
|
539 |
this.txtDescription.setCaretPosition(0); |
|
540 |
newInfoPanel = this.txtDescription; |
|
488 | 541 |
} else { |
489 |
html.append("<b>Generic element<br>\n<br>\n"); |
|
490 |
html.append("<b>Name</b> ").append(element.getName()).append("</br>\n"); |
|
491 |
|
|
542 |
newInfoPanel = aditionalPanel.asJComponent(); |
|
492 | 543 |
} |
493 |
|
|
494 |
if( element.getDescription()!=null ) { |
|
495 |
html.append("<b>Description</b><br>\n"); |
|
496 |
html.append(element.getDescription().replace("\n", "<br>")).append("<br>\n"); |
|
544 |
|
|
545 |
if( this.infoPanel != newInfoPanel ) { |
|
546 |
this.pnlDescription.removeAll(); |
|
547 |
this.pnlDescription.add(newInfoPanel, BorderLayout.CENTER); |
|
548 |
this.pnlDescription.revalidate(); |
|
549 |
this.pnlDescription.repaint(); |
|
550 |
this.infoPanel = newInfoPanel; |
|
497 | 551 |
} |
498 |
html.append("</html>\n"); |
|
499 |
this.txtDescription.setContentType("text/html"); |
|
500 |
this.txtDescription.setText(html.toString()); |
|
501 |
this.txtDescription.setCaretPosition(0); |
|
552 |
|
|
502 | 553 |
} |
503 | 554 |
|
504 | 555 |
private void updateSimpleElementsList(GroupElement group) { |
... | ... | |
508 | 559 |
this.txtGroupElement.setText(group.getName()); |
509 | 560 |
this.txtSimpleElementFilter.setText(""); |
510 | 561 |
this.txtDescription.setText(""); |
562 |
if( this.infoPanel != this.txtDescription ) { |
|
563 |
this.pnlDescription.removeAll(); |
|
564 |
this.pnlDescription.add(this.txtDescription, BorderLayout.CENTER); |
|
565 |
this.pnlDescription.repaint(); |
|
566 |
this.infoPanel = this.txtDescription; |
|
567 |
} |
|
511 | 568 |
if (group instanceof SimpleElement) { |
512 | 569 |
this.btnGroupElementInsert.setEnabled(true); |
513 | 570 |
} else { |
Also available in: Unified diff