functions

This commit is contained in:
Sergey Solovyev 2012-11-14 21:44:23 +04:00
parent 357eed9f3e
commit 75a3a72a84
15 changed files with 342 additions and 110 deletions

View File

@ -16,6 +16,7 @@ import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -96,7 +97,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
final AFunction result = new AFunction(); final AFunction result = new AFunction();
result.setName(entity.getName()); result.setName(entity.getName());
result.setContent(((CustomFunction) entity).getContent()); result.setContent(((CustomFunction) entity).getContent());
result.setParameterNames(((CustomFunction) entity).getParameterNames()); result.setParameterNames(new ArrayList<String>(((CustomFunction) entity).getParameterNames()));
return result; return result;
} else { } else {
return null; return null;

View File

@ -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); i = mathTypeResult.processToJscl(result, i);
} }
return result; return result;

View File

@ -6,16 +6,18 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import jscl.math.function.IConstant;
import jscl.math.function.IFunction; import jscl.math.function.IFunction;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Element; import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root; import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient; import org.simpleframework.xml.Transient;
import org.solovyev.android.calculator.MathPersistenceEntity; import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.text.StringUtils;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -25,7 +27,7 @@ import java.util.List;
* Time: 5:25 PM * Time: 5:25 PM
*/ */
@Root @Root(name = "function")
public class AFunction implements IFunction, MathPersistenceEntity { public class AFunction implements IFunction, MathPersistenceEntity {
/* /*
@ -42,20 +44,20 @@ public class AFunction implements IFunction, MathPersistenceEntity {
@NotNull @NotNull
private String name; private String name;
@Element @Element(name = "body")
@NotNull @NotNull
private String content; private String content;
@Element(required = false) @ElementList
@NotNull @NotNull
private List<String> parameterNames = Collections.emptyList(); private List<String> parameterNames = new ArrayList<String>();
@Element @Element
private boolean system; private boolean system;
@Element(required = false) @Element(required = false)
@Nullable @NotNull
private String description; private String description = "";
/* /*
********************************************************************** **********************************************************************
@ -86,7 +88,7 @@ public class AFunction implements IFunction, MathPersistenceEntity {
final IFunction that = ((IFunction) mathEntity); final IFunction that = ((IFunction) mathEntity);
this.name = that.getName(); this.name = that.getName();
this.content = that.getContent(); this.content = that.getContent();
this.description = this.getContent(); this.description = StringUtils.getNotEmpty(this.getDescription(), "");
this.system = that.isSystem(); this.system = that.isSystem();
if (that.isIdDefined()) { if (that.isIdDefined()) {
this.id = that.getId(); this.id = that.getId();
@ -104,7 +106,7 @@ public class AFunction implements IFunction, MathPersistenceEntity {
/* /*
********************************************************************** **********************************************************************
* *
* GETTERS/SEETTERS * GETTERS/SETTERS
* *
********************************************************************** **********************************************************************
*/ */
@ -144,7 +146,7 @@ public class AFunction implements IFunction, MathPersistenceEntity {
return content; return content;
} }
@Nullable @NotNull
@Override @Override
public String getDescription() { public String getDescription() {
return this.description; return this.description;
@ -201,28 +203,14 @@ public class AFunction implements IFunction, MathPersistenceEntity {
this.id = function.getId(); this.id = function.getId();
} }
public Builder(@NotNull IConstant function) { public Builder(@NotNull String name,
this.name = function.getName(); @NotNull String value,
@NotNull List<String> 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.name = name;
this.value = value; this.value = value;
this.parameterNames = parameterNames;
} }
@NotNull @NotNull
public Builder setName(@NotNull String name) { public Builder setName(@NotNull String name) {
this.name = name; this.name = name;
@ -262,8 +250,8 @@ public class AFunction implements IFunction, MathPersistenceEntity {
result.name = name; result.name = name;
result.content = value; result.content = value;
result.system = system; result.system = system;
result.description = description; result.description = StringUtils.getNotEmpty(description, "");
result.parameterNames = parameterNames; result.parameterNames = new ArrayList<String>(parameterNames);
return result; return result;
} }

View File

@ -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) {
}
});
}
}

View File

@ -49,7 +49,7 @@
<string name="c_remove">Borrar</string> <string name="c_remove">Borrar</string>
<string name="c_yes">Si</string> <string name="c_yes">Si</string>
<string name="c_no">No</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_removal_confirmation_question">¿Realmente desea borrar la variable \'%s\'?</string>
<string name="c_var_name">Nombre</string> <string name="c_var_name">Nombre</string>
<string name="c_var_value">Valor</string> <string name="c_var_value">Valor</string>

View File

@ -49,7 +49,7 @@
<string name="c_remove">Rimuovi</string> <string name="c_remove">Rimuovi</string>
<string name="c_yes"></string> <string name="c_yes"></string>
<string name="c_no">No</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_removal_confirmation_question">Vuoi davvero cancellare la variabile \'%s\'?</string>
<string name="c_var_name">Nome</string> <string name="c_var_name">Nome</string>
<string name="c_var_value">Valore</string> <string name="c_var_value">Valore</string>

View File

@ -54,7 +54,7 @@
<string name="c_remove">Удалить</string> <string name="c_remove">Удалить</string>
<string name="c_yes">Да</string> <string name="c_yes">Да</string>
<string name="c_no">Нет</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_removal_confirmation_question">Вы действительно хотите удалить переменную \'%s\'?</string>
<string name="c_var_name">Имя</string> <string name="c_var_name">Имя</string>
<string name="c_var_value">Значение</string> <string name="c_var_value">Значение</string>

View File

@ -54,7 +54,7 @@
<string name="c_remove">Вилучити</string> <string name="c_remove">Вилучити</string>
<string name="c_yes">Так</string> <string name="c_yes">Так</string>
<string name="c_no">Ні</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_removal_confirmation_question">Ви дійсно хочете вилучити змінну \'%s\'?</string>
<string name="c_var_name">Ім’я</string> <string name="c_var_name">Ім’я</string>
<string name="c_var_value">Значення</string> <string name="c_var_value">Значення</string>

View File

@ -54,7 +54,7 @@
<string name="c_remove">移除</string> <string name="c_remove">移除</string>
<string name="c_yes"></string> <string name="c_yes"></string>
<string name="c_no"></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_removal_confirmation_question">您真的要移除變數 \'%s\' 嗎?</string>
<string name="c_var_name">名稱</string> <string name="c_var_name">名稱</string>
<string name="c_var_value"></string> <string name="c_var_value"></string>

View File

@ -54,7 +54,7 @@
<string name="c_remove">Remove</string> <string name="c_remove">Remove</string>
<string name="c_yes">Yes</string> <string name="c_yes">Yes</string>
<string name="c_no">No</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_removal_confirmation_question">Do you really want to delete \'%s\' variable?</string>
<string name="c_var_name">Name</string> <string name="c_var_name">Name</string>
<string name="c_var_value">Value</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_name_clashes">Function name clashes with function name!</string>
<string name="function_is_empty">Function body could not be empty!</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_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> </resources>

View File

@ -6,20 +6,19 @@ import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.EditText;
import jscl.math.function.CustomFunction; import jscl.math.function.CustomFunction;
import jscl.math.function.Function; import jscl.math.function.Function;
import jscl.math.function.IFunction; import jscl.math.function.IFunction;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils2; import org.solovyev.android.AndroidUtils2;
import org.solovyev.android.calculator.CalculatorEventData; import org.solovyev.android.calculator.*;
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.math.edit.MathEntityRemover; import org.solovyev.android.calculator.math.edit.MathEntityRemover;
import org.solovyev.android.calculator.model.AFunction; import org.solovyev.android.calculator.model.AFunction;
import java.util.List;
/** /**
* User: serso * User: serso
* Date: 11/13/12 * Date: 11/13/12
@ -30,9 +29,6 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
@NotNull @NotNull
private final Input input; private final Input input;
@NotNull
private FunctionParamsView paramsView;
public FunctionEditDialogFragment() { public FunctionEditDialogFragment() {
this(Input.newInstance()); this(Input.newInstance());
} }
@ -50,18 +46,34 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
public void onViewCreated(@NotNull View root, Bundle savedInstanceState) { public void onViewCreated(@NotNull View root, Bundle savedInstanceState) {
super.onViewCreated(root, 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 AFunction.Builder builder;
final IFunction function = input.getFunction(); final IFunction function = input.getFunction();
if (function != null) { if (function != null) {
builder = new AFunction.Builder(function); builder = new AFunction.Builder(function);
paramsView.init(function.getParameterNames());
} else { } else {
builder = new AFunction.Builder(); builder = new AFunction.Builder();
}
final List<String> parameterNames = input.getParameterNames();
if (parameterNames != null) {
paramsView.init(parameterNames);
} else {
paramsView.init(); 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() { root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -81,7 +93,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
getDialog().setTitle(R.string.function_edit_function); getDialog().setTitle(R.string.function_edit_function);
Function customFunction = new CustomFunction.Builder(function).create(); 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_removed:
case function_added: case function_added:
case function_changed: case function_changed:
if ( calculatorEventData.getSource() == this ) { if ( calculatorEventData.getSource() == FunctionEditDialogFragment.this ) {
dismiss(); dismiss();
} }
break; break;
@ -134,11 +146,14 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
private String name; private String name;
@Nullable @Nullable
private String value; private String content;
@Nullable @Nullable
private String description; private String description;
@Nullable
private List<String> parameterNames;
private Input() { private Input() {
} }
@ -157,16 +172,20 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
@NotNull @NotNull
public static Input newFromValue(@Nullable String value) { public static Input newFromValue(@Nullable String value) {
final Input result = new Input(); final Input result = new Input();
result.value = value; result.content = value;
return result; return result;
} }
@NotNull @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(); final Input result = new Input();
result.function = function; result.function = function;
result.name = name; result.name = name;
result.value = value; result.content = value;
result.description = description; result.description = description;
return result; return result;
} }
@ -182,13 +201,18 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
} }
@Nullable @Nullable
public String getValue() { public String getContent() {
return value == null ? (function == null ? null : function.getContent()) : value; return content == null ? (function == null ? null : function.getContent()) : content;
} }
@Nullable @Nullable
public String getDescription() { 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;
} }
} }
} }

View File

@ -11,7 +11,6 @@ import org.solovyev.android.calculator.CalculatorFunctionsMathRegistry;
import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.CalculatorMathRegistry; import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.R; 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.math.edit.VarEditorSaver;
import org.solovyev.android.calculator.model.AFunction; import org.solovyev.android.calculator.model.AFunction;
import org.solovyev.android.calculator.model.MathEntityBuilder; import org.solovyev.android.calculator.model.MathEntityBuilder;
@ -84,9 +83,6 @@ public class FunctionEditorSaver implements View.OnClickListener {
if (canBeSaved) { if (canBeSaved) {
if (validateParameters(parameterNames)) { 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)) { if (!StringUtils.isEmpty(content)) {
builder.setParameterNames(parameterNames); builder.setParameterNames(parameterNames);
@ -97,9 +93,6 @@ public class FunctionEditorSaver implements View.OnClickListener {
} else { } else {
error = R.string.function_is_empty; error = R.string.function_is_empty;
} }
} else {
error = R.string.function_name_clashes;
}
} else { } else {
error = R.string.function_param_not_empty; error = R.string.function_param_not_empty;
} }

View File

@ -234,7 +234,7 @@ public class CalculatorVarsFragment extends AbstractMathEntityListFragment<ICons
remove(R.string.c_remove) { remove(R.string.c_remove) {
@Override @Override
public void onClick(@NotNull IConstant constant, @NotNull Context context) { 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();
} }
}, },

View File

@ -11,6 +11,8 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import jscl.math.function.Function;
import jscl.math.function.IConstant;
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.CalculatorEventType; import org.solovyev.android.calculator.CalculatorEventType;
@ -43,40 +45,66 @@ public class MathEntityRemover<T extends MathEntity> implements View.OnClickList
@NotNull @NotNull
private final Object source; private final Object source;
public MathEntityRemover(@NotNull T mathEntity, @NotNull
@Nullable DialogInterface.OnClickListener callbackOnCancel, private final Params params;
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull Context context,
@NotNull Object source) {
this(mathEntity, callbackOnCancel, false, varsRegistry, context, source);
}
public MathEntityRemover(@NotNull T mathEntity, /*
**********************************************************************
*
* CONSTRUCTORS
*
**********************************************************************
*/
private MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel, @Nullable DialogInterface.OnClickListener callbackOnCancel,
boolean confirmed, boolean confirmed,
@NotNull CalculatorMathRegistry<? super T> varsRegistry, @NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull Context context, @NotNull Context context,
@NotNull Object source) { @NotNull Object source,
@NotNull Params params) {
this.mathEntity = mathEntity; this.mathEntity = mathEntity;
this.callbackOnCancel = callbackOnCancel; this.callbackOnCancel = callbackOnCancel;
this.confirmed = confirmed; this.confirmed = confirmed;
this.varsRegistry = varsRegistry; this.varsRegistry = varsRegistry;
this.context = context; this.context = context;
this.source = source; 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); final TextView question = new TextView(context);
question.setText(String.format(context.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName())); question.setText(String.format(context.getString(params.getRemovalConfirmationQuestionResId()), mathEntity.getName()));
question.setPadding(6, 6, 6, 6); question.setPadding(6, 6, 6, 6);
final AlertDialog.Builder builder = new AlertDialog.Builder(context) final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCancelable(true) .setCancelable(true)
.setView(question) .setView(question)
.setTitle(R.string.c_var_removal_confirmation) .setTitle(params.getRemovalConfirmationTitleResId())
.setNegativeButton(R.string.c_no, callbackOnCancel) .setNegativeButton(R.string.c_no, callbackOnCancel)
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, context, source)); .setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, context, source, params));
builder.create().show(); builder.create().show();
} }
@ -89,7 +117,7 @@ public class MathEntityRemover<T extends MathEntity> implements View.OnClickList
varsRegistry.remove(mathEntity); varsRegistry.remove(mathEntity);
varsRegistry.save(); 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) { public void onClick(DialogInterface dialog, int which) {
onClick(null); 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;
}
}
} }

View File

@ -121,7 +121,7 @@ public class VarEditDialogFragment extends DialogFragment implements CalculatorE
// EDIT MODE // EDIT MODE
getDialog().setTitle(R.string.c_var_edit_var); 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));
} }
} }