Changes
This commit is contained in:
parent
b91c8bc19d
commit
39b45c509e
@ -68,4 +68,7 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl<Cal
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
|
CalculatorEventData fireCalculatorEvent(@NotNull CalculatorEventType calculatorEventType, @Nullable Object data, @NotNull Long sequenceId);
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException;
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ public enum CalculatorEventType {
|
|||||||
// @NotNull Function
|
// @NotNull Function
|
||||||
function_added,
|
function_added,
|
||||||
|
|
||||||
// @NotNull Change<Function>
|
// @NotNull Change<IFunction>
|
||||||
function_changed,
|
function_changed,
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -9,6 +9,7 @@ package org.solovyev.android.calculator;
|
|||||||
import jscl.CustomFunctionCalculationException;
|
import jscl.CustomFunctionCalculationException;
|
||||||
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 org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.calculator.model.AFunction;
|
import org.solovyev.android.calculator.model.AFunction;
|
||||||
@ -16,8 +17,8 @@ import org.solovyev.android.calculator.model.Functions;
|
|||||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
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 org.solovyev.common.text.StringUtils;
|
||||||
|
|
||||||
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;
|
||||||
@ -52,7 +53,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
|||||||
|
|
||||||
public static void saveFunction(@NotNull CalculatorMathRegistry<Function> registry,
|
public static void saveFunction(@NotNull CalculatorMathRegistry<Function> registry,
|
||||||
@NotNull MathEntityBuilder<? extends Function> builder,
|
@NotNull MathEntityBuilder<? extends Function> builder,
|
||||||
@Nullable Function editedInstance,
|
@Nullable IFunction editedInstance,
|
||||||
@NotNull Object source, boolean save) throws CustomFunctionCalculationException {
|
@NotNull Object source, boolean save) throws CustomFunctionCalculationException {
|
||||||
final Function addedFunction = registry.add(builder);
|
final Function addedFunction = registry.add(builder);
|
||||||
|
|
||||||
@ -84,22 +85,34 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public String getDescription(@NotNull String functionName) {
|
||||||
|
final Function function = get(functionName);
|
||||||
|
|
||||||
|
String result = null;
|
||||||
|
if ( function instanceof CustomFunction ) {
|
||||||
|
result = ((CustomFunction) function).getDescription();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.isEmpty(result) ) {
|
||||||
|
result = super.getDescription(functionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction entity) {
|
protected JBuilder<? extends Function> createBuilder(@NotNull AFunction function) {
|
||||||
CustomFunction.Builder builder = new CustomFunction.Builder(entity.getName(), entity.getParameterNames(), entity.getContent());
|
return new CustomFunction.Builder(function);
|
||||||
builder.setDescription(entity.getDescription());
|
|
||||||
return builder;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AFunction transform(@NotNull Function entity) {
|
protected AFunction transform(@NotNull Function function) {
|
||||||
if (entity instanceof CustomFunction) {
|
if (function instanceof CustomFunction) {
|
||||||
final AFunction result = new AFunction();
|
return AFunction.fromIFunction((CustomFunction) function);
|
||||||
result.setName(entity.getName());
|
|
||||||
result.setContent(((CustomFunction) entity).getContent());
|
|
||||||
result.setParameterNames(new ArrayList<String>(((CustomFunction) entity).getParameterNames()));
|
|
||||||
return result;
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
if (StringUtils.isEmpty(expression)) {
|
if (StringUtils.isEmpty(expression)) {
|
||||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
|
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation));
|
||||||
} else {
|
} else {
|
||||||
preparedExpression = preprocessor.process(expression);
|
preparedExpression = prepareExpression(expression);
|
||||||
|
|
||||||
final String jsclExpression = preparedExpression.toString();
|
final String jsclExpression = preparedExpression.toString();
|
||||||
|
|
||||||
@ -204,6 +204,12 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
||||||
|
return preprocessor.process(expression);
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
|
private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation,
|
||||||
@NotNull String expression,
|
@NotNull String expression,
|
||||||
|
@ -13,13 +13,19 @@ import org.simpleframework.xml.Element;
|
|||||||
import org.simpleframework.xml.ElementList;
|
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.CalculatorLocatorImpl;
|
||||||
|
import org.solovyev.android.calculator.CalculatorParseException;
|
||||||
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.msg.Message;
|
||||||
|
import org.solovyev.common.msg.MessageType;
|
||||||
import org.solovyev.common.text.StringUtils;
|
import org.solovyev.common.text.StringUtils;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -28,7 +34,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@Root(name = "function")
|
@Root(name = "function")
|
||||||
public class AFunction implements IFunction, MathPersistenceEntity {
|
public class AFunction implements IFunction, MathPersistenceEntity, Serializable {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
@ -74,6 +80,14 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static AFunction fromIFunction(@NotNull IFunction function) {
|
||||||
|
final AFunction result = new AFunction();
|
||||||
|
|
||||||
|
copy(result, function);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
*
|
*
|
||||||
@ -85,19 +99,24 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
|||||||
@Override
|
@Override
|
||||||
public void copy(@NotNull MathEntity mathEntity) {
|
public void copy(@NotNull MathEntity mathEntity) {
|
||||||
if (mathEntity instanceof IFunction) {
|
if (mathEntity instanceof IFunction) {
|
||||||
final IFunction that = ((IFunction) mathEntity);
|
copy(this, (IFunction) mathEntity);
|
||||||
this.name = that.getName();
|
|
||||||
this.content = that.getContent();
|
|
||||||
this.description = StringUtils.getNotEmpty(this.getDescription(), "");
|
|
||||||
this.system = that.isSystem();
|
|
||||||
if (that.isIdDefined()) {
|
|
||||||
this.id = that.getId();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + mathEntity.getClass());
|
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + mathEntity.getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void copy(@NotNull AFunction target,
|
||||||
|
@NotNull IFunction source) {
|
||||||
|
target.name = source.getName();
|
||||||
|
target.content = source.getContent();
|
||||||
|
target.description = StringUtils.getNotEmpty(source.getDescription(), "");
|
||||||
|
target.system = source.isSystem();
|
||||||
|
if (source.isIdDefined()) {
|
||||||
|
target.id = source.getId();
|
||||||
|
}
|
||||||
|
target.parameterNames = new ArrayList<String>(source.getParameterNames());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toJava() {
|
public String toJava() {
|
||||||
return String.valueOf(this.content);
|
return String.valueOf(this.content);
|
||||||
@ -248,12 +267,57 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result.name = name;
|
result.name = name;
|
||||||
result.content = value;
|
try {
|
||||||
|
result.content = CalculatorLocatorImpl.getInstance().getCalculator().prepareExpression(value).toString();
|
||||||
|
} catch (CalculatorParseException e) {
|
||||||
|
throw new CreationException(e);
|
||||||
|
}
|
||||||
result.system = system;
|
result.system = system;
|
||||||
result.description = StringUtils.getNotEmpty(description, "");
|
result.description = StringUtils.getNotEmpty(description, "");
|
||||||
result.parameterNames = new ArrayList<String>(parameterNames);
|
result.parameterNames = new ArrayList<String>(parameterNames);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class CreationException extends RuntimeException implements Message {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private final CalculatorParseException message;
|
||||||
|
|
||||||
|
public CreationException(@NotNull CalculatorParseException cause) {
|
||||||
|
super(cause);
|
||||||
|
message = cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String getMessageCode() {
|
||||||
|
return message.getMessageCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<Object> getParameters() {
|
||||||
|
return message.getParameters();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public MessageType getMessageType() {
|
||||||
|
return message.getMessageType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public String getLocalizedMessage() {
|
||||||
|
return message.getLocalizedMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String getLocalizedMessage(@NotNull Locale locale) {
|
||||||
|
return message.getLocalizedMessage(locale);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,15 +15,15 @@
|
|||||||
a:id="@+id/function_up_param_button"
|
a:id="@+id/function_up_param_button"
|
||||||
a:layout_height="wrap_content"
|
a:layout_height="wrap_content"
|
||||||
a:layout_width="wrap_content"
|
a:layout_width="wrap_content"
|
||||||
a:text="^" />
|
a:text="↑" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
a:id="@+id/function_down_param_button"
|
a:id="@+id/function_down_param_button"
|
||||||
a:layout_height="wrap_content"
|
a:layout_height="wrap_content"
|
||||||
a:layout_width="wrap_content"
|
a:layout_width="wrap_content"
|
||||||
a:text="v" />
|
a:text="↓" />
|
||||||
|
|
||||||
<EditText
|
<org.solovyev.android.calculator.function.FunctionParamEditText
|
||||||
a:id="@+id/function_param_edit_text"
|
a:id="@+id/function_param_edit_text"
|
||||||
a:layout_height="wrap_content"
|
a:layout_height="wrap_content"
|
||||||
a:layout_width="match_parent"
|
a:layout_width="match_parent"
|
||||||
|
@ -127,6 +127,12 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
|
|||||||
return calculator.fireCalculatorEvent(calculatorEventType, data, sequenceId);
|
return calculator.fireCalculatorEvent(calculatorEventType, data, sequenceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
||||||
|
return calculator.prepareExpression(expression);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
this.calculator.init();
|
this.calculator.init();
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.solovyev.android.calculator.function;
|
package org.solovyev.android.calculator.function;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@ -13,10 +15,15 @@ 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.*;
|
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.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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,8 +33,10 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class FunctionEditDialogFragment extends DialogFragment implements CalculatorEventListener {
|
public class FunctionEditDialogFragment extends DialogFragment implements CalculatorEventListener {
|
||||||
|
|
||||||
|
private static final String INPUT = "input";
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Input input;
|
private Input input;
|
||||||
|
|
||||||
public FunctionEditDialogFragment() {
|
public FunctionEditDialogFragment() {
|
||||||
this(Input.newInstance());
|
this(Input.newInstance());
|
||||||
@ -39,7 +48,16 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
return inflater.inflate(R.layout.function_edit, container, false);
|
final View result = inflater.inflate(R.layout.function_edit, container, false);
|
||||||
|
|
||||||
|
if (savedInstanceState != null) {
|
||||||
|
final Parcelable input = savedInstanceState.getParcelable(INPUT);
|
||||||
|
if ( input instanceof Input ) {
|
||||||
|
this.input = (Input)input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -49,7 +67,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
|||||||
final FunctionParamsView 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 AFunction function = input.getFunction();
|
||||||
if (function != null) {
|
if (function != null) {
|
||||||
builder = new AFunction.Builder(function);
|
builder = new AFunction.Builder(function);
|
||||||
} else {
|
} else {
|
||||||
@ -92,11 +110,18 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
|||||||
// EDIT MODE
|
// EDIT MODE
|
||||||
getDialog().setTitle(R.string.function_edit_function);
|
getDialog().setTitle(R.string.function_edit_function);
|
||||||
|
|
||||||
Function customFunction = new CustomFunction.Builder(function).create();
|
final Function customFunction = new CustomFunction.Builder(function).create();
|
||||||
root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newFunctionRemover(customFunction, null, this.getActivity(), FunctionEditDialogFragment.this));
|
root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newFunctionRemover(customFunction, null, this.getActivity(), FunctionEditDialogFragment.this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(@NotNull Bundle out) {
|
||||||
|
super.onSaveInstanceState(out);
|
||||||
|
|
||||||
|
out.putParcelable(INPUT, FunctionEditorSaver.readInput(input.getFunction(), getView()));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
@ -137,10 +162,50 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
|||||||
AndroidUtils2.showDialog(new FunctionEditDialogFragment(input), "function-editor", fm);
|
AndroidUtils2.showDialog(new FunctionEditDialogFragment(input), "function-editor", fm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Input {
|
public static class Input implements Parcelable {
|
||||||
|
|
||||||
|
public static final Parcelable.Creator<Input> CREATOR = new Creator<Input>() {
|
||||||
|
@Override
|
||||||
|
public Input createFromParcel(@NotNull Parcel in) {
|
||||||
|
return Input.fromParcel(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Input[] newArray(int size) {
|
||||||
|
return new Input[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final Parcelable.Creator<String> STRING_CREATOR = new Creator<String>() {
|
||||||
|
@Override
|
||||||
|
public String createFromParcel(@NotNull Parcel in) {
|
||||||
|
return in.readString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] newArray(int size) {
|
||||||
|
return new String[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static Input fromParcel(@NotNull Parcel in) {
|
||||||
|
final Input result = new Input();
|
||||||
|
result.name = in.readString();
|
||||||
|
result.content = in.readString();
|
||||||
|
result.description = in.readString();
|
||||||
|
|
||||||
|
final List<String> parameterNames = new ArrayList<String>();
|
||||||
|
in.readTypedList(parameterNames, STRING_CREATOR);
|
||||||
|
result.parameterNames = parameterNames;
|
||||||
|
|
||||||
|
result.function = (AFunction) in.readSerializable();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private IFunction function;
|
private AFunction function;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private String name;
|
private String name;
|
||||||
@ -165,14 +230,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static Input newFromFunction(@NotNull IFunction function) {
|
public static Input newFromFunction(@NotNull IFunction function) {
|
||||||
final Input result = new Input();
|
final Input result = new Input();
|
||||||
result.function = function;
|
result.function = AFunction.fromIFunction(function);
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static Input newFromValue(@Nullable String value) {
|
|
||||||
final Input result = new Input();
|
|
||||||
result.content = value;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,18 +238,23 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
|||||||
public static Input newInstance(@Nullable IFunction function,
|
public static Input newInstance(@Nullable IFunction function,
|
||||||
@Nullable String name,
|
@Nullable String name,
|
||||||
@Nullable String value,
|
@Nullable String value,
|
||||||
@Nullable String description) {
|
@Nullable String description,
|
||||||
|
@NotNull List<String> parameterNames) {
|
||||||
|
|
||||||
final Input result = new Input();
|
final Input result = new Input();
|
||||||
result.function = function;
|
if (function != null) {
|
||||||
|
result.function = AFunction.fromIFunction(function);
|
||||||
|
}
|
||||||
result.name = name;
|
result.name = name;
|
||||||
result.content = value;
|
result.content = value;
|
||||||
result.description = description;
|
result.description = description;
|
||||||
|
result.parameterNames = new ArrayList<String>(parameterNames);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public IFunction getFunction() {
|
public AFunction getFunction() {
|
||||||
return function;
|
return function;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,5 +277,19 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul
|
|||||||
public List<String> getParameterNames() {
|
public List<String> getParameterNames() {
|
||||||
return parameterNames == null ? (function == null ? null : function.getParameterNames()) : parameterNames;
|
return parameterNames == null ? (function == null ? null : function.getParameterNames()) : parameterNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(@NotNull Parcel out, int flags) {
|
||||||
|
out.writeString(name);
|
||||||
|
out.writeString(content);
|
||||||
|
out.writeString(description);
|
||||||
|
out.writeList(parameterNames);
|
||||||
|
out.writeSerializable(function);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import org.solovyev.android.calculator.model.MathEntityBuilder;
|
|||||||
import org.solovyev.common.msg.MessageType;
|
import org.solovyev.common.msg.MessageType;
|
||||||
import org.solovyev.common.text.StringUtils;
|
import org.solovyev.common.text.StringUtils;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class FunctionEditorSaver implements View.OnClickListener {
|
public class FunctionEditorSaver implements View.OnClickListener {
|
||||||
@ -29,7 +30,7 @@ public class FunctionEditorSaver implements View.OnClickListener {
|
|||||||
private final AFunction.Builder builder;
|
private final AFunction.Builder builder;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private final Function editedInstance;
|
private final IFunction editedInstance;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final View view;
|
private final View view;
|
||||||
@ -45,11 +46,7 @@ public class FunctionEditorSaver implements View.OnClickListener {
|
|||||||
@NotNull Object source) {
|
@NotNull Object source) {
|
||||||
|
|
||||||
this.builder = builder;
|
this.builder = builder;
|
||||||
if (editedInstance instanceof Function || editedInstance == null) {
|
this.editedInstance = editedInstance;
|
||||||
this.editedInstance = (Function)editedInstance;
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.mathRegistry = registry;
|
this.mathRegistry = registry;
|
||||||
this.source = source;
|
this.source = source;
|
||||||
@ -59,17 +56,16 @@ public class FunctionEditorSaver implements View.OnClickListener {
|
|||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
final Integer error;
|
final Integer error;
|
||||||
|
|
||||||
final EditText editName = (EditText) view.findViewById(R.id.function_edit_name);
|
final FunctionEditDialogFragment.Input input = readInput(null, view);
|
||||||
String name = editName.getText().toString();
|
|
||||||
|
|
||||||
final EditText editValue = (EditText) view.findViewById(R.id.function_edit_value);
|
final String name = input.getName();
|
||||||
String content = editValue.getText().toString();
|
final String content = input.getContent();
|
||||||
|
final String description = input.getDescription();
|
||||||
|
|
||||||
final EditText editDescription = (EditText) view.findViewById(R.id.function_edit_description);
|
List<String> parameterNames = input.getParameterNames();
|
||||||
String description = editDescription.getText().toString();
|
if ( parameterNames == null ) {
|
||||||
|
parameterNames = Collections.emptyList();
|
||||||
final FunctionParamsView editParams = (FunctionParamsView) view.findViewById(R.id.function_params_layout);
|
}
|
||||||
List<String> parameterNames = editParams.getParameterNames();
|
|
||||||
|
|
||||||
if (VarEditorSaver.isValidName(name)) {
|
if (VarEditorSaver.isValidName(name)) {
|
||||||
|
|
||||||
@ -111,10 +107,29 @@ public class FunctionEditorSaver implements View.OnClickListener {
|
|||||||
CalculatorFunctionsMathRegistry.saveFunction(mathRegistry, new BuilderAdapter(builder), editedInstance, source, true);
|
CalculatorFunctionsMathRegistry.saveFunction(mathRegistry, new BuilderAdapter(builder), editedInstance, source, true);
|
||||||
} catch (CustomFunctionCalculationException e) {
|
} catch (CustomFunctionCalculationException e) {
|
||||||
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(e);
|
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(e);
|
||||||
|
} catch (AFunction.Builder.CreationException e) {
|
||||||
|
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static FunctionEditDialogFragment.Input readInput(@Nullable IFunction function, @NotNull View root) {
|
||||||
|
final EditText editName = (EditText) root.findViewById(R.id.function_edit_name);
|
||||||
|
String name = editName.getText().toString();
|
||||||
|
|
||||||
|
final EditText editValue = (EditText) root.findViewById(R.id.function_edit_value);
|
||||||
|
String content = editValue.getText().toString();
|
||||||
|
|
||||||
|
final EditText editDescription = (EditText) root.findViewById(R.id.function_edit_description);
|
||||||
|
String description = editDescription.getText().toString();
|
||||||
|
|
||||||
|
final FunctionParamsView editParams = (FunctionParamsView) root.findViewById(R.id.function_params_layout);
|
||||||
|
List<String> parameterNames = editParams.getParameterNames();
|
||||||
|
|
||||||
|
return FunctionEditDialogFragment.Input.newInstance(function, name, content, description, parameterNames);
|
||||||
|
}
|
||||||
|
|
||||||
private boolean validateParameters(@NotNull List<String> parameterNames) {
|
private boolean validateParameters(@NotNull List<String> parameterNames) {
|
||||||
for (String parameterName : parameterNames) {
|
for (String parameterName : parameterNames) {
|
||||||
if ( !VarEditorSaver.isValidName(parameterName) ) {
|
if ( !VarEditorSaver.isValidName(parameterName) ) {
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
package org.solovyev.android.calculator.function;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.view.AbsSavedState;
|
||||||
|
import android.widget.EditText;
|
||||||
|
|
||||||
|
public class FunctionParamEditText extends EditText {
|
||||||
|
|
||||||
|
public FunctionParamEditText(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionParamEditText(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionParamEditText(Context context, AttributeSet attrs, int defStyle) {
|
||||||
|
super(context, attrs, defStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// we restore state manually outside
|
||||||
|
@Override
|
||||||
|
public Parcelable onSaveInstanceState() {
|
||||||
|
super.onSaveInstanceState();
|
||||||
|
return AbsSavedState.EMPTY_STATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRestoreInstanceState(Parcelable state) {
|
||||||
|
super.onRestoreInstanceState(null);
|
||||||
|
}
|
||||||
|
}
|
@ -294,6 +294,11 @@ public abstract class AbstractMathEntityListFragment<T extends MathEntity> exten
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
protected MathEntityArrayAdapter<T> getAdapter() {
|
||||||
|
return adapter;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected Handler getUiHandler() {
|
protected Handler getUiHandler() {
|
||||||
return uiHandler;
|
return uiHandler;
|
||||||
|
@ -18,7 +18,12 @@ 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.calculator.*;
|
import org.solovyev.android.calculator.CalculatorEventData;
|
||||||
|
import org.solovyev.android.calculator.CalculatorEventType;
|
||||||
|
import org.solovyev.android.calculator.CalculatorLocatorImpl;
|
||||||
|
import org.solovyev.android.calculator.CalculatorMathRegistry;
|
||||||
|
import org.solovyev.android.calculator.Change;
|
||||||
|
import org.solovyev.android.calculator.R;
|
||||||
import org.solovyev.android.calculator.about.CalculatorFragmentType;
|
import org.solovyev.android.calculator.about.CalculatorFragmentType;
|
||||||
import org.solovyev.android.calculator.function.FunctionEditDialogFragment;
|
import org.solovyev.android.calculator.function.FunctionEditDialogFragment;
|
||||||
import org.solovyev.android.menu.AMenuItem;
|
import org.solovyev.android.menu.AMenuItem;
|
||||||
@ -126,7 +131,7 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case function_changed:
|
case function_changed:
|
||||||
processFunctionChanged((Change<Function>) data);
|
processFunctionChanged((Change<IFunction>) data);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case function_removed:
|
case function_removed:
|
||||||
@ -148,18 +153,39 @@ public class CalculatorFunctionsFragment extends AbstractMathEntityListFragment<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processFunctionChanged(@NotNull final Change<Function> change) {
|
private void processFunctionChanged(@NotNull final Change<IFunction> change) {
|
||||||
final Function newFunction = change.getNewValue();
|
final IFunction newFunction = change.getNewValue();
|
||||||
if (this.isInCategory(newFunction)) {
|
|
||||||
|
if (newFunction instanceof Function) {
|
||||||
|
|
||||||
|
if (this.isInCategory((Function)newFunction)) {
|
||||||
|
|
||||||
getUiHandler().post(new Runnable() {
|
getUiHandler().post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
removeFromAdapter(change.getOldValue());
|
IFunction oldValue = change.getOldValue();
|
||||||
addToAdapter(newFunction);
|
|
||||||
|
if (oldValue.isIdDefined()) {
|
||||||
|
final MathEntityArrayAdapter<Function> adapter = getAdapter();
|
||||||
|
if ( adapter != null ) {
|
||||||
|
for (int i = 0; i < adapter.getCount(); i++) {
|
||||||
|
final Function functionFromAdapter = adapter.getItem(i);
|
||||||
|
if ( functionFromAdapter.isIdDefined() && oldValue.getId().equals(functionFromAdapter.getId()) ) {
|
||||||
|
adapter.remove(functionFromAdapter);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addToAdapter((Function)newFunction);
|
||||||
sort();
|
sort();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Function must be instance of jscl.math.function.Function class!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processFunctionAdded(@NotNull final Function function) {
|
private void processFunctionAdded(@NotNull final Function function) {
|
||||||
|
Loading…
Reference in New Issue
Block a user