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 / QuotesForFieldNameSuggestionProviderFactory.java @ 44855
History | View | Annotate | Download (2.54 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions; |
---|---|
2 |
|
3 |
import java.util.Collections; |
4 |
import java.util.regex.Pattern; |
5 |
import org.apache.commons.lang.StringUtils; |
6 |
import org.apache.commons.lang3.RandomUtils; |
7 |
import org.gvsig.expressionevaluator.swing.SuggestionProvider; |
8 |
import org.gvsig.fmap.dal.exception.DataException; |
9 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
10 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
11 |
import org.gvsig.tools.ToolsLocator; |
12 |
import org.gvsig.tools.i18n.I18nManager; |
13 |
import org.gvsig.tools.util.Factory; |
14 |
|
15 |
/**
|
16 |
*
|
17 |
* @author jjdelcerro
|
18 |
*/
|
19 |
@SuppressWarnings("UseSpecificCatch") |
20 |
public class QuotesForFieldNameSuggestionProviderFactory implements Factory { |
21 |
|
22 |
private static long lastSuggestedTime = 0; |
23 |
private final static Pattern VALID_IDENTIFIER = Pattern.compile("[A-Za-z_]*[A-Za-z0-9_]*"); |
24 |
|
25 |
private final FeatureStore store; |
26 |
private boolean suggestionApplies; |
27 |
|
28 |
public QuotesForFieldNameSuggestionProviderFactory(FeatureStore store) {
|
29 |
this.store = store;
|
30 |
String storename = store.getName();
|
31 |
this.suggestionApplies = false; |
32 |
for (FeatureAttributeDescriptor attr : getFeatureAttributeDescriptors()) {
|
33 |
if (!VALID_IDENTIFIER.matcher(attr.getName()).matches()) {
|
34 |
this.suggestionApplies = true; |
35 |
break;
|
36 |
} |
37 |
} |
38 |
} |
39 |
|
40 |
private Iterable<FeatureAttributeDescriptor> getFeatureAttributeDescriptors() { |
41 |
try {
|
42 |
return this.store.getDefaultFeatureType(); |
43 |
} catch (DataException ex) {
|
44 |
return Collections.EMPTY_LIST; |
45 |
} |
46 |
} |
47 |
|
48 |
@Override
|
49 |
public String getName() { |
50 |
return "QuotesForFieldNameSuggestion"; |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public Object create(Object... parameters) { |
55 |
SuggestionProvider suggestionProvider = new QuotesForFieldNameSuggestionProvider();
|
56 |
return suggestionProvider;
|
57 |
|
58 |
} |
59 |
|
60 |
private class QuotesForFieldNameSuggestionProvider implements SuggestionProvider { |
61 |
|
62 |
@Override
|
63 |
public String getSuggestion(String expression) { |
64 |
if (!suggestionApplies || StringUtils.isBlank(expression)) {
|
65 |
return null; |
66 |
} |
67 |
long now = System.currentTimeMillis(); |
68 |
if ( now - lastSuggestedTime >= RandomUtils.nextInt(5, 15) * 60 * 1000) { |
69 |
lastSuggestedTime = System.currentTimeMillis();
|
70 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
71 |
String suggestion = i18n.getTranslation("_You_must_use_double_quotes_to_use_a_field_that_is_not_a_valid_identifier_When_the_name_does_not_start_with_a_letter_or_contains_characters_that_are_not_letters_numbers_or_underscore"); |
72 |
return suggestion;
|
73 |
} |
74 |
return null; |
75 |
} |
76 |
|
77 |
} |
78 |
|
79 |
} |