From de52ca451273f8b600e8daf01d6dc079f6eb0c0c Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 12 Oct 2012 11:20:39 +0400 Subject: [PATCH 1/3] Matrix support --- calculatorpp/AndroidManifest.xml | 3 + calculatorpp/res/layout/calc_vars_button.xml | 33 +- .../res/layout/matrix_edit_fragment.xml | 35 ++ .../res/layout/matrix_edit_fragment_item.xml | 12 + .../android/calculator/CalculatorButtons.java | 481 +++++++++--------- .../about/CalculatorFragmentType.java | 156 +++--- .../matrix/CalculatorMatrixActivity.java | 23 + .../matrix/CalculatorMatrixEditFragment.java | 83 +++ 8 files changed, 496 insertions(+), 330 deletions(-) create mode 100644 calculatorpp/res/layout/matrix_edit_fragment.xml create mode 100644 calculatorpp/res/layout/matrix_edit_fragment_item.xml create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixActivity.java create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java diff --git a/calculatorpp/AndroidManifest.xml b/calculatorpp/AndroidManifest.xml index ee1ce70d..86e51de9 100644 --- a/calculatorpp/AndroidManifest.xml +++ b/calculatorpp/AndroidManifest.xml @@ -37,6 +37,9 @@ + + + diff --git a/calculatorpp/res/layout/calc_vars_button.xml b/calculatorpp/res/layout/calc_vars_button.xml index 0005f0f3..7d23094c 100644 --- a/calculatorpp/res/layout/calc_vars_button.xml +++ b/calculatorpp/res/layout/calc_vars_button.xml @@ -1,17 +1,18 @@ - - - - - + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/matrix_edit_fragment.xml b/calculatorpp/res/layout/matrix_edit_fragment.xml new file mode 100644 index 00000000..2dc0b9b6 --- /dev/null +++ b/calculatorpp/res/layout/matrix_edit_fragment.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/res/layout/matrix_edit_fragment_item.xml b/calculatorpp/res/layout/matrix_edit_fragment_item.xml new file mode 100644 index 00000000..880d08b1 --- /dev/null +++ b/calculatorpp/res/layout/matrix_edit_fragment_item.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java index 27ae4778..3f63389a 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java @@ -1,238 +1,243 @@ -package org.solovyev.android.calculator; - -import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Configuration; -import android.preference.PreferenceManager; -import android.util.Log; -import android.view.MotionEvent; -import android.view.View; -import android.widget.Button; -import android.widget.Toast; -import jscl.AngleUnit; -import jscl.NumeralBase; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.AndroidUtils; -import org.solovyev.android.calculator.model.AndroidCalculatorEngine; -import org.solovyev.android.calculator.view.AngleUnitsButton; -import org.solovyev.android.calculator.view.NumeralBasesButton; -import org.solovyev.android.view.ColorButton; -import org.solovyev.android.view.drag.DragButton; -import org.solovyev.android.view.drag.DragDirection; -import org.solovyev.android.view.drag.SimpleOnDragListener; -import org.solovyev.common.math.Point2d; - -/** - * User: serso - * Date: 9/28/12 - * Time: 12:06 AM - */ -public final class CalculatorButtons { - - private CalculatorButtons () { - } - - - public static void processButtons(boolean fixMagicFlames, - @NotNull CalculatorPreferences.Gui.Theme theme, - @NotNull View root) { - if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { - - if (fixMagicFlames) { - // for metro themes we should turn off magic flames - AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor() { - @Override - public void process(@NotNull ColorButton colorButton) { - colorButton.setDrawMagicFlame(false); - } - }); - } - } - } - - static void initMultiplicationButton(@NotNull View root) { - final View multiplicationButton = root.findViewById(R.id.multiplicationButton); - if ( multiplicationButton instanceof Button) { - ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); - } - } - - - - public static void toggleEqualsButton(@Nullable SharedPreferences preferences, - @NotNull Activity activity) { - preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences; - - final boolean large = AndroidUtils.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, activity.getResources().getConfiguration()); - - if (!large) { - if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT - || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { - - final DragButton equalsButton = (DragButton)activity.findViewById(R.id.equalsButton); - if (equalsButton != null) { - if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) { - equalsButton.setVisibility(View.VISIBLE); - } else { - equalsButton.setVisibility(View.GONE); - } - } - } - } - } - - @Nullable - private static AndroidCalculatorDisplayView getCalculatorDisplayView() { - return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView(); - } - - /* - ********************************************************************** - * - * STATIC CLASSES - * - ********************************************************************** - */ - - static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { - final boolean result; - - if (dragDirection == DragDirection.left) { - getKeyboard().roundBracketsButtonPressed(); - result = true; - } else { - result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); - } - - return result; - } - } - - @NotNull - private static CalculatorKeyboard getKeyboard() { - return CalculatorLocatorImpl.getInstance().getKeyboard(); - } - - static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { - - @NotNull - private Context context; - - VarsDragProcessor(Context context) { - this.context = context; - } - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragDirection == DragDirection.up) { - CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay()); - result = true; - } - - return result; - } - } - - static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { - - @NotNull - private final DigitButtonDragProcessor processor; - - @NotNull - private final Context context; - - AngleUnitsChanger(@NotNull Context context) { - this.context = context; - this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard()); - } - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragButton instanceof AngleUnitsButton) { - if (dragDirection != DragDirection.left) { - final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); - if (directionText != null) { - try { - - final AngleUnit angleUnits = AngleUnit.valueOf(directionText); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - - final AngleUnit oldAngleUnits = AndroidCalculatorEngine.Preferences.angleUnit.getPreference(preferences); - if (oldAngleUnits != angleUnits) { - AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); - - Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); - } - - result = true; - } catch (IllegalArgumentException e) { - Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText); - } - } - } else if (dragDirection == DragDirection.left) { - result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); - } - } - - return result; - } - } - - static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { - - @NotNull - private final Context context; - - NumeralBasesChanger(@NotNull Context context) { - this.context = context; - } - - @Override - public boolean processDragEvent(@NotNull DragDirection dragDirection, - @NotNull DragButton dragButton, - @NotNull Point2d startPoint2d, - @NotNull MotionEvent motionEvent) { - boolean result = false; - - if (dragButton instanceof NumeralBasesButton) { - final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); - if (directionText != null) { - try { - - final NumeralBase numeralBase = NumeralBase.valueOf(directionText); - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - - final NumeralBase oldNumeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences); - if (oldNumeralBase != numeralBase) { - AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); - - Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); - } - - result = true; - } catch (IllegalArgumentException e) { - Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText); - } - } - } - - return result; - } - } -} +package org.solovyev.android.calculator; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.preference.PreferenceManager; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; +import jscl.AngleUnit; +import jscl.NumeralBase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; +import org.solovyev.android.calculator.matrix.CalculatorMatrixActivity; +import org.solovyev.android.calculator.model.AndroidCalculatorEngine; +import org.solovyev.android.calculator.view.AngleUnitsButton; +import org.solovyev.android.calculator.view.NumeralBasesButton; +import org.solovyev.android.view.ColorButton; +import org.solovyev.android.view.drag.DragButton; +import org.solovyev.android.view.drag.DragDirection; +import org.solovyev.android.view.drag.SimpleOnDragListener; +import org.solovyev.common.math.Point2d; + +/** + * User: serso + * Date: 9/28/12 + * Time: 12:06 AM + */ +public final class CalculatorButtons { + + private CalculatorButtons () { + } + + + public static void processButtons(boolean fixMagicFlames, + @NotNull CalculatorPreferences.Gui.Theme theme, + @NotNull View root) { + if (theme.getThemeType() == CalculatorPreferences.Gui.ThemeType.metro) { + + if (fixMagicFlames) { + // for metro themes we should turn off magic flames + AndroidUtils.processViewsOfType(root, ColorButton.class, new AndroidUtils.ViewProcessor() { + @Override + public void process(@NotNull ColorButton colorButton) { + colorButton.setDrawMagicFlame(false); + } + }); + } + } + } + + static void initMultiplicationButton(@NotNull View root) { + final View multiplicationButton = root.findViewById(R.id.multiplicationButton); + if ( multiplicationButton instanceof Button) { + ((Button) multiplicationButton).setText(CalculatorLocatorImpl.getInstance().getEngine().getMultiplicationSign()); + } + } + + + + public static void toggleEqualsButton(@Nullable SharedPreferences preferences, + @NotNull Activity activity) { + preferences = preferences == null ? PreferenceManager.getDefaultSharedPreferences(activity) : preferences; + + final boolean large = AndroidUtils.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE, activity.getResources().getConfiguration()); + + if (!large) { + if (AndroidUtils.getScreenOrientation(activity) == Configuration.ORIENTATION_PORTRAIT + || !CalculatorPreferences.Gui.autoOrientation.getPreference(preferences)) { + + final DragButton equalsButton = (DragButton)activity.findViewById(R.id.equalsButton); + if (equalsButton != null) { + if (CalculatorPreferences.Gui.showEqualsButton.getPreference(preferences)) { + equalsButton.setVisibility(View.VISIBLE); + } else { + equalsButton.setVisibility(View.GONE); + } + } + } + } + } + + @Nullable + private static AndroidCalculatorDisplayView getCalculatorDisplayView() { + return (AndroidCalculatorDisplayView) CalculatorLocatorImpl.getInstance().getDisplay().getView(); + } + + /* + ********************************************************************** + * + * STATIC CLASSES + * + ********************************************************************** + */ + + static class RoundBracketsDragProcessor implements SimpleOnDragListener.DragProcessor { + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) { + final boolean result; + + if (dragDirection == DragDirection.left) { + getKeyboard().roundBracketsButtonPressed(); + result = true; + } else { + result = new DigitButtonDragProcessor(getKeyboard()).processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); + } + + return result; + } + } + + @NotNull + private static CalculatorKeyboard getKeyboard() { + return CalculatorLocatorImpl.getInstance().getKeyboard(); + } + + static class VarsDragProcessor implements SimpleOnDragListener.DragProcessor { + + @NotNull + private Context context; + + VarsDragProcessor(Context context) { + this.context = context; + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragDirection == DragDirection.up) { + CalculatorActivityLauncher.createVar(context, CalculatorLocatorImpl.getInstance().getDisplay()); + result = true; + } else if ( dragDirection == DragDirection.down ) { + context.startActivity(new Intent(context, CalculatorMatrixActivity.class)); + result = true; + } + + return result; + } + } + + static class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor { + + @NotNull + private final DigitButtonDragProcessor processor; + + @NotNull + private final Context context; + + AngleUnitsChanger(@NotNull Context context) { + this.context = context; + this.processor = new DigitButtonDragProcessor(CalculatorLocatorImpl.getInstance().getKeyboard()); + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragButton instanceof AngleUnitsButton) { + if (dragDirection != DragDirection.left) { + final String directionText = ((AngleUnitsButton) dragButton).getText(dragDirection); + if (directionText != null) { + try { + + final AngleUnit angleUnits = AngleUnit.valueOf(directionText); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + final AngleUnit oldAngleUnits = AndroidCalculatorEngine.Preferences.angleUnit.getPreference(preferences); + if (oldAngleUnits != angleUnits) { + AndroidCalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); + + Toast.makeText(context, context.getString(R.string.c_angle_units_changed_to, angleUnits.name()), Toast.LENGTH_LONG).show(); + } + + result = true; + } catch (IllegalArgumentException e) { + Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText); + } + } + } else if (dragDirection == DragDirection.left) { + result = processor.processDragEvent(dragDirection, dragButton, startPoint2d, motionEvent); + } + } + + return result; + } + } + + static class NumeralBasesChanger implements SimpleOnDragListener.DragProcessor { + + @NotNull + private final Context context; + + NumeralBasesChanger(@NotNull Context context) { + this.context = context; + } + + @Override + public boolean processDragEvent(@NotNull DragDirection dragDirection, + @NotNull DragButton dragButton, + @NotNull Point2d startPoint2d, + @NotNull MotionEvent motionEvent) { + boolean result = false; + + if (dragButton instanceof NumeralBasesButton) { + final String directionText = ((NumeralBasesButton) dragButton).getText(dragDirection); + if (directionText != null) { + try { + + final NumeralBase numeralBase = NumeralBase.valueOf(directionText); + + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + final NumeralBase oldNumeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences); + if (oldNumeralBase != numeralBase) { + AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); + + Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show(); + } + + result = true; + } catch (IllegalArgumentException e) { + Log.d(this.getClass().getName(), "Unsupported numeral base: " + directionText); + } + } + } + + return result; + } + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java index 693f660f..65a33d52 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/about/CalculatorFragmentType.java @@ -1,76 +1,80 @@ -package org.solovyev.android.calculator.about; - -import android.support.v4.app.Fragment; -import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.CalculatorEditorFragment; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.help.CalculatorHelpFaqFragment; -import org.solovyev.android.calculator.help.CalculatorHelpHintsFragment; -import org.solovyev.android.calculator.help.CalculatorHelpScreensFragment; -import org.solovyev.android.calculator.history.CalculatorHistoryFragment; -import org.solovyev.android.calculator.history.CalculatorSavedHistoryFragment; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment; -import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment; -import org.solovyev.android.calculator.plot.CalculatorPlotFragment; - -/** - * User: Solovyev_S - * Date: 03.10.12 - * Time: 11:30 - */ -public enum CalculatorFragmentType { - - editor(CalculatorEditorFragment.class, R.layout.calc_editor, R.string.editor), - //display(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), - //keyboard(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), - history(CalculatorHistoryFragment.class, R.layout.history_fragment, R.string.c_history), - saved_history(CalculatorSavedHistoryFragment.class, R.layout.history_fragment, R.string.c_saved_history), - variables(CalculatorVarsFragment.class, R.layout.vars_fragment, R.string.c_vars), - functions(CalculatorFunctionsFragment.class, R.layout.math_entities_fragment, R.string.c_functions), - operators(CalculatorOperatorsFragment.class, R.layout.math_entities_fragment, R.string.c_operators), - plotter(CalculatorPlotFragment.class, R.layout.plot_fragment, R.string.c_graph), - about(CalculatorAboutFragment.class, R.layout.about_fragment, R.string.c_about), - faq(CalculatorHelpFaqFragment.class, R.layout.help_faq_fragment, R.string.c_faq), - hints(CalculatorHelpHintsFragment.class, R.layout.help_hints_fragment, R.string.c_hints), - screens(CalculatorHelpScreensFragment.class, R.layout.help_screens_fragment, R.string.c_screens), - release_notes(CalculatorReleaseNotesFragment.class, R.layout.release_notes_fragment, R.string.c_release_notes); - - @NotNull - private Class fragmentClass; - - private final int defaultLayoutId; - - private int defaultTitleResId; - - private CalculatorFragmentType(@NotNull Class fragmentClass, - int defaultLayoutId, - int defaultTitleResId) { - this.fragmentClass = fragmentClass; - this.defaultLayoutId = defaultLayoutId; - this.defaultTitleResId = defaultTitleResId; - } - - @NotNull - public String getFragmentTag() { - return this.name(); - } - - public int getDefaultTitleResId() { - return defaultTitleResId; - } - - @NotNull - public Class getFragmentClass() { - return fragmentClass; - } - - public int getDefaultLayoutId() { - return defaultLayoutId; - } - - @NotNull - public String createSubFragmentTag(@NotNull String subFragmentTag) { - return this.getFragmentTag() + "_" + subFragmentTag; - } -} +package org.solovyev.android.calculator.about; + +import android.support.v4.app.Fragment; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorEditorFragment; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.help.CalculatorHelpFaqFragment; +import org.solovyev.android.calculator.help.CalculatorHelpHintsFragment; +import org.solovyev.android.calculator.help.CalculatorHelpScreensFragment; +import org.solovyev.android.calculator.history.CalculatorHistoryFragment; +import org.solovyev.android.calculator.history.CalculatorSavedHistoryFragment; +import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragment; +import org.solovyev.android.calculator.math.edit.CalculatorOperatorsFragment; +import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment; +import org.solovyev.android.calculator.matrix.CalculatorMatrixEditFragment; +import org.solovyev.android.calculator.plot.CalculatorPlotFragment; + +/** + * User: Solovyev_S + * Date: 03.10.12 + * Time: 11:30 + */ +public enum CalculatorFragmentType { + + editor(CalculatorEditorFragment.class, R.layout.calc_editor, R.string.editor), + //display(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), + //keyboard(CalculatorHistoryFragment.class, "history", R.layout.history_fragment, R.string.c_history), + history(CalculatorHistoryFragment.class, R.layout.history_fragment, R.string.c_history), + saved_history(CalculatorSavedHistoryFragment.class, R.layout.history_fragment, R.string.c_saved_history), + variables(CalculatorVarsFragment.class, R.layout.vars_fragment, R.string.c_vars), + functions(CalculatorFunctionsFragment.class, R.layout.math_entities_fragment, R.string.c_functions), + operators(CalculatorOperatorsFragment.class, R.layout.math_entities_fragment, R.string.c_operators), + plotter(CalculatorPlotFragment.class, R.layout.plot_fragment, R.string.c_graph), + about(CalculatorAboutFragment.class, R.layout.about_fragment, R.string.c_about), + faq(CalculatorHelpFaqFragment.class, R.layout.help_faq_fragment, R.string.c_faq), + hints(CalculatorHelpHintsFragment.class, R.layout.help_hints_fragment, R.string.c_hints), + screens(CalculatorHelpScreensFragment.class, R.layout.help_screens_fragment, R.string.c_screens), + + // todo serso: strings + matrix_edit(CalculatorMatrixEditFragment.class, R.layout.matrix_edit_fragment, R.string.c_screens), + release_notes(CalculatorReleaseNotesFragment.class, R.layout.release_notes_fragment, R.string.c_release_notes); + + @NotNull + private Class fragmentClass; + + private final int defaultLayoutId; + + private int defaultTitleResId; + + private CalculatorFragmentType(@NotNull Class fragmentClass, + int defaultLayoutId, + int defaultTitleResId) { + this.fragmentClass = fragmentClass; + this.defaultLayoutId = defaultLayoutId; + this.defaultTitleResId = defaultTitleResId; + } + + @NotNull + public String getFragmentTag() { + return this.name(); + } + + public int getDefaultTitleResId() { + return defaultTitleResId; + } + + @NotNull + public Class getFragmentClass() { + return fragmentClass; + } + + public int getDefaultLayoutId() { + return defaultLayoutId; + } + + @NotNull + public String createSubFragmentTag(@NotNull String subFragmentTag) { + return this.getFragmentTag() + "_" + subFragmentTag; + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixActivity.java new file mode 100644 index 00000000..3ec80741 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixActivity.java @@ -0,0 +1,23 @@ +package org.solovyev.android.calculator.matrix; + +import android.app.ActionBar; +import android.os.Bundle; +import org.solovyev.android.calculator.CalculatorFragmentActivity; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.about.CalculatorFragmentType; + +/** + * User: Solovyev_S + * Date: 12.10.12 + * Time: 10:56 + */ +public class CalculatorMatrixActivity extends CalculatorFragmentActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); + getActivityHelper().setFragment(this, CalculatorFragmentType.matrix_edit, null, R.id.main_layout); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java new file mode 100644 index 00000000..1d36190e --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java @@ -0,0 +1,83 @@ +package org.solovyev.android.calculator.matrix; + +import android.os.Bundle; +import android.view.View; +import android.widget.TableLayout; +import org.jetbrains.annotations.NotNull; +import org.solovyev.android.calculator.CalculatorFragment; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.about.CalculatorFragmentType; +import org.solovyev.android.view.IntegerRange; +import org.solovyev.android.view.Picker; + +/** + * User: Solovyev_S + * Date: 12.10.12 + * Time: 10:41 + */ +public class CalculatorMatrixEditFragment extends CalculatorFragment implements Picker.OnChangedListener { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + private static final int MAX_COUNT = 10; + private static final int MIN_COUNT = 2; + + /* + ********************************************************************** + * + * CONSTRUCTORS + * + ********************************************************************** + */ + + public CalculatorMatrixEditFragment() { + super(CalculatorFragmentType.matrix_edit); + } + + @Override + public void onViewCreated(View root, Bundle savedInstanceState) { + super.onViewCreated(root, savedInstanceState); + + final Picker matrixRowsCountPicker = (Picker) root.findViewById(R.id.matrix_rows_count_picker); + initPicker(matrixRowsCountPicker); + final Picker matrixColsCountPicker = (Picker) root.findViewById(R.id.matrix_cols_count_picker); + initPicker(matrixColsCountPicker); + + getMatrixTable(root); + } + + @NotNull + private TableLayout getMatrixTable(@NotNull View root) { + return (TableLayout) root.findViewById(R.id.matrix_layout); + } + + private void initPicker(@NotNull Picker picker) { + picker.setRange(new IntegerRange(MIN_COUNT, MAX_COUNT, 1, 2, null)); + picker.setOnChangeListener(this); + } + + @Override + public void onChanged(@NotNull Picker picker, @NotNull Integer value) { + switch (picker.getId()) { + case R.id.matrix_rows_count_picker: + onRowsCountChange(value); + break; + case R.id.matrix_cols_count_picker: + onColsCountChange(value); + break; + } + } + + private void onColsCountChange(@NotNull Integer cols) { + final TableLayout matrixTable = getMatrixTable(getView()); + } + + private void onRowsCountChange(@NotNull Integer rows) { + } +} From ad7596bca26a176f23aa870194fc90936e96c23b Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 12 Oct 2012 17:41:13 +0400 Subject: [PATCH 2/3] Matrix support --- calculatorpp/project.properties | 18 +- .../res/layout/matrix_edit_fragment.xml | 64 +++-- .../matrix/CalculatorMatrixEditFragment.java | 28 +- .../android/calculator/matrix/MatrixView.java | 259 ++++++++++++++++++ 4 files changed, 334 insertions(+), 35 deletions(-) create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java diff --git a/calculatorpp/project.properties b/calculatorpp/project.properties index 0b0ebc4d..03180409 100644 --- a/calculatorpp/project.properties +++ b/calculatorpp/project.properties @@ -9,12 +9,16 @@ # Project target. target=android-15 -android.library.reference.1=../calculatorpp-service -android.library.reference.2=gen-external-apklibs/org.solovyev.android_android-common-core_1.0.0 -android.library.reference.3=gen-external-apklibs/org.solovyev.android_android-common-ads_1.0.0 -android.library.reference.4=gen-external-apklibs/org.solovyev.android_android-common-view_1.0.0 -android.library.reference.5=gen-external-apklibs/org.solovyev.android_android-common-preferences_1.0.0 -android.library.reference.6=gen-external-apklibs/org.solovyev.android_android-common-other_1.0.0 -android.library.reference.7=gen-external-apklibs/org.solovyev.android_android-common-menu_1.0.0 +android.library.reference.1=gen-external-apklibs/org.solovyev.android_android-common-core_1.0.0 +android.library.reference.2=gen-external-apklibs/org.solovyev.android_android-common-ads_1.0.0 +android.library.reference.3=gen-external-apklibs/org.solovyev.android_billing_0.2 +android.library.reference.4=gen-external-apklibs/org.solovyev.android_android-common-db_1.0.0 +android.library.reference.5=gen-external-apklibs/org.solovyev.android_android-common-view_1.0.0 +android.library.reference.6=gen-external-apklibs/org.solovyev.android_android-common-preferences_1.0.0 +android.library.reference.7=gen-external-apklibs/org.solovyev.android_android-common-other_1.0.0 +android.library.reference.8=gen-external-apklibs/org.solovyev.android_android-common-menu_1.0.0 +android.library.reference.9=gen-external-apklibs/org.solovyev.android_android-common-sherlock_1.0.0 +android.library.reference.10=gen-external-apklibs/com.actionbarsherlock_library_4.1.0 +android.library.reference.11=gen-external-apklibs/org.solovyev.android_android-common-list_1.0.0 diff --git a/calculatorpp/res/layout/matrix_edit_fragment.xml b/calculatorpp/res/layout/matrix_edit_fragment.xml index 2dc0b9b6..d3133f42 100644 --- a/calculatorpp/res/layout/matrix_edit_fragment.xml +++ b/calculatorpp/res/layout/matrix_edit_fragment.xml @@ -1,35 +1,59 @@ + + a:layout_height="match_parent"> - + + + + - - - - - - + a:orientation="vertical"> + a:layout_width="wrap_content" + a:layout_gravity="center" + picker:orientation="horizontal"/> + + + + + + + + + + + + + + + + - \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java index 1d36190e..02f4753c 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java @@ -2,7 +2,6 @@ package org.solovyev.android.calculator.matrix; import android.os.Bundle; import android.view.View; -import android.widget.TableLayout; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.CalculatorFragment; import org.solovyev.android.calculator.R; @@ -27,6 +26,9 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements private static final int MAX_COUNT = 10; private static final int MIN_COUNT = 2; + private static final int DEFAULT_ROWS = 2; + private static final int DEFAULT_COLS = 2; + /* ********************************************************************** @@ -40,6 +42,15 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements super(CalculatorFragmentType.matrix_edit); } + /* + ********************************************************************** + * + * METHODS + * + ********************************************************************** + */ + + @Override public void onViewCreated(View root, Bundle savedInstanceState) { super.onViewCreated(root, savedInstanceState); @@ -49,16 +60,16 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements final Picker matrixColsCountPicker = (Picker) root.findViewById(R.id.matrix_cols_count_picker); initPicker(matrixColsCountPicker); - getMatrixTable(root); + getMatrixView(root).setMatrixDimensions(DEFAULT_ROWS, DEFAULT_COLS); } @NotNull - private TableLayout getMatrixTable(@NotNull View root) { - return (TableLayout) root.findViewById(R.id.matrix_layout); + private MatrixView getMatrixView(@NotNull View root) { + return (MatrixView) root.findViewById(R.id.matrix_layout); } private void initPicker(@NotNull Picker picker) { - picker.setRange(new IntegerRange(MIN_COUNT, MAX_COUNT, 1, 2, null)); + picker.setRange(new IntegerRange(MIN_COUNT, MAX_COUNT, 1, 0, null)); picker.setOnChangeListener(this); } @@ -74,10 +85,11 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements } } - private void onColsCountChange(@NotNull Integer cols) { - final TableLayout matrixTable = getMatrixTable(getView()); + private void onColsCountChange(@NotNull Integer newCols) { + getMatrixView(getView()).setMatrixCols(newCols); } - private void onRowsCountChange(@NotNull Integer rows) { + private void onRowsCountChange(@NotNull Integer newRows) { + getMatrixView(getView()).setMatrixRows(newRows); } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java new file mode 100644 index 00000000..ab18dd04 --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java @@ -0,0 +1,259 @@ +package org.solovyev.android.calculator.matrix; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.TableLayout; +import android.widget.TableRow; +import android.widget.TextView; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * User: Solovyev_S + * Date: 12.10.12 + * Time: 15:41 + */ +public class MatrixView extends TableLayout { + + /* + ********************************************************************** + * + * CONSTANTS + * + ********************************************************************** + */ + + private static final CharSequence DEFAULT_CELL_TEXT = "0"; + private static final int NUMBER_INDEX = -1; + + + /* + ********************************************************************** + * + * FIELDS + * + ********************************************************************** + */ + + private int rows = 0; + private int cols = 0; + + @Nullable + private CharSequence defaultCellText = DEFAULT_CELL_TEXT; + + private boolean initialized = false; + + /* + ********************************************************************** + * + * CONSTRUCTORS + * + ********************************************************************** + */ + + public MatrixView(Context context) { + super(context); + } + + public MatrixView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + /* + ********************************************************************** + * + * METHODS + * + ********************************************************************** + */ + + public void setMatrixCols(int newCols) { + setMatrixDimensions(rows, newCols); + } + + public void setMatrixRows(int newRows) { + setMatrixDimensions(newRows, cols); + } + + public void setMatrixDimensions(int newRows, int newCols) { + if ( newRows <= 1 ) { + throw new IllegalArgumentException("Number of rows must be more than 1: " + newRows); + } + + if ( newCols <= 1 ) { + throw new IllegalArgumentException("Number of columns must be more than 1: " + newCols); + } + + boolean rowsChanged = this.rows != newRows; + boolean colsChanged = this.cols != newCols; + + if (rowsChanged || colsChanged) { + if ( !initialized ) { + addRow(NUMBER_INDEX, 0); + initialized = true; + } + + if (this.cols > newCols) { + removeCols(newCols); + } else if (this.cols < newCols) { + addCols(newCols); + } + + this.cols = newCols; + + if (this.rows > newRows) { + removeRows(newRows); + } else if (this.rows < newRows) { + addRows(newRows); + } + + this.rows = newRows; + } + } + + public void setMatrix(@NotNull Object[][] matrix) { + final int rows = matrix.length; + final int cols = matrix[0].length; + + setMatrixDimensions(rows, cols); + for ( int row = 0; row < rows; row++ ) { + for ( int col = 0; col < cols; col++ ) { + setCell(row, col, matrix[row][col]); + } + } + + } + + private void setCell(int row, int col, @Nullable Object o) { + getCell(this, row, col); + } + + /* + ********************************************************************** + * + * PRIVATE METHODS + * + ********************************************************************** + */ + + private void addRows(int newRows) { + for (int row = this.rows; row < newRows; row++) { + addRow(row, cols); + } + } + + private void removeRows(int newRows) { + for (int row = this.rows - 1; row >= newRows; row--) { + removeRow(row); + } + } + + private void addCols(int newCols) { + for (int row = NUMBER_INDEX; row < rows; row++) { + final ViewGroup rowView = getRow(row); + if (rowView != null) { + for (int col = this.cols; col < newCols; col++) { + rowView.addView(createCellView(row, col)); + } + } + } + } + + private void removeCols(int newCols) { + for (int row = NUMBER_INDEX; row < rows; row++) { + final ViewGroup rowView = getRow(row); + if (rowView != null) { + for (int col = this.cols - 1; col >= newCols; col--) { + final View cellView = getCell(rowView, row, col); + if (cellView != null) { + rowView.removeView(cellView); + } + } + } + } + } + + private void addRow(int row, int newCols) { + this.addView(createRowView(row, newCols)); + } + + private void removeRow(int row) { + final View rowView = getRow(row); + if (rowView != null) { + this.removeView(rowView); + } + } + + @Nullable + private TableRow getRow(int row) { + return (TableRow) this.findViewWithTag(getRowTag(row)); + } + + @Nullable + private View getCell(@NotNull View view, int row, int col) { + return view.findViewWithTag(getCellTag(row, col)); + } + + @NotNull + private String getRowTag(int row) { + if (row != NUMBER_INDEX) { + return "row_" + row; + } else { + return "row_index"; + } + } + + @NotNull + private View createRowView(int row, int cols) { + final ViewGroup rowView = new TableRow(this.getContext()); + + rowView.setTag(getRowTag(row)); + + if (row != NUMBER_INDEX) { + rowView.addView(createCellView(row, NUMBER_INDEX)); + } else { + // empty cell + rowView.addView(new View(this.getContext())); + } + + for (int col = 0; col < cols; col++) { + rowView.addView(createCellView(row, col)); + } + return rowView; + } + + @NotNull + private View createCellView(int row, int col) { + final TextView result; + + if (row != NUMBER_INDEX && col != NUMBER_INDEX) { + result = new EditText(this.getContext()); + result.setText(defaultCellText); + } else { + result = new TextView(this.getContext()); + if (row == NUMBER_INDEX) { + result.setText(String.valueOf(col + 1)); + } else { + result.setText(String.valueOf(row + 1)); + } + } + + result.setTag(getCellTag(row, col)); + + return result; + + } + + @NotNull + private String getCellTag(int row, int col) { + if (row != NUMBER_INDEX) { + return "cell_" + row + "_" + col; + } else { + return "cell_index_" + col; + } + } + +} From be6dce7c4fd5bf37aedfee5a7025b09fb539e8b5 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 12 Oct 2012 18:04:20 +0400 Subject: [PATCH 3/3] Matrix support --- .../matrix/CalculatorMatrixEditFragment.java | 48 +++++++++++++++++-- .../android/calculator/matrix/MatrixView.java | 42 +++++++++++++--- 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java index 02f4753c..9f8e3c8e 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/CalculatorMatrixEditFragment.java @@ -3,12 +3,15 @@ package org.solovyev.android.calculator.matrix; import android.os.Bundle; import android.view.View; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.CalculatorFragment; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.about.CalculatorFragmentType; import org.solovyev.android.view.IntegerRange; import org.solovyev.android.view.Picker; +import java.io.Serializable; + /** * User: Solovyev_S * Date: 12.10.12 @@ -29,6 +32,8 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements private static final int DEFAULT_ROWS = 2; private static final int DEFAULT_COLS = 2; + private static final String MATRIX = "matrix"; + /* ********************************************************************** @@ -40,6 +45,8 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements public CalculatorMatrixEditFragment() { super(CalculatorFragmentType.matrix_edit); + + setRetainInstance(true); } /* @@ -52,15 +59,37 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements @Override - public void onViewCreated(View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); + public void onViewCreated(View root, @Nullable Bundle in) { + super.onViewCreated(root, in); final Picker matrixRowsCountPicker = (Picker) root.findViewById(R.id.matrix_rows_count_picker); initPicker(matrixRowsCountPicker); final Picker matrixColsCountPicker = (Picker) root.findViewById(R.id.matrix_cols_count_picker); initPicker(matrixColsCountPicker); - getMatrixView(root).setMatrixDimensions(DEFAULT_ROWS, DEFAULT_COLS); + Matrix matrix = null; + if (in != null) { + final Object matrixObject = in.getSerializable(MATRIX); + if (matrixObject instanceof Matrix) { + matrix = (Matrix) matrixObject; + } + } + + final MatrixView matrixView = getMatrixView(root); + if (matrix == null) { + matrixView.setMatrixDimensions(DEFAULT_ROWS, DEFAULT_COLS); + } else { + matrixView.setMatrix(matrix.bakingArray); + } + matrixRowsCountPicker.setCurrent(matrixView.getRows()); + matrixColsCountPicker.setCurrent(matrixView.getCols()); + } + + @Override + public void onSaveInstanceState(@NotNull Bundle out) { + super.onSaveInstanceState(out); + + out.putSerializable(MATRIX, new Matrix(getMatrixView(getView()).toMatrix())); } @NotNull @@ -92,4 +121,17 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements private void onRowsCountChange(@NotNull Integer newRows) { getMatrixView(getView()).setMatrixRows(newRows); } + + public static class Matrix implements Serializable { + + @NotNull + private String[][] bakingArray; + + public Matrix() { + } + + public Matrix(@NotNull String[][] bakingArray) { + this.bakingArray = bakingArray; + } + } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java index ab18dd04..16d96d65 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/matrix/MatrixView.java @@ -70,6 +70,14 @@ public class MatrixView extends TableLayout { ********************************************************************** */ + public int getRows() { + return rows; + } + + public int getCols() { + return cols; + } + public void setMatrixCols(int newCols) { setMatrixDimensions(rows, newCols); } @@ -79,11 +87,11 @@ public class MatrixView extends TableLayout { } public void setMatrixDimensions(int newRows, int newCols) { - if ( newRows <= 1 ) { + if (newRows <= 1) { throw new IllegalArgumentException("Number of rows must be more than 1: " + newRows); } - if ( newCols <= 1 ) { + if (newCols <= 1) { throw new IllegalArgumentException("Number of columns must be more than 1: " + newCols); } @@ -91,7 +99,7 @@ public class MatrixView extends TableLayout { boolean colsChanged = this.cols != newCols; if (rowsChanged || colsChanged) { - if ( !initialized ) { + if (!initialized) { addRow(NUMBER_INDEX, 0); initialized = true; } @@ -119,16 +127,38 @@ public class MatrixView extends TableLayout { final int cols = matrix[0].length; setMatrixDimensions(rows, cols); - for ( int row = 0; row < rows; row++ ) { - for ( int col = 0; col < cols; col++ ) { + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { setCell(row, col, matrix[row][col]); } } + } + @NotNull + public String[][] toMatrix() { + final String[][] result = new String[rows][cols]; + + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { + final TextView cellTextView = (TextView) getCell(this, row, col); + if (cellTextView != null) { + result[row][col] = cellTextView.getText().toString(); + } + } + } + + return result; } private void setCell(int row, int col, @Nullable Object o) { - getCell(this, row, col); + final TextView cellTextView = (TextView) getCell(this, row, col); + if (cellTextView != null) { + if (o == null) { + cellTextView.setText(null); + } else { + cellTextView.setText(String.valueOf(o)); + } + } } /*