move
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,137 @@
|
||||
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
|
||||
* Time: 10:41
|
||||
*/
|
||||
public class CalculatorMatrixEditFragment extends CalculatorFragment implements Picker.OnChangedListener<Integer> {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
private static final String MATRIX = "matrix";
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public CalculatorMatrixEditFragment() {
|
||||
super(CalculatorFragmentType.matrix_edit);
|
||||
|
||||
setRetainInstance(true);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
|
||||
@Override
|
||||
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);
|
||||
|
||||
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
|
||||
private MatrixView getMatrixView(@NotNull View root) {
|
||||
return (MatrixView) root.findViewById(R.id.matrix_layout);
|
||||
}
|
||||
|
||||
private void initPicker(@NotNull Picker<Integer> picker) {
|
||||
picker.setRange(new IntegerRange(MIN_COUNT, MAX_COUNT, 1, 0, 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 newCols) {
|
||||
getMatrixView(getView()).setMatrixCols(newCols);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,289 @@
|
||||
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 int getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public int getCols() {
|
||||
return cols;
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
final TextView cellTextView = (TextView) getCell(this, row, col);
|
||||
if (cellTextView != null) {
|
||||
if (o == null) {
|
||||
cellTextView.setText(null);
|
||||
} else {
|
||||
cellTextView.setText(String.valueOf(o));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user