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

History | View | Annotate | Download (9.88 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

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

    
32
    private List<Element> elements;
33
    private int limit;
34
    private Map<String, SymbolTable> symbolTables;
35
    private final ExpressionEvaluatorSwingManager manager;
36
    private SymbolTable previewSymbolTable;
37
    private boolean SQLCompatible;
38
    private final PropertiesSupportHelper propertiesSupportHelper;
39
    private boolean showLabelsOfAttributes;
40
    private boolean allowAggregates;
41
    private java.util.function.Function<String, Integer>allowedFunctions;
42
    private Map<String,Factory> suggestionFactories;
43

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

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

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

    
94
    @Override
95
    public Preferences getPreferences() {
96
        return this;
97
    }
98

    
99
    @Override
100
    public Collection<Factory> getSuggestionFactories() {
101
      return this.suggestionFactories.values();
102
    }
103

    
104
    @Override
105
    public java.util.function.Function<String, Integer> getAllowedFunctions() {
106
      return this.allowedFunctions;
107
    }
108

    
109
    private void initSymbolTables() {
110
        ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager();
111
        for (SymbolTableFactory factory : theManager.getSymbolTableFactories()) {
112
            if (factory.isAutoload()) {
113
                this.addSymbolTable(factory.create());
114
            }
115
        }
116
    }
117

    
118
    @Override
119
    public List<Element> getElements() {
120
        if (this.elements == null || this.elements.isEmpty() ) {
121
            this.elements = manager.createElements(this.getSymbolTables(), (Function f) -> {
122
              if( f.group().equalsIgnoreCase(Function.GROUP_AGGREGATE) &&
123
                      !this.allowAggregates() ) {
124
                return false;
125
              }
126
              java.util.function.Function<String, Integer> isFunctionAllowed = this.isFunctionAllowed();
127
              if( isFunctionAllowed!=null && isFunctionAllowed.apply(f.name())==DataType.NO ) {
128
                return false;
129
              }
130
              return true;
131
            });
132
            for (ElementFactory factory : manager.getElementFactories()) {
133
                if (factory.isAutoload()) {
134
                    Element e = factory.createElement();
135
                    this.elements.add(e);
136
                    e.setConfig(this);
137
                }
138
            }
139
        }
140
        return Collections.unmodifiableList(elements);
141
    }
142

    
143
    @Override
144
    public void addElement(Element element) {
145
        if( element==null ) {
146
            return;
147
        }
148
        this.getElements(); // Forzamos a crear la lista de elementos
149
        for (Element theElement : this.elements) {
150
            if (theElement.getName().equalsIgnoreCase(element.getName())) {
151
                return;
152
            }
153
        }
154
        this.elements.add(element);
155
        element.setConfig(this);
156
    }
157

    
158
    @Override
159
    public Collection<SymbolTable> getSymbolTables() {
160
        return Collections.unmodifiableCollection(this.symbolTables.values());
161
    }
162

    
163
    @Override
164
    public void addSymbolTable(String name) {
165
        ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager();
166
        SymbolTable symbolTable = theManager.getSymbolTable(name);
167
        if (symbolTable == null) {
168
            throw new IllegalArgumentException("Can't locate symbol table '" + name + "'.");
169
        }
170
        addSymbolTable(symbolTable);
171
    }
172

    
173
    @Override
174
    public void addSymbolTable(SymbolTable symbolTable) {
175
        if (symbolTable == null) {
176
            return;
177
        }
178
        if (this.elements != null && !this.elements.isEmpty() ) {
179
            throw new IllegalStateException("Can not add symbol tables once the elements are built.");
180
        }
181
        if (this.symbolTables == null) {
182
            this.symbolTables = new HashMap<>();
183
        }
184
        this.symbolTables.put(symbolTable.getName(), symbolTable);
185
    }
186

    
187
    @Override
188
    public void removeAllSymbolTables() {
189
        this.symbolTables.clear();
190
    }
191

    
192
    @Override
193
    public void removeAllElements() {
194
        this.elements.clear();
195
    }
196

    
197
    @Override
198
    public int getSimpleElementsLimit() {
199
        return this.limit;
200
    }
201

    
202
    @Override
203
    public void setSimpleElementsLimit(int limit) {
204
        this.limit = limit;
205
    }
206

    
207
    @Override
208
    public void removeSymbolTable(String name) {
209
        this.symbolTables.remove(name);
210
    }
211

    
212
//    @Override
213
//    public void setScriptsEnabled(boolean enabled) {
214
//        this.scriptsEnabled = enabled;
215
//    }
216
//
217
//    @Override
218
//    public boolean getScriptsEnabled() {
219
//        return this.scriptsEnabled;
220
//    }
221
//
222
    @Override
223
    public SymbolTable getPreviewSymbolTable() {
224
        return this.previewSymbolTable;
225
    }
226

    
227
    @Override
228
    public void setPreviewSymbolTable(SymbolTable symbolTable) {
229
        this.previewSymbolTable = symbolTable;
230
    }
231

    
232
    @Override
233
    public void setSQLCompatible(boolean SQLCompatible) {
234
        this.SQLCompatible = SQLCompatible;
235
    }
236

    
237
    @Override
238
    public boolean isSQLCompatible() {
239
        return SQLCompatible;
240
    }
241

    
242
    @Override
243
    public Object getProperty(String string) {
244
        return this.propertiesSupportHelper.getProperty(string);
245
    }
246

    
247
    @Override
248
    public void setProperty(String string, Object o) {
249
        this.propertiesSupportHelper.setProperty(string, o);
250
    }
251

    
252
    @Override
253
    public Map<String, Object> getProperties() {
254
        return this.propertiesSupportHelper.getProperties();
255
    }
256

    
257
    @Override
258
    public boolean getShowLabelsOfElements() {
259
        return this.showLabelsOfAttributes;
260
    }
261

    
262
    @Override
263
    public void setShowLabelsOfElements(boolean showLabels) {
264
        this.showLabelsOfAttributes = showLabels;
265
    }
266

    
267
  @Override
268
  public boolean allowAggregates() {
269
    return this.allowAggregates;
270
  }
271

    
272
  @Override
273
  public boolean allowAggregates(boolean allow) {
274
    boolean r = this.allowAggregates;
275
    this.allowAggregates = allow;
276
    return r;
277
  }
278

    
279
  @Override
280
  public java.util.function.Function<String, Integer> isFunctionAllowed() {
281
    return this.allowedFunctions;
282
  }
283

    
284
  @Override
285
  public void setAllowedFunctions(java.util.function.Function<String, Integer> allow) {
286
    this.allowedFunctions = allow;
287
  }
288

    
289
  @Override
290
  public String getSuggestion(Expression expression) {
291
    if( expression == null ) {
292
      return null;
293
    }
294
    String text = expression.getPhrase();
295
    if( StringUtils.isBlank(text) ) {
296
      return null;
297
    }
298
    boolean needseparator = false;
299
    StringBuilder builder = null;
300
    for (Factory factory : suggestionFactories.values()) {
301
      SuggestionProvider provider = (SuggestionProvider) factory.create();
302
      String suggestion = provider.getSuggestion(text);
303
      if( suggestion != null ) {
304
        if( builder == null ) {
305
          builder = new StringBuilder();
306
          builder.append(suggestion);
307
        } else {
308
          builder.append("\n\n");
309
          builder.append(suggestion);
310
        }
311
      }
312
    }
313
    if( builder!=null ) {
314
      return builder.toString();
315
    }
316
    return null;
317
  }
318

    
319
  @Override
320
  public void addSuggestionFactory(Factory factory) {
321
    if( factory == null ) {
322
      return;
323
    }
324
    this.suggestionFactories.put(factory.getName(), factory);
325
  }
326

    
327
}