From be6dce7c4fd5bf37aedfee5a7025b09fb539e8b5 Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 12 Oct 2012 18:04:20 +0400 Subject: [PATCH] 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)); + } + } } /*