functions
This commit is contained in:
parent
357eed9f3e
commit
75a3a72a84
@ -16,6 +16,7 @@ import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -96,7 +97,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
final AFunction result = new AFunction();
|
||||
result.setName(entity.getName());
|
||||
result.setContent(((CustomFunction) entity).getContent());
|
||||
result.setParameterNames(((CustomFunction) entity).getParameterNames());
|
||||
result.setParameterNames(new ArrayList<String>(((CustomFunction) entity).getParameterNames()));
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
|
@ -72,12 +72,6 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
}
|
||||
}
|
||||
|
||||
if (mathTypeBefore != null &&
|
||||
(mathTypeBefore.getMathType() == MathType.function || mathTypeBefore.getMathType() == MathType.operator) &&
|
||||
CollectionsUtils.find(MathType.openGroupSymbols, startsWithFinder) != null) {
|
||||
throw new CalculatorParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.getMatch()));
|
||||
}
|
||||
|
||||
i = mathTypeResult.processToJscl(result, i);
|
||||
}
|
||||
return result;
|
||||
|
@ -6,16 +6,18 @@
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.function.IFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.MathPersistenceEntity;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@ -25,7 +27,7 @@ import java.util.List;
|
||||
* Time: 5:25 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
@Root(name = "function")
|
||||
public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
|
||||
/*
|
||||
@ -42,20 +44,20 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Element
|
||||
@Element(name = "body")
|
||||
@NotNull
|
||||
private String content;
|
||||
|
||||
@Element(required = false)
|
||||
@ElementList
|
||||
@NotNull
|
||||
private List<String> parameterNames = Collections.emptyList();
|
||||
private List<String> parameterNames = new ArrayList<String>();
|
||||
|
||||
@Element
|
||||
private boolean system;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String description;
|
||||
@NotNull
|
||||
private String description = "";
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
@ -86,7 +88,7 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
final IFunction that = ((IFunction) mathEntity);
|
||||
this.name = that.getName();
|
||||
this.content = that.getContent();
|
||||
this.description = this.getContent();
|
||||
this.description = StringUtils.getNotEmpty(this.getDescription(), "");
|
||||
this.system = that.isSystem();
|
||||
if (that.isIdDefined()) {
|
||||
this.id = that.getId();
|
||||
@ -104,7 +106,7 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* GETTERS/SEETTERS
|
||||
* GETTERS/SETTERS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@ -144,8 +146,8 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
@ -201,29 +203,15 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
this.id = function.getId();
|
||||
}
|
||||
|
||||
public Builder(@NotNull IConstant function) {
|
||||
this.name = function.getName();
|
||||
public Builder(@NotNull String name,
|
||||
@NotNull String value,
|
||||
@NotNull List<String> parameterNames) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
this.value = function.getValue();
|
||||
|
||||
this.system = function.isSystem();
|
||||
this.description = function.getDescription();
|
||||
if (function.isIdDefined()) {
|
||||
this.id = function.getId();
|
||||
}
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name, @NotNull Double value) {
|
||||
this(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name, @Nullable String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
public Builder setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
@ -262,8 +250,8 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
result.name = name;
|
||||
result.content = value;
|
||||
result.system = system;
|
||||
result.description = description;
|
||||
result.parameterNames = parameterNames;
|
||||
result.description = StringUtils.getNotEmpty(description, "");
|
||||
result.parameterNames = new ArrayList<String>(parameterNames);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -0,0 +1,155 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.util.ExpressionGenerator;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
import org.solovyev.common.equals.CollectionEqualizer;
|
||||
import org.solovyev.common.equals.EqualsUtils;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/14/12
|
||||
* Time: 8:06 PM
|
||||
*/
|
||||
public class FunctionsTest {
|
||||
|
||||
private static final String xml = "<functions>\n" +
|
||||
" <functions class=\"java.util.ArrayList\">\n" +
|
||||
" <function>\n" +
|
||||
" <name>test</name>\n" +
|
||||
" <body>x+y</body>\n" +
|
||||
" <parameterNames class=\"java.util.ArrayList\">\n" +
|
||||
" <string>x</string>\n" +
|
||||
" <string>y</string>\n" +
|
||||
" </parameterNames>\n" +
|
||||
" <system>false</system>\n" +
|
||||
" <description>description</description>\n" +
|
||||
" </function>\n" +
|
||||
" <function>\n" +
|
||||
" <name>z_2</name>\n" +
|
||||
" <body>e^(z_1^2+z_2^2)</body>\n" +
|
||||
" <parameterNames class=\"java.util.ArrayList\">\n" +
|
||||
" <string>z_1</string>\n" +
|
||||
" <string>z_2</string>\n" +
|
||||
" </parameterNames>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" <description></description>\n" +
|
||||
" </function>\n" +
|
||||
" <function>\n" +
|
||||
" <name>z_2</name>\n" +
|
||||
" <body>e^(z_1^2+z_2^2)</body>\n" +
|
||||
" <parameterNames class=\"java.util.ArrayList\">\n" +
|
||||
" <string>z_1</string>\n" +
|
||||
" <string>z_2</string>\n" +
|
||||
" </parameterNames>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" <description></description>\n" +
|
||||
" </function>\n" +
|
||||
" </functions>\n" +
|
||||
"</functions>";
|
||||
|
||||
@Test
|
||||
public void testXml() throws Exception {
|
||||
final Functions in = new Functions();
|
||||
|
||||
AFunction first = new AFunction.Builder("test", "x+y", Arrays.asList("x", "y")).setDescription("description").setSystem(false).create();
|
||||
in.getEntities().add(first);
|
||||
|
||||
AFunction second = new AFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).create();
|
||||
in.getEntities().add(second);
|
||||
|
||||
AFunction third = new AFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).setDescription("").create();
|
||||
in.getEntities().add(third);
|
||||
|
||||
final Functions out = testXml(in, xml);
|
||||
|
||||
Assert.assertTrue(!out.getEntities().isEmpty());
|
||||
|
||||
final AFunction firstOut = out.getEntities().get(0);
|
||||
final AFunction secondOut = out.getEntities().get(1);
|
||||
|
||||
assertEquals(first, firstOut);
|
||||
assertEquals(second, secondOut);
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Functions testXml(@NotNull Functions in, @Nullable String expectedXml) throws Exception {
|
||||
final String actualXml = toXml(in);
|
||||
|
||||
if (expectedXml != null) {
|
||||
Assert.assertEquals(expectedXml, actualXml);
|
||||
}
|
||||
|
||||
final Serializer serializer = new Persister();
|
||||
final Functions out = serializer.read(Functions.class, actualXml);
|
||||
final String actualXml2 = toXml(out);
|
||||
Assert.assertEquals(actualXml, actualXml2);
|
||||
return out;
|
||||
}
|
||||
|
||||
private String toXml(Functions in) throws Exception {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final Serializer serializer = new Persister();
|
||||
serializer.write(in, sw);
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomXml() throws Exception {
|
||||
final Functions in = new Functions();
|
||||
|
||||
final Random random = new Random(new Date().getTime());
|
||||
|
||||
ExpressionGenerator generator = new ExpressionGenerator(10);
|
||||
for ( int i = 0; i < 1000; i++ ) {
|
||||
final String content = generator.generate();
|
||||
|
||||
final String paramsString = StringUtils.generateRandomString(random.nextInt(10));
|
||||
final List<String> parameterNames = new ArrayList<String>();
|
||||
for (int j = 0; j < paramsString.length(); j++) {
|
||||
parameterNames.add(String.valueOf(paramsString.charAt(j)));
|
||||
}
|
||||
|
||||
final AFunction.Builder builder = new AFunction.Builder("test_" + i, content, parameterNames);
|
||||
|
||||
if ( random.nextBoolean() ) {
|
||||
builder.setDescription(StringUtils.generateRandomString(random.nextInt(100)));
|
||||
}
|
||||
|
||||
builder.setSystem(random.nextBoolean());
|
||||
|
||||
in.getEntities().add(builder.create());
|
||||
}
|
||||
|
||||
testXml(in, null);
|
||||
}
|
||||
|
||||
private void assertEquals(@NotNull final AFunction expected,
|
||||
@NotNull AFunction actual) {
|
||||
//Assert.assertEquals(expected.getId(), actual.getId());
|
||||
Assert.assertEquals(expected.getContent(), actual.getContent());
|
||||
Assert.assertEquals(expected.getDescription(), actual.getDescription());
|
||||
Assert.assertEquals(expected.getName(), actual.getName());
|
||||
Assert.assertThat(actual.getParameterNames(), new BaseMatcher<List<String>>() {
|
||||
@Override
|
||||
public boolean matches(Object item) {
|
||||
return EqualsUtils.areEqual(expected.getParameterNames(), (List<String>)item, new CollectionEqualizer<String>(null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -49,7 +49,7 @@
|
||||
<string name="c_remove">Borrar</string>
|
||||
<string name="c_yes">Si</string>
|
||||
<string name="c_no">No</string>
|
||||
<string name="c_var_removal_confirmation">Confirmación de Borrado</string>
|
||||
<string name="removal_confirmation">Confirmación de Borrado</string>
|
||||
<string name="c_var_removal_confirmation_question">¿Realmente desea borrar la variable \'%s\'?</string>
|
||||
<string name="c_var_name">Nombre</string>
|
||||
<string name="c_var_value">Valor</string>
|
||||
|
@ -49,7 +49,7 @@
|
||||
<string name="c_remove">Rimuovi</string>
|
||||
<string name="c_yes">Sì</string>
|
||||
<string name="c_no">No</string>
|
||||
<string name="c_var_removal_confirmation">Conferma rimozione</string>
|
||||
<string name="removal_confirmation">Conferma rimozione</string>
|
||||
<string name="c_var_removal_confirmation_question">Vuoi davvero cancellare la variabile \'%s\'?</string>
|
||||
<string name="c_var_name">Nome</string>
|
||||
<string name="c_var_value">Valore</string>
|
||||
|
@ -54,7 +54,7 @@
|
||||
<string name="c_remove">Удалить</string>
|
||||
<string name="c_yes">Да</string>
|
||||
<string name="c_no">Нет</string>
|
||||
<string name="c_var_removal_confirmation">Подтверждение удаления</string>
|
||||
<string name="removal_confirmation">Подтверждение удаления</string>
|
||||
<string name="c_var_removal_confirmation_question">Вы действительно хотите удалить переменную \'%s\'?</string>
|
||||
<string name="c_var_name">Имя</string>
|
||||
<string name="c_var_value">Значение</string>
|
||||
|
@ -54,7 +54,7 @@
|
||||
<string name="c_remove">Вилучити</string>
|
||||
<string name="c_yes">Так</string>
|
||||
<string name="c_no">Ні</string>
|
||||
<string name="c_var_removal_confirmation">Підтвердження вилучення</string>
|
||||
<string name="removal_confirmation">Підтвердження вилучення</string>
|
||||
<string name="c_var_removal_confirmation_question">Ви дійсно хочете вилучити змінну \'%s\'?</string>
|
||||
<string name="c_var_name">Ім’я</string>
|
||||
<string name="c_var_value">Значення</string>
|
||||
|
@ -54,7 +54,7 @@
|
||||
<string name="c_remove">移除</string>
|
||||
<string name="c_yes">是</string>
|
||||
<string name="c_no">否</string>
|
||||
<string name="c_var_removal_confirmation">確認移除</string>
|
||||
<string name="removal_confirmation">確認移除</string>
|
||||
<string name="c_var_removal_confirmation_question">您真的要移除變數 \'%s\' 嗎?</string>
|
||||
<string name="c_var_name">名稱</string>
|
||||
<string name="c_var_value">值</string>
|
||||
|
@ -54,7 +54,7 @@
|
||||
<string name="c_remove">Remove</string>
|
||||
<string name="c_yes">Yes</string>
|
||||
<string name="c_no">No</string>
|
||||
<string name="c_var_removal_confirmation">Removal confirmation</string>
|
||||
<string name="removal_confirmation">Removal confirmation</string>
|
||||
<string name="c_var_removal_confirmation_question">Do you really want to delete \'%s\' variable?</string>
|
||||
<string name="c_var_name">Name</string>
|
||||
<string name="c_var_value">Value</string>
|
||||
@ -246,5 +246,6 @@
|
||||
<string name="function_name_clashes">Function name clashes with function name!</string>
|
||||
<string name="function_is_empty">Function body could not be empty!</string>
|
||||
<string name="function_param_not_empty">Function parameter should not be empty!</string>
|
||||
<string name="function_removal_confirmation_question">Do you really want to delete \'%s\' function?</string>
|
||||
|
||||
</resources>
|
@ -6,20 +6,19 @@ import android.support.v4.app.FragmentManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.AndroidUtils2;
|
||||
import org.solovyev.android.calculator.CalculatorEventData;
|
||||
import org.solovyev.android.calculator.CalculatorEventListener;
|
||||
import org.solovyev.android.calculator.CalculatorEventType;
|
||||
import org.solovyev.android.calculator.CalculatorLocatorImpl;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.math.edit.MathEntityRemover;
|
||||
import org.solovyev.android.calculator.model.AFunction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/13/12
|
||||
@ -30,10 +29,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
@NotNull
|
||||
private final Input input;
|
||||
|
||||
@NotNull
|
||||
private FunctionParamsView paramsView;
|
||||
|
||||
public FunctionEditDialogFragment() {
|
||||
public FunctionEditDialogFragment() {
|
||||
this(Input.newInstance());
|
||||
}
|
||||
|
||||
@ -50,17 +46,33 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
public void onViewCreated(@NotNull View root, Bundle savedInstanceState) {
|
||||
super.onViewCreated(root, savedInstanceState);
|
||||
|
||||
paramsView = (FunctionParamsView) root.findViewById(R.id.function_params_layout);
|
||||
final FunctionParamsView paramsView = (FunctionParamsView) root.findViewById(R.id.function_params_layout);
|
||||
|
||||
final AFunction.Builder builder;
|
||||
final IFunction function = input.getFunction();
|
||||
if (function != null) {
|
||||
builder = new AFunction.Builder(function);
|
||||
paramsView.init(function.getParameterNames());
|
||||
} else {
|
||||
builder = new AFunction.Builder();
|
||||
paramsView.init();
|
||||
}
|
||||
}
|
||||
|
||||
final List<String> parameterNames = input.getParameterNames();
|
||||
if (parameterNames != null) {
|
||||
paramsView.init(parameterNames);
|
||||
} else {
|
||||
paramsView.init();
|
||||
}
|
||||
|
||||
final EditText editName = (EditText) root.findViewById(R.id.function_edit_name);
|
||||
// show soft keyboard automatically
|
||||
editName.requestFocus();
|
||||
editName.setText(input.getName());
|
||||
|
||||
final EditText editDescription = (EditText) root.findViewById(R.id.function_edit_description);
|
||||
editDescription.setText(input.getDescription());
|
||||
|
||||
final EditText editContent = (EditText) root.findViewById(R.id.function_edit_value);
|
||||
editContent.setText(input.getContent());
|
||||
|
||||
root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@ -81,7 +93,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
getDialog().setTitle(R.string.function_edit_function);
|
||||
|
||||
Function customFunction = new CustomFunction.Builder(function).create();
|
||||
root.findViewById(R.id.remove_button).setOnClickListener(new MathEntityRemover<Function>(customFunction, null, CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry(), getActivity(), this));
|
||||
root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newFunctionRemover(customFunction, null, this.getActivity(), FunctionEditDialogFragment.this));
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,7 +117,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
case function_removed:
|
||||
case function_added:
|
||||
case function_changed:
|
||||
if ( calculatorEventData.getSource() == this ) {
|
||||
if ( calculatorEventData.getSource() == FunctionEditDialogFragment.this ) {
|
||||
dismiss();
|
||||
}
|
||||
break;
|
||||
@ -134,11 +146,14 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
private String name;
|
||||
|
||||
@Nullable
|
||||
private String value;
|
||||
private String content;
|
||||
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Nullable
|
||||
private List<String> parameterNames;
|
||||
|
||||
private Input() {
|
||||
}
|
||||
|
||||
@ -157,16 +172,20 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
@NotNull
|
||||
public static Input newFromValue(@Nullable String value) {
|
||||
final Input result = new Input();
|
||||
result.value = value;
|
||||
result.content = value;
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Input newInstance(@Nullable IFunction function, @Nullable String name, @Nullable String value, @Nullable String description) {
|
||||
public static Input newInstance(@Nullable IFunction function,
|
||||
@Nullable String name,
|
||||
@Nullable String value,
|
||||
@Nullable String description) {
|
||||
|
||||
final Input result = new Input();
|
||||
result.function = function;
|
||||
result.name = name;
|
||||
result.value = value;
|
||||
result.content = value;
|
||||
result.description = description;
|
||||
return result;
|
||||
}
|
||||
@ -182,13 +201,18 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getValue() {
|
||||
return value == null ? (function == null ? null : function.getContent()) : value;
|
||||
public String getContent() {
|
||||
return content == null ? (function == null ? null : function.getContent()) : content;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getDescription() {
|
||||
return description == null ? (function == null ? null : function.getContent()) : description;
|
||||
return description == null ? (function == null ? null : function.getDescription()) : description;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public List<String> getParameterNames() {
|
||||
return parameterNames == null ? (function == null ? null : function.getParameterNames()) : parameterNames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import org.solovyev.android.calculator.CalculatorFunctionsMathRegistry;
|
||||
import org.solovyev.android.calculator.CalculatorLocatorImpl;
|
||||
import org.solovyev.android.calculator.CalculatorMathRegistry;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.android.calculator.math.edit.VarEditorSaver;
|
||||
import org.solovyev.android.calculator.model.AFunction;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
@ -84,23 +83,17 @@ public class FunctionEditorSaver implements View.OnClickListener {
|
||||
|
||||
if (canBeSaved) {
|
||||
if (validateParameters(parameterNames)) {
|
||||
final MathType.Result mathType = MathType.getType(name, 0, false);
|
||||
|
||||
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.function ) {
|
||||
|
||||
if (!StringUtils.isEmpty(content)) {
|
||||
builder.setParameterNames(parameterNames);
|
||||
builder.setName(name);
|
||||
builder.setDescription(description);
|
||||
builder.setValue(content);
|
||||
error = null;
|
||||
} else {
|
||||
error = R.string.function_is_empty;
|
||||
}
|
||||
} else {
|
||||
error = R.string.function_name_clashes;
|
||||
}
|
||||
} else {
|
||||
if (!StringUtils.isEmpty(content)) {
|
||||
builder.setParameterNames(parameterNames);
|
||||
builder.setName(name);
|
||||
builder.setDescription(description);
|
||||
builder.setValue(content);
|
||||
error = null;
|
||||
} else {
|
||||
error = R.string.function_is_empty;
|
||||
}
|
||||
} else {
|
||||
error = R.string.function_param_not_empty;
|
||||
}
|
||||
} else {
|
||||
|
@ -234,7 +234,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
|
||||
remove(R.string.c_remove) {
|
||||
@Override
|
||||
public void onClick(@NotNull IConstant constant, @NotNull Context context) {
|
||||
new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, context).showConfirmationDialog();
|
||||
MathEntityRemover.newConstantRemover(constant, null, context, context).showConfirmationDialog();
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -11,6 +11,8 @@ import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.CalculatorEventType;
|
||||
@ -43,43 +45,69 @@ public class MathEntityRemover<T extends MathEntity> implements View.OnClickList
|
||||
@NotNull
|
||||
private final Object source;
|
||||
|
||||
public MathEntityRemover(@NotNull T mathEntity,
|
||||
@Nullable DialogInterface.OnClickListener callbackOnCancel,
|
||||
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
|
||||
@NotNull Context context,
|
||||
@NotNull Object source) {
|
||||
this(mathEntity, callbackOnCancel, false, varsRegistry, context, source);
|
||||
}
|
||||
@NotNull
|
||||
private final Params params;
|
||||
|
||||
public MathEntityRemover(@NotNull T mathEntity,
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private MathEntityRemover(@NotNull T mathEntity,
|
||||
@Nullable DialogInterface.OnClickListener callbackOnCancel,
|
||||
boolean confirmed,
|
||||
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
|
||||
@NotNull Context context,
|
||||
@NotNull Object source) {
|
||||
@NotNull Object source,
|
||||
@NotNull Params params) {
|
||||
this.mathEntity = mathEntity;
|
||||
this.callbackOnCancel = callbackOnCancel;
|
||||
this.confirmed = confirmed;
|
||||
this.varsRegistry = varsRegistry;
|
||||
this.context = context;
|
||||
this.source = source;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public static MathEntityRemover<IConstant> newConstantRemover(@NotNull IConstant constant,
|
||||
@Nullable DialogInterface.OnClickListener callbackOnCancel,
|
||||
@NotNull Context context,
|
||||
@NotNull Object source) {
|
||||
return new MathEntityRemover<IConstant>(constant, callbackOnCancel, false, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), context, source, Params.newConstantInstance());
|
||||
}
|
||||
|
||||
public static MathEntityRemover<Function> newFunctionRemover(@NotNull Function function,
|
||||
@Nullable DialogInterface.OnClickListener callbackOnCancel,
|
||||
@NotNull Context context,
|
||||
@NotNull Object source) {
|
||||
return new MathEntityRemover<Function>(function, callbackOnCancel, false, CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry(), context, source, Params.newFunctionInstance());
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
public void showConfirmationDialog() {
|
||||
public void showConfirmationDialog() {
|
||||
final TextView question = new TextView(context);
|
||||
question.setText(String.format(context.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName()));
|
||||
question.setPadding(6, 6, 6, 6);
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
|
||||
.setCancelable(true)
|
||||
.setView(question)
|
||||
.setTitle(R.string.c_var_removal_confirmation)
|
||||
.setNegativeButton(R.string.c_no, callbackOnCancel)
|
||||
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, context, source));
|
||||
question.setText(String.format(context.getString(params.getRemovalConfirmationQuestionResId()), mathEntity.getName()));
|
||||
question.setPadding(6, 6, 6, 6);
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
|
||||
.setCancelable(true)
|
||||
.setView(question)
|
||||
.setTitle(params.getRemovalConfirmationTitleResId())
|
||||
.setNegativeButton(R.string.c_no, callbackOnCancel)
|
||||
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, context, source, params));
|
||||
|
||||
builder.create().show();
|
||||
}
|
||||
builder.create().show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(@Nullable View v) {
|
||||
@ -89,7 +117,7 @@ public class MathEntityRemover<T extends MathEntity> implements View.OnClickList
|
||||
varsRegistry.remove(mathEntity);
|
||||
varsRegistry.save();
|
||||
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.constant_removed, mathEntity, source);
|
||||
CalculatorLocatorImpl.getInstance().getCalculator().fireCalculatorEvent(params.getCalculatorEventType(), mathEntity, source);
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,4 +125,52 @@ public class MathEntityRemover<T extends MathEntity> implements View.OnClickList
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
onClick(null);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final class Params {
|
||||
|
||||
private int removalConfirmationTitleResId;
|
||||
|
||||
private int removalConfirmationQuestionResId;
|
||||
|
||||
private CalculatorEventType calculatorEventType;
|
||||
|
||||
private Params() {
|
||||
}
|
||||
|
||||
public int getRemovalConfirmationTitleResId() {
|
||||
return removalConfirmationTitleResId;
|
||||
}
|
||||
|
||||
public int getRemovalConfirmationQuestionResId() {
|
||||
return removalConfirmationQuestionResId;
|
||||
}
|
||||
|
||||
public CalculatorEventType getCalculatorEventType() {
|
||||
return calculatorEventType;
|
||||
}
|
||||
|
||||
private static <T extends MathEntity> Params newConstantInstance() {
|
||||
final Params result = new Params();
|
||||
result.removalConfirmationTitleResId = R.string.removal_confirmation;
|
||||
result.removalConfirmationQuestionResId = R.string.c_var_removal_confirmation_question;
|
||||
result.calculatorEventType = CalculatorEventType.constant_removed;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static <T extends MathEntity> Params newFunctionInstance() {
|
||||
final Params result = new Params();
|
||||
result.removalConfirmationTitleResId = R.string.removal_confirmation;
|
||||
result.removalConfirmationQuestionResId = R.string.function_removal_confirmation_question;
|
||||
result.calculatorEventType = CalculatorEventType.function_removed;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ public class VarEditDialogFragment extends DialogFragment implements CalculatorE
|
||||
// EDIT MODE
|
||||
getDialog().setTitle(R.string.c_var_edit_var);
|
||||
|
||||
root.findViewById(R.id.remove_button).setOnClickListener(new MathEntityRemover<IConstant>(constant, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), getActivity(), this));
|
||||
root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newConstantRemover(constant, null, getActivity(), VarEditDialogFragment.this));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user