From 3fbce934455dad2f5d3adac15f7186a4028743cc Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 15 Oct 2012 14:37:14 +0400 Subject: [PATCH] Ans fix --- .../android/calculator/CalculatorImpl.java | 2 +- .../calculator/CalculatorVarsRegistry.java | 22 +- .../calculator/math/edit/VarEditorSaver.java | 286 +++++++++--------- 3 files changed, 163 insertions(+), 147 deletions(-) diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index e8f6e5d7..0ae353da 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -444,7 +444,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { varBuilder.setValue(result); varBuilder.setDescription(CalculatorMessages.getBundle().getString("ans_description")); - varsRegistry.add(varBuilder); + CalculatorVarsRegistry.saveVariable(varsRegistry, varBuilder, ansVar, this, false); } } } diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java index c7345015..80ee81ce 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorVarsRegistry.java @@ -8,9 +8,12 @@ package org.solovyev.android.calculator; import jscl.math.function.IConstant; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.model.MathEntityBuilder; import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Vars; import org.solovyev.common.JBuilder; +import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathRegistry; import java.util.HashMap; @@ -41,7 +44,24 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry void saveVariable(@NotNull CalculatorMathRegistry registry, + @NotNull MathEntityBuilder builder, + @Nullable T editedInstance, + @NotNull Object source, boolean save) { + final T addedVar = registry.add(builder); + + if (save) { + registry.save(); + } + + if (editedInstance == null) { + CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source); + } else { + CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_changed, ChangeImpl.newInstance(editedInstance, addedVar), source); + } + } + + @NotNull @Override protected Map getSubstitutes() { return substitutes; diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java index 7ac4d12d..64e29d75 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/math/edit/VarEditorSaver.java @@ -1,145 +1,141 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.calculator.math.edit; - -import android.view.View; -import android.widget.EditText; -import jscl.text.Identifier; -import jscl.text.MutableInt; -import jscl.text.ParseException; -import jscl.text.Parser; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.math.MathType; -import org.solovyev.android.calculator.model.MathEntityBuilder; -import org.solovyev.common.math.MathEntity; -import org.solovyev.common.msg.MessageType; -import org.solovyev.common.text.StringUtils; - -/** - * User: serso - * Date: 12/22/11 - * Time: 9:52 PM - */ -class VarEditorSaver implements View.OnClickListener { - - @NotNull - private final MathEntityBuilder varBuilder; - - @Nullable - private final T editedInstance; - - @NotNull - private final CalculatorMathRegistry mathRegistry; - - @NotNull - private final Object source; - - @NotNull - private View editView; - - public VarEditorSaver(@NotNull MathEntityBuilder varBuilder, - @Nullable T editedInstance, - @NotNull View editView, - @NotNull CalculatorMathRegistry mathRegistry, - @NotNull Object source) { - this.varBuilder = varBuilder; - this.editedInstance = editedInstance; - this.editView = editView; - this.mathRegistry = mathRegistry; - this.source = source; - } - - @Override - public void onClick(View v) { - final Integer error; - - final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); - String name = editName.getText().toString(); - - final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); - String value = editValue.getText().toString(); - - final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); - String description = editDescription.getText().toString(); - - if (isValidName(name)) { - - boolean canBeSaved = false; - - final T entityFromRegistry = mathRegistry.get(name); - if (entityFromRegistry == null) { - canBeSaved = true; - } else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) { - canBeSaved = true; - } - - if (canBeSaved) { - final MathType.Result mathType = MathType.getType(name, 0, false); - - if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) { - - if (StringUtils.isEmpty(value)) { - // value is empty => undefined variable - varBuilder.setName(name); - varBuilder.setDescription(description); - varBuilder.setValue(null); - error = null; - } else { - // value is not empty => must be a number - boolean valid = CalculatorVarsFragment.isValidValue(value); - - if (valid) { - varBuilder.setName(name); - varBuilder.setDescription(description); - varBuilder.setValue(value); - error = null; - } else { - error = R.string.c_value_is_not_a_number; - } - } - } else { - error = R.string.c_var_name_clashes; - } - } else { - error = R.string.c_var_already_exists; - } - } else { - error = R.string.c_name_is_not_valid; - } - - if (error != null) { - CalculatorLocatorImpl.getInstance().getNotifier().showMessage(error, MessageType.error); - } else { - final T addedVar = mathRegistry.add(varBuilder); - mathRegistry.save(); - - if (editedInstance == null) { - CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_added, addedVar, source); - } else { - CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_changed, ChangeImpl.newInstance(editedInstance, addedVar), source); - } - } - } - - boolean isValidName(@Nullable String name) { - boolean result = false; - - if (!StringUtils.isEmpty(name)) { - try { - assert name != null; - Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0()), null); - result = true; - } catch (ParseException e) { - // not valid name; - } - } - - return result; - } -} +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.calculator.math.edit; + +import android.view.View; +import android.widget.EditText; +import jscl.text.Identifier; +import jscl.text.MutableInt; +import jscl.text.ParseException; +import jscl.text.Parser; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.CalculatorMathRegistry; +import org.solovyev.android.calculator.CalculatorVarsRegistry; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.math.MathType; +import org.solovyev.android.calculator.model.MathEntityBuilder; +import org.solovyev.common.math.MathEntity; +import org.solovyev.common.msg.MessageType; +import org.solovyev.common.text.StringUtils; + +/** + * User: serso + * Date: 12/22/11 + * Time: 9:52 PM + */ +class VarEditorSaver implements View.OnClickListener { + + @NotNull + private final MathEntityBuilder varBuilder; + + @Nullable + private final T editedInstance; + + @NotNull + private final CalculatorMathRegistry mathRegistry; + + @NotNull + private final Object source; + + @NotNull + private View editView; + + public VarEditorSaver(@NotNull MathEntityBuilder varBuilder, + @Nullable T editedInstance, + @NotNull View editView, + @NotNull CalculatorMathRegistry mathRegistry, + @NotNull Object source) { + this.varBuilder = varBuilder; + this.editedInstance = editedInstance; + this.editView = editView; + this.mathRegistry = mathRegistry; + this.source = source; + } + + @Override + public void onClick(View v) { + final Integer error; + + final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name); + String name = editName.getText().toString(); + + final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value); + String value = editValue.getText().toString(); + + final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description); + String description = editDescription.getText().toString(); + + if (isValidName(name)) { + + boolean canBeSaved = false; + + final T entityFromRegistry = mathRegistry.get(name); + if (entityFromRegistry == null) { + canBeSaved = true; + } else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) { + canBeSaved = true; + } + + if (canBeSaved) { + final MathType.Result mathType = MathType.getType(name, 0, false); + + if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) { + + if (StringUtils.isEmpty(value)) { + // value is empty => undefined variable + varBuilder.setName(name); + varBuilder.setDescription(description); + varBuilder.setValue(null); + error = null; + } else { + // value is not empty => must be a number + boolean valid = CalculatorVarsFragment.isValidValue(value); + + if (valid) { + varBuilder.setName(name); + varBuilder.setDescription(description); + varBuilder.setValue(value); + error = null; + } else { + error = R.string.c_value_is_not_a_number; + } + } + } else { + error = R.string.c_var_name_clashes; + } + } else { + error = R.string.c_var_already_exists; + } + } else { + error = R.string.c_name_is_not_valid; + } + + if (error != null) { + CalculatorLocatorImpl.getInstance().getNotifier().showMessage(error, MessageType.error); + } else { + CalculatorVarsRegistry.saveVariable(mathRegistry, varBuilder, editedInstance, source, true); + } + } + + boolean isValidName(@Nullable String name) { + boolean result = false; + + if (!StringUtils.isEmpty(name)) { + try { + assert name != null; + Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0()), null); + result = true; + } catch (ParseException e) { + // not valid name; + } + } + + return result; + } +}