From e517686eb5f32cfab97dd677f437d7e84d91de90 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 30 Sep 2011 00:08:43 +0400 Subject: [PATCH] vars activity --- res/layout-land/var.xml | 10 +-- res/layout-port/var.xml | 10 +-- .../calculator/CalculatorActivity.java | 5 +- .../android/calculator/CalculatorModel.java | 36 +++++++---- .../calculator/CalculatorVarsActivity.java | 35 +++++++++-- .../org/solovyev/android/calculator/Var.java | 17 +++++ .../android/calculator/VarsRegister.java | 63 ++++++++++++------- .../calculator/math/MathEntityType.java | 12 +++- 8 files changed, 139 insertions(+), 49 deletions(-) diff --git a/res/layout-land/var.xml b/res/layout-land/var.xml index c92c018f..3fddfcbb 100644 --- a/res/layout-land/var.xml +++ b/res/layout-land/var.xml @@ -16,11 +16,13 @@ a:layout_width="match_parent" a:layout_height="match_parent"> - + + \ No newline at end of file diff --git a/res/layout-port/var.xml b/res/layout-port/var.xml index c92c018f..3fddfcbb 100644 --- a/res/layout-port/var.xml +++ b/res/layout-port/var.xml @@ -16,11 +16,13 @@ a:layout_width="match_parent" a:layout_height="match_parent"> - + + \ No newline at end of file diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index b025ee62..52ed45ec 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -21,7 +21,6 @@ import org.jetbrains.annotations.Nullable; import org.solovyev.android.view.FontSizeAdjuster; import org.solovyev.android.view.widgets.*; import org.solovyev.common.BooleanMapper; -import org.solovyev.common.NumberMapper; import org.solovyev.common.utils.Announcer; import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.history.HistoryAction; @@ -57,7 +56,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh setContentView(R.layout.main); try { - CalculatorModel.init(this); + if (!CalculatorModel.isLoaded()) { + CalculatorModel.init(this); + } this.calculatorModel = CalculatorModel.getInstance(); } catch (EvalError evalError) { // todo serso: create serso runtime exception diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java index bcec4bee..3f195c86 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java @@ -39,9 +39,7 @@ public class CalculatorModel { private static CalculatorModel instance; private CalculatorModel(@Nullable Context context) throws EvalError { - if (context != null) { - load(context); - } + load(context); interpreter = new Interpreter(); interpreter.eval(ToJsclPreprocessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands")); @@ -49,11 +47,21 @@ public class CalculatorModel { public String evaluate(@NotNull JsclOperation operation, @NotNull String expression) throws EvalError, ParseException { - final String preprocessedExpression = preprocessor.process(expression); + final StringBuilder sb = new StringBuilder(); + +/* + for (Var var : varsRegister.getVars()) { + if (!var.isSystem()) { + sb.append(var.getName()).append("=").append(var.getValue()).append(";"); + } + } +*/ + + sb.append(preprocessor.process(expression)); //Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression); - Object evaluationObject = interpreter.eval(ToJsclPreprocessor.wrap(operation, preprocessedExpression)); + Object evaluationObject = interpreter.eval(ToJsclPreprocessor.wrap(operation, sb.toString())); String result = String.valueOf(evaluationObject).trim(); try { @@ -112,11 +120,13 @@ public class CalculatorModel { return MathUtils.round(dResult, numberOfFractionDigits); } - public synchronized void load(@NotNull Context context) { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + public synchronized void load(@Nullable Context context) { + if (context != null) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - final NumberMapper integerNumberMapper = new NumberMapper(Integer.class); - this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(context.getString(R.string.p_calc_result_precision_key), context.getString(R.string.p_calc_result_precision)))); + final NumberMapper integerNumberMapper = new NumberMapper(Integer.class); + this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(context.getString(R.string.p_calc_result_precision_key), context.getString(R.string.p_calc_result_precision)))); + } varsRegister.load(context); } @@ -136,7 +146,7 @@ public class CalculatorModel { } public static synchronized void init(@Nullable Context context) throws EvalError { - if ( instance == null ) { + if (!isLoaded()) { instance = new CalculatorModel(context); } else { throw new RuntimeException("Calculator model already instantiated!"); @@ -144,13 +154,17 @@ public class CalculatorModel { } public static CalculatorModel getInstance() { - if ( instance == null ) { + if (!isLoaded()) { throw new RuntimeException("CalculatorModel must be instantiated!"); } return instance; } + public static boolean isLoaded() { + return instance != null; + } + @NotNull public VarsRegister getVarsRegister() { return varsRegister; diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java index 225cd54b..1461fbff 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java @@ -7,12 +7,13 @@ package org.solovyev.android.calculator; import android.app.ListActivity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListView; +import android.view.ViewGroup; +import android.widget.*; +import org.solovyev.common.utils.StringUtils; import java.util.ArrayList; import java.util.List; @@ -31,7 +32,7 @@ public class CalculatorVarsActivity extends ListActivity { setTheme(android.R.style.Theme_Dialog); final List vars = new ArrayList(CalculatorModel.getInstance().getVarsRegister().getVars()); - setListAdapter(new ArrayAdapter(this, R.layout.var, R.id.var_text, vars)); + setListAdapter(new VarsArrayAdapter(this, R.layout.var, R.id.var_text, vars)); final ListView lv = getListView(); lv.setTextFilterEnabled(true); @@ -48,4 +49,30 @@ public class CalculatorVarsActivity extends ListActivity { }); } + + private class VarsArrayAdapter extends ArrayAdapter { + + private VarsArrayAdapter(Context context, int resource, int textViewResourceId, List objects) { + super(context, resource, textViewResourceId, objects); + } + + public VarsArrayAdapter(Context context, int resource, List objects) { + super(context, resource, objects); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + final ViewGroup result = (ViewGroup)super.getView(position, convertView, parent); + + final Var var = getItem(position); + + if (!StringUtils.isEmpty(var.getDescription())) { + final TextView description = new TextView(getContext()); + description.setText(var.getDescription()); + result.addView(description, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + } + + return result; + } + } } diff --git a/src/main/java/org/solovyev/android/calculator/Var.java b/src/main/java/org/solovyev/android/calculator/Var.java index 2f0d4a7f..409d6ee3 100644 --- a/src/main/java/org/solovyev/android/calculator/Var.java +++ b/src/main/java/org/solovyev/android/calculator/Var.java @@ -86,4 +86,21 @@ public class Var { public String toString() { return getName() + " = " + value; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Var var = (Var) o; + + if (!name.equals(var.name)) return false; + + return true; + } + + @Override + public int hashCode() { + return name.hashCode(); + } } diff --git a/src/main/java/org/solovyev/android/calculator/VarsRegister.java b/src/main/java/org/solovyev/android/calculator/VarsRegister.java index ed7822a6..48124883 100644 --- a/src/main/java/org/solovyev/android/calculator/VarsRegister.java +++ b/src/main/java/org/solovyev/android/calculator/VarsRegister.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.calculator; import android.content.Context; @@ -8,15 +14,11 @@ import org.jetbrains.annotations.Nullable; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import org.solovyev.android.calculator.math.MathEntityType; -import org.solovyev.android.view.widgets.SimpleOnDragListener; import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.Finder; import java.io.StringWriter; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** * User: serso @@ -26,19 +28,19 @@ import java.util.Set; public class VarsRegister { @NotNull - private final Set vars = new HashSet(); + private final List vars = new ArrayList(); @NotNull - private final Set systemVars = new HashSet(); + private final List systemVars = new ArrayList(); @NotNull - public Set getVars() { - return Collections.unmodifiableSet(vars); + public List getVars() { + return Collections.unmodifiableList(vars); } @NotNull - public Set getSystemVars() { - return Collections.unmodifiableSet(systemVars); + public List getSystemVars() { + return Collections.unmodifiableList(systemVars); } @Nullable @@ -64,20 +66,34 @@ public class VarsRegister { vars.addAll(result); } - public synchronized void load(@NotNull Context context) { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + public synchronized void load(@Nullable Context context) { this.vars.clear(); this.systemVars.clear(); - final String value = preferences.getString(context.getString(R.string.p_calc_vars), null); - if (value != null) { - final Serializer serializer = new Persister(); - try { - final Vars vars = serializer.read(Vars.class, value); - this.vars.addAll(vars.getVars()); - } catch (Exception e) { - throw new RuntimeException(e); + Var t = new Var("T", 0d, false); + t.setDescription("Some text description!"); + this.vars.add(t); + + Var t2 = new Var("T2", 2d, false); + this.vars.add(t2); + + Var t1 = new Var("T1", 1d, false); + t1.setDescription("Не так давно в рунете появились упоминания англоязычного проекта DearPhotograph.com. Сайт нас растрогал, зарядил идеей и вдохновением."); + this.vars.add(t1); + + if (context != null) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + final String value = preferences.getString(context.getString(R.string.p_calc_vars), null); + if (value != null) { + final Serializer serializer = new Persister(); + try { + final Vars vars = serializer.read(Vars.class, value); + this.vars.addAll(vars.getVars()); + } catch (Exception e) { + throw new RuntimeException(e); + } } } @@ -89,6 +105,11 @@ public class VarsRegister { vars.add(systemVar); } } + + /*Log.d(VarsRegister.class.getName(), vars.size() + " variables registered!"); + for (Var var : vars) { + Log.d(VarsRegister.class.getName(), var.toString()); + }*/ } public synchronized void save(@NotNull Context context) { diff --git a/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java b/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java index 3f81e051..8c4ec32e 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathEntityType.java @@ -7,11 +7,11 @@ package org.solovyev.android.calculator.math; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorModel; import org.solovyev.android.calculator.Var; import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.Finder; -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -71,7 +71,9 @@ public enum MathEntityType { } if ( result == null ) { - if ( prefixFunctions.contains(s) ) { + if ( isConstant(s) ) { + result = MathEntityType.constant; + } else if ( prefixFunctions.contains(s) ) { result = MathEntityType.function; } else if ( groupSymbols.contains(s) ) { result = MathEntityType.group_symbols; @@ -107,7 +109,11 @@ public enum MathEntityType { private static boolean isConstant(final char ch) { final String name = String.valueOf(ch); - return CollectionsUtils.get(constants, new Finder() { + return isConstant(name); + } + + private static boolean isConstant(final String name) { + return CollectionsUtils.get(CalculatorModel.getInstance().getVarsRegister().getVars(), new Finder() { @Override public boolean isFound(@Nullable Var var) { return var != null && var.getName().equals(name);