From 1ab714b04481c0dfca035fc6e8f5e32b804046a6 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sat, 17 Nov 2012 21:58:48 +0400 Subject: [PATCH] Calculation fixable messages --- .../calculator/CalculatorEventType.java | 4 + .../calculator/CalculatorFixableError.java | 43 ++++ .../android/calculator/CalculatorImpl.java | 14 +- .../android/calculator/CalculatorLocator.java | 6 +- .../calculator/CalculatorLocatorImpl.java | 13 +- .../CalculatorPreferenceService.java | 14 ++ .../calculator/AbstractCalculatorTest.java | 2 +- .../calculator/CalculatorTestUtils.java | 2 +- calculatorpp/AndroidManifest.xml | 2 + .../layout/calculation_messages_dialog.xml | 34 +++ .../calculation_messages_dialog_message.xml | 20 ++ calculatorpp/res/values/attributes.xml | 1 + calculatorpp/res/values/text_strings.xml | 9 + calculatorpp/res/values/theme_default.xml | 6 + .../res/xml/calculations_preferences.xml | 5 + .../org/solovyev/android/AndroidUtils2.java | 26 --- .../calculator/AbstractCalculatorHelper.java | 2 +- .../android/calculator/AndroidCalculator.java | 4 + .../calculator/AndroidCalculatorNotifier.java | 6 +- .../AndroidCalculatorPreferenceService.java | 33 +++ .../calculator/CalculationMessage.java | 87 ++++++++ .../CalculatorActivityLauncher.java | 13 ++ .../calculator/CalculatorApplication.java | 3 +- .../android/calculator/CalculatorButtons.java | 10 +- .../CalculatorKeyboardFragment.java | 9 + .../calculator/CalculatorMessagesDialog.java | 201 ++++++++++++++++++ .../calculator/CalculatorPreferences.java | 22 +- .../function/FunctionEditDialogFragment.java | 4 +- .../math/edit/VarEditDialogFragment.java | 4 +- .../plot/CalculatorPlotFragment.java | 16 +- .../android/calculator/plot/PlotUtils.java | 17 +- .../calculator/CalculatorTestUtils.java | 2 +- pom.xml | 2 +- 33 files changed, 571 insertions(+), 65 deletions(-) create mode 100644 calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java create mode 100644 calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java create mode 100644 calculatorpp/res/layout/calculation_messages_dialog.xml create mode 100644 calculatorpp/res/layout/calculation_messages_dialog_message.xml create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidCalculatorPreferenceService.java create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/CalculationMessage.java create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMessagesDialog.java diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java index d12b7c85..8ab0f3ad 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java @@ -132,6 +132,10 @@ public enum CalculatorEventType { * ********************************************************************** */ + + // List + calculation_messages, + show_history, show_history_detached, diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java new file mode 100644 index 00000000..6ba533d6 --- /dev/null +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java @@ -0,0 +1,43 @@ +package org.solovyev.android.calculator; + +import jscl.AngleUnit; +import jscl.text.msg.Messages; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.collections.CollectionsUtils; + +import java.util.List; + +/** + * User: serso + * Date: 11/17/12 + * Time: 7:30 PM + */ +public enum CalculatorFixableError { + + must_be_rad(Messages.msg_23, Messages.msg_24, Messages.msg_25) { + @Override + public void fix() { + CalculatorLocatorImpl.getInstance().getPreferenceService().setAngleUnits(AngleUnit.rad); + } + }; + + @NotNull + private final List messageCodes; + + CalculatorFixableError(@Nullable String... messageCodes) { + this.messageCodes = CollectionsUtils.asList(messageCodes); + } + + @Nullable + public static CalculatorFixableError getErrorByMessageCode(@NotNull String messageCode) { + for (CalculatorFixableError fixableError : values()) { + if (fixableError.messageCodes.contains(messageCode)) { + return fixableError; + } + } + return null; + } + + public abstract void fix(); +} 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 d37e77a0..3f1a6c2a 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 @@ -25,6 +25,7 @@ import org.solovyev.common.text.StringUtils; import org.solovyev.math.units.ConversionException; import org.solovyev.math.units.ConversionUtils; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -189,10 +190,14 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger(); try { + final List messages = new ArrayList(); while (messageRegistry.hasMessage()) { - handleJsclMessage(messageRegistry.getMessage()); + messages.add(messageRegistry.getMessage()); } - } catch (Throwable e) { + if (!messages.isEmpty()) { + fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages); + } + } catch (Throwable e) { // todo serso: not good be we need proper synchronization logger.error("Calculator", e.getMessage(), e); } @@ -221,11 +226,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener { } } - private void handleJsclMessage(@NotNull Message message) { - final CalculatorNotifier notifier = CalculatorLocatorImpl.getInstance().getNotifier(); - notifier.showMessage(message); - } - @NotNull @Override public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException { diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java index 04a321a6..0d911403 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocator.java @@ -15,7 +15,8 @@ public interface CalculatorLocator { @NotNull CalculatorClipboard clipboard, @NotNull CalculatorNotifier notifier, @NotNull CalculatorHistory history, - @NotNull CalculatorLogger logger); + @NotNull CalculatorLogger logger, + @NotNull CalculatorPreferenceService preferenceService); @NotNull Calculator getCalculator(); @@ -43,4 +44,7 @@ public interface CalculatorLocator { @NotNull CalculatorLogger getLogger(); + + @NotNull + CalculatorPreferenceService getPreferenceService(); } diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java index d1ab0604..68d519ab 100644 --- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorLocatorImpl.java @@ -40,6 +40,9 @@ public class CalculatorLocatorImpl implements CalculatorLocator { @NotNull private static final CalculatorLocator instance = new CalculatorLocatorImpl(); + @NotNull + private CalculatorPreferenceService calculatorPreferenceService; + public CalculatorLocatorImpl() { } @@ -49,7 +52,8 @@ public class CalculatorLocatorImpl implements CalculatorLocator { @NotNull CalculatorClipboard clipboard, @NotNull CalculatorNotifier notifier, @NotNull CalculatorHistory history, - @NotNull CalculatorLogger logger) { + @NotNull CalculatorLogger logger, + @NotNull CalculatorPreferenceService preferenceService) { this.calculator = calculator; this.calculatorEngine = engine; @@ -57,6 +61,7 @@ public class CalculatorLocatorImpl implements CalculatorLocator { this.calculatorNotifier = notifier; this.calculatorHistory = history; this.calculatorLogger = logger; + this.calculatorPreferenceService = preferenceService; calculatorEditor = new CalculatorEditorImpl(this.calculator); calculatorDisplay = new CalculatorDisplayImpl(this.calculator); @@ -121,4 +126,10 @@ public class CalculatorLocatorImpl implements CalculatorLocator { public CalculatorLogger getLogger() { return calculatorLogger; } + + @NotNull + @Override + public CalculatorPreferenceService getPreferenceService() { + return this.calculatorPreferenceService; + } } diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java new file mode 100644 index 00000000..ca6a5d70 --- /dev/null +++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java @@ -0,0 +1,14 @@ +package org.solovyev.android.calculator; + +import jscl.AngleUnit; +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 11/17/12 + * Time: 7:45 PM + */ +public interface CalculatorPreferenceService { + + void setAngleUnits(@NotNull AngleUnit angleUnit); +} diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java index 8b4156fb..80547dbe 100644 --- a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java @@ -11,7 +11,7 @@ import org.solovyev.android.calculator.history.CalculatorHistory; public class AbstractCalculatorTest { protected void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger()); + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class)); CalculatorLocatorImpl.getInstance().getEngine().init(); } diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java index e881f69e..88a4e987 100644 --- a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -23,7 +23,7 @@ public class CalculatorTestUtils { public static final int TIMEOUT = 1; public static void staticSetUp() throws Exception { - CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger()); + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class)); CalculatorLocatorImpl.getInstance().getEngine().init(); } diff --git a/calculatorpp/AndroidManifest.xml b/calculatorpp/AndroidManifest.xml index b2625377..e5d5dbe8 100644 --- a/calculatorpp/AndroidManifest.xml +++ b/calculatorpp/AndroidManifest.xml @@ -25,6 +25,8 @@ + + diff --git a/calculatorpp/res/layout/calculation_messages_dialog.xml b/calculatorpp/res/layout/calculation_messages_dialog.xml new file mode 100644 index 00000000..eb4be7a7 --- /dev/null +++ b/calculatorpp/res/layout/calculation_messages_dialog.xml @@ -0,0 +1,34 @@ + + + + + + + + + + +