+ Row row = getSelectedRow();
+ Cell selectedColumnCell = null;
+ for (int i = 0; i < row.cells.size(); i++) {
+ if (i == selectedColumn) {
+ selectedColumnCell = row.cells.get(i);
+ break;
+ }
+ leftCellX += row.cells.get(i).getWidth() + 1;
+ }
+ // There should always be a selected column.
+ assert (selectedColumnCell != null);
+
+ int excessWidth = leftCellX + selectedColumnCell.getWidth() + 1 - getWidth();
+ if (excessWidth > 0) {
+ leftCellX -= excessWidth;
+ }
+ if (leftCellX < 0) {
+ if (showRowLabels == true) {
+ leftCellX = 8;
+ } else {
+ leftCellX = 0;
+ }
+ }
+
+ /*
+ * leftCellX now contains the basic left offset necessary to draw the
+ * cells such that the selected cell (column) is fully visible within
+ * this widget's given width. Or, if the widget is too narrow to
+ * display the full cell, leftCellX is 0 or 8.
+ *
+ * Now reset all of the X positions of the other cells so that the
+ * selected cell X is leftCellX.
+ */
+ for (int y = 0; y < rows.size(); y++) {
+ // All cells to the left of selected cell.
+ int newCellX = leftCellX;
+ left = selectedColumn;
+ for (int x = selectedColumn - 1; x >= 0; x--) {
+ newCellX -= rows.get(y).cells.get(x).getWidth() + 1;
+ if (newCellX - rows.get(y).cells.get(x).getWidth() + 1 >= 0) {
+ rows.get(y).cells.get(x).setVisible(true);
+ rows.get(y).cells.get(x).setX(newCellX);
+ left--;
+ } else {
+ // This cell won't be visible.
+ rows.get(y).cells.get(x).setVisible(false);
+ }
+ }
+
+ // Selected cell.
+ rows.get(y).cells.get(selectedColumn).setX(leftCellX);
+ assert (rows.get(y).cells.get(selectedColumn).isVisible());
+
+ // All cells to the right of selected cell.
+ newCellX = leftCellX + selectedColumnCell.getWidth() + 1;
+ for (int x = selectedColumn + 1; x < columns.size(); x++) {
+ if (newCellX <= getWidth()) {
+ rows.get(y).cells.get(x).setVisible(true);
+ rows.get(y).cells.get(x).setX(newCellX);
+ } else {
+ // This cell won't be visible.
+ rows.get(y).cells.get(x).setVisible(false);
+ }
+ newCellX += rows.get(y).cells.get(x).getWidth() + 1;
+ }
+ }
+
+ // Adjust Y locations to be visible -----------------------------------
+ // The same logic as above, but applied to the column Y.
+
+ // 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);