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 / DivSuggestionProviderFactory.java @ 46901
History | View | Annotate | Download (5.44 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions; |
---|---|
2 |
|
3 |
import java.math.BigDecimal; |
4 |
import java.util.HashMap; |
5 |
import java.util.Map; |
6 |
import static java.util.concurrent.Executors.callable; |
7 |
import org.apache.commons.lang.StringUtils; |
8 |
import org.apache.commons.lang3.mutable.MutableBoolean; |
9 |
import org.apache.commons.text.similarity.LevenshteinDistance; |
10 |
import org.gvsig.expressionevaluator.Code; |
11 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
12 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
13 |
import org.gvsig.expressionevaluator.swing.SuggestionProvider; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
16 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
17 |
import org.gvsig.fmap.dal.feature.FeatureType; |
18 |
import org.gvsig.tools.ToolsLocator; |
19 |
import org.gvsig.tools.dataTypes.DataTypes; |
20 |
import org.gvsig.tools.i18n.I18nManager; |
21 |
import org.gvsig.tools.util.Factory; |
22 |
|
23 |
/**
|
24 |
*
|
25 |
* @author jjdelcerro
|
26 |
*/
|
27 |
@SuppressWarnings("UseSpecificCatch") |
28 |
public class DivSuggestionProviderFactory implements Factory { |
29 |
|
30 |
private FeatureType featureType;
|
31 |
|
32 |
public DivSuggestionProviderFactory(FeatureStore store) {
|
33 |
try {
|
34 |
this.featureType = store.getDefaultFeatureType();
|
35 |
} catch (DataException ex) {
|
36 |
this.featureType = null; |
37 |
} |
38 |
} |
39 |
|
40 |
@Override
|
41 |
public String getName() { |
42 |
return "DivSuggestion"; |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public Object create(Object... parameters) { |
47 |
SuggestionProvider suggestionProvider = new DivSuggestionProvider();
|
48 |
return suggestionProvider;
|
49 |
|
50 |
} |
51 |
|
52 |
private class DivSuggestionProvider implements SuggestionProvider { |
53 |
|
54 |
@Override
|
55 |
public String getSuggestion(String expression) { |
56 |
if ( StringUtils.isBlank(expression)) {
|
57 |
return null; |
58 |
} |
59 |
MutableBoolean showSuggestion = new MutableBoolean(false); |
60 |
try {
|
61 |
Code code = ExpressionUtils.compile(expression); |
62 |
if (code == null) { |
63 |
return null; |
64 |
} |
65 |
code.accept((Object obj) -> {
|
66 |
if (obj instanceof Code.Callable) { |
67 |
Code.Callable callable = (Code.Callable) obj; |
68 |
String name = callable.name();
|
69 |
if( !StringUtils.equalsIgnoreCase(name, ExpressionBuilder.OPERATOR_DIV)) {
|
70 |
return;
|
71 |
} |
72 |
boolean isInt1 = false; |
73 |
boolean isDec1 = false; |
74 |
boolean isInt2 = false; |
75 |
boolean isDec2 = false; |
76 |
boolean isCast = false; |
77 |
Code p1 = callable.parameters().get(0);
|
78 |
Code p2 = callable.parameters().get(1);
|
79 |
if( p1 instanceof Code.Constant ) { |
80 |
Code.Constant c1 = (Code.Constant)p1; |
81 |
Object v1 = c1.value();
|
82 |
isInt1 = (v1 instanceof Integer || v1 instanceof Long || v1 instanceof Short); |
83 |
isDec1 = (v1 instanceof Double || v1 instanceof Float || v1 instanceof BigDecimal); |
84 |
} else if( p1 instanceof Code.Identifier ) { |
85 |
Code.Identifier i1 = (Code.Identifier)p1; |
86 |
FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(i1.name()); |
87 |
if( attr != null ) { |
88 |
int type1 = attr.getDataType().getType();
|
89 |
isInt1 = (type1 == DataTypes.INT || type1 == DataTypes.LONG ); |
90 |
isDec1 = (type1 == DataTypes.DECIMAL || type1 == DataTypes.DOUBLE || type1 == DataTypes.FLOAT ); |
91 |
} |
92 |
} else if( p1 instanceof Code.Callable && StringUtils.equalsIgnoreCase(((Code.Callable)p1).name(), ExpressionBuilder.FUNCTION_CAST) ) { |
93 |
isCast=true;
|
94 |
} |
95 |
if( p2 instanceof Code.Constant ) { |
96 |
Code.Constant c2 = (Code.Constant)p2; |
97 |
Object v2 = c2.value();
|
98 |
isInt2 = (v2 instanceof Integer || v2 instanceof Long || v2 instanceof Short); |
99 |
isDec2 = (v2 instanceof Double || v2 instanceof Float || v2 instanceof BigDecimal); |
100 |
} else if( p2 instanceof Code.Identifier ) { |
101 |
Code.Identifier i2 = (Code.Identifier)p2; |
102 |
FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(i2.name()); |
103 |
if( attr != null ) { |
104 |
int type2 = attr.getDataType().getType();
|
105 |
isInt2 = (type2 == DataTypes.INT || type2 == DataTypes.LONG ); |
106 |
isDec2 = (type2 == DataTypes.DECIMAL || type2 == DataTypes.DOUBLE || type2 == DataTypes.FLOAT ); |
107 |
} |
108 |
} else if( p2 instanceof Code.Callable && StringUtils.equalsIgnoreCase(((Code.Callable)p2).name(), ExpressionBuilder.FUNCTION_CAST) ) { |
109 |
isCast=true;
|
110 |
} |
111 |
if( isCast ) {
|
112 |
return;
|
113 |
} |
114 |
if( isDec1 || isDec2 ) {
|
115 |
return;
|
116 |
} |
117 |
if( isInt1 || isInt2 || (!isInt1 && !isInt2) ) {
|
118 |
showSuggestion.setValue(true);
|
119 |
return;
|
120 |
} |
121 |
// if( (isInt1 && !isInt2) || (!isInt1 && isInt2)) {
|
122 |
// showSuggestion.setValue(true);
|
123 |
// }
|
124 |
} |
125 |
}); |
126 |
} catch (Throwable th) { |
127 |
} |
128 |
if( showSuggestion.isFalse() ) {
|
129 |
return null; |
130 |
} |
131 |
|
132 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
133 |
StringBuilder suggestion = new StringBuilder(); |
134 |
suggestion.append(i18n.getTranslation("_Division_of_two_integer_values_returns_an_integer_value_You_can_force_an_operand_to_decimal_to_get_decimal_values"));
|
135 |
return suggestion.toString();
|
136 |
} |
137 |
|
138 |
} |
139 |
|
140 |
} |