var activity

This commit is contained in:
serso 2011-10-02 14:12:13 +04:00
parent 9dcbd4633b
commit f6c20191d7
16 changed files with 312 additions and 90 deletions

View File

@ -29,4 +29,13 @@
</LinearLayout>
<TextView a:id="@+id/var_description"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:paddingBottom="6dp"
a:paddingLeft="6dp"
a:paddingRight="6dp"
a:paddingTop="0dp">
</TextView>
</LinearLayout>

View File

@ -16,7 +16,7 @@
a:layout_width="wrap_content"
a:text="Name"/>
<EditText a:id="@+id/var_name"
<EditText a:id="@+id/var_edit_name"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:textSize="20dp">
@ -26,7 +26,7 @@
a:layout_width="wrap_content"
a:text="Value"/>
<EditText a:id="@+id/var_value"
<EditText a:id="@+id/var_edit_value"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:inputType="number"
@ -37,7 +37,7 @@
a:layout_width="wrap_content"
a:text="Description"/>
<EditText a:id="@+id/var_description"
<EditText a:id="@+id/var_edit_description"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:textSize="20dp">

View File

@ -2,9 +2,12 @@
<menu xmlns:a="http://schemas.android.com/apk/res/android">
<item a:id="@+id/menu_item_settings"
<item a:id="@+id/main_menu_item_settings"
a:title="@string/c_settings"/>
<item a:id="@+id/menu_item_about"
<item a:id="@+id/main_menu_item_about"
a:title="@string/c_about"/>
<item a:id="@+id/main_menu_item_exit"
a:title="@string/c_exit"/>
</menu>

13
res/menu/var_menu.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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
-->
<menu xmlns:a="http://schemas.android.com/apk/res/android">
<item a:id="@+id/var_menu_add_var"
a:title="@string/c_add"/>
</menu>

View File

@ -1,4 +1,5 @@
<resources>
<color name="magic_flame">#00ffffff</color>
<color name="button_text">#ffffffff</color>
<color name="button_text_color">#ffffffff</color>
<color name="text_color">#ffffffff</color>
</resources>

View File

@ -33,4 +33,8 @@
<string name="c_calc_color_display_title">Color expressions</string>
<string name="p_calc_result_precision_title">Precision of result</string>
<string name="c_exit">Exit</string>
<string name="c_add">Add</string>
<string name="c_cancel">Cancel</string>
<string name="c_save">Save</string>
</resources>

View File

@ -29,7 +29,7 @@
<style name="display_style">
<item name="android:textSize">25dp</item>
<item name="android:background">#000000</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textColor">@color/text_color</item>
<item name="android:gravity">left|top</item>
<item name="android:padding">5dp</item>
<item name="android:layout_width">match_parent</item>

View File

@ -234,14 +234,18 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
boolean result;
switch (item.getItemId()) {
case R.id.menu_item_settings:
case R.id.main_menu_item_settings:
showSettings();
result = true;
break;
case R.id.menu_item_about:
case R.id.main_menu_item_about:
showAbout();
result = true;
break;
case R.id.main_menu_item_exit:
this.finish();
result = true;
break;
default:
result = super.onOptionsItemSelected(item);
}

View File

@ -5,9 +5,7 @@
package org.solovyev.android.calculator;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
/**
@ -21,6 +19,6 @@ public class CalculatorPreferencesActivity extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
addPreferencesFromResource(R.xml.main_preferences);
}
}

View File

@ -12,10 +12,10 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.*;
import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.utils.StringUtils;
import java.util.ArrayList;
@ -26,13 +26,17 @@ import java.util.List;
* Date: 9/28/11
* Time: 10:55 PM
*/
public class CalculatorVarsActivity extends ListActivity implements DialogInterface.OnClickListener {
public class CalculatorVarsActivity extends ListActivity {
@NotNull
private VarsArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars())));
adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars()));
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
@ -41,23 +45,8 @@ public class CalculatorVarsActivity extends ListActivity implements DialogInterf
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
final Var var = (Var) parent.getItemAtPosition(position);
final EditText editName = (EditText)editView.findViewById(R.id.var_name);
editName.setText(var.getName());
final EditText editValue = (EditText)editView.findViewById(R.id.var_value);
editValue.setText(var.getValue());
final EditText editDescription = (EditText)editView.findViewById(R.id.var_description);
editDescription.setText(var.getDescription());
new AlertDialog.Builder(CalculatorVarsActivity.this)
.setCancelable(true)
.setPositiveButton("Save", CalculatorVarsActivity.this)
.setView(editView).create().show();
createEditVariableDialog(var, var.getName(), var.getValue(), var.getDescription());
return true;
}
});
@ -75,9 +64,118 @@ public class CalculatorVarsActivity extends ListActivity implements DialogInterf
}
private void createEditVariableDialog(@Nullable Var var, @Nullable String name, @Nullable String value, @Nullable 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 EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
editName.setText(name);
final EditText editValue = (EditText) editView.findViewById(R.id.var_edit_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();
}
new AlertDialog.Builder(this)
.setCancelable(true)
.setNegativeButton(getString(R.string.c_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//To change body of implemented methods use File | Settings | File Templates.
// do nothing
}
})
.setPositiveButton(getString(R.string.c_save), new VarEditorSaver(varBuilder, var, editView))
.setView(editView).create().show();
} else {
Toast.makeText(this, "System variable 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 String 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 VarsRegister varsRegister = CalculatorModel.getInstance().getVarsRegister();
if (!StringUtils.isEmpty(name)) {
final Var varFromRegister = varsRegister.getVar(name);
if (varFromRegister == null || varFromRegister == editedInstance) {
boolean correctDouble = true;
try {
Double.valueOf(value);
} catch (NumberFormatException e) {
correctDouble = false;
}
if (correctDouble) {
varBuilder.setName(name);
varBuilder.setValue(value);
varBuilder.setDescription(description);
error = null;
} else {
error = "Value is not a number!";
}
} else {
error = "Variable with same name already exist!";
}
} else {
error = "Name is empty!";
}
if (error != null) {
Toast.makeText(CalculatorVarsActivity.this, error, Toast.LENGTH_LONG).show();
createEditVariableDialog(editedInstance, name, value, description);
} else {
if (editedInstance != null && !editedInstance.getName().equals(name)) {
varsRegister.addVar(editedInstance.getName(), varBuilder);
} else {
CalculatorVarsActivity.this.adapter.add(varsRegister.addVar(null, varBuilder));
}
varsRegister.save(CalculatorVarsActivity.this);
CalculatorVarsActivity.this.adapter.notifyDataSetChanged();
}
}
}
}
private class VarsArrayAdapter extends ArrayAdapter<Var> {
@ -86,26 +184,54 @@ public class CalculatorVarsActivity extends ListActivity implements DialogInterf
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);
if (convertView == null) {
final Var var = getItem(position);
if (!StringUtils.isEmpty(var.getDescription())) {
final TextView description = new TextView(getContext());
description.setText(var.getDescription());
description.setPadding(6, 0, 6, 6);
result.addView(description, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView description = (TextView) result.findViewById(R.id.var_description);
if (description == null) {
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final ViewGroup itemView = (ViewGroup) layoutInflater.inflate(R.layout.var, null);
description = (TextView) itemView.findViewById(R.id.var_description);
itemView.removeView(description);
result.addView(description);
}
description.setText(var.getDescription());
} else {
TextView description = (TextView) result.findViewById(R.id.var_description);
if (description != null) {
result.removeView(description);
}
}
return result;
}
}
@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;
}
}
}

View File

@ -28,23 +28,83 @@ public class Var {
@NotNull
private String value;
@Element
private boolean system;
@Element(required = false)
@Nullable
private String description;
public Var() {
public static class Builder {
@NotNull
private String name;
@NotNull
private String value;
private boolean system = false;
@Nullable
private String description;
public Builder() {
}
public Var(@NotNull String name, @NotNull Double value, boolean system) {
this(name, String.valueOf(value), system);
public Builder(@NotNull Var var) {
this.name = var.name;
this.value = var.value;
this.system = var.system;
this.description = var.description;
}
public Var(@NotNull String name, @NotNull String value, boolean system) {
public Builder(@NotNull String name, @NotNull Double value) {
this(name, String.valueOf(value));
}
public Builder(@NotNull String name, @NotNull String value) {
this.name = name;
this.value = value;
}
public void setName(@NotNull String name) {
this.name = name;
}
public void setValue(@NotNull String value) {
this.value = value;
}
protected Builder setSystem(boolean system) {
this.system = system;
return this;
}
public Builder setDescription(@Nullable String description) {
this.description = description;
return this;
}
protected Var create () {
final Var var = new Var();
var.name = name;
var.value = value;
var.system = system;
var.description = description;
return var;
}
}
private Var() {
}
public void copy(@NotNull Var var) {
this.name = var.name;
this.value = var.value;
this.description = var.description;
this.system = var.system;
}
@NotNull
@ -52,27 +112,15 @@ public class Var {
return value;
}
public void setValue(@NotNull String value) {
this.value = value;
}
public boolean isSystem() {
return system;
}
public void setSystem(boolean system) {
this.system = system;
}
@NotNull
public String getName() {
return name;
}
public void setName(@NotNull String name) {
this.name = name;
}
@Nullable
public String getDescription() {
return description;

View File

@ -16,7 +16,7 @@ import java.util.List;
public class Vars {
@ElementList
private final List<Var> vars = new ArrayList<Var>();
private List<Var> vars = new ArrayList<Var>();
public Vars() {
}

View File

@ -43,6 +43,19 @@ public class VarsRegister {
return Collections.unmodifiableList(systemVars);
}
public Var addVar(@Nullable String name, @NotNull Var.Builder builder) {
final Var var = builder.create();
final Var varFromRegister = getVar(name == null ? var.getName() : name);
if (varFromRegister == null) {
vars.add(var);
} else {
varFromRegister.copy(var);
}
return var;
}
@Nullable
public Var getVar(@NotNull final String name) {
return CollectionsUtils.get(vars, new Finder<Var>() {
@ -53,6 +66,15 @@ public class VarsRegister {
});
}
public boolean contains(@NotNull final String name) {
return CollectionsUtils.get(vars, new Finder<Var>() {
@Override
public boolean isFound(@Nullable Var var) {
return var != null && name.equals(var.getName());
}
}) != null;
}
public void merge(@NotNull final List<Var> varsParam) {
final Set<Var> result = new HashSet<Var>(varsParam);
@ -71,17 +93,6 @@ public class VarsRegister {
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);
@ -98,7 +109,18 @@ public class VarsRegister {
}
for (Var systemVar : MathEntityType.constants) {
for (String systemVarName : MathEntityType.constants) {
final Var systemVar;
if ( systemVarName.equals("e") ){
systemVar = new Var.Builder(systemVarName, Math.E).setSystem(true).create();
} else if (systemVarName.equals("π")) {
systemVar = new Var.Builder(systemVarName, Math.PI).setSystem(true).create();
} else if (systemVarName.equals("i")) {
systemVar = new Var.Builder(systemVarName, "√(-1)").setSystem(true).create();
} else {
throw new IllegalArgumentException(systemVarName + " is not supported yet!");
}
systemVars.add(systemVar);
if (!vars.contains(systemVar)) {

View File

@ -29,14 +29,7 @@ public enum MathEntityType {
group_symbols,
group_symbol;
public static final List<Var> constants;
static {
final List<Var> result = new ArrayList<Var>();
result.add(new Var("e", Math.exp(1), true));
result.add(new Var("π", Math.PI, true));
result.add(new Var("i", "√(-1)", true));
constants = Collections.unmodifiableList(result);
}
public static final List<String> constants = Arrays.asList("e", "π", "i");
public static final List<Character> dots = Arrays.asList('.');

View File

@ -30,6 +30,7 @@ import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R;
import org.solovyev.android.view.FontSizeAdjuster;
import org.solovyev.common.utils.Point2d;
@ -68,7 +69,7 @@ public class ColorButton extends Button {
mFeedbackPaint = new Paint();
mFeedbackPaint.setStyle(Style.STROKE);
mFeedbackPaint.setStrokeWidth(2);
getPaint().setColor(res.getColor(org.solovyev.android.calculator.R.color.button_text));
getPaint().setColor(res.getColor(R.color.button_text_color));
mAnimStart = -1;