From 0dc95a3279846a02fde461bdf8b76d38677594a8 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 26 Nov 2011 13:18:51 +0400 Subject: [PATCH] messages --- res/values-ru/strings.xml | 7 ++ res/values/strings.xml | 27 ++----- .../calculator/CalculatorActivity.java | 9 --- .../calculator/model/ParseException.java | 73 ++++++++----------- .../solovyev/android/msg/AndroidMessage.java | 21 ++++-- .../android/msg/AndroidMessageRegistry.java | 66 ----------------- .../android/view/prefs/ResourceCache.java | 7 +- .../java/org/solovyev/util/math/Complex.java | 40 ---------- .../model/CalculatorEngineTest.java | 4 +- 9 files changed, 67 insertions(+), 187 deletions(-) delete mode 100644 src/main/java/org/solovyev/android/msg/AndroidMessageRegistry.java delete mode 100644 src/main/java/org/solovyev/util/math/Complex.java diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index f9127a83..5599619c 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -266,4 +266,11 @@ e ^ i = 0.5403 + 0.84147i\n 6. Экран настроек: содержит список настроек приложения. На экран можно попасть нажав кнопку Настройки в меню. + Арифметическая ошибка: {0} + Слишком сложное выражение + Вычисление выражения занимает слишком много времени - проверьте выражение + Вычисление было отменено + Для функции {0} не определены параметры + В выражении найден Бесконечный цикл - проверьте выражение + diff --git a/res/values/strings.xml b/res/values/strings.xml index b5c123d4..4385074d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -269,26 +269,11 @@ Check the \'Round result\' preference in application settings - it should be tur - Arithmetic error occurred: {0} - Too complex expression - Too long execution time - check the expression - Evaluation was cancelled - No parameters are specified for function: {0} - Infinite loop is detected in expression - - Premature end of processing - There is no operator with name: {0} - Operator name is not valid: {0} - Postfix function with name {0} doesn\'\'t exist - Constant name must start with character - Cannot be implicit function - usual function or operator with same name is defined: {0} - Digit is expected - Invalid number: {0} - First letter of number must be digit - Expected characters are {0} or {1} - Expected characters are {0} - Expected character is {0} - Function name is not valid: {0} - Expected number of parameters differs from actual {0} + Arithmetic error occurred: {0} + Too complex expression + Too long execution time - check the expression + Evaluation was cancelled + No parameters are specified for function: {0} + Infinite loop is detected in expression diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 22f10133..536f969a 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -19,7 +19,6 @@ import android.text.method.LinkMovementMethod; import android.util.Log; import android.util.TypedValue; import android.view.*; -import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import jscl.AngleUnit; @@ -27,7 +26,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.model.CalculatorEngine; -import org.solovyev.android.msg.AndroidMessageRegistry; import org.solovyev.android.view.FontSizeAdjuster; import org.solovyev.android.view.prefs.ResourceCache; import org.solovyev.android.view.widgets.*; @@ -36,11 +34,7 @@ import org.solovyev.common.utils.Announcer; import org.solovyev.common.utils.Point2d; import org.solovyev.common.utils.history.HistoryAction; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.text.DecimalFormatSymbols; -import java.util.ArrayList; -import java.util.List; import java.util.Locale; import java.util.Map; @@ -500,14 +494,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } calculatorModel = CalculatorModel.instance.init(this, preferences, CalculatorEngine.instance); - AndroidMessageRegistry.instance.init(this); } @Override protected void onDestroy() { super.onDestroy(); - - AndroidMessageRegistry.instance.finish(); } @Override diff --git a/src/main/java/org/solovyev/android/calculator/model/ParseException.java b/src/main/java/org/solovyev/android/calculator/model/ParseException.java index b39fdb3a..340fa9da 100644 --- a/src/main/java/org/solovyev/android/calculator/model/ParseException.java +++ b/src/main/java/org/solovyev/android/calculator/model/ParseException.java @@ -6,30 +6,25 @@ package org.solovyev.android.calculator.model; -import android.content.Context; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.view.prefs.ResourceCache; +import org.solovyev.android.msg.AndroidMessage; import org.solovyev.common.exceptions.SersoException; -import org.solovyev.common.utils.CollectionsUtils; +import org.solovyev.common.msg.Message; +import org.solovyev.common.msg.MessageType; -import java.text.MessageFormat; -import java.util.Arrays; -import java.util.Collections; import java.util.List; +import java.util.Locale; /** * User: serso * Date: 10/6/11 * Time: 9:25 PM */ -public class ParseException extends SersoException { +public class ParseException extends SersoException implements Message { @NotNull - private final String messageId; - - @NotNull - private final List parameters; + private final Message message; @NotNull private final String expression; @@ -38,38 +33,21 @@ public class ParseException extends SersoException { private final Integer position; public ParseException(@NotNull jscl.text.ParseException jsclParseException) { - this.messageId = "jscl_" + jsclParseException.getMessageId(); + this.message = jsclParseException; this.expression = jsclParseException.getExpression(); this.position = jsclParseException.getPosition(); - this.parameters = jsclParseException.getParameters(); } public ParseException(@NotNull String messageId, @Nullable Integer position, @NotNull String expression, Object... parameters) { - this.messageId = messageId; + this.message = new AndroidMessage(messageId, MessageType.error, parameters); this.expression = expression; this.position = position; - - if (CollectionsUtils.isEmpty(parameters)) { - this.parameters = Collections.emptyList(); - } else { - this.parameters = Arrays.asList(parameters); - } } public ParseException(@NotNull String messageId, @NotNull String expression, Object... parameters) { this(messageId, null, expression, parameters); } - @NotNull - public String getMessageId() { - return messageId; - } - - @NotNull - public List getParameters() { - return parameters; - } - @NotNull public String getExpression() { return expression; @@ -80,20 +58,33 @@ public class ParseException extends SersoException { return position; } + @NotNull + @Override + public String getMessageCode() { + return this.message.getMessageCode(); + } + + @NotNull + @Override + public List getParameters() { + return this.message.getParameters(); + } + + @NotNull + @Override + public MessageType getMessageType() { + return this.message.getMessageType(); + } + @Override @Nullable public String getLocalizedMessage() { - String result = null; + return this.message.getLocalizedMessage(Locale.getDefault()); + } - final String message = ResourceCache.instance.getCaption("msg_" + getMessageId()); - if (message != null) { - if ( parameters.size() > 0 ) { - result = MessageFormat.format(message, parameters.toArray(new Object[parameters.size()])); - } else { - result = message; - } - } - - return result; + @NotNull + @Override + public String getLocalizedMessage(@NotNull Locale locale) { + return this.message.getLocalizedMessage(locale); } } diff --git a/src/main/java/org/solovyev/android/msg/AndroidMessage.java b/src/main/java/org/solovyev/android/msg/AndroidMessage.java index 36aec500..ca8e1757 100644 --- a/src/main/java/org/solovyev/android/msg/AndroidMessage.java +++ b/src/main/java/org/solovyev/android/msg/AndroidMessage.java @@ -7,27 +7,34 @@ package org.solovyev.android.msg; import org.jetbrains.annotations.NotNull; +import org.solovyev.android.view.prefs.ResourceCache; import org.solovyev.common.msg.AbstractMessage; import org.solovyev.common.msg.MessageType; import java.util.List; +import java.util.Locale; /** * User: serso * Date: 10/18/11 * Time: 11:57 PM */ -public class AndroidMessage extends AbstractMessage { +public class AndroidMessage extends AbstractMessage { - public AndroidMessage(@NotNull Integer messageCode, - @NotNull MessageType messageType, - @org.jetbrains.annotations.Nullable Object... arguments) { + public AndroidMessage(@NotNull String messageCode, + @NotNull MessageType messageType, + @org.jetbrains.annotations.Nullable Object... arguments) { super(messageCode, messageType, arguments); } - public AndroidMessage(@NotNull Integer messageCode, - @NotNull MessageType messageType, - @org.jetbrains.annotations.Nullable List arguments) { + public AndroidMessage(@NotNull String messageCode, + @NotNull MessageType messageType, + @NotNull List arguments) { super(messageCode, messageType, arguments); } + + @Override + protected String getMessagePattern(@NotNull Locale locale) { + return ResourceCache.instance.getCaption(getMessageCode()); + } } diff --git a/src/main/java/org/solovyev/android/msg/AndroidMessageRegistry.java b/src/main/java/org/solovyev/android/msg/AndroidMessageRegistry.java deleted file mode 100644 index 64e950f8..00000000 --- a/src/main/java/org/solovyev/android/msg/AndroidMessageRegistry.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - * or visit http://se.solovyev.org - */ - -package org.solovyev.android.msg; - -import android.content.Context; -import android.widget.Toast; -import org.jetbrains.annotations.NotNull; -import org.solovyev.common.msg.MessageRegistry; - -import java.util.Locale; - -/** - * User: serso - * Date: 10/18/11 - * Time: 11:49 PM - */ -public enum AndroidMessageRegistry implements MessageRegistry { - - instance; - - private Context context; - - public void init(@NotNull Context context) { - this.context = context; - } - - @Override - public void addMessage(@NotNull AndroidMessage message) { - if (context != null) { - Toast.makeText(context, formatMessage(message), Toast.LENGTH_SHORT).show(); - } - } - - @NotNull - @Override - public AndroidMessage getMessage() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean hasMessage() { - throw new UnsupportedOperationException(); - } - - @NotNull - public String formatMessage(@NotNull AndroidMessage message) { - final String messagePattern = context.getString(message.getMessageCode()); - - final String result; - if (messagePattern != null) { - result = message.formatMessage(messagePattern, Locale.getDefault()); - } else { - result = ""; - } - - return result; - } - - public void finish() { - this.context = null; - } -} diff --git a/src/main/java/org/solovyev/android/view/prefs/ResourceCache.java b/src/main/java/org/solovyev/android/view/prefs/ResourceCache.java index 241c2b2c..cd4bf37f 100644 --- a/src/main/java/org/solovyev/android/view/prefs/ResourceCache.java +++ b/src/main/java/org/solovyev/android/view/prefs/ResourceCache.java @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + package org.solovyev.android.view.prefs; import android.app.Activity; @@ -6,7 +12,6 @@ import android.view.View; import android.widget.Button; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.solovyev.android.calculator.R; import org.solovyev.android.view.widgets.DragButton; import java.lang.reflect.Field; diff --git a/src/main/java/org/solovyev/util/math/Complex.java b/src/main/java/org/solovyev/util/math/Complex.java deleted file mode 100644 index 90fe9cf0..00000000 --- a/src/main/java/org/solovyev/util/math/Complex.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - */ - -package org.solovyev.util.math; - -import org.jetbrains.annotations.Nullable; - -/** -* User: serso -* Date: 9/17/11 -* Time: 11:35 PM -*/ -public class Complex { - - @Nullable - private Double real; - - @Nullable - private Double imaginary; - - @Nullable - public Double getReal() { - return real; - } - - public void setReal(@Nullable Double real) { - this.real = real; - } - - @Nullable - public Double getImaginary() { - return imaginary; - } - - public void setImaginary(@Nullable Double imaginary) { - this.imaginary = imaginary; - } -} diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index cf52024c..c61c1cf0 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -40,7 +40,7 @@ public class CalculatorEngineTest { cm.evaluate(JsclOperation.numeric, "3^10^10^10"); Assert.fail(); } catch (ParseException e) { - if (e.getMessageId().equals(Messages.msg_3)) { + if (e.getMessageCode().equals(Messages.msg_3)) { } else { System.out.print(e.getCause().getMessage()); @@ -52,7 +52,7 @@ public class CalculatorEngineTest { cm.evaluate(JsclOperation.numeric, "9999999!"); Assert.fail(); } catch (ParseException e) { - if (e.getMessageId().equals(Messages.msg_3)) { + if (e.getMessageCode().equals(Messages.msg_3)) { } else { System.out.print(e.getCause().getMessage());