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.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 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<ICons
|
||||
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
|
||||
protected Map<String, String> getSubstitutes() {
|
||||
return substitutes;
|
||||
|
@ -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<T extends MathEntity> implements View.OnClickListener {
|
||||
|
||||
@NotNull
|
||||
private final MathEntityBuilder<? extends T> varBuilder;
|
||||
|
||||
@Nullable
|
||||
private final T editedInstance;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<T> mathRegistry;
|
||||
|
||||
@NotNull
|
||||
private final Object source;
|
||||
|
||||
@NotNull
|
||||
private View editView;
|
||||
|
||||
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
|
||||
@Nullable T editedInstance,
|
||||
@NotNull View editView,
|
||||
@NotNull CalculatorMathRegistry<T> 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<T extends MathEntity> implements View.OnClickListener {
|
||||
|
||||
@NotNull
|
||||
private final MathEntityBuilder<? extends T> varBuilder;
|
||||
|
||||
@Nullable
|
||||
private final T editedInstance;
|
||||
|
||||
@NotNull
|
||||
private final CalculatorMathRegistry<T> mathRegistry;
|
||||
|
||||
@NotNull
|
||||
private final Object source;
|
||||
|
||||
@NotNull
|
||||
private View editView;
|
||||
|
||||
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
|
||||
@Nullable T editedInstance,
|
||||
@NotNull View editView,
|
||||
@NotNull CalculatorMathRegistry<T> 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user