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/DefaultExpressionEvaluatorSwingManager.java
DefaultExpressionEvaluatorSwingManager.java | ||
---|---|---|
1 |
|
|
2 | 1 |
package org.gvsig.expressionevaluator.swing.impl; |
3 | 2 |
|
3 |
import java.awt.TextComponent; |
|
4 | 4 |
import java.util.ArrayList; |
5 |
import java.util.Collection; |
|
5 | 6 |
import java.util.HashMap; |
6 | 7 |
import java.util.List; |
7 | 8 |
import java.util.Map; |
8 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
|
|
9 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
|
|
9 |
import javax.swing.JButton;
|
|
10 |
import javax.swing.text.JTextComponent;
|
|
10 | 11 |
import org.gvsig.expressionevaluator.Function; |
12 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
11 | 13 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
12 | 14 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
13 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder.Element; |
|
14 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder.GroupElement; |
|
15 |
import org.gvsig.expressionevaluator.swing.Element; |
|
16 |
import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
|
17 |
import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
|
18 |
import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
|
19 |
import org.gvsig.expressionevaluator.swing.Element.VariableElement; |
|
20 |
import org.gvsig.expressionevaluator.swing.ElementFactory; |
|
21 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
|
22 |
import org.gvsig.expressionevaluator.swing.JExpressionPicker; |
|
23 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultConstantValueElement; |
|
24 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultFunctionElement; |
|
25 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultGroupElement; |
|
26 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultVariableElement; |
|
15 | 27 |
|
28 |
public class DefaultExpressionEvaluatorSwingManager |
|
29 |
implements ExpressionEvaluatorSwingManager { |
|
16 | 30 |
|
17 |
public class DefaultExpressionEvaluatorSwingManager |
|
18 |
implements ExpressionEvaluatorSwingManager { |
|
31 |
private Map<String, ElementFactory> elementsFactories; |
|
19 | 32 |
|
20 |
private List<Element> elements; |
|
21 |
|
|
22 | 33 |
public DefaultExpressionEvaluatorSwingManager() { |
23 |
|
|
34 |
this.elementsFactories = new HashMap<>(); |
|
24 | 35 |
} |
25 |
|
|
26 |
private void initElements() {
|
|
27 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
|
|
36 |
|
|
37 |
@Override
|
|
38 |
public List<Element> createElements(Collection<SymbolTable> symbolTables) {
|
|
28 | 39 |
Map<String, Function> functions = new HashMap<>(); |
29 |
functions.putAll(manager.getAllFunctions()); |
|
30 |
functions.putAll(manager.getAllOperators()); |
|
31 | 40 |
|
32 |
Map<String,GroupElement> functionGroups = new HashMap<>(); |
|
33 |
Map<String,GroupElement> operatorGroups = new HashMap<>(); |
|
41 |
for (SymbolTable symbolTable : symbolTables) { |
|
42 |
for (Function function : symbolTable.functions()) { |
|
43 |
functions.put(function.name().toUpperCase(), function); |
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 |
Map<String, GroupElement> functionGroups = new HashMap<>(); |
|
48 |
Map<String, GroupElement> operatorGroups = new HashMap<>(); |
|
34 | 49 |
for (Function function : functions.values()) { |
35 |
if( function.isOperator() ) {
|
|
50 |
if (function.isOperator()) {
|
|
36 | 51 |
String groupName = function.group(); |
37 | 52 |
GroupElement group = operatorGroups.get(groupName); |
38 |
if( group == null ) {
|
|
53 |
if (group == null) {
|
|
39 | 54 |
group = new DefaultGroupElement(groupName); |
40 | 55 |
operatorGroups.put(groupName, group); |
41 | 56 |
} |
... | ... | |
43 | 58 |
} else { |
44 | 59 |
String groupName = function.group(); |
45 | 60 |
GroupElement group = functionGroups.get(groupName); |
46 |
if( group == null ) {
|
|
61 |
if (group == null) {
|
|
47 | 62 |
group = new DefaultGroupElement(groupName); |
48 | 63 |
functionGroups.put(groupName, group); |
49 | 64 |
} |
50 | 65 |
group.addElement(new DefaultFunctionElement(function)); |
51 | 66 |
} |
52 | 67 |
} |
53 |
GroupElement builtinFunctions = this.createGroup("_Builtin_functions"); |
|
68 |
GroupElement builtinFunctions = this.createGroupElement("_Builtin_functions");
|
|
54 | 69 |
for (GroupElement group : functionGroups.values()) { |
55 | 70 |
builtinFunctions.addElement(group); |
56 | 71 |
} |
57 |
GroupElement operatorFunctions = new DefaultGroupElement("_Operators");
|
|
72 |
GroupElement operatorFunctions = this.createGroupElement("_Operators");
|
|
58 | 73 |
for (GroupElement group : operatorGroups.values()) { |
59 | 74 |
operatorFunctions.addElement(group); |
60 | 75 |
} |
61 |
this.elements = new ArrayList<>(); |
|
62 |
this.elements.add(operatorFunctions); |
|
63 |
this.elements.add(builtinFunctions); |
|
76 |
List<Element> elements = new ArrayList<>(); |
|
77 |
elements.add(operatorFunctions); |
|
78 |
elements.add(builtinFunctions); |
|
79 |
return elements; |
|
64 | 80 |
} |
65 |
|
|
81 |
|
|
66 | 82 |
@Override |
67 |
public List<Element> getElements() {
|
|
68 |
if( this.elements==null ) {
|
|
69 |
initElements();
|
|
83 |
public void registerElement(ElementFactory factory) {
|
|
84 |
if (this.elementsFactories == null) {
|
|
85 |
this.elementsFactories = new HashMap<>();
|
|
70 | 86 |
} |
71 |
return this.elements;
|
|
87 |
this.elementsFactories.put(factory.getName().toLowerCase(), factory);
|
|
72 | 88 |
} |
73 |
|
|
89 |
|
|
74 | 90 |
@Override |
75 |
public GroupElement createGroup(String name) { |
|
76 |
DefaultGroupElement g = new DefaultGroupElement(name); |
|
77 |
return g; |
|
91 |
public JExpressionBuilder createJExpressionBuilder() { |
|
92 |
DefaultJExpressionBuilder x = new DefaultJExpressionBuilder( |
|
93 |
this, |
|
94 |
new DefaultExpressionBuilderConfig(this) |
|
95 |
); |
|
96 |
return x; |
|
78 | 97 |
} |
79 |
|
|
98 |
|
|
80 | 99 |
@Override |
81 |
public GroupElement createGroup(String name, List<Element>elements) { |
|
82 |
DefaultGroupElement g = new DefaultGroupElement(name); |
|
83 |
return g; |
|
100 |
public Element createElement(String name, Object... parameters) { |
|
101 |
ElementFactory factory = this.elementsFactories.get(name.toLowerCase()); |
|
102 |
if( factory==null ) { |
|
103 |
throw new RuntimeException("Can't locate element factory '"+name+"'."); |
|
104 |
} |
|
105 |
return factory.createElement(parameters); |
|
84 | 106 |
} |
85 | 107 |
|
86 | 108 |
@Override |
87 |
public Element createElement(Function function) {
|
|
88 |
Element f = new DefaultFunctionElement(function);
|
|
89 |
return f;
|
|
109 |
public GroupElement createGroupElement(String name) {
|
|
110 |
GroupElement e = new DefaultGroupElement(name);
|
|
111 |
return e;
|
|
90 | 112 |
} |
91 |
|
|
113 |
|
|
92 | 114 |
@Override |
93 |
public JExpressionBuilder createJExpressionBuilder() { |
|
94 |
DefaultJExpressionBuilder x = new DefaultJExpressionBuilder(this); |
|
115 |
public GroupElement createGroupElement(String name, List<Element> elements) { |
|
116 |
GroupElement e = new DefaultGroupElement(this, name, elements); |
|
117 |
return e; |
|
118 |
} |
|
119 |
|
|
120 |
@Override |
|
121 |
public FunctionElement createFunctionElement(Function function) { |
|
122 |
FunctionElement e = new DefaultFunctionElement(function); |
|
123 |
return e; |
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public ConstantElement createConstantElement(Object value) { |
|
128 |
ConstantElement e = new DefaultConstantValueElement(value, null); |
|
129 |
return e; |
|
130 |
} |
|
131 |
|
|
132 |
@Override |
|
133 |
public ConstantElement createConstantElement(Object value, String description) { |
|
134 |
ConstantElement e = new DefaultConstantValueElement(value, description); |
|
135 |
return e; |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
|
139 |
public VariableElement createVariableElement(String label, String source, String name, String typeName, String description) { |
|
140 |
VariableElement e = new DefaultVariableElement(label, source, name, typeName, description); |
|
141 |
return e; |
|
142 |
} |
|
143 |
|
|
144 |
@Override |
|
145 |
public ExpressionPickerController createExpressionPickerController(JTextComponent text, JButton button) { |
|
146 |
ExpressionPickerController c = new DefaultExpressionPickerController(this, text, button); |
|
147 |
return c; |
|
148 |
} |
|
149 |
|
|
150 |
@Override |
|
151 |
public JExpressionPicker createJExpressionPicker() { |
|
152 |
JExpressionPicker x = new DefaultJExpressionPicker(this); |
|
95 | 153 |
return x; |
96 | 154 |
} |
97 | 155 |
|
Also available in: Unified diff