From 39b45c509e33dc40fac371b7b790c64344e2c786 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Thu, 15 Nov 2012 17:41:13 +0400 Subject: [PATCH] Changes --- .../android/calculator/Calculator.java | 3 + .../calculator/CalculatorEventType.java | 2 +- .../CalculatorFunctionsMathRegistry.java | 39 ++++-- .../android/calculator/CalculatorImpl.java | 12 +- .../android/calculator/model/AFunction.java | 84 ++++++++++-- .../res/layout/function_edit_param.xml | 6 +- .../android/calculator/AndroidCalculator.java | 8 +- .../function/FunctionEditDialogFragment.java | 123 ++++++++++++++---- .../function/FunctionEditorSaver.java | 47 ++++--- .../function/FunctionParamEditText.java | 34 +++++ .../edit/AbstractMathEntityListFragment.java | 7 +- .../edit/CalculatorFunctionsFragment.java | 56 +++++--- 12 files changed, 335 insertions(+), 86 deletions(-) create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/function/FunctionParamEditText.java diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/Calculator.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/Calculator.java index 907354a8..c560daea 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/Calculator.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/Calculator.java @@ -68,4 +68,7 @@ public interface Calculator extends CalculatorEventContainer, HistoryControl + // @NotNull Change function_changed, /* diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java index 06ff82b0..e97be40e 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java @@ -9,6 +9,7 @@ package org.solovyev.android.calculator; import jscl.CustomFunctionCalculationException; 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.calculator.model.AFunction; @@ -16,8 +17,8 @@ import org.solovyev.android.calculator.model.Functions; import org.solovyev.android.calculator.model.MathEntityBuilder; import org.solovyev.common.JBuilder; import org.solovyev.common.math.MathRegistry; +import org.solovyev.common.text.StringUtils; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -52,7 +53,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis public static void saveFunction(@NotNull CalculatorMathRegistry registry, @NotNull MathEntityBuilder builder, - @Nullable Function editedInstance, + @Nullable IFunction editedInstance, @NotNull Object source, boolean save) throws CustomFunctionCalculationException { final Function addedFunction = registry.add(builder); @@ -84,22 +85,34 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis 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 @Override - protected JBuilder createBuilder(@NotNull AFunction entity) { - CustomFunction.Builder builder = new CustomFunction.Builder(entity.getName(), entity.getParameterNames(), entity.getContent()); - builder.setDescription(entity.getDescription()); - return builder; + protected JBuilder createBuilder(@NotNull AFunction function) { + return new CustomFunction.Builder(function); } @Override - protected AFunction transform(@NotNull Function entity) { - if (entity instanceof CustomFunction) { - final AFunction result = new AFunction(); - result.setName(entity.getName()); - result.setContent(((CustomFunction) entity).getContent()); - result.setParameterNames(new ArrayList(((CustomFunction) entity).getParameterNames())); - return result; + protected AFunction transform(@NotNull Function function) { + if (function instanceof CustomFunction) { + return AFunction.fromIFunction((CustomFunction) function); } else { return null; } diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java index 79325e0a..2ef7e607 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java @@ -169,9 +169,9 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { if (StringUtils.isEmpty(expression)) { fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, CalculatorOutputImpl.newEmptyOutput(operation)); } else { - preparedExpression = preprocessor.process(expression); + preparedExpression = prepareExpression(expression); - final String jsclExpression = preparedExpression.toString(); + final String jsclExpression = preparedExpression.toString(); try { @@ -204,7 +204,13 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } } - @NotNull + @NotNull + @Override + public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException { + return preprocessor.process(expression); + } + + @NotNull private CalculatorEventData newCalculationEventData(@NotNull JsclOperation operation, @NotNull String expression, @NotNull Long calculationId) { diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/AFunction.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/AFunction.java index b23b747f..1e2c8a4d 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/AFunction.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/model/AFunction.java @@ -13,13 +13,19 @@ 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.CalculatorLocatorImpl; +import org.solovyev.android.calculator.CalculatorParseException; import org.solovyev.android.calculator.MathPersistenceEntity; 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 java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Locale; /** * User: serso @@ -28,7 +34,7 @@ import java.util.List; */ @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; } + 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 public void copy(@NotNull MathEntity mathEntity) { if (mathEntity instanceof IFunction) { - final IFunction that = ((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(); - } + copy(this, (IFunction) mathEntity); } else { 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(source.getParameterNames()); + } + @Override public String toJava() { return String.valueOf(this.content); @@ -248,12 +267,57 @@ public class AFunction implements IFunction, MathPersistenceEntity { } 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.description = StringUtils.getNotEmpty(description, ""); result.parameterNames = new ArrayList(parameterNames); 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 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); + } + } } } diff --git a/calculatorpp/res/layout/function_edit_param.xml b/calculatorpp/res/layout/function_edit_param.xml index d1469510..02a7926c 100644 --- a/calculatorpp/res/layout/function_edit_param.xml +++ b/calculatorpp/res/layout/function_edit_param.xml @@ -15,15 +15,15 @@ a:id="@+id/function_up_param_button" a:layout_height="wrap_content" a:layout_width="wrap_content" - a:text="^" /> + a:text="↑" />