Matrix support

This commit is contained in:
serso 2012-10-12 18:04:20 +04:00
parent ad7596bca2
commit be6dce7c4f
2 changed files with 81 additions and 9 deletions

View File

@ -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<Integer> matrixRowsCountPicker = (Picker<Integer>) root.findViewById(R.id.matrix_rows_count_picker);
initPicker(matrixRowsCountPicker);
final Picker<Integer> matrixColsCountPicker = (Picker<Integer>) 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;
}
}
}

View File

@ -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));
}
}
}
/*