persistenceContainer = mathEntityDao.load();
+ final List notCreatedEntities = new ArrayList
();
+
if (persistenceContainer != null) {
for (P entity : persistenceContainer.getEntities()) {
if (!contains(entity.getName())) {
try {
final JBuilder extends T> builder = createBuilder(entity);
add(builder);
- } catch (ArithmeticException e) {
+ } catch (RuntimeException e) {
Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
- if (e instanceof Message) {
- Locator.getInstance().getNotifier().showMessage((Message)e);
- }
+ notCreatedEntities.add(entity);
}
}
}
}
- /*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
- for (Var var : vars) {
- Log.d(AndroidVarsRegistry.class.getName(), var.toString());
- }*/
- }
+ try {
+ if (!notCreatedEntities.isEmpty()) {
+ final StringBuilder errorMessage = new StringBuilder(notCreatedEntities.size() * 100);
+ for (P notCreatedEntity : notCreatedEntities) {
+ errorMessage.append(notCreatedEntity).append("\n\n");
+ }
- @NotNull
+ Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_message_dialog, MessageDialogData.newInstance(CalculatorMessages.newErrorMessage(CalculatorMessages.msg_007, errorMessage.toString()), null));
+ }
+ } catch (RuntimeException e) {
+ // just in case
+ Locator.getInstance().getLogger().error(null, e.getLocalizedMessage(), e);
+ }
+ }
+
+ @NotNull
protected abstract JBuilder extends T> createBuilder(@NotNull P entity);
-
@Override
public synchronized void save() {
final MathEntityPersistenceContainer
container = createPersistenceContainer();
diff --git a/core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java b/core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java
index 073ccab4..6a88707c 100644
--- a/core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java
+++ b/core/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java
@@ -159,6 +159,9 @@ public enum CalculatorEventType {
show_create_matrix_dialog,
show_create_function_dialog,
+ /** {@link DialogData} */
+ show_message_dialog,
+
plot_graph,
/** {@link org.solovyev.android.calculator.plot.PlotData} */
diff --git a/core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java b/core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java
index 338a4787..b3d95373 100644
--- a/core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java
+++ b/core/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsMathRegistry.java
@@ -12,6 +12,7 @@ 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.function.FunctionBuilderAdapter;
import org.solovyev.android.calculator.model.AFunction;
import org.solovyev.android.calculator.model.Functions;
import org.solovyev.android.calculator.model.MathEntityBuilder;
@@ -46,20 +47,20 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
@Override
public void load() {
- super.load();
-
- add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
- add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)"));
- add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)"));
- add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
+ add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
+ add(new CustomFunction.Builder(true, "√3", Arrays.asList("x"), "x^(1/3)"));
+ add(new CustomFunction.Builder(true, "√4", Arrays.asList("x"), "x^(1/4)"));
+ add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
add(new CustomFunction.Builder(true, "re", Arrays.asList("x"), "(x+conjugate(x))/2"));
add(new CustomFunction.Builder(true, "im", Arrays.asList("x"), "(x-conjugate(x))/(2*i)"));
+
+ super.load();
}
public static void saveFunction(@NotNull CalculatorMathRegistry registry,
@NotNull MathEntityBuilder extends Function> builder,
@Nullable IFunction editedInstance,
- @NotNull Object source, boolean save) throws CustomFunctionCalculationException {
+ @NotNull Object source, boolean save) throws CustomFunctionCalculationException, AFunction.Builder.CreationException {
final Function addedFunction = registry.add(builder);
if (save) {
@@ -111,7 +112,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
@NotNull
@Override
protected JBuilder extends Function> createBuilder(@NotNull AFunction function) {
- return new CustomFunction.Builder(function);
+ return new FunctionBuilderAdapter(new AFunction.Builder(function));
}
@Override
diff --git a/core/src/main/java/org/solovyev/android/calculator/CalculatorLogger.java b/core/src/main/java/org/solovyev/android/calculator/CalculatorLogger.java
index 568e218f..37fff1c5 100644
--- a/core/src/main/java/org/solovyev/android/calculator/CalculatorLogger.java
+++ b/core/src/main/java/org/solovyev/android/calculator/CalculatorLogger.java
@@ -16,4 +16,6 @@ public interface CalculatorLogger {
void error(@Nullable String tag, @Nullable String message, @NotNull Throwable e);
+ void error(@Nullable String tag, @Nullable String message);
+
}
diff --git a/core/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java b/core/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java
index 59ad99a7..97b73917 100644
--- a/core/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java
+++ b/core/src/main/java/org/solovyev/android/calculator/CalculatorMessages.java
@@ -1,6 +1,8 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.solovyev.common.msg.MessageType;
import java.util.Locale;
import java.util.ResourceBundle;
@@ -45,12 +47,23 @@ public final class CalculatorMessages {
/* Infinite loop is detected in expression*/
public static final String msg_006 = "msg_6";
+ /** Some data could not be loaded. Contact authors of application with information below.\n\nUnable to load:\n{0} */
+ public static final String msg_007 = "msg_7";
+
/* Error */
public static final String syntax_error = "syntax_error";
/* Result copied to clipboard! */
public static final String result_copied = "result_copied";
+ /* Text copied to clipboard! */
+ public static final String text_copied = "text_copied";
+
/* Last calculated value */
public static final String ans_description = "ans_description";
+
+ @NotNull
+ public static CalculatorMessage newErrorMessage(@NotNull String messageCode, @Nullable Object... parameters ) {
+ return new CalculatorMessage(messageCode, MessageType.error, parameters);
+ }
}
diff --git a/core/src/main/java/org/solovyev/android/calculator/DialogData.java b/core/src/main/java/org/solovyev/android/calculator/DialogData.java
new file mode 100644
index 00000000..616ac541
--- /dev/null
+++ b/core/src/main/java/org/solovyev/android/calculator/DialogData.java
@@ -0,0 +1,22 @@
+package org.solovyev.android.calculator;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.solovyev.common.msg.MessageType;
+
+/**
+ * User: serso
+ * Date: 1/20/13
+ * Time: 12:47 PM
+ */
+public interface DialogData {
+
+ @NotNull
+ String getMessage();
+
+ @NotNull
+ MessageType getMessageType();
+
+ @Nullable
+ String getTitle();
+}
diff --git a/core/src/main/java/org/solovyev/android/calculator/MessageDialogData.java b/core/src/main/java/org/solovyev/android/calculator/MessageDialogData.java
new file mode 100644
index 00000000..d17fbfdb
--- /dev/null
+++ b/core/src/main/java/org/solovyev/android/calculator/MessageDialogData.java
@@ -0,0 +1,48 @@
+package org.solovyev.android.calculator;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.solovyev.common.msg.Message;
+import org.solovyev.common.msg.MessageType;
+
+/**
+ * User: serso
+ * Date: 1/20/13
+ * Time: 12:45 PM
+ */
+public class MessageDialogData implements DialogData {
+
+ @NotNull
+ private Message message;
+
+ @Nullable
+ private String title;
+
+ private MessageDialogData(@NotNull Message message, @Nullable String title) {
+ this.message = message;
+ this.title = title;
+ }
+
+ @NotNull
+ public static DialogData newInstance(@NotNull Message message, @Nullable String title) {
+ return new MessageDialogData(message, title);
+ }
+
+ @Override
+ @NotNull
+ public String getMessage() {
+ return message.getLocalizedMessage();
+ }
+
+ @NotNull
+ @Override
+ public MessageType getMessageType() {
+ return message.getMessageType();
+ }
+
+ @Override
+ @Nullable
+ public String getTitle() {
+ return title;
+ }
+}
diff --git a/core/src/main/java/org/solovyev/android/calculator/StringDialogData.java b/core/src/main/java/org/solovyev/android/calculator/StringDialogData.java
new file mode 100644
index 00000000..75b92617
--- /dev/null
+++ b/core/src/main/java/org/solovyev/android/calculator/StringDialogData.java
@@ -0,0 +1,51 @@
+package org.solovyev.android.calculator;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.solovyev.common.msg.MessageType;
+
+/**
+ * User: serso
+ * Date: 1/20/13
+ * Time: 1:01 PM
+ */
+public class StringDialogData implements DialogData {
+
+ @NotNull
+ private final String message;
+
+ @NotNull
+ private final MessageType messageType;
+
+ @Nullable
+ private final String title;
+
+ private StringDialogData(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
+ this.message = message;
+ this.messageType = messageType;
+ this.title = title;
+ }
+
+ @NotNull
+ public static DialogData newInstance(@NotNull String message, @NotNull MessageType messageType, @Nullable String title) {
+ return new StringDialogData(message, messageType, title);
+ }
+
+ @NotNull
+ @Override
+ public String getMessage() {
+ return message;
+ }
+
+ @NotNull
+ @Override
+ public MessageType getMessageType() {
+ return messageType;
+ }
+
+ @Nullable
+ @Override
+ public String getTitle() {
+ return title;
+ }
+}
diff --git a/core/src/main/java/org/solovyev/android/calculator/SystemOutCalculatorLogger.java b/core/src/main/java/org/solovyev/android/calculator/SystemOutCalculatorLogger.java
index 0146f95e..a2f939b4 100644
--- a/core/src/main/java/org/solovyev/android/calculator/SystemOutCalculatorLogger.java
+++ b/core/src/main/java/org/solovyev/android/calculator/SystemOutCalculatorLogger.java
@@ -34,4 +34,9 @@ public class SystemOutCalculatorLogger implements CalculatorLogger {
System.out.println(getTag(tag) + ": " + message);
e.printStackTrace(System.out);
}
+
+ @Override
+ public void error(@Nullable String tag, @Nullable String message) {
+ System.out.println(getTag(tag) + ": " + message);
+ }
}
diff --git a/core/src/main/java/org/solovyev/android/calculator/function/FunctionBuilderAdapter.java b/core/src/main/java/org/solovyev/android/calculator/function/FunctionBuilderAdapter.java
new file mode 100644
index 00000000..05b1f814
--- /dev/null
+++ b/core/src/main/java/org/solovyev/android/calculator/function/FunctionBuilderAdapter.java
@@ -0,0 +1,52 @@
+package org.solovyev.android.calculator.function;
+
+import jscl.CustomFunctionCalculationException;
+import jscl.math.function.CustomFunction;
+import jscl.math.function.Function;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.solovyev.android.calculator.model.AFunction;
+import org.solovyev.android.calculator.model.MathEntityBuilder;
+
+/**
+* User: serso
+* Date: 1/20/13
+* Time: 12:21 PM
+*/
+public final class FunctionBuilderAdapter implements MathEntityBuilder {
+
+ @NotNull
+ private final AFunction.Builder nestedBuilder;
+
+ public FunctionBuilderAdapter(@NotNull AFunction.Builder nestedBuilder) {
+ this.nestedBuilder = nestedBuilder;
+ }
+
+ @NotNull
+ @Override
+ public MathEntityBuilder setName(@NotNull String name) {
+ nestedBuilder.setName(name);
+ return this;
+ }
+
+ @NotNull
+ @Override
+ public MathEntityBuilder setDescription(@Nullable String description) {
+ nestedBuilder.setDescription(description);
+ return this;
+ }
+
+ @NotNull
+ @Override
+ public MathEntityBuilder setValue(@Nullable String value) {
+ nestedBuilder.setValue(value);
+ return this;
+ }
+
+ @NotNull
+ @Override
+ public Function create() throws CustomFunctionCalculationException, AFunction.Builder.CreationException{
+ final AFunction function = nestedBuilder.create();
+ return new CustomFunction.Builder(function).create();
+ }
+}
diff --git a/core/src/main/java/org/solovyev/android/calculator/model/AFunction.java b/core/src/main/java/org/solovyev/android/calculator/model/AFunction.java
index b86aaecb..f38fea25 100644
--- a/core/src/main/java/org/solovyev/android/calculator/model/AFunction.java
+++ b/core/src/main/java/org/solovyev/android/calculator/model/AFunction.java
@@ -13,8 +13,8 @@ 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.Locator;
import org.solovyev.android.calculator.CalculatorParseException;
+import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.MathPersistenceEntity;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.msg.Message;
@@ -122,7 +122,16 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
return String.valueOf(this.content);
}
- /*
+ @Override
+ public String toString() {
+ return "AFunction{" +
+ "name='" + name + '\'' +
+ ", parameterNames=" + parameterNames +
+ ", content='" + content + '\'' +
+ '}';
+ }
+
+ /*
**********************************************************************
*
* GETTERS/SETTERS
@@ -258,7 +267,7 @@ public class AFunction implements IFunction, MathPersistenceEntity, Serializable
}
@NotNull
- public AFunction create() {
+ public AFunction create() throws AFunction.Builder.CreationException{
final AFunction result;
if (id != null) {
result = new AFunction(id);
diff --git a/core/src/main/java/org/solovyev/android/calculator/model/Var.java b/core/src/main/java/org/solovyev/android/calculator/model/Var.java
index e3e7b396..9e3f2746 100644
--- a/core/src/main/java/org/solovyev/android/calculator/model/Var.java
+++ b/core/src/main/java/org/solovyev/android/calculator/model/Var.java
@@ -229,10 +229,10 @@ public class Var implements IConstant, MathPersistenceEntity {
return !StringUtils.isEmpty(value);
}
- @Override
- public String toString() {
- return ExtendedConstant.toString(this);
- }
+ @Override
+ public String toString() {
+ return ExtendedConstant.toString(this);
+ }
@Override
public boolean equals(Object o) {
@@ -250,4 +250,5 @@ public class Var implements IConstant, MathPersistenceEntity {
public int hashCode() {
return name.hashCode();
}
+
}
diff --git a/core/src/main/resources/org/solovyev/android/calculator/messages.properties b/core/src/main/resources/org/solovyev/android/calculator/messages.properties
index 5407c943..1db9067f 100644
--- a/core/src/main/resources/org/solovyev/android/calculator/messages.properties
+++ b/core/src/main/resources/org/solovyev/android/calculator/messages.properties
@@ -4,7 +4,9 @@ msg_3=Too long execution time - check the expression
msg_4=Evaluation was cancelled
msg_5=No parameters are specified for function: {0}
msg_6=Infinite loop is detected in expression
+msg_7=Some user data could not be loaded. Please contact authors of application with information below.\n\nUnable to load:\n{0}
syntax_error=Error
result_copied=Result copied to clipboard!
+text_copied=Text copied to clipboard!
ans_description=Last calculated value
\ No newline at end of file
diff --git a/core/src/main/resources/org/solovyev/android/calculator/messages_ru.properties b/core/src/main/resources/org/solovyev/android/calculator/messages_ru.properties
index 756ff7ad..df1885aa 100644
--- a/core/src/main/resources/org/solovyev/android/calculator/messages_ru.properties
+++ b/core/src/main/resources/org/solovyev/android/calculator/messages_ru.properties
@@ -4,7 +4,10 @@ msg_3=Вычисление выражения занимает слишком м
msg_4=Вычисление было отменено
msg_5=Для функции {0} не определены параметры
msg_6=В выражении найден Бесконечный цикл - проверьте выражение
+msg_7=Некоторые пользовательские данные не могут быть загружены. Пожалуйста, свяжитесь с авторами приложения с информацией ниже.\n\nНевозможно загрузить:\n{0}
+
syntax_error=Ошибка
result_copied=Результат скопирован в буфер!
+text_copied=Текст скопирован в буфер!
ans_description=Последний посчитанный результат
\ No newline at end of file