math entities list activities refactored + categories added

This commit is contained in:
Sergey Solovyev 2011-12-23 00:37:34 +04:00
parent 08e3f7cef1
commit 9252ff265e
25 changed files with 909 additions and 458 deletions

View File

@ -61,23 +61,23 @@
a:label="@string/c_help" a:label="@string/c_help"
a:configChanges="orientation|keyboardHidden"/> a:configChanges="orientation|keyboardHidden"/>
<activity a:name=".CalculatorFunctionsActivity" <activity a:name=".math.edit.CalculatorFunctionsActivity"
a:label="@string/c_functions" a:label="@string/c_functions"
a:configChanges="orientation|keyboardHidden"/> a:configChanges="orientation|keyboardHidden"/>
<activity a:name=".CalculatorFunctionsTabActivity" <activity a:name=".math.edit.CalculatorFunctionsTabActivity"
a:label="@string/c_functions" a:label="@string/c_functions"
a:configChanges="orientation|keyboardHidden"/> a:configChanges="orientation|keyboardHidden"/>
<activity a:name=".CalculatorOperatorsActivity" <activity a:name=".math.edit.CalculatorOperatorsActivity"
a:label="@string/c_operators" a:label="@string/c_operators"
a:configChanges="orientation|keyboardHidden"/> a:configChanges="orientation|keyboardHidden"/>
<activity a:name=".CalculatorVarsActivity" <activity a:name=".math.edit.CalculatorVarsActivity"
a:label="@string/c_vars_and_constants" a:label="@string/c_vars_and_constants"
a:configChanges="orientation|keyboardHidden"/> a:configChanges="orientation|keyboardHidden"/>
<activity a:name=".CalculatorVarsTabActivity" <activity a:name=".math.edit.CalculatorVarsTabActivity"
a:label="@string/c_vars_and_constants" a:label="@string/c_vars_and_constants"
a:configChanges="orientation|keyboardHidden"/> a:configChanges="orientation|keyboardHidden"/>

View File

@ -75,7 +75,7 @@
<string name="c_sys.var.cannot.be.changed">System variable cannot be changed!</string> <string name="c_sys.var.cannot.be.changed">System variable cannot be changed!</string>
<string name="c_var_description_pi">Ratio of any circle\'s circumference to its diameter. Note: this constant is degree unit dependent => rad: 3.14, deg: 180.0</string> <string name="c_var_description_pi">Ratio of any circle\'s circumference to its diameter. Note: this constant is degree unit dependent => rad: 3.14, deg: 180.0</string>
<string name="c_var_description_PI">Ratio of any circle\'s circumference to its diameter. Note: this constant is degree unit independent => rad: 3.14, rad: 3.14</string> <string name="c_var_description_PI">Ratio of any circle\'s circumference to its diameter. Note: this constant is degree unit independent => rad: 3.14, deg: 3.14</string>
<string name="c_var_description_e">Unique real number such that the value of the derivative (slope of the tangent line) of the function f(x) = e^x at the point x = 0 is equal to 1</string> <string name="c_var_description_e">Unique real number such that the value of the derivative (slope of the tangent line) of the function f(x) = e^x at the point x = 0 is equal to 1</string>
<string name="c_var_description_i">Imaginary unit, defined such that i^2 = 1</string> <string name="c_var_description_i">Imaginary unit, defined such that i^2 = 1</string>
<string name="c_var_description_c">Speed of light in vacuum, m·s^-1</string> <string name="c_var_description_c">Speed of light in vacuum, m·s^-1</string>
@ -327,6 +327,7 @@ Check the \'Round result\' preference in application settings - it should be tur
<string name="c_fun_category_hyper_trig">Hyperbolic trigonometric</string> <string name="c_fun_category_hyper_trig">Hyperbolic trigonometric</string>
<string name="c_fun_category_comparison">Comparison</string> <string name="c_fun_category_comparison">Comparison</string>
<string name="c_fun_category_common">Common</string> <string name="c_fun_category_common">Common</string>
<string name="c_fun_category_my">My</string>
<string name="c_var_system">System</string> <string name="c_var_system">System</string>
<string name="c_var_my">My</string> <string name="c_var_my">My</string>

View File

@ -9,6 +9,10 @@ import org.achartengine.ChartFactory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.help.HelpActivity; import org.solovyev.android.calculator.help.HelpActivity;
import org.solovyev.android.calculator.history.CalculatorHistoryActivity; import org.solovyev.android.calculator.history.CalculatorHistoryActivity;
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorOperatorsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
/** /**

View File

@ -1,39 +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.calculator;
import jscl.math.function.Function;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.CalculatorEngine;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 10/29/11
* Time: 4:55 PM
*/
public class CalculatorFunctionsTabActivity extends AbstractMathEntityListActivity<Function> {
@NotNull
@Override
protected MathEntityDescriptionGetter getDescriptionGetter() {
return new MathEntityDescriptionGetterImpl(CalculatorEngine.instance.getFunctionsRegistry());
}
@NotNull
@Override
protected List<Function> getMathEntities() {
return new ArrayList<Function>(CalculatorEngine.instance.getFunctionsRegistry().getEntities());
}
@Override
protected String getMathEntityCategory(@NotNull Function function) {
return CalculatorEngine.instance.getFunctionsRegistry().getCategory(function);
}
}

View File

@ -8,7 +8,6 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.text.ClipboardManager; import android.text.ClipboardManager;
@ -31,7 +30,6 @@ import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.CalculatorEvalException; import org.solovyev.android.calculator.model.CalculatorEvalException;
import org.solovyev.android.calculator.model.CalculatorParseException; import org.solovyev.android.calculator.model.CalculatorParseException;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.view.*; import org.solovyev.android.view.*;
import org.solovyev.common.msg.Message; import org.solovyev.common.msg.Message;
import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.CollectionsUtils;
@ -52,7 +50,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
instance; instance;
// millis to wait before evaluation after user edit action // millis to wait before evaluation after user edit action
public static final int EVAL_DELAY_MILLIS = 1000; public static final int EVAL_DELAY_MILLIS = 400;
@NotNull @NotNull
private CalculatorEditor editor; private CalculatorEditor editor;
@ -390,7 +388,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
if ( genericResult != null ) { if ( genericResult != null ) {
final Set<Constant> notSystemConstants = new HashSet<Constant>(); final Set<Constant> notSystemConstants = new HashSet<Constant>();
for (Constant constant : genericResult.getConstants()) { for (Constant constant : genericResult.getConstants()) {
IConstant var = CalculatorEngine.instance.getVarsRegister().get(constant.getName()); IConstant var = CalculatorEngine.instance.getVarsRegistry().get(constant.getName());
if (var != null && !var.isSystem() && !var.isDefined()) { if (var != null && !var.isSystem() && !var.isDefined()) {
notSystemConstants.add(constant); notSystemConstants.add(constant);
} }

View File

@ -1,376 +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.calculator;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import jscl.math.function.IConstant;
import jscl.text.Identifier;
import jscl.text.MutableInt;
import jscl.text.ParseException;
import jscl.text.Parser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.AndroidMathRegistry;
import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Finder;
import org.solovyev.common.utils.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* User: serso
* Date: 9/28/11
* Time: 10:55 PM
*/
public class CalculatorVarsTabActivity extends AbstractMathEntityListActivity<IConstant> {
public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_create_var";
private final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray()));
@Override
protected int getLayoutId() {
return R.layout.vars;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final Var var = (Var) parent.getItemAtPosition(position);
createEditVariableDialog(var, var.getName(), StringUtils.getNotEmpty(var.getValue(), ""), var.getDescription());
return true;
}
});
final Intent intent = getIntent();
if ( intent != null ) {
final String varValue = intent.getStringExtra(CREATE_VAR_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
createEditVariableDialog(null, null, varValue, null);
// in order to stop intent for other tabs
intent.removeExtra(CREATE_VAR_EXTRA_STRING);
}
}
}
@NotNull
@Override
protected MathEntityDescriptionGetter getDescriptionGetter() {
return new MathEntityDescriptionGetterImpl(CalculatorEngine.instance.getVarsRegister());
}
@SuppressWarnings({"UnusedDeclaration"})
public void addVarButtonClickHandler(@NotNull View v) {
createEditVariableDialog(null, null, null, null);
}
@NotNull
@Override
protected List<IConstant> getMathEntities() {
final List<IConstant> result = new ArrayList<IConstant>(CalculatorEngine.instance.getVarsRegister().getEntities());
CollectionsUtils.removeAll(result, new Finder<IConstant>() {
@Override
public boolean isFound(@Nullable IConstant var) {
return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN);
}
});
return result;
}
@Override
protected String getMathEntityCategory(@NotNull IConstant var) {
return CalculatorEngine.instance.getVarsRegister().getCategory(var);
}
private void createEditVariableDialog(@Nullable final Var var, @Nullable final String name, @Nullable final String value, @Nullable final String description) {
if (var == null || !var.isSystem()) {
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
final String errorMsg = CalculatorVarsTabActivity.this.getString(R.string.c_char_is_not_accepted);
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
editName.setText(name);
editName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
for ( int i = 0; i < s.length(); i++ ) {
char c = s.charAt(i);
if (!acceptableChars.contains(c)) {
s.delete(i, i+1);
Toast.makeText(CalculatorVarsTabActivity.this, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
}
}
}
});
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
if (!StringUtils.isEmpty(value)) {
editValue.setText(value);
}
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
editDescription.setText(description);
final Var.Builder varBuilder;
if (var != null) {
varBuilder = new Var.Builder(var);
} else {
varBuilder = new Var.Builder();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setCancelable(true)
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_save, new VarEditorSaver(varBuilder, var, editView))
.setView(editView);
if ( var != null ) {
// EDIT mode
builder.setTitle(R.string.c_var_edit_var);
builder.setNeutralButton(R.string.c_remove, new VarEditorRemover(var, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createEditVariableDialog(var, name, value, description);
}
}));
} else {
// CREATE mode
builder.setTitle(R.string.c_var_create_var);
}
builder.create().show();
} else {
Toast.makeText(this, getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
}
private class VarEditorSaver implements DialogInterface.OnClickListener {
@NotNull
private Var.Builder varBuilder;
@Nullable
private final Var editedInstance;
@NotNull
private View editView;
public VarEditorSaver(@NotNull Var.Builder varBuilder, @Nullable Var editedInstance, @NotNull View editView) {
this.varBuilder = varBuilder;
this.editedInstance = editedInstance;
this.editView = editView;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
final Integer error;
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
String name = editName.getText().toString();
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
String value = editValue.getText().toString();
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
String description = editDescription.getText().toString();
final AndroidMathRegistry<IConstant> varsRegistry = CalculatorEngine.instance.getVarsRegister();
if (isValidName(name)) {
boolean canBeSaved = false;
final IConstant varFromRegister = varsRegistry.get(name);
if ( varFromRegister == null ) {
canBeSaved = true;
} else if ( editedInstance != null && varFromRegister.getId().equals(editedInstance.getId()) ) {
canBeSaved = true;
}
if (canBeSaved) {
final MathType.Result mathType = MathType.getType(name, 0, false);
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
if (StringUtils.isEmpty(value)) {
// value is empty => undefined variable
varBuilder.setName(name);
varBuilder.setDescription(description);
varBuilder.setValue(null);
error = null;
} else {
// value is not empty => must be a number
boolean valid = isValidValue(value);
if (valid) {
varBuilder.setName(name);
varBuilder.setDescription(description);
varBuilder.setValue(value);
error = null;
} else {
error = R.string.c_value_is_not_a_number;
}
}
} else {
error = R.string.c_var_name_clashes;
}
} else {
error = R.string.c_var_already_exists;
}
} else {
error = R.string.c_name_is_not_valid;
}
if (error != null) {
Toast.makeText(CalculatorVarsTabActivity.this, getString(error), Toast.LENGTH_LONG).show();
createEditVariableDialog(editedInstance, name, value, description);
} else {
final IConstant addedVar = varsRegistry.add(varBuilder);
if (isInCategory(addedVar)) {
if ( editedInstance != null ) {
CalculatorVarsTabActivity.this.getAdapter().remove(editedInstance);
}
CalculatorVarsTabActivity.this.getAdapter().add(addedVar);
}
varsRegistry.save(CalculatorVarsTabActivity.this);
if (isInCategory(addedVar)) {
sort();
}
}
}
}
}
private static boolean isValidName(@Nullable String name) {
boolean result = false;
if (!StringUtils.isEmpty(name)) {
try {
Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), CalculatorEngine.instance.getEngine()), null);
result = true;
} catch (ParseException e) {
// not valid name;
}
}
return result;
}
public static boolean isValidValue(@NotNull String value) {
// now every string might be constant
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.var_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean result;
switch (item.getItemId()) {
case R.id.var_menu_add_var:
createEditVariableDialog(null, null, null, null);
result = true;
break;
default:
result = super.onOptionsItemSelected(item);
}
return result;
}
private class VarEditorRemover implements DialogInterface.OnClickListener {
@NotNull
private final Var var;
@Nullable
private final DialogInterface.OnClickListener callbackOnCancel;
private final boolean confirmed;
public VarEditorRemover(@NotNull Var var, @Nullable DialogInterface.OnClickListener callbackOnCancel) {
this(var, callbackOnCancel, false);
}
public VarEditorRemover(@NotNull Var var, @Nullable DialogInterface.OnClickListener callbackOnCancel, boolean confirmed) {
this.var = var;
this.callbackOnCancel = callbackOnCancel;
this.confirmed = confirmed;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (!confirmed) {
final TextView question = new TextView(CalculatorVarsTabActivity.this);
question.setText(String.format(getString(R.string.c_var_removal_confirmation_question), var.getName()));
question.setPadding(6, 6, 6, 6);
final AlertDialog.Builder builder = new AlertDialog.Builder(CalculatorVarsTabActivity.this)
.setCancelable(true)
.setView(question)
.setTitle(R.string.c_var_removal_confirmation)
.setNegativeButton(R.string.c_no, callbackOnCancel)
.setPositiveButton(R.string.c_yes, new VarEditorRemover(var, callbackOnCancel, true));
builder.create().show();
} else {
if (isInCategory(var)) {
getAdapter().remove(var);
}
final AndroidMathRegistry<IConstant> varsRegistry = CalculatorEngine.instance.getVarsRegister();
varsRegistry.remove(var);
varsRegistry.save(CalculatorVarsTabActivity.this);
if (isInCategory(var)) {
CalculatorVarsTabActivity.this.getAdapter().notifyDataSetChanged();
}
}
}
}
}

View File

@ -120,7 +120,7 @@ public enum MathType {
@NotNull @NotNull
@Override @Override
public List<String> getTokens() { public List<String> getTokens() {
return CalculatorEngine.instance.getVarsRegister().getNames(); return CalculatorEngine.instance.getVarsRegistry().getNames();
} }
@Override @Override

View File

@ -4,7 +4,7 @@
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator.math.edit;
import android.app.Activity; import android.app.Activity;
import android.app.ListActivity; import android.app.ListActivity;
@ -17,6 +17,8 @@ import android.view.ViewGroup;
import android.widget.*; import android.widget.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorModel;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidMathRegistry; import org.solovyev.android.calculator.model.AndroidMathRegistry;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.utils.EqualsTool; import org.solovyev.common.utils.EqualsTool;
@ -24,6 +26,7 @@ import org.solovyev.common.utils.Filter;
import org.solovyev.common.utils.FilterRule; import org.solovyev.common.utils.FilterRule;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -36,6 +39,8 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category"; public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category";
protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray()));
@NotNull @NotNull
private MathEntityArrayAdapter<T> adapter; private MathEntityArrayAdapter<T> adapter;
@ -138,7 +143,7 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
protected abstract List<T> getMathEntities(); protected abstract List<T> getMathEntities();
@Nullable @Nullable
protected abstract String getMathEntityCategory(@NotNull T t); abstract String getMathEntityCategory(@NotNull T t);
protected void sort() { protected void sort() {
AbstractMathEntityListActivity.this.adapter.sort(new Comparator<T>() { AbstractMathEntityListActivity.this.adapter.sort(new Comparator<T>() {
@ -151,12 +156,17 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
AbstractMathEntityListActivity.this.adapter.notifyDataSetChanged(); AbstractMathEntityListActivity.this.adapter.notifyDataSetChanged();
} }
protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> { protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> {
@NotNull @NotNull
private final MathEntityDescriptionGetter descriptionGetter; private final MathEntityDescriptionGetter descriptionGetter;
private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter, @NotNull Context context, int resource, int textViewResourceId, @NotNull List<T> objects) { private MathEntityArrayAdapter(@NotNull MathEntityDescriptionGetter descriptionGetter,
@NotNull Context context,
int resource,
int textViewResourceId,
@NotNull List<T> objects) {
super(context, resource, textViewResourceId, objects); super(context, resource, textViewResourceId, objects);
this.descriptionGetter = descriptionGetter; this.descriptionGetter = descriptionGetter;
} }

View File

@ -4,12 +4,13 @@
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator.math.edit;
import android.app.TabActivity; import android.app.TabActivity;
import android.os.Bundle; import android.os.Bundle;
import android.widget.TabHost; import android.widget.TabHost;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
import org.solovyev.android.view.prefs.AndroidUtils; import org.solovyev.android.view.prefs.AndroidUtils;

View File

@ -0,0 +1,160 @@
/*
* 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.math.edit;
import android.os.Bundle;
import jscl.math.function.Function;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.CalculatorEngine;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 10/29/11
* Time: 4:55 PM
*/
public class CalculatorFunctionsTabActivity extends AbstractMathEntityListActivity<Function> {
public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final Function function = (Function) parent.getItemAtPosition(position);
if (function instanceof CustomFunction) {
createEditVariableDialog(CalculatorFunctionsTabActivity.this,
((CustomFunction) function),
function.getName(),
((CustomFunction) function).getContent(),
((CustomFunction) function).getParameterNames(),
null);
}
return true;
}
});*/
/*final Intent intent = getIntent();
if (intent != null) {
final String varValue = intent.getStringExtra(CREATE_FUN_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
createEditVariableDialog(this, null, null, varValue, null, null);
// in order to stop intent for other tabs
intent.removeExtra(CREATE_FUN_EXTRA_STRING);
}
}*/
}
/* private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity<Function> activity,
@Nullable final CustomFunction function,
@Nullable final String name,
@Nullable final String expression,
@Nullable final String[] parameterNames,
@Nullable final String description) {
if (function == null || !function.isSystem()) {
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
final String errorMsg = activity.getString(R.string.c_char_is_not_accepted);
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
editName.setText(name);
editName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!acceptableChars.contains(c)) {
s.delete(i, i + 1);
Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
}
}
}
});
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
if (!StringUtils.isEmpty(expression)) {
editValue.setText(expression);
}
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
editDescription.setText(description);
final CustomFunction.Builder functionBuilder;
if (function != null) {
functionBuilder = new CustomFunction.Builder(function);
} else {
functionBuilder = new CustomFunction.Builder();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setCancelable(true)
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_save, new FunctionEditorSaver(functionBuilder, function, editView, activity, CalculatorEngine.instance.getFunctionsRegistry(), new FunctionEditorSaver.EditorCreator<Function>() {
@Override
public void showEditor(@NotNull AbstractMathEntityListActivity<Function> activity, @Nullable CustomFunction editedInstance, @Nullable String name, @Nullable String value, @Nullable String[] parameterNames, @Nullable String description) {
createEditVariableDialog(activity, editedInstance, name, value, parameterNames, description);
}
}))
.setView(editView);
if (function != null) {
// EDIT mode
builder.setTitle(R.string.c_var_edit_var);
builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<Function>(function, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createEditVariableDialog(activity, function, name, expression, parameterNames, description);
}
}, CalculatorEngine.instance.getFunctionsRegistry(), activity));
} else {
// CREATE mode
builder.setTitle(R.string.c_var_create_var);
}
builder.create().show();
} else {
Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
}*/
@NotNull
@Override
protected MathEntityDescriptionGetter getDescriptionGetter() {
return new MathEntityDescriptionGetterImpl(CalculatorEngine.instance.getFunctionsRegistry());
}
@NotNull
@Override
protected List<Function> getMathEntities() {
return new ArrayList<Function>(CalculatorEngine.instance.getFunctionsRegistry().getEntities());
}
@Override
protected String getMathEntityCategory(@NotNull Function function) {
return CalculatorEngine.instance.getFunctionsRegistry().getCategory(function);
}
}

View File

@ -1,4 +1,4 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator.math.edit;
import android.content.Context; import android.content.Context;
import jscl.math.operator.Operator; import jscl.math.operator.Operator;

View File

@ -4,12 +4,13 @@
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator.math.edit;
import android.app.TabActivity; import android.app.TabActivity;
import android.os.Bundle; import android.os.Bundle;
import android.widget.TabHost; import android.widget.TabHost;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.VarCategory; import org.solovyev.android.calculator.model.VarCategory;
import org.solovyev.android.view.prefs.AndroidUtils; import org.solovyev.android.view.prefs.AndroidUtils;

View File

@ -0,0 +1,215 @@
/*
* 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.math.edit;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Finder;
import org.solovyev.common.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 9/28/11
* Time: 10:55 PM
*/
public class CalculatorVarsTabActivity extends AbstractMathEntityListActivity<IConstant> {
public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity_create_var";
@Override
protected int getLayoutId() {
return R.layout.vars;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final IConstant iConstant = (IConstant) parent.getItemAtPosition(position);
createEditVariableDialog(CalculatorVarsTabActivity.this, iConstant, iConstant.getName(), StringUtils.getNotEmpty(iConstant.getValue(), ""), iConstant.getDescription());
return true;
}
});
final Intent intent = getIntent();
if (intent != null) {
final String varValue = intent.getStringExtra(CREATE_VAR_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
createEditVariableDialog(this, null, null, varValue, null);
// in order to stop intent for other tabs
intent.removeExtra(CREATE_VAR_EXTRA_STRING);
}
}
}
@NotNull
@Override
protected MathEntityDescriptionGetter getDescriptionGetter() {
return new MathEntityDescriptionGetterImpl(CalculatorEngine.instance.getVarsRegistry());
}
@SuppressWarnings({"UnusedDeclaration"})
public void addVarButtonClickHandler(@NotNull View v) {
createEditVariableDialog(this, null, null, null, null);
}
@NotNull
@Override
protected List<IConstant> getMathEntities() {
final List<IConstant> result = new ArrayList<IConstant>(CalculatorEngine.instance.getVarsRegistry().getEntities());
CollectionsUtils.removeAll(result, new Finder<IConstant>() {
@Override
public boolean isFound(@Nullable IConstant var) {
return var != null && CollectionsUtils.contains(var.getName(), MathType.INFINITY_JSCL, MathType.NAN);
}
});
return result;
}
@Override
protected String getMathEntityCategory(@NotNull IConstant var) {
return CalculatorEngine.instance.getVarsRegistry().getCategory(var);
}
private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity<IConstant> activity,
@Nullable final IConstant var,
@Nullable final String name,
@Nullable final String value,
@Nullable final String description) {
if (var == null || !var.isSystem()) {
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
final String errorMsg = activity.getString(R.string.c_char_is_not_accepted);
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
editName.setText(name);
editName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!acceptableChars.contains(c)) {
s.delete(i, i + 1);
Toast.makeText(activity, String.format(errorMsg, c), Toast.LENGTH_SHORT).show();
}
}
}
});
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
if (!StringUtils.isEmpty(value)) {
editValue.setText(value);
}
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
editDescription.setText(description);
final Var.Builder varBuilder;
if (var != null) {
varBuilder = new Var.Builder(var);
} else {
varBuilder = new Var.Builder();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setCancelable(true)
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_save, new VarEditorSaver<IConstant>(varBuilder, var, editView, activity, CalculatorEngine.instance.getVarsRegistry(), new VarEditorSaver.EditorCreator<IConstant>() {
@Override
public void showEditor(@NotNull AbstractMathEntityListActivity<IConstant> activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) {
createEditVariableDialog(activity, editedInstance, name, value, description);
}
}))
.setView(editView);
if (var != null) {
// EDIT mode
builder.setTitle(R.string.c_var_edit_var);
builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<IConstant>(var, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createEditVariableDialog(activity, var, name, value, description);
}
}, CalculatorEngine.instance.getVarsRegistry(), activity));
} else {
// CREATE mode
builder.setTitle(R.string.c_var_create_var);
}
builder.create().show();
} else {
Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
}
public static boolean isValidValue(@NotNull String value) {
// now every string might be constant
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.var_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean result;
switch (item.getItemId()) {
case R.id.var_menu_add_var:
createEditVariableDialog(this, null, null, null, null);
result = true;
break;
default:
result = super.onOptionsItemSelected(item);
}
return result;
}
}

View File

@ -0,0 +1,173 @@
/*
* 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.math.edit;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.text.Identifier;
import jscl.text.MutableInt;
import jscl.text.ParseException;
import jscl.text.Parser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.AndroidMathRegistry;
import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.utils.StringUtils;
/**
* User: serso
* Date: 12/22/11
* Time: 11:26 PM
*/
public class FunctionEditorSaver implements DialogInterface.OnClickListener{
public static interface EditorCreator<T extends MathEntity> {
void showEditor(@NotNull AbstractMathEntityListActivity<T> activity,
@Nullable CustomFunction editedInstance,
@Nullable String name,
@Nullable String value,
@Nullable String[] parameterNames,
@Nullable String description);
}
@NotNull
private final EditorCreator<Function> editorCreator;
@NotNull
private final MathEntityBuilder<CustomFunction> varBuilder;
@Nullable
private final CustomFunction editedInstance;
@NotNull
private final AndroidMathRegistry<Function> mathRegistry;
@NotNull
private final AbstractMathEntityListActivity<Function> activity;
@NotNull
private View editView;
public FunctionEditorSaver(@NotNull MathEntityBuilder<CustomFunction> varBuilder,
@Nullable CustomFunction editedInstance,
@NotNull View editView,
@NotNull AbstractMathEntityListActivity<Function> activity,
@NotNull AndroidMathRegistry<Function> mathRegistry,
@NotNull EditorCreator<Function> editorCreator) {
this.varBuilder = varBuilder;
this.editedInstance = editedInstance;
this.editView = editView;
this.activity = activity;
this.mathRegistry = mathRegistry;
this.editorCreator = editorCreator;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
final Integer error;
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
String name = editName.getText().toString();
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
String value = editValue.getText().toString();
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
String description = editDescription.getText().toString();
if (isValidName(name)) {
boolean canBeSaved = false;
final Function entityFromRegistry = mathRegistry.get(name);
if (entityFromRegistry == null) {
canBeSaved = true;
} else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) {
canBeSaved = true;
}
if (canBeSaved) {
final MathType.Result mathType = MathType.getType(name, 0, false);
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
if (StringUtils.isEmpty(value)) {
// value is empty => undefined variable
varBuilder.setName(name);
varBuilder.setDescription(description);
varBuilder.setValue(null);
error = null;
} else {
// value is not empty => must be a number
boolean valid = CalculatorVarsTabActivity.isValidValue(value);
if (valid) {
varBuilder.setName(name);
varBuilder.setDescription(description);
varBuilder.setValue(value);
error = null;
} else {
error = R.string.c_value_is_not_a_number;
}
}
} else {
error = R.string.c_var_name_clashes;
}
} else {
error = R.string.c_var_already_exists;
}
} else {
error = R.string.c_name_is_not_valid;
}
if (error != null) {
Toast.makeText(activity, activity.getString(error), Toast.LENGTH_LONG).show();
editorCreator.showEditor(activity, editedInstance, name, value, null, description);
} else {
final Function addedVar = mathRegistry.add(varBuilder);
if (activity.isInCategory(addedVar)) {
if (editedInstance != null) {
activity.getAdapter().remove(editedInstance);
}
activity.getAdapter().add(addedVar);
}
mathRegistry.save(activity);
if (activity.isInCategory(addedVar)) {
activity.sort();
}
}
}
}
boolean isValidName(@Nullable String name) {
boolean result = false;
if (!StringUtils.isEmpty(name)) {
try {
assert name != null;
Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), CalculatorEngine.instance.getEngine()), null);
result = true;
} catch (ParseException e) {
// not valid name;
}
}
return result;
}
}

View File

@ -0,0 +1,84 @@
/*
* 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.math.edit;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidMathRegistry;
import org.solovyev.common.math.MathEntity;
/**
* User: serso
* Date: 12/22/11
* Time: 9:36 PM
*/
class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClickListener {
@NotNull
private final T mathEntity;
@Nullable
private final DialogInterface.OnClickListener callbackOnCancel;
private final boolean confirmed;
@NotNull
private final AndroidMathRegistry<? super T> varsRegistry;
@NotNull
private final AbstractMathEntityListActivity<T> activity;
public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel,
@NotNull AndroidMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListActivity<T> activity) {
this(mathEntity, callbackOnCancel, false, varsRegistry, activity);
}
public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel,
boolean confirmed,
@NotNull AndroidMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListActivity<T> activity) {
this.mathEntity = mathEntity;
this.callbackOnCancel = callbackOnCancel;
this.confirmed = confirmed;
this.varsRegistry = varsRegistry;
this.activity = activity;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (!confirmed) {
final TextView question = new TextView(activity);
question.setText(String.format(activity.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName()));
question.setPadding(6, 6, 6, 6);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setCancelable(true)
.setView(question)
.setTitle(R.string.c_var_removal_confirmation)
.setNegativeButton(R.string.c_no, callbackOnCancel)
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, activity));
builder.create().show();
} else {
if (activity.isInCategory(mathEntity)) {
activity.getAdapter().remove(mathEntity);
}
varsRegistry.remove(mathEntity);
varsRegistry.save(activity);
if (activity.isInCategory(mathEntity)) {
activity.getAdapter().notifyDataSetChanged();
}
}
}
}

View File

@ -0,0 +1,169 @@
/*
* 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.math.edit;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import jscl.text.Identifier;
import jscl.text.MutableInt;
import jscl.text.ParseException;
import jscl.text.Parser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.AndroidMathRegistry;
import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.MathEntityBuilder;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.utils.StringUtils;
/**
* User: serso
* Date: 12/22/11
* Time: 9:52 PM
*/
class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickListener {
public static interface EditorCreator<T extends MathEntity> {
void showEditor(@NotNull AbstractMathEntityListActivity<T> activity,
@Nullable T editedInstance,
@Nullable String name,
@Nullable String value,
@Nullable String description);
}
@NotNull
private final EditorCreator<T> editorCreator;
@NotNull
private final MathEntityBuilder<? extends T> varBuilder;
@Nullable
private final T editedInstance;
@NotNull
private final AndroidMathRegistry<T> mathRegistry;
@NotNull
private final AbstractMathEntityListActivity<T> activity;
@NotNull
private View editView;
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
@Nullable T editedInstance,
@NotNull View editView,
@NotNull AbstractMathEntityListActivity<T> activity,
@NotNull AndroidMathRegistry<T> mathRegistry,
@NotNull EditorCreator<T> editorCreator) {
this.varBuilder = varBuilder;
this.editedInstance = editedInstance;
this.editView = editView;
this.activity = activity;
this.mathRegistry = mathRegistry;
this.editorCreator = editorCreator;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
final Integer error;
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
String name = editName.getText().toString();
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_value);
String value = editValue.getText().toString();
final EditText editDescription = (EditText) editView.findViewById(R.id.var_edit_description);
String description = editDescription.getText().toString();
if (isValidName(name)) {
boolean canBeSaved = false;
final T entityFromRegistry = mathRegistry.get(name);
if (entityFromRegistry == null) {
canBeSaved = true;
} else if (editedInstance != null && entityFromRegistry.getId().equals(editedInstance.getId())) {
canBeSaved = true;
}
if (canBeSaved) {
final MathType.Result mathType = MathType.getType(name, 0, false);
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
if (StringUtils.isEmpty(value)) {
// value is empty => undefined variable
varBuilder.setName(name);
varBuilder.setDescription(description);
varBuilder.setValue(null);
error = null;
} else {
// value is not empty => must be a number
boolean valid = CalculatorVarsTabActivity.isValidValue(value);
if (valid) {
varBuilder.setName(name);
varBuilder.setDescription(description);
varBuilder.setValue(value);
error = null;
} else {
error = R.string.c_value_is_not_a_number;
}
}
} else {
error = R.string.c_var_name_clashes;
}
} else {
error = R.string.c_var_already_exists;
}
} else {
error = R.string.c_name_is_not_valid;
}
if (error != null) {
Toast.makeText(activity, activity.getString(error), Toast.LENGTH_LONG).show();
editorCreator.showEditor(activity, editedInstance, name, value, description);
} else {
final T addedVar = mathRegistry.add(varBuilder);
if (activity.isInCategory(addedVar)) {
if (editedInstance != null) {
activity.getAdapter().remove(editedInstance);
}
activity.getAdapter().add(addedVar);
}
mathRegistry.save(activity);
if (activity.isInCategory(addedVar)) {
activity.sort();
}
}
}
}
boolean isValidName(@Nullable String name) {
boolean result = false;
if (!StringUtils.isEmpty(name)) {
try {
assert name != null;
Identifier.parser.parse(Parser.Parameters.newInstance(name, new MutableInt(0), CalculatorEngine.instance.getEngine()), null);
result = true;
} catch (ParseException e) {
// not valid name;
}
}
return result;
}
}

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.model; package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View File

@ -6,11 +6,8 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import android.content.Context;
import android.content.SharedPreferences;
import jscl.math.function.*; import jscl.math.function.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.common.definitions.IBuilder; import org.solovyev.common.definitions.IBuilder;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
@ -50,8 +47,15 @@ public class AndroidFunctionsMathRegistry extends AbstractAndroidMathRegistry<Fu
return function instanceof Comparison; return function instanceof Comparison;
} }
}, },
my(R.string.c_fun_category_my, 0) {
@Override
boolean isInCategory(@NotNull Function function) {
return !function.isSystem();
}
},
common(R.string.c_fun_category_common, 0) { common(R.string.c_fun_category_common, 100) {
@Override @Override
boolean isInCategory(@NotNull Function function) { boolean isInCategory(@NotNull Function function) {
for (Category category : values()) { for (Category category : values()) {

View File

@ -65,7 +65,7 @@ public enum CalculatorEngine {
public final TextProcessor<PreparedExpression, String> preprocessor = new ToJsclTextProcessor(); public final TextProcessor<PreparedExpression, String> preprocessor = new ToJsclTextProcessor();
@NotNull @NotNull
private final AndroidMathRegistry<IConstant> varsRegister = new AndroidVarsRegistryImpl(engine.getConstantsRegistry()); private final AndroidMathRegistry<IConstant> varsRegistry = new AndroidVarsRegistryImpl(engine.getConstantsRegistry());
@NotNull @NotNull
private final AndroidMathRegistry<jscl.math.function.Function> functionsRegistry = new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry()); private final AndroidMathRegistry<jscl.math.function.Function> functionsRegistry = new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry());
@ -283,7 +283,7 @@ public enum CalculatorEngine {
} }
} }
varsRegister.load(context, preferences); varsRegistry.load(context, preferences);
} }
} }
@ -305,8 +305,8 @@ public enum CalculatorEngine {
} }
@NotNull @NotNull
public AndroidMathRegistry<IConstant> getVarsRegister() { public AndroidMathRegistry<IConstant> getVarsRegistry() {
return varsRegister; return varsRegistry;
} }
@NotNull @NotNull

View File

@ -0,0 +1,29 @@
/*
* 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.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.definitions.IBuilder;
import org.solovyev.common.math.MathEntity;
/**
* User: serso
* Date: 12/22/11
* Time: 9:21 PM
*/
public interface MathEntityBuilder<T extends MathEntity> extends IBuilder<T> {
@NotNull
public MathEntityBuilder<T> setName(@NotNull String name);
@NotNull
public MathEntityBuilder<T> setDescription(@Nullable String description);
@NotNull
public MathEntityBuilder<T> setValue(@Nullable String value);
}

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.model; package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View File

@ -88,9 +88,9 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
if (functionName == null) { if (functionName == null) {
String operatorName = CollectionsUtils.find(MathType.operator.getTokens(), startsWithFinder); String operatorName = CollectionsUtils.find(MathType.operator.getTokens(), startsWithFinder);
if (operatorName == null) { if (operatorName == null) {
String varName = CollectionsUtils.find(CalculatorEngine.instance.getVarsRegister().getNames(), startsWithFinder); String varName = CollectionsUtils.find(CalculatorEngine.instance.getVarsRegistry().getNames(), startsWithFinder);
if (varName != null) { if (varName != null) {
final IConstant var = CalculatorEngine.instance.getVarsRegister().get(varName); final IConstant var = CalculatorEngine.instance.getVarsRegistry().get(varName);
if (var != null) { if (var != null) {
if (!var.isDefined()) { if (!var.isDefined()) {
undefinedVars.add(var); undefinedVars.add(var);

View File

@ -48,7 +48,7 @@ public class Var implements IConstant, MathPersistenceEntity {
@Transient @Transient
private Constant constant; private Constant constant;
public static class Builder implements IBuilder<Var> { public static class Builder implements IBuilder<Var>, MathEntityBuilder<Var> {
@NotNull @NotNull
private String name; private String name;
@ -97,12 +97,16 @@ public class Var implements IConstant, MathPersistenceEntity {
} }
public void setName(@NotNull String name) { @NotNull
public Builder setName(@NotNull String name) {
this.name = name; this.name = name;
return this;
} }
public void setValue(@Nullable String value) { @NotNull
public Builder setValue(@Nullable String value) {
this.value = value; this.value = value;
return this;
} }
protected Builder setSystem(boolean system) { protected Builder setSystem(boolean system) {
@ -110,6 +114,7 @@ public class Var implements IConstant, MathPersistenceEntity {
return this; return this;
} }
@NotNull
public Builder setDescription(@Nullable String description) { public Builder setDescription(@Nullable String description) {
this.description = description; this.description = description;
return this; return this;

View File

@ -178,7 +178,7 @@ public class CalculatorEngineTest {
} }
junit.framework.Assert.assertEquals("24i", cm.evaluate(JsclOperation.numeric, "4!i").getResult()); junit.framework.Assert.assertEquals("24i", cm.evaluate(JsclOperation.numeric, "4!i").getResult());
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("si", 5d)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("si", 5d));
try { try {
cm.getEngine().setAngleUnits(AngleUnit.rad); cm.getEngine().setAngleUnits(AngleUnit.rad);
@ -192,14 +192,14 @@ public class CalculatorEngineTest {
cm.getEngine().setAngleUnits(defaultAngleUnit); cm.getEngine().setAngleUnits(defaultAngleUnit);
} }
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("s", 1d)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("s", 1d));
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult()); Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("k", 3.5d)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("k", 3.5d));
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("k1", 4d)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("k1", 4d));
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11").getResult()); Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11").getResult());
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", (String) null)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String) null));
Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getResult()); Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getResult());
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getResult()); Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getResult());
Assert.assertEquals("", cm.evaluate(JsclOperation.numeric, "").getResult()); Assert.assertEquals("", cm.evaluate(JsclOperation.numeric, "").getResult());
@ -245,10 +245,10 @@ public class CalculatorEngineTest {
cm.setTimeout(3000); cm.setTimeout(3000);
}*/ }*/
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", (String) null)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String) null));
Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getResult()); Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getResult());
Assert.assertEquals("2t", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getResult()); Assert.assertEquals("2t", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getResult());
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", "2")); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", "2"));
Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getResult()); Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getResult());
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getResult()); Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getResult());
} }
@ -301,7 +301,7 @@ public class CalculatorEngineTest {
cm.getEngine().setAngleUnits(defaultAngleUnit); cm.getEngine().setAngleUnits(defaultAngleUnit);
} }
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("si", 5d)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("si", 5d));
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult()); Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
try { try {

View File

@ -33,15 +33,15 @@ public class FromJsclSimplifyTextProcessorTest {
//Assert.assertEquals("e", tp.process("2.718281828459045")); //Assert.assertEquals("e", tp.process("2.718281828459045"));
//Assert.assertEquals("tee", tp.process("t2.718281828459045*2.718281828459045")); //Assert.assertEquals("tee", tp.process("t2.718281828459045*2.718281828459045"));
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t2.718281828459045", "2")); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t2.718281828459045", "2"));
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", (String)null)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String)null));
//Assert.assertEquals("t2.718281828459045e", tp.process("t2.718281828459045*2.718281828459045")); //Assert.assertEquals("t2.718281828459045e", tp.process("t2.718281828459045*2.718281828459045"));
//Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045")); //Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045"));
Assert.assertEquals("t×", tp.process("t*")); Assert.assertEquals("t×", tp.process("t*"));
Assert.assertEquals("×t", tp.process("*t")); Assert.assertEquals("×t", tp.process("*t"));
Assert.assertEquals("t2", tp.process("t*2")); Assert.assertEquals("t2", tp.process("t*2"));
Assert.assertEquals("2t", tp.process("2*t")); Assert.assertEquals("2t", tp.process("2*t"));
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("t", (String) null)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String) null));
Assert.assertEquals("t×", tp.process("t*")); Assert.assertEquals("t×", tp.process("t*"));
Assert.assertEquals("×t", tp.process("*t")); Assert.assertEquals("×t", tp.process("*t"));
@ -54,7 +54,7 @@ public class FromJsclSimplifyTextProcessorTest {
Assert.assertEquals("t^[2×2t]", tp.process("t^[2*2*t]")); Assert.assertEquals("t^[2×2t]", tp.process("t^[2*2*t]"));
Assert.assertEquals("2t^2[2t]", tp.process("2*t^2[2*t]")); Assert.assertEquals("2t^2[2t]", tp.process("2*t^2[2*t]"));
CalculatorEngine.instance.getVarsRegister().add(new Var.Builder("k", (String) null)); CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("k", (String) null));
Assert.assertEquals("(t+2k)[k+2t]", tp.process("(t+2*k)*[k+2*t]")); Assert.assertEquals("(t+2k)[k+2t]", tp.process("(t+2*k)*[k+2*t]"));
Assert.assertEquals("(te+2k)e[k+2te]", tp.process("(t*e+2*k)*e*[k+2*t*e]")); Assert.assertEquals("(te+2k)e[k+2te]", tp.process("(t*e+2*k)*e*[k+2*t*e]"));