Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / expressionevaluator / suggestions / StorePointFieldSuggestionProviderFactory.java @ 44855

History | View | Annotate | Download (3.53 KB)

1
package org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions;
2

    
3
import java.util.Collections;
4
import org.apache.commons.lang.StringUtils;
5
import org.apache.commons.lang3.RandomUtils;
6
import org.apache.commons.lang3.mutable.MutableObject;
7
import org.gvsig.expressionevaluator.Code;
8
import org.gvsig.expressionevaluator.ExpressionUtils;
9
import org.gvsig.expressionevaluator.swing.SuggestionProvider;
10
import org.gvsig.fmap.dal.exception.DataException;
11
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
12
import org.gvsig.fmap.dal.feature.FeatureStore;
13
import org.gvsig.tools.ToolsLocator;
14
import org.gvsig.tools.i18n.I18nManager;
15
import org.gvsig.tools.util.Factory;
16
import org.gvsig.tools.visitor.VisitCanceledException;
17

    
18
/**
19
 *
20
 * @author jjdelcerro
21
 */
22
@SuppressWarnings("UseSpecificCatch")
23
public class StorePointFieldSuggestionProviderFactory implements Factory {
24

    
25
  private static long lastSuggestedTime = 0;
26
  private final FeatureStore store;
27
  private boolean suggestionApplies;
28

    
29
  public StorePointFieldSuggestionProviderFactory(FeatureStore store) {
30
    this.store = store;
31
    String storename = store.getName();
32
    this.suggestionApplies = false;
33
    for (FeatureAttributeDescriptor attr : getFeatureAttributeDescriptors() ) {
34
      if (StringUtils.equalsIgnoreCase(storename, attr.getName())) {
35
        this.suggestionApplies = true;
36
        break;
37
      }
38
    }
39
  }
40
  
41
  private Iterable<FeatureAttributeDescriptor> getFeatureAttributeDescriptors() {
42
    try {
43
      return this.store.getDefaultFeatureType();
44
    } catch (DataException ex) {
45
      return Collections.EMPTY_LIST;
46
    }
47
  }
48

    
49
  @Override
50
  public String getName() {
51
    return "StorePointFieldSuggestion";
52
  }
53

    
54
  @Override
55
  public Object create(Object... parameters) {
56
    SuggestionProvider suggestionProvider = new StorePointFieldSuggestionProvider();
57
    return suggestionProvider;
58

    
59
  }
60

    
61
  private class StorePointFieldSuggestionProvider implements SuggestionProvider {
62

    
63
    @Override
64
    public String getSuggestion(String expression) {
65
      if (!suggestionApplies || StringUtils.isBlank(expression)) {
66
        return null;
67
      }
68
      long now = System.currentTimeMillis();
69
      if ( now - lastSuggestedTime >= RandomUtils.nextInt(5, 15) * 60 * 1000) {
70
        lastSuggestedTime = System.currentTimeMillis();
71
        I18nManager i18n = ToolsLocator.getI18nManager();
72
        String suggestion = i18n.getTranslation("_You_can_access_the_fields_in_a_table_by_prefixing_the_table_name_followed_by_a_period_For_example_TABLE_FIELD_This_is_necessary_whenever_the_name_of_the_field_and_the_table_match");
73
        return suggestion;
74
      }
75
      MutableObject<String> suggestion = new MutableObject<>();
76
      try {
77
        Code code = ExpressionUtils.compile(expression);
78
        if (code == null) {
79
          return null;
80
        }
81
        final String storename = store.getName();
82

    
83
        code.accept((Object obj) -> {
84
          if (obj instanceof Code.Identifier) {
85
            String varname = ((Code.Identifier) obj).name();
86
            if (StringUtils.equalsIgnoreCase(varname, storename)) {
87
              I18nManager i18n = ToolsLocator.getI18nManager();
88
              suggestion.setValue(
89
                      i18n.getTranslation("_You_can_access_the_fields_in_a_table_by_prefixing_the_table_name_followed_by_a_period_For_example_TABLE_FIELD_This_is_necessary_whenever_the_name_of_the_field_and_the_table_match")
90
              );
91
              throw new VisitCanceledException();
92
            }
93
          }
94
        });
95
      } catch (Throwable th) {
96
      }
97

    
98
      return suggestion.getValue();
99
    }
100
  }
101

    
102
}