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 @ 44080
History | View | Annotate | Download (7.05 KB)
1 |
package org.gvsig.expressionevaluator.swing.impl; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collection; |
5 |
import java.util.Collections; |
6 |
import java.util.Comparator; |
7 |
import java.util.HashMap; |
8 |
import java.util.List; |
9 |
import java.util.Map; |
10 |
import javax.swing.JButton; |
11 |
import javax.swing.text.JTextComponent; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.expressionevaluator.Function; |
14 |
import org.gvsig.expressionevaluator.SymbolTable; |
15 |
import org.gvsig.expressionevaluator.swing.CalculatorController; |
16 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
17 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
18 |
import org.gvsig.expressionevaluator.swing.Element; |
19 |
import org.gvsig.expressionevaluator.swing.Element.ConstantElement; |
20 |
import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
21 |
import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
22 |
import org.gvsig.expressionevaluator.swing.Element.VariableElement; |
23 |
import org.gvsig.expressionevaluator.swing.ElementFactory; |
24 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
25 |
import org.gvsig.expressionevaluator.swing.JExpressionPicker; |
26 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultConstantValueElement; |
27 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultFunctionElement; |
28 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultGroupElement; |
29 |
import org.gvsig.expressionevaluator.swing.impl.elements.DefaultVariableElement; |
30 |
import org.gvsig.tools.ToolsLocator; |
31 |
import org.gvsig.tools.i18n.I18nManager; |
32 |
|
33 |
public class DefaultExpressionEvaluatorSwingManager |
34 |
implements ExpressionEvaluatorSwingManager {
|
35 |
|
36 |
private Map<String, ElementFactory> elementsFactories; |
37 |
|
38 |
public DefaultExpressionEvaluatorSwingManager() {
|
39 |
this.elementsFactories = new HashMap<>(); |
40 |
} |
41 |
|
42 |
@Override
|
43 |
public List<Element> createElements(Collection<SymbolTable> symbolTables) { |
44 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
45 |
Map<String, Function> functions = new HashMap<>(); |
46 |
|
47 |
for (SymbolTable symbolTable : symbolTables) {
|
48 |
for (Function function : symbolTable.functions()) {
|
49 |
functions.put(function.name().toUpperCase(), function); |
50 |
} |
51 |
} |
52 |
|
53 |
Map<String, GroupElement> functionGroups = new HashMap<>(); |
54 |
Map<String, GroupElement> operatorGroups = new HashMap<>(); |
55 |
for (Function function : functions.values()) {
|
56 |
if (function.isOperator()) {
|
57 |
String groupName = function.group();
|
58 |
GroupElement group = operatorGroups.get(groupName); |
59 |
if (group == null) { |
60 |
group = new DefaultGroupElement(groupName);
|
61 |
operatorGroups.put(groupName, group); |
62 |
} |
63 |
group.addElement(new DefaultFunctionElement(function));
|
64 |
} else {
|
65 |
String groupName = function.group();
|
66 |
GroupElement group = functionGroups.get(groupName); |
67 |
if (group == null) { |
68 |
group = new DefaultGroupElement(groupName);
|
69 |
functionGroups.put(groupName, group); |
70 |
} |
71 |
group.addElement(new DefaultFunctionElement(function));
|
72 |
} |
73 |
} |
74 |
GroupElement builtinFunctions = this.createGroupElement(i18n.getTranslation("_Functions")); |
75 |
for (GroupElement group : functionGroups.values()) {
|
76 |
builtinFunctions.addElement(group); |
77 |
} |
78 |
GroupElement operatorFunctions = this.createGroupElement(i18n.getTranslation("_Operators")); |
79 |
for (GroupElement group : operatorGroups.values()) {
|
80 |
operatorFunctions.addElement(group); |
81 |
} |
82 |
List<Element> elements = new ArrayList<>(); |
83 |
elements.add(operatorFunctions); |
84 |
elements.add(builtinFunctions); |
85 |
return elements;
|
86 |
} |
87 |
|
88 |
@Override
|
89 |
public void registerElement(ElementFactory factory) { |
90 |
if (this.elementsFactories == null) { |
91 |
this.elementsFactories = new HashMap<>(); |
92 |
} |
93 |
this.elementsFactories.put(factory.getName().toLowerCase(), factory);
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public Collection<ElementFactory> getElementFactories() { |
98 |
if (this.elementsFactories == null) { |
99 |
this.elementsFactories = new HashMap<>(); |
100 |
} |
101 |
List<ElementFactory> factories = new ArrayList<>(); |
102 |
factories.addAll(this.elementsFactories.values());
|
103 |
Collections.sort(factories, new Comparator<ElementFactory>() { |
104 |
@Override
|
105 |
public int compare(ElementFactory o1, ElementFactory o2) { |
106 |
return StringUtils.compare(o1.getName(), o2.getName());
|
107 |
} |
108 |
}); |
109 |
return factories;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public JExpressionBuilder createJExpressionBuilder() {
|
114 |
DefaultJExpressionBuilder x = new DefaultJExpressionBuilder(
|
115 |
this,
|
116 |
new DefaultExpressionBuilderConfig(this) |
117 |
); |
118 |
return x;
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public Element createElement(String name, Object... parameters) { |
123 |
ElementFactory factory = this.elementsFactories.get(name.toLowerCase());
|
124 |
if( factory==null ) { |
125 |
throw new RuntimeException("Can't locate element factory '"+name+"'."); |
126 |
} |
127 |
return factory.createElement(parameters);
|
128 |
} |
129 |
|
130 |
@Override
|
131 |
public GroupElement createGroupElement(String name) { |
132 |
GroupElement e = new DefaultGroupElement(name);
|
133 |
return e;
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public GroupElement createGroupElement(String name, List<Element> elements) { |
138 |
GroupElement e = new DefaultGroupElement(this, name, elements); |
139 |
return e;
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public FunctionElement createFunctionElement(Function function) {
|
144 |
FunctionElement e = new DefaultFunctionElement(function);
|
145 |
return e;
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public ConstantElement createConstantElement(Object value) { |
150 |
ConstantElement e = new DefaultConstantValueElement(value, null); |
151 |
return e;
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public ConstantElement createConstantElement(Object value, String description) { |
156 |
ConstantElement e = new DefaultConstantValueElement(value, description);
|
157 |
return e;
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public VariableElement createVariableElement(String label, String source, String name, String typeName, String description) { |
162 |
VariableElement e = new DefaultVariableElement(label, source, name, typeName, description);
|
163 |
return e;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
public ExpressionPickerController createExpressionPickerController(JTextComponent text, JButton button) { |
168 |
ExpressionPickerController c = new DefaultExpressionPickerController(this, text, button); |
169 |
return c;
|
170 |
} |
171 |
|
172 |
@Override
|
173 |
public JExpressionPicker createJExpressionPicker() {
|
174 |
JExpressionPicker x = new DefaultJExpressionPicker(this); |
175 |
return x;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
public CalculatorController createCalculatorController(JTextComponent text, int dataType) { |
180 |
DefaultCalculatorController controller = new DefaultCalculatorController(this,text,dataType); |
181 |
return controller;
|
182 |
} |
183 |
|
184 |
} |