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 / DefaultExpressionEvaluatorSwingManager.java @ 43983
History | View | Annotate | Download (5.92 KB)
1 |
package org.gvsig.expressionevaluator.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.TextComponent; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Collection; |
6 |
import java.util.HashMap; |
7 |
import java.util.List; |
8 |
import java.util.Map; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.text.JTextComponent; |
11 |
import org.gvsig.expressionevaluator.Function; |
12 |
import org.gvsig.expressionevaluator.SymbolTable; |
13 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
14 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
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; |
27 |
|
28 |
public class DefaultExpressionEvaluatorSwingManager |
29 |
implements ExpressionEvaluatorSwingManager {
|
30 |
|
31 |
private Map<String, ElementFactory> elementsFactories; |
32 |
|
33 |
public DefaultExpressionEvaluatorSwingManager() {
|
34 |
this.elementsFactories = new HashMap<>(); |
35 |
} |
36 |
|
37 |
@Override
|
38 |
public List<Element> createElements(Collection<SymbolTable> symbolTables) { |
39 |
Map<String, Function> functions = new HashMap<>(); |
40 |
|
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<>(); |
49 |
for (Function function : functions.values()) {
|
50 |
if (function.isOperator()) {
|
51 |
String groupName = function.group();
|
52 |
GroupElement group = operatorGroups.get(groupName); |
53 |
if (group == null) { |
54 |
group = new DefaultGroupElement(groupName);
|
55 |
operatorGroups.put(groupName, group); |
56 |
} |
57 |
group.addElement(new DefaultFunctionElement(function));
|
58 |
} else {
|
59 |
String groupName = function.group();
|
60 |
GroupElement group = functionGroups.get(groupName); |
61 |
if (group == null) { |
62 |
group = new DefaultGroupElement(groupName);
|
63 |
functionGroups.put(groupName, group); |
64 |
} |
65 |
group.addElement(new DefaultFunctionElement(function));
|
66 |
} |
67 |
} |
68 |
GroupElement builtinFunctions = this.createGroupElement("_Builtin_functions"); |
69 |
for (GroupElement group : functionGroups.values()) {
|
70 |
builtinFunctions.addElement(group); |
71 |
} |
72 |
GroupElement operatorFunctions = this.createGroupElement("_Operators"); |
73 |
for (GroupElement group : operatorGroups.values()) {
|
74 |
operatorFunctions.addElement(group); |
75 |
} |
76 |
List<Element> elements = new ArrayList<>(); |
77 |
elements.add(operatorFunctions); |
78 |
elements.add(builtinFunctions); |
79 |
return elements;
|
80 |
} |
81 |
|
82 |
@Override
|
83 |
public void registerElement(ElementFactory factory) { |
84 |
if (this.elementsFactories == null) { |
85 |
this.elementsFactories = new HashMap<>(); |
86 |
} |
87 |
this.elementsFactories.put(factory.getName().toLowerCase(), factory);
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
public JExpressionBuilder createJExpressionBuilder() {
|
92 |
DefaultJExpressionBuilder x = new DefaultJExpressionBuilder(
|
93 |
this,
|
94 |
new DefaultExpressionBuilderConfig(this) |
95 |
); |
96 |
return x;
|
97 |
} |
98 |
|
99 |
@Override
|
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);
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public GroupElement createGroupElement(String name) { |
110 |
GroupElement e = new DefaultGroupElement(name);
|
111 |
return e;
|
112 |
} |
113 |
|
114 |
@Override
|
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); |
153 |
return x;
|
154 |
} |
155 |
|
156 |
} |