From f2d260706e3b9dd8c5d92cb9675815d95dec37ae Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 2 Oct 2011 22:44:11 +0400 Subject: [PATCH] var activity --- AndroidManifest.xml | 4 +- res/layout/var_edit.xml | 73 ++++++++++-------- res/values/strings.xml | 10 +++ .../calculator/CalculatorVarsActivity.java | 77 ++++++++++++++++--- .../android/calculator/VarsRegister.java | 4 + 5 files changed, 126 insertions(+), 42 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index ddac321a..187f8c57 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -19,7 +19,9 @@ - + \ No newline at end of file diff --git a/res/layout/var_edit.xml b/res/layout/var_edit.xml index f5350a67..c7910d30 100644 --- a/res/layout/var_edit.xml +++ b/res/layout/var_edit.xml @@ -6,41 +6,52 @@ ~ or visit http://se.solovyev.org --> - + - + - - + - + + - - + - + + - - + - \ No newline at end of file + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index cce93c68..71633cc2 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -37,4 +37,14 @@ Add Cancel Save + Remove + Yes + No + Removal confirmation + Do you really want to delete \'%s\' variable? + Name + value + Description + Create variable + Edit variable diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java index 8f5f065f..3a898056 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java @@ -64,7 +64,7 @@ public class CalculatorVarsActivity extends ListActivity { } - private void createEditVariableDialog(@Nullable Var var, @Nullable String name, @Nullable String value, @Nullable String description) { + 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); @@ -86,16 +86,29 @@ public class CalculatorVarsActivity extends ListActivity { varBuilder = new Var.Builder(); } - new AlertDialog.Builder(this) + final AlertDialog.Builder builder = new AlertDialog.Builder(this) .setCancelable(true) - .setNegativeButton(getString(R.string.c_cancel), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // do nothing - } - }) - .setPositiveButton(getString(R.string.c_save), new VarEditorSaver(varBuilder, var, editView)) - .setView(editView).create().show(); + .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, "System variable cannot be changed!", Toast.LENGTH_LONG).show(); } @@ -234,4 +247,48 @@ public class CalculatorVarsActivity extends ListActivity { 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(CalculatorVarsActivity.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(CalculatorVarsActivity.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 { + adapter.remove(var); + final VarsRegister varsRegister = CalculatorModel.getInstance().getVarsRegister(); + varsRegister.remove(var); + varsRegister.save(CalculatorVarsActivity.this); + CalculatorVarsActivity.this.adapter.notifyDataSetChanged(); + } + } + } } diff --git a/src/main/java/org/solovyev/android/calculator/VarsRegister.java b/src/main/java/org/solovyev/android/calculator/VarsRegister.java index a5032558..9441d79e 100644 --- a/src/main/java/org/solovyev/android/calculator/VarsRegister.java +++ b/src/main/java/org/solovyev/android/calculator/VarsRegister.java @@ -56,6 +56,10 @@ public class VarsRegister { return var; } + public void remove (@NotNull Var var) { + this.vars.remove(var); + } + @Nullable public Var getVar(@NotNull final String name) { return CollectionsUtils.get(vars, new Finder() {