Ans fix
This commit is contained in:
parent
f31d033caf
commit
3fbce93445
@ -444,7 +444,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
varBuilder.setValue(result);
|
varBuilder.setValue(result);
|
||||||
varBuilder.setDescription(CalculatorMessages.getBundle().getString("ans_description"));
|
varBuilder.setDescription(CalculatorMessages.getBundle().getString("ans_description"));
|
||||||
|
|
||||||
varsRegistry.add(varBuilder);
|
CalculatorVarsRegistry.saveVariable(varsRegistry, varBuilder, ansVar, this, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,12 @@ package org.solovyev.android.calculator;
|
|||||||
|
|
||||||
import jscl.math.function.IConstant;
|
import jscl.math.function.IConstant;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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.Var;
|
||||||
import org.solovyev.android.calculator.model.Vars;
|
import org.solovyev.android.calculator.model.Vars;
|
||||||
import org.solovyev.common.JBuilder;
|
import org.solovyev.common.JBuilder;
|
||||||
|
import org.solovyev.common.math.MathEntity;
|
||||||
import org.solovyev.common.math.MathRegistry;
|
import org.solovyev.common.math.MathRegistry;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -41,7 +44,24 @@ public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<ICons
|
|||||||
super(mathRegistry, "c_var_description_", mathEntityDao);
|
super(mathRegistry, "c_var_description_", mathEntityDao);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
public static <T extends MathEntity> void saveVariable(@NotNull CalculatorMathRegistry<T> registry,
|
||||||
|
@NotNull MathEntityBuilder<? extends T> 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
|
@Override
|
||||||
protected Map<String, String> getSubstitutes() {
|
protected Map<String, String> getSubstitutes() {
|
||||||
return substitutes;
|
return substitutes;
|
||||||
|
@ -1,145 +1,141 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||||
* For more information, please, contact se.solovyev@gmail.com
|
* For more information, please, contact se.solovyev@gmail.com
|
||||||
* or visit http://se.solovyev.org
|
* or visit http://se.solovyev.org
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.solovyev.android.calculator.math.edit;
|
package org.solovyev.android.calculator.math.edit;
|
||||||
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import jscl.text.Identifier;
|
import jscl.text.Identifier;
|
||||||
import jscl.text.MutableInt;
|
import jscl.text.MutableInt;
|
||||||
import jscl.text.ParseException;
|
import jscl.text.ParseException;
|
||||||
import jscl.text.Parser;
|
import jscl.text.Parser;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.calculator.*;
|
import org.solovyev.android.calculator.CalculatorLocatorImpl;
|
||||||
import org.solovyev.android.calculator.math.MathType;
|
import org.solovyev.android.calculator.CalculatorMathRegistry;
|
||||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
import org.solovyev.android.calculator.CalculatorVarsRegistry;
|
||||||
import org.solovyev.common.math.MathEntity;
|
import org.solovyev.android.calculator.R;
|
||||||
import org.solovyev.common.msg.MessageType;
|
import org.solovyev.android.calculator.math.MathType;
|
||||||
import org.solovyev.common.text.StringUtils;
|
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||||
|
import org.solovyev.common.math.MathEntity;
|
||||||
/**
|
import org.solovyev.common.msg.MessageType;
|
||||||
* User: serso
|
import org.solovyev.common.text.StringUtils;
|
||||||
* Date: 12/22/11
|
|
||||||
* Time: 9:52 PM
|
/**
|
||||||
*/
|
* User: serso
|
||||||
class VarEditorSaver<T extends MathEntity> implements View.OnClickListener {
|
* Date: 12/22/11
|
||||||
|
* Time: 9:52 PM
|
||||||
@NotNull
|
*/
|
||||||
private final MathEntityBuilder<? extends T> varBuilder;
|
class VarEditorSaver<T extends MathEntity> implements View.OnClickListener {
|
||||||
|
|
||||||
@Nullable
|
@NotNull
|
||||||
private final T editedInstance;
|
private final MathEntityBuilder<? extends T> varBuilder;
|
||||||
|
|
||||||
@NotNull
|
@Nullable
|
||||||
private final CalculatorMathRegistry<T> mathRegistry;
|
private final T editedInstance;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Object source;
|
private final CalculatorMathRegistry<T> mathRegistry;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private View editView;
|
private final Object source;
|
||||||
|
|
||||||
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
|
@NotNull
|
||||||
@Nullable T editedInstance,
|
private View editView;
|
||||||
@NotNull View editView,
|
|
||||||
@NotNull CalculatorMathRegistry<T> mathRegistry,
|
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
|
||||||
@NotNull Object source) {
|
@Nullable T editedInstance,
|
||||||
this.varBuilder = varBuilder;
|
@NotNull View editView,
|
||||||
this.editedInstance = editedInstance;
|
@NotNull CalculatorMathRegistry<T> mathRegistry,
|
||||||
this.editView = editView;
|
@NotNull Object source) {
|
||||||
this.mathRegistry = mathRegistry;
|
this.varBuilder = varBuilder;
|
||||||
this.source = source;
|
this.editedInstance = editedInstance;
|
||||||
}
|
this.editView = editView;
|
||||||
|
this.mathRegistry = mathRegistry;
|
||||||
@Override
|
this.source = source;
|
||||||
public void onClick(View v) {
|
}
|
||||||
final Integer error;
|
|
||||||
|
@Override
|
||||||
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
|
public void onClick(View v) {
|
||||||
String name = editName.getText().toString();
|
final Integer error;
|
||||||
|
|
||||||
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
|
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
|
||||||
String value = editValue.getText().toString();
|
String name = editName.getText().toString();
|
||||||
|
|
||||||
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
|
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
|
||||||
String description = editDescription.getText().toString();
|
String value = editValue.getText().toString();
|
||||||
|
|
||||||
if (isValidName(name)) {
|
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
|
||||||
|
String description = editDescription.getText().toString();
|
||||||
boolean canBeSaved = false;
|
|
||||||
|
if (isValidName(name)) {
|
||||||
final T entityFromRegistry = mathRegistry.get(name);
|
|
||||||
if (entityFromRegistry == null) {
|
boolean canBeSaved = false;
|
||||||
canBeSaved = true;
|
|
||||||
} else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) {
|
final T entityFromRegistry = mathRegistry.get(name);
|
||||||
canBeSaved = true;
|
if (entityFromRegistry == null) {
|
||||||
}
|
canBeSaved = true;
|
||||||
|
} else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) {
|
||||||
if (canBeSaved) {
|
canBeSaved = true;
|
||||||
final MathType.Result mathType = MathType.getType(name, 0, false);
|
}
|
||||||
|
|
||||||
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
|
if (canBeSaved) {
|
||||||
|
final MathType.Result mathType = MathType.getType(name, 0, false);
|
||||||
if (StringUtils.isEmpty(value)) {
|
|
||||||
// value is empty => undefined variable
|
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
|
||||||
varBuilder.setName(name);
|
|
||||||
varBuilder.setDescription(description);
|
if (StringUtils.isEmpty(value)) {
|
||||||
varBuilder.setValue(null);
|
// value is empty => undefined variable
|
||||||
error = null;
|
varBuilder.setName(name);
|
||||||
} else {
|
varBuilder.setDescription(description);
|
||||||
// value is not empty => must be a number
|
varBuilder.setValue(null);
|
||||||
boolean valid = CalculatorVarsFragment.isValidValue(value);
|
error = null;
|
||||||
|
} else {
|
||||||
if (valid) {
|
// value is not empty => must be a number
|
||||||
varBuilder.setName(name);
|
boolean valid = CalculatorVarsFragment.isValidValue(value);
|
||||||
varBuilder.setDescription(description);
|
|
||||||
varBuilder.setValue(value);
|
if (valid) {
|
||||||
error = null;
|
varBuilder.setName(name);
|
||||||
} else {
|
varBuilder.setDescription(description);
|
||||||
error = R.string.c_value_is_not_a_number;
|
varBuilder.setValue(value);
|
||||||
}
|
error = null;
|
||||||
}
|
} else {
|
||||||
} else {
|
error = R.string.c_value_is_not_a_number;
|
||||||
error = R.string.c_var_name_clashes;
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error = R.string.c_var_already_exists;
|
error = R.string.c_var_name_clashes;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error = R.string.c_name_is_not_valid;
|
error = R.string.c_var_already_exists;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (error != null) {
|
error = R.string.c_name_is_not_valid;
|
||||||
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(error, MessageType.error);
|
}
|
||||||
} else {
|
|
||||||
final T addedVar = mathRegistry.add(varBuilder);
|
if (error != null) {
|
||||||
mathRegistry.save();
|
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(error, MessageType.error);
|
||||||
|
} else {
|
||||||
if (editedInstance == null) {
|
CalculatorVarsRegistry.saveVariable(mathRegistry, varBuilder, editedInstance, source, true);
|
||||||
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)) {
|
||||||
boolean isValidName(@Nullable String name) {
|
try {
|
||||||
boolean result = false;
|
assert name != null;
|
||||||
|
Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0()), null);
|
||||||
if (!StringUtils.isEmpty(name)) {
|
result = true;
|
||||||
try {
|
} catch (ParseException e) {
|
||||||
assert name != null;
|
// not valid name;
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user