Statistics
| Revision:

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 @ 44887

History | View | Annotate | Download (10.3 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.util.Factory;
23
import org.gvsig.tools.util.PropertiesSupportHelper;
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
/**
28
 *
29
 * @author jjdelcerro
30
 */
31
public class DefaultExpressionBuilderConfig
32
        implements ExpressionBuilderConfig, ExpressionBuilderConfig.Preferences {
33

    
34
    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultExpressionBuilderConfig.class);
35
    
36
    private List<Element> elements;
37
    private int limit;
38
    private Map<String, SymbolTable> symbolTables;
39
    private final ExpressionEvaluatorSwingManager manager;
40
    private SymbolTable previewSymbolTable;
41
    private boolean SQLCompatible;
42
    private final PropertiesSupportHelper propertiesSupportHelper;
43
    private boolean showLabelsOfAttributes;
44
    private boolean allowAggregates;
45
    private java.util.function.Function<String, Integer>allowedFunctions;
46
    private Map<String,Factory> suggestionFactories;
47

    
48
    
49
    public DefaultExpressionBuilderConfig(ExpressionEvaluatorSwingManager manager) {
50
        this.manager = manager;
51
        this.limit = 6;
52
        this.SQLCompatible = false;
53
        this.propertiesSupportHelper = new PropertiesSupportHelper();
54
        this.showLabelsOfAttributes = true;
55
        this.allowAggregates = false;
56
        this.suggestionFactories = new HashMap<>();
57
        initSymbolTables();
58
    }
59

    
60
    @Override
61
    public void copyConfigFrom(ExpressionBuilderConfig other) {
62
      // Hay que tener cuidado al copiar ya que puede darse el caso
63
      // que "other = this", por eso lo de las variables other_*
64
      this.getPreferences().copyPreferencesFrom(other.getPreferences());
65
      
66
      this.allowAggregates = other.allowAggregates();
67
      this.allowedFunctions = other.getAllowedFunctions();      
68
      this.previewSymbolTable = other.getPreviewSymbolTable();
69
      
70
      Collection<SymbolTable> other_symbolTables = other.getSymbolTables();
71
      List<Element> other_elements = other.getElements();
72
      Collection<Factory> other_suggestionFactories = other.getSuggestionFactories();
73
      
74
      this.symbolTables = new HashMap<>();
75
      this.elements = new ArrayList<>();
76
      this.suggestionFactories = new HashMap<>();
77

    
78
      for (SymbolTable symbolTable : other_symbolTables) {
79
        this.addSymbolTable(symbolTable);
80
      }
81
      
82
      for (Element element : other_elements) {
83
        this.elements.add(element);
84
      }
85
      
86
      for (Factory suggestionFactory : other_suggestionFactories) {
87
        this.addSuggestionFactory(suggestionFactory);
88
      }
89
    }
90
    
91
    @Override
92
    public void copyPreferencesFrom(Preferences other) {
93
      other.setSQLCompatible(this.SQLCompatible);
94
      other.setShowLabelsOfElements(this.showLabelsOfAttributes);
95
      other.setSimpleElementsLimit(limit);
96
    }
97

    
98
    @Override
99
    public Preferences getPreferences() {
100
        return this;
101
    }
102

    
103
    @Override
104
    public Collection<Factory> getSuggestionFactories() {
105
      return this.suggestionFactories.values();
106
    }
107

    
108
    @Override
109
    public java.util.function.Function<String, Integer> getAllowedFunctions() {
110
      return this.allowedFunctions;
111
    }
112

    
113
    private void initSymbolTables() {
114
        ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager();
115
        for (SymbolTableFactory factory : theManager.getSymbolTableFactories()) {
116
          if( factory == null ) {
117
            continue;
118
          }
119
          String factoryName = "(unknown)";
120
          try {
121
            factoryName = factory.getName();
122
            if (factory.isAutoload()) {
123
                this.addSymbolTable(factory.create());
124
            }
125
          } catch(Throwable th) {
126
            LOGGER.warn("Can't create symbol table '"+factoryName+"'.",th);
127
          }
128
        }
129
    }
130

    
131
    @Override
132
    public List<Element> getElements() {
133
        if (this.elements == null || this.elements.isEmpty() ) {
134
            this.elements = manager.createElements(this.getSymbolTables(), (Function f) -> {
135
              if( f.group().equalsIgnoreCase(Function.GROUP_AGGREGATE) &&
136
                      !this.allowAggregates() ) {
137
                return false;
138
              }
139
              java.util.function.Function<String, Integer> isFunctionAllowed = this.isFunctionAllowed();
140
              if( isFunctionAllowed!=null && isFunctionAllowed.apply(f.name())==DataType.NO ) {
141
                return false;
142
              }
143
              return true;
144
            });
145
            for (ElementFactory factory : manager.getElementFactories()) {
146
                if (factory.isAutoload()) {
147
                    Element e = factory.createElement();
148
                    this.elements.add(e);
149
                    e.setConfig(this);
150
                }
151
            }
152
        }
153
        return Collections.unmodifiableList(elements);
154
    }
155

    
156
    @Override
157
    public void addElement(Element element) {
158
        if( element==null ) {
159
            return;
160
        }
161
        this.getElements(); // Forzamos a crear la lista de elementos
162
        for (Element theElement : this.elements) {
163
            if (theElement.getName().equalsIgnoreCase(element.getName())) {
164
                return;
165
            }
166
        }
167
        this.elements.add(element);
168
        element.setConfig(this);
169
    }
170

    
171
    @Override
172
    public Collection<SymbolTable> getSymbolTables() {
173
        return Collections.unmodifiableCollection(this.symbolTables.values());
174
    }
175

    
176
    @Override
177
    public void addSymbolTable(String name) {
178
        ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager();
179
        SymbolTable symbolTable = theManager.getSymbolTable(name);
180
        if (symbolTable == null) {
181
            throw new IllegalArgumentException("Can't locate symbol table '" + name + "'.");
182
        }
183
        addSymbolTable(symbolTable);
184
    }
185

    
186
    @Override
187
    public void addSymbolTable(SymbolTable symbolTable) {
188
        if (symbolTable == null) {
189
            return;
190
        }
191
        if (this.elements != null && !this.elements.isEmpty() ) {
192
            throw new IllegalStateException("Can not add symbol tables once the elements are built.");
193
        }
194
        if (this.symbolTables == null) {
195
            this.symbolTables = new HashMap<>();
196
        }
197
        this.symbolTables.put(symbolTable.getName(), symbolTable);
198
    }
199

    
200
    @Override
201
    public void removeAllSymbolTables() {
202
        this.symbolTables.clear();
203
    }
204

    
205
    @Override
206
    public void removeAllElements() {
207
        this.elements.clear();
208
    }
209

    
210
    @Override
211
    public int getSimpleElementsLimit() {
212
        return this.limit;
213
    }
214

    
215
    @Override
216
    public void setSimpleElementsLimit(int limit) {
217
        this.limit = limit;
218
    }
219

    
220
    @Override
221
    public void removeSymbolTable(String name) {
222
        this.symbolTables.remove(name);
223
    }
224

    
225
//    @Override
226
//    public void setScriptsEnabled(boolean enabled) {
227
//        this.scriptsEnabled = enabled;
228
//    }
229
//
230
//    @Override
231
//    public boolean getScriptsEnabled() {
232
//        return this.scriptsEnabled;
233
//    }
234
//
235
    @Override
236
    public SymbolTable getPreviewSymbolTable() {
237
        return this.previewSymbolTable;
238
    }
239

    
240
    @Override
241
    public void setPreviewSymbolTable(SymbolTable symbolTable) {
242
        this.previewSymbolTable = symbolTable;
243
    }
244

    
245
    @Override
246
    public void setSQLCompatible(boolean SQLCompatible) {
247
        this.SQLCompatible = SQLCompatible;
248
    }
249

    
250
    @Override
251
    public boolean isSQLCompatible() {
252
        return SQLCompatible;
253
    }
254

    
255
    @Override
256
    public Object getProperty(String string) {
257
        return this.propertiesSupportHelper.getProperty(string);
258
    }
259

    
260
    @Override
261
    public void setProperty(String string, Object o) {
262
        this.propertiesSupportHelper.setProperty(string, o);
263
    }
264

    
265
    @Override
266
    public Map<String, Object> getProperties() {
267
        return this.propertiesSupportHelper.getProperties();
268
    }
269

    
270
    @Override
271
    public boolean getShowLabelsOfElements() {
272
        return this.showLabelsOfAttributes;
273
    }
274

    
275
    @Override
276
    public void setShowLabelsOfElements(boolean showLabels) {
277
        this.showLabelsOfAttributes = showLabels;
278
    }
279

    
280
  @Override
281
  public boolean allowAggregates() {
282
    return this.allowAggregates;
283
  }
284

    
285
  @Override
286
  public boolean allowAggregates(boolean allow) {
287
    boolean r = this.allowAggregates;
288
    this.allowAggregates = allow;
289
    return r;
290
  }
291

    
292
  @Override
293
  public java.util.function.Function<String, Integer> isFunctionAllowed() {
294
    return this.allowedFunctions;
295
  }
296

    
297
  @Override
298
  public void setAllowedFunctions(java.util.function.Function<String, Integer> allow) {
299
    this.allowedFunctions = allow;
300
  }
301

    
302
  @Override
303
  public String getSuggestion(Expression expression) {
304
    if( expression == null ) {
305
      return null;
306
    }
307
    String text = expression.getPhrase();
308
    if( StringUtils.isBlank(text) ) {
309
      return null;
310
    }
311
    boolean needseparator = false;
312
    StringBuilder builder = null;
313
    for (Factory factory : suggestionFactories.values()) {
314
      SuggestionProvider provider = (SuggestionProvider) factory.create();
315
      String suggestion = provider.getSuggestion(text);
316
      if( suggestion != null ) {
317
        if( builder == null ) {
318
          builder = new StringBuilder();
319
          builder.append(suggestion);
320
        } else {
321
          builder.append("\n\n");
322
          builder.append(suggestion);
323
        }
324
      }
325
    }
326
    if( builder!=null ) {
327
      return builder.toString();
328
    }
329
    return null;
330
  }
331

    
332
  @Override
333
  public void addSuggestionFactory(Factory factory) {
334
    if( factory == null ) {
335
      return;
336
    }
337
    this.suggestionFactories.put(factory.getName(), factory);
338
  }
339

    
340
}