functions
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user