+ // Determine if we need to shift up or down.
+ int topCellY = 0;
+ if (showColumnLabels == true) {
+ // For now, all column labels are 1 cell high. TODO: make this
+ // adjustable.
+ topCellY += 1;
+ }
+ Column column = getSelectedColumn();
+ Cell selectedRowCell = null;
+ for (int i = 0; i < column.cells.size(); i++) {
+ if (i == selectedRow) {
+ selectedRowCell = column.cells.get(i);
+ break;
+ }
+ topCellY += column.cells.get(i).getHeight();
+ // TODO: if a border is selected, add 1 to topCellY.
+ }
+ // There should always be a selected row.
+ assert (selectedRowCell != null);
+
+ int excessHeight = topCellY + selectedRowCell.getHeight() - getHeight() - 1;
+ if (showColumnLabels == true) {
+ excessHeight += 1;
+ }
+ if (excessHeight > 0) {
+ topCellY -= excessHeight;
+ }
+ if (topCellY < 0) {
+ if (showColumnLabels == true) {
+ topCellY = 1;
+ } else {
+ topCellY = 0;
+ }
+ }
+
+ /*
+ * topCellY now contains the basic top offset necessary to draw the
+ * cells such that the selected cell (row) is fully visible within
+ * this widget's given height. Or, if the widget is too short to
+ * display the full cell, topCellY is 0 or 1.
+ *
+ * Now reset all of the Y positions of the other cells so that the
+ * selected cell Y is topCellY.
+ */
+ for (int x = 0; x < columns.size(); x++) {
+
+ if (columns.get(x).get(0).isVisible() == false) {
+ // This column won't be visible as determined by the checks
+ // above, just continue to the next.
+ continue;
+ }
+
+ // All cells above the selected cell.
+ int newCellY = topCellY;
+ top = selectedRow;
+ for (int y = selectedRow - 1; y >= 0; y--) {
+ newCellY -= rows.get(y).cells.get(x).getHeight();
+ if (newCellY >= (showColumnLabels == true ? 1 : 0)) {
+ rows.get(y).cells.get(x).setVisible(true);
+ rows.get(y).cells.get(x).setY(newCellY);
+ top--;
+ } else {
+ // This cell won't be visible.
+ rows.get(y).cells.get(x).setVisible(false);
+ }
+ }
+
+ // Selected cell.
+ columns.get(x).cells.get(selectedRow).setY(topCellY);
+
+ // All cells below the selected cell.
+ newCellY = topCellY + selectedRowCell.getHeight();
+ for (int y = selectedRow + 1; y < rows.size(); y++) {
+ if (newCellY <= getHeight()) {
+ rows.get(y).cells.get(x).setVisible(true);
+ rows.get(y).cells.get(x).setY(newCellY);
+ } else {
+ // This cell won't be visible.
+ rows.get(y).cells.get(x).setVisible(false);
+ }
+ newCellY += rows.get(y).cells.get(x).getHeight();
+ }
+ }