+ // Determine if we need to shift up or down.
+ int height = getMaximumHeight();
+ 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);
+
+ while (topCellY + selectedRowCell.getHeight() > getHeight()) {
+ topCellY -= (getHeight() - selectedRowCell.getHeight());
+ }
+ if (topCellY < 0) {
+ 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.
+ *
+ * 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++) {
+ // 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 - rows.get(y).cells.get(x).getHeight() > 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(selectedColumn).setY(topCellY);
+
+ // All cells below of 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();
+ }
+ }