Revision 46901

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/operator/DivOperator.java
27 27
            return null;
28 28
        }
29 29
        int type = this.getType(op1, op2);
30
        if( (type & TYPE_BIGDECIMAL) == TYPE_BIGDECIMAL ) {
31
            BigDecimal value = getBigDecimal(op1,1).divide(getBigDecimal(op2,2));
32
            return value;
33
        }
34 30
        if( (type & TYPE_DOUBLE) == TYPE_DOUBLE ) {
35 31
            double value = getDouble(op1,1) / getDouble(op2,2);
36 32
            checkDoubleValue(value);
......
41 37
            checkDoubleValue(value);
42 38
            return value;
43 39
        }
40
        if( (type & TYPE_BIGDECIMAL) == TYPE_BIGDECIMAL ) {
41
            try {
42
                BigDecimal value = getBigDecimal(op1,1).divide(getBigDecimal(op2,2));
43
                return value;
44
            } catch(ArithmeticException ex) {
45
                double v1 = getDouble(op1,1) / getDouble(op2,2);
46
                BigDecimal value = BigDecimal.valueOf(v1);
47
                return value;
48
            }
49
        }
44 50
        if( (type & TYPE_LONG) == TYPE_LONG ) {
45 51
            long value = getLong(op1,1) / getLong(op2,2);
46 52
            return value;
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
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
}
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/DefaultDataSwingManager.java
111 111
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory;
112 112
import org.gvsig.fmap.dal.swing.impl.expressionevaluator.DefaultFeatureStoreElement2;
113 113
import org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions.CheckTableFieldsSuggestionProviderFactory;
114
import org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions.DivSuggestionProviderFactory;
114 115
import org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions.QuotesForFieldNameSuggestionProviderFactory;
115 116
import org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions.StorePointFieldSuggestionProviderFactory;
116 117
import org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions.WrongNameFieldSuggestionProviderFactory;
......
259 260
        builder.addSuggestionFactory(new QuotesForFieldNameSuggestionProviderFactory(store));
260 261
        builder.addSuggestionFactory(new WrongNameFieldSuggestionProviderFactory(store));
261 262
        builder.addSuggestionFactory(new CheckTableFieldsSuggestionProviderFactory(store));
263
        builder.addSuggestionFactory(new DivSuggestionProviderFactory(store));    
262 264
    }
263 265

  
264 266
    @Override
......
311 313
        builder.addSuggestionFactory(new QuotesForFieldNameSuggestionProviderFactory(store));
312 314
        builder.addSuggestionFactory(new WrongNameFieldSuggestionProviderFactory(store));
313 315
        builder.addSuggestionFactory(new CheckTableFieldsSuggestionProviderFactory(store));    
316
        builder.addSuggestionFactory(new DivSuggestionProviderFactory(store));    
314 317
    }
315 318

  
316 319
    @Override
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/resources-plugin/i18n/text_en.properties
1630 1630

  
1631 1631
_The_precision_is_greater_than_the_display_size=The precision is greater than the display size
1632 1632
_This_can_cause_problems_with_some_data_providers_like_SHP_or_DBF=This can cause problems with some data providers like SHP or DBF
1633
_Do_you_want_to_continue_accepting_the_current_values=Do you want to continue accepting the current values?
1633
_Do_you_want_to_continue_accepting_the_current_values=Do you want to continue accepting the current values?
1634

  
1635
_Division_of_two_integer_values_returns_an_integer_value_You_can_force_an_operand_to_decimal_to_get_decimal_values=Division of two integer values returns an integer value.\nYou can force an operand to decimal to get decimal values.
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/resources-plugin/i18n/text.properties
1713 1713

  
1714 1714
_The_precision_is_greater_than_the_display_size=La ''precisi\u00f3n'' es mayor que los ''caracteres en pantalla''.
1715 1715
_This_can_cause_problems_with_some_data_providers_like_SHP_or_DBF=Esto puede ocasionar problemas con algunos proveedores de datos como SHP o DBF.
1716
_Do_you_want_to_continue_accepting_the_current_values=\u00bfDesea continuar aceptando los valores actuales?
1716
_Do_you_want_to_continue_accepting_the_current_values=\u00bfDesea continuar aceptando los valores actuales?
1717
_Division_of_two_integer_values_returns_an_integer_value_You_can_force_an_operand_to_decimal_to_get_decimal_values=La divisi\u00f3n de dos valores enteros devuelve un valor entero.\nUsted puede forzar un operando a decimal para obtener valores decimales.

Also available in: Unified diff