Matrix support

This commit is contained in:
serso 2012-10-12 17:41:13 +04:00
parent de52ca4512
commit ad7596bca2
4 changed files with 334 additions and 35 deletions

View File

@ -9,12 +9,16 @@
# Project target. # Project target.
target=android-15 target=android-15
android.library.reference.1=../calculatorpp-service 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-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_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-view_1.0.0 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-preferences_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-other_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-menu_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

View File

@ -1,35 +1,59 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:picker="http://schemas.android.com/apk/res-auto"
a:id="@+id/main_fragment_layout" a:id="@+id/main_fragment_layout"
a:layout_height="match_parent" style="?fragmentLayoutStyle"
a:layout_width="match_parent" a:layout_width="match_parent"
style="?fragmentLayoutStyle"> a:layout_height="match_parent">
<LinearLayout a:layout_height="wrap_content" <TextView a:id="@+id/fragmentTitle"
a:layout_height="wrap_content"
a:layout_width="match_parent" a:layout_width="match_parent"
a:gravity="center"> style="?fragmentTitleStyle"/>
<TextView a:layout_height="wrap_content" <include layout="@layout/ad"/>
<LinearLayout a:layout_height="match_parent"
a:layout_width="match_parent"
a:orientation="vertical">
<org.solovyev.android.view.Picker a:id="@+id/matrix_cols_count_picker"
a:layout_height="wrap_content"
a:layout_width="wrap_content" a:layout_width="wrap_content"
a:text="Rows"/> a:layout_gravity="center"
picker:orientation="horizontal"/>
<LinearLayout a:layout_height="match_parent"
a:layout_width="match_parent"
a:orientation="horizontal"
a:gravity="center">
<org.solovyev.android.view.Picker a:id="@+id/matrix_rows_count_picker" <org.solovyev.android.view.Picker a:id="@+id/matrix_rows_count_picker"
a:layout_height="wrap_content" a:layout_height="wrap_content"
a:layout_width="wrap_content"/> a:layout_width="wrap_content"/>
<TextView a:layout_height="wrap_content" <ScrollView a:layout_height="match_parent"
a:layout_width="wrap_content" a:layout_width="match_parent"
a:text="Columns"/> a:scrollbars="vertical">
<org.solovyev.android.view.Picker a:id="@+id/matrix_cols_count_picker" <HorizontalScrollView a:layout_height="wrap_content"
a:layout_height="wrap_content" a:layout_width="match_parent"
a:layout_width="wrap_content"/> a:minHeight="300dp">
<org.solovyev.android.calculator.matrix.MatrixView a:id="@+id/matrix_layout"
a:layout_height="match_parent"
a:layout_width="match_parent"
a:minWidth="300dp"/>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
</LinearLayout> </LinearLayout>
<TableLayout a:id="@+id/matrix_layout"
a:layout_height="match_parent"
a:layout_width="match_parent"/>
</LinearLayout> </LinearLayout>

View File

@ -2,7 +2,6 @@ package org.solovyev.android.calculator.matrix;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.TableLayout;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorFragment; import org.solovyev.android.calculator.CalculatorFragment;
import org.solovyev.android.calculator.R; 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 MAX_COUNT = 10;
private static final int MIN_COUNT = 2; 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); super(CalculatorFragmentType.matrix_edit);
} }
/*
**********************************************************************
*
* METHODS
*
**********************************************************************
*/
@Override @Override
public void onViewCreated(View root, Bundle savedInstanceState) { public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState); super.onViewCreated(root, savedInstanceState);
@ -49,16 +60,16 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements
final Picker<Integer> matrixColsCountPicker = (Picker<Integer>) root.findViewById(R.id.matrix_cols_count_picker); final Picker<Integer> matrixColsCountPicker = (Picker<Integer>) root.findViewById(R.id.matrix_cols_count_picker);
initPicker(matrixColsCountPicker); initPicker(matrixColsCountPicker);
getMatrixTable(root); getMatrixView(root).setMatrixDimensions(DEFAULT_ROWS, DEFAULT_COLS);
} }
@NotNull @NotNull
private TableLayout getMatrixTable(@NotNull View root) { private MatrixView getMatrixView(@NotNull View root) {
return (TableLayout) root.findViewById(R.id.matrix_layout); return (MatrixView) root.findViewById(R.id.matrix_layout);
} }
private void initPicker(@NotNull Picker<Integer> picker) { private void initPicker(@NotNull Picker<Integer> 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); picker.setOnChangeListener(this);
} }
@ -74,10 +85,11 @@ public class CalculatorMatrixEditFragment extends CalculatorFragment implements
} }
} }
private void onColsCountChange(@NotNull Integer cols) { private void onColsCountChange(@NotNull Integer newCols) {
final TableLayout matrixTable = getMatrixTable(getView()); getMatrixView(getView()).setMatrixCols(newCols);
} }
private void onRowsCountChange(@NotNull Integer rows) { private void onRowsCountChange(@NotNull Integer newRows) {
getMatrixView(getView()).setMatrixRows(newRows);
} }
} }

View File

@ -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;
}
}
}