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 / DefaultExpressionBuilderConfig.java @ 47173
History | View | Annotate | Download (13.8 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.HashMap; |
7 |
import java.util.List; |
8 |
import java.util.Map; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.gvsig.expressionevaluator.Expression; |
11 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
12 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
13 |
import org.gvsig.expressionevaluator.Function; |
14 |
import org.gvsig.expressionevaluator.SymbolTable; |
15 |
import org.gvsig.expressionevaluator.SymbolTableFactory; |
16 |
import org.gvsig.expressionevaluator.swing.Element; |
17 |
import org.gvsig.expressionevaluator.swing.ElementFactory; |
18 |
import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
19 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
20 |
import org.gvsig.expressionevaluator.swing.SuggestionProvider; |
21 |
import org.gvsig.tools.dataTypes.DataType; |
22 |
import org.gvsig.tools.dispose.Disposable; |
23 |
import org.gvsig.tools.dispose.DisposeUtils; |
24 |
import org.gvsig.tools.util.Factory; |
25 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
26 |
import org.slf4j.Logger; |
27 |
import org.slf4j.LoggerFactory; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
public class DefaultExpressionBuilderConfig |
34 |
implements ExpressionBuilderConfig, ExpressionBuilderConfig.Preferences, Disposable {
|
35 |
|
36 |
protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultExpressionBuilderConfig.class); |
37 |
|
38 |
private List<Element> elements; |
39 |
private List<Element> addElements; |
40 |
private int limit; |
41 |
private Map<String, SymbolTable> symbolTables; |
42 |
private final ExpressionEvaluatorSwingManager manager; |
43 |
private SymbolTable previewSymbolTable;
|
44 |
private boolean SQLCompatible; |
45 |
private final PropertiesSupportHelper propertiesSupportHelper; |
46 |
private boolean showLabelsOfAttributes; |
47 |
private boolean allowAggregates; |
48 |
private java.util.function.Function<String, Integer>allowedFunctions; |
49 |
private Map<String,Factory> suggestionFactories; |
50 |
|
51 |
|
52 |
public DefaultExpressionBuilderConfig(ExpressionEvaluatorSwingManager manager) {
|
53 |
this.manager = manager;
|
54 |
this.limit = 6; |
55 |
this.SQLCompatible = false; |
56 |
this.propertiesSupportHelper = new PropertiesSupportHelper(); |
57 |
this.showLabelsOfAttributes = true; |
58 |
this.allowAggregates = false; |
59 |
this.suggestionFactories = new HashMap<>(); |
60 |
initSymbolTables(); |
61 |
} |
62 |
|
63 |
// @Override
|
64 |
// public void copyConfigFrom(ExpressionBuilderConfig other) {
|
65 |
// // Hay que tener cuidado al copiar ya que puede darse el caso
|
66 |
// // que "other = this", por eso lo de las variables other_*
|
67 |
// this.getPreferences().copyPreferencesFrom(other.getPreferences());
|
68 |
//
|
69 |
// this.allowAggregates = other.allowAggregates();
|
70 |
// this.allowedFunctions = other.getAllowedFunctions();
|
71 |
// this.previewSymbolTable = other.getPreviewSymbolTable();
|
72 |
//
|
73 |
// Collection<SymbolTable> other_symbolTables = other.getSymbolTables();
|
74 |
// List<Element> other_addElements = other.getAddedElements();
|
75 |
// Collection<Factory> other_suggestionFactories = other.getSuggestionFactories();
|
76 |
//
|
77 |
// this.symbolTables = new HashMap<>();
|
78 |
// this.elements = new ArrayList<>();
|
79 |
// this.addElements = new ArrayList<>();
|
80 |
// this.suggestionFactories = new HashMap<>();
|
81 |
//
|
82 |
// for (SymbolTable symbolTable : other_symbolTables) {
|
83 |
// this.addSymbolTable(symbolTable);
|
84 |
// }
|
85 |
//
|
86 |
// for (Element element : other_addElements) {
|
87 |
// this.addElements.add((Element) Cloneable.cloneQuietly(element));
|
88 |
// }
|
89 |
//
|
90 |
// for (Factory suggestionFactory : other_suggestionFactories) {
|
91 |
// this.addSuggestionFactory(suggestionFactory);
|
92 |
// }
|
93 |
// }
|
94 |
|
95 |
// @Override
|
96 |
// public void copyPreferencesFrom(Preferences other) {
|
97 |
// other.setSQLCompatible(this.SQLCompatible);
|
98 |
// other.setShowLabelsOfElements(this.showLabelsOfAttributes);
|
99 |
// other.setSimpleElementsLimit(limit);
|
100 |
// }
|
101 |
|
102 |
@Override
|
103 |
public Preferences getPreferences() { |
104 |
return this; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public Collection<Factory> getSuggestionFactories() { |
109 |
return this.suggestionFactories.values(); |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public java.util.function.Function<String, Integer> getAllowedFunctions() { |
114 |
return this.allowedFunctions; |
115 |
} |
116 |
|
117 |
private void initSymbolTables() { |
118 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
119 |
for (SymbolTableFactory factory : theManager.getSymbolTableFactories()) {
|
120 |
if( factory == null ) { |
121 |
continue;
|
122 |
} |
123 |
String factoryName = "(unknown)"; |
124 |
try {
|
125 |
factoryName = factory.getName(); |
126 |
if (factory.isAutoload()) {
|
127 |
this.addSymbolTable(factory.create());
|
128 |
} |
129 |
} catch(Throwable th) { |
130 |
LOGGER.warn("Can't create symbol table '"+factoryName+"'.",th); |
131 |
} |
132 |
} |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public synchronized List<Element> getElements() { |
137 |
if (this.elements == null || this.elements.isEmpty() ) { |
138 |
this.elements = manager.createElements(this.getSymbolTables(), (Function f) -> { |
139 |
if( f.group().equalsIgnoreCase(Function.GROUP_AGGREGATE) &&
|
140 |
!this.allowAggregates() ) {
|
141 |
return false; |
142 |
} |
143 |
java.util.function.Function<String, Integer> isFunctionAllowed = this.isFunctionAllowed(); |
144 |
if( isFunctionAllowed!=null && isFunctionAllowed.apply(f.name())==DataType.NO ) { |
145 |
return false; |
146 |
} |
147 |
return true; |
148 |
}); |
149 |
for (ElementFactory factory : manager.getElementFactories()) {
|
150 |
if (factory.isAutoload()) {
|
151 |
Element e = factory.createElement();
|
152 |
this.elements.add(e);
|
153 |
e.setConfig(this);
|
154 |
} |
155 |
} |
156 |
if( addElements!=null ) { |
157 |
for (Element element : addElements) { |
158 |
this.elements.add(element);
|
159 |
} |
160 |
} |
161 |
} |
162 |
return Collections.unmodifiableList(elements); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public synchronized void addElement(Element element) { |
167 |
if( element==null ) { |
168 |
return;
|
169 |
} |
170 |
this.getElements(); // Forzamos a crear la lista de elementos |
171 |
for (Element theElement : this.elements) { |
172 |
if (theElement.getName().equalsIgnoreCase(element.getName())) {
|
173 |
return;
|
174 |
} |
175 |
} |
176 |
this.elements.add(element);
|
177 |
if( this.addElements==null ) { |
178 |
this.addElements = new ArrayList<>(); |
179 |
} |
180 |
this.addElements.add(element);
|
181 |
element.setConfig(this);
|
182 |
} |
183 |
|
184 |
@Override
|
185 |
public Collection<SymbolTable> getSymbolTables() { |
186 |
if( this.symbolTables==null ) { |
187 |
return Collections.EMPTY_LIST; |
188 |
} |
189 |
return Collections.unmodifiableCollection(this.symbolTables.values()); |
190 |
} |
191 |
|
192 |
@Override
|
193 |
public void addSymbolTable(String name) { |
194 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
195 |
SymbolTable symbolTable = theManager.getSymbolTable(name); |
196 |
if (symbolTable == null) { |
197 |
throw new IllegalArgumentException("Can't locate symbol table '" + name + "'."); |
198 |
} |
199 |
addSymbolTable(symbolTable); |
200 |
} |
201 |
|
202 |
@Override
|
203 |
public synchronized void addSymbolTable(SymbolTable symbolTable) { |
204 |
if (symbolTable == null) { |
205 |
return;
|
206 |
} |
207 |
if (this.elements != null && !this.elements.isEmpty() ) { |
208 |
// throw new IllegalStateException("Can not add symbol tables once the elements are built.");
|
209 |
LOGGER.warn("Added a symbol table after the elements are build.");
|
210 |
for (Element element : this.elements) { |
211 |
DisposeUtils.disposeQuietly(element); |
212 |
} |
213 |
this.elements.clear();
|
214 |
} |
215 |
if (this.symbolTables == null) { |
216 |
this.symbolTables = new HashMap<>(); |
217 |
} |
218 |
this.symbolTables.put(symbolTable.getName(), symbolTable);
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public void removeAllSymbolTables() { |
223 |
if( this.symbolTables!=null ) { |
224 |
this.symbolTables.clear();
|
225 |
} |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public synchronized void removeAllElements() { |
230 |
if( this.elements!=null ) { |
231 |
for (Element element : this.elements) { |
232 |
DisposeUtils.disposeQuietly(element); |
233 |
} |
234 |
this.elements.clear();
|
235 |
} |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public synchronized void removeAddedElements() { |
240 |
if( this.addElements !=null ) { |
241 |
for (Element element : this.addElements) { |
242 |
DisposeUtils.disposeQuietly(element); |
243 |
} |
244 |
this.addElements.clear();
|
245 |
} |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public int getSimpleElementsLimit() { |
250 |
return this.limit; |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void setSimpleElementsLimit(int limit) { |
255 |
this.limit = limit;
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
public void removeSymbolTable(String name) { |
260 |
if( this.symbolTables!=null ) { |
261 |
this.symbolTables.remove(name);
|
262 |
} |
263 |
} |
264 |
|
265 |
// @Override
|
266 |
// public void setScriptsEnabled(boolean enabled) {
|
267 |
// this.scriptsEnabled = enabled;
|
268 |
// }
|
269 |
//
|
270 |
// @Override
|
271 |
// public boolean getScriptsEnabled() {
|
272 |
// return this.scriptsEnabled;
|
273 |
// }
|
274 |
//
|
275 |
@Override
|
276 |
public SymbolTable getPreviewSymbolTable() {
|
277 |
return this.previewSymbolTable; |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public void setPreviewSymbolTable(SymbolTable symbolTable) { |
282 |
this.previewSymbolTable = symbolTable;
|
283 |
} |
284 |
|
285 |
@Override
|
286 |
public void addPreviewSymbolTable(SymbolTable symbolTable) { |
287 |
if( symbolTable==null ) { |
288 |
return;
|
289 |
} |
290 |
if( this.previewSymbolTable==null ) { |
291 |
this.previewSymbolTable = symbolTable;
|
292 |
return;
|
293 |
} |
294 |
this.previewSymbolTable.addSymbolTable(symbolTable);
|
295 |
} |
296 |
|
297 |
@Override
|
298 |
public void setSQLCompatible(boolean SQLCompatible) { |
299 |
this.SQLCompatible = SQLCompatible;
|
300 |
} |
301 |
|
302 |
@Override
|
303 |
public boolean isSQLCompatible() { |
304 |
return SQLCompatible;
|
305 |
} |
306 |
|
307 |
@Override
|
308 |
public Object getProperty(String string) { |
309 |
return this.propertiesSupportHelper.getProperty(string); |
310 |
} |
311 |
|
312 |
@Override
|
313 |
public void setProperty(String string, Object o) { |
314 |
this.propertiesSupportHelper.setProperty(string, o);
|
315 |
} |
316 |
|
317 |
@Override
|
318 |
public Map<String, Object> getProperties() { |
319 |
return this.propertiesSupportHelper.getProperties(); |
320 |
} |
321 |
|
322 |
@Override
|
323 |
public boolean getShowLabelsOfElements() { |
324 |
return this.showLabelsOfAttributes; |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public void setShowLabelsOfElements(boolean showLabels) { |
329 |
this.showLabelsOfAttributes = showLabels;
|
330 |
} |
331 |
|
332 |
@Override
|
333 |
public boolean allowAggregates() { |
334 |
return this.allowAggregates; |
335 |
} |
336 |
|
337 |
@Override
|
338 |
public boolean allowAggregates(boolean allow) { |
339 |
boolean r = this.allowAggregates; |
340 |
this.allowAggregates = allow;
|
341 |
return r;
|
342 |
} |
343 |
|
344 |
@Override
|
345 |
public java.util.function.Function<String, Integer> isFunctionAllowed() { |
346 |
return this.allowedFunctions; |
347 |
} |
348 |
|
349 |
@Override
|
350 |
public void setAllowedFunctions(java.util.function.Function<String, Integer> allow) { |
351 |
this.allowedFunctions = allow;
|
352 |
} |
353 |
|
354 |
@Override
|
355 |
public synchronized String getSuggestion(Expression expression) { |
356 |
if( expression == null ) { |
357 |
return null; |
358 |
} |
359 |
String text = expression.getPhrase();
|
360 |
if( StringUtils.isBlank(text) ) {
|
361 |
return null; |
362 |
} |
363 |
boolean needseparator = false; |
364 |
StringBuilder builder = null; |
365 |
for (Factory factory : suggestionFactories.values()) {
|
366 |
SuggestionProvider provider = (SuggestionProvider) factory.create(); |
367 |
String suggestion = provider.getSuggestion(text);
|
368 |
if( suggestion != null ) { |
369 |
if( builder == null ) { |
370 |
builder = new StringBuilder(); |
371 |
builder.append(suggestion); |
372 |
} else {
|
373 |
builder.append("\n\n");
|
374 |
builder.append(suggestion); |
375 |
} |
376 |
} |
377 |
} |
378 |
if( builder!=null ) { |
379 |
return builder.toString();
|
380 |
} |
381 |
return null; |
382 |
} |
383 |
|
384 |
@Override
|
385 |
public synchronized void addSuggestionFactory(Factory factory) { |
386 |
if( factory == null ) { |
387 |
return;
|
388 |
} |
389 |
this.suggestionFactories.put(factory.getName(), factory);
|
390 |
} |
391 |
|
392 |
@Override
|
393 |
public synchronized void dispose() { |
394 |
this.removeAllElements();
|
395 |
this.removeAllSymbolTables();
|
396 |
if( this.addElements!=null ) { |
397 |
for (Element element : this.addElements) { |
398 |
DisposeUtils.disposeQuietly(element); |
399 |
} |
400 |
this.addElements.clear();
|
401 |
} |
402 |
} |
403 |
|
404 |
@Override
|
405 |
public List<Element> getAddedElements() { |
406 |
return this.addElements; |
407 |
} |
408 |
|
409 |
@Override
|
410 |
public ExpressionBuilderConfig cloneConfig() {
|
411 |
try {
|
412 |
return (ExpressionBuilderConfig) this.clone(); |
413 |
} catch (CloneNotSupportedException ex) { |
414 |
return null; |
415 |
} |
416 |
} |
417 |
|
418 |
@Override
|
419 |
public synchronized Object clone() throws CloneNotSupportedException { |
420 |
DefaultExpressionBuilderConfig other = (DefaultExpressionBuilderConfig) super.clone();
|
421 |
|
422 |
other.symbolTables = new HashMap<>(); |
423 |
other.elements = new ArrayList<>(); |
424 |
other.addElements = new ArrayList<>(); |
425 |
other.suggestionFactories = new HashMap<>(); |
426 |
|
427 |
if(this.symbolTables != null){ |
428 |
for (SymbolTable symbolTable : this.symbolTables.values()) { |
429 |
other.addSymbolTable(symbolTable); |
430 |
} |
431 |
} |
432 |
|
433 |
if(this.addElements != null){ |
434 |
for (Element element : this.addElements) { |
435 |
other.addElements.add(element.cloneElement()); |
436 |
} |
437 |
} |
438 |
|
439 |
if(this.suggestionFactories != null){ |
440 |
for (Factory suggestionFactory : this.suggestionFactories.values()) { |
441 |
other.addSuggestionFactory(suggestionFactory); |
442 |
} |
443 |
} |
444 |
|
445 |
return other;
|
446 |
} |
447 |
|
448 |
} |