vars activity

This commit is contained in:
serso 2011-09-30 00:08:43 +04:00
parent 4306320cc1
commit 55c6cbcc20
8 changed files with 139 additions and 49 deletions

View File

@ -16,11 +16,13 @@
a:layout_width="match_parent"
a:layout_height="match_parent">
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/var_text"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView a:id="@+id/var_text"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:textSize="20dp"
a:padding="5dp">
</TextView>
</LinearLayout>
</LinearLayout>

View File

@ -16,11 +16,13 @@
a:layout_width="match_parent"
a:layout_height="match_parent">
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/var_text"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView a:id="@+id/var_text"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:textSize="20dp"
a:padding="5dp">
</TextView>
</LinearLayout>
</LinearLayout>

View File

@ -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 {
if (!CalculatorModel.isLoaded()) {
CalculatorModel.init(this);
}
this.calculatorModel = CalculatorModel.getInstance();
} catch (EvalError evalError) {
// todo serso: create serso runtime exception

View File

@ -39,9 +39,7 @@ public class CalculatorModel {
private static CalculatorModel instance;
private CalculatorModel(@Nullable Context context) throws EvalError {
if (context != null) {
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) {
public synchronized void load(@Nullable Context context) {
if (context != null) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(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;

View File

@ -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<Var> vars = new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars());
setListAdapter(new ArrayAdapter<Var>(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<Var> {
private VarsArrayAdapter(Context context, int resource, int textViewResourceId, List<Var> objects) {
super(context, resource, textViewResourceId, objects);
}
public VarsArrayAdapter(Context context, int resource, List<Var> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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<Var> vars = new HashSet<Var>();
private final List<Var> vars = new ArrayList<Var>();
@NotNull
private final Set<Var> systemVars = new HashSet<Var>();
private final List<Var> systemVars = new ArrayList<Var>();
@NotNull
public Set<Var> getVars() {
return Collections.unmodifiableSet(vars);
public List<Var> getVars() {
return Collections.unmodifiableList(vars);
}
@NotNull
public Set<Var> getSystemVars() {
return Collections.unmodifiableSet(systemVars);
public List<Var> getSystemVars() {
return Collections.unmodifiableList(systemVars);
}
@Nullable
@ -64,12 +66,25 @@ 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();
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();
@ -80,6 +95,7 @@ public class VarsRegister {
throw new RuntimeException(e);
}
}
}
for (Var systemVar : MathEntityType.constants) {
@ -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) {

View File

@ -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<Var>() {
return isConstant(name);
}
private static boolean isConstant(final String name) {
return CollectionsUtils.get(CalculatorModel.getInstance().getVarsRegister().getVars(), new Finder<Var>() {
@Override
public boolean isFound(@Nullable Var var) {
return var != null && var.getName().equals(name);