+ /**
+ * Get the currently-selected column number. 0 is the left-most column.
+ *
+ * @return the selected column number
+ */
+ public int getSelectedColumnNumber() {
+ return selectedColumn;
+ }
+
+ /**
+ * Set the currently-selected column number. 0 is the left-most column.
+ *
+ * @param column the column number to select
+ */
+ public void setSelectedColumnNumber(final int column) {
+ if ((column < 0) || (column > columns.size() - 1)) {
+ throw new IndexOutOfBoundsException("Column count is " +
+ columns.size() + ", requested index " + column);
+ }
+ selectedColumn = column;
+ activate(columns.get(selectedColumn).get(selectedRow));
+ alignGrid();
+ }
+
+ /**
+ * Get the currently-selected row number. 0 is the top-most row.
+ *
+ * @return the selected row number
+ */
+ public int getSelectedRowNumber() {
+ return selectedRow;
+ }
+
+ /**
+ * Set the currently-selected row number. 0 is the left-most column.
+ *
+ * @param row the row number to select
+ */
+ public void setSelectedRowNumber(final int row) {
+ if ((row < 0) || (row > rows.size() - 1)) {
+ throw new IndexOutOfBoundsException("Row count is " +
+ rows.size() + ", requested index " + row);
+ }
+ selectedRow = row;
+ activate(columns.get(selectedColumn).get(selectedRow));
+ alignGrid();
+ }
+
+ /**
+ * Get the number of columns.
+ *
+ * @return the number of columns
+ */
+ public int getColumnCount() {
+ return columns.size();
+ }
+
+ /**
+ * Get the number of rows.
+ *
+ * @return the number of rows
+ */
+ public int getRowCount() {
+ return rows.size();
+ }
+
+ /**
+ * Align the grid so that the selected cell is fully visible.
+ */
+ private void alignGrid() {
+ int viewColumns = getWidth();
+ if (showRowLabels == true) {
+ viewColumns -= ROW_LABEL_WIDTH;
+ }
+ if (leftBorder != Border.NONE) {
+ viewColumns--;
+ }
+ int viewRows = getHeight();
+ if (showColumnLabels == true) {
+ viewRows -= COLUMN_LABEL_HEIGHT;
+ }
+ if (topBorder != Border.NONE) {
+ viewRows--;
+ }
+
+ // If we pushed left or right, adjust the box to include the new
+ // selected cell.
+ if (selectedColumn < left) {
+ left = selectedColumn - 1;
+ }
+ if (left < 0) {
+ left = 0;
+ }
+ if (selectedRow < top) {
+ top = selectedRow - 1;
+ }
+ if (top < 0) {
+ top = 0;
+ }
+
+ /*
+ * viewColumns and viewRows now contain the available columns and
+ * rows available to view the selected cell. We adjust left and top
+ * to ensure the selected cell is within view, and then make all
+ * cells outside the box between (left, top) and (right, bottom)
+ * invisible.
+ *
+ * We need to calculate right and bottom now.
+ */
+ int right = left;
+
+ boolean done = false;
+ while (!done) {
+ int rightCellX = (showRowLabels ? ROW_LABEL_WIDTH : 0);
+ if (leftBorder != Border.NONE) {
+ rightCellX++;
+ }
+ int maxCellX = rightCellX + viewColumns;
+ right = left;
+ boolean selectedIsVisible = false;
+ int selectedX = 0;
+ for (int x = left; x < columns.size(); x++) {
+ if (x == selectedColumn) {
+ selectedX = rightCellX;
+ if (selectedX + columns.get(x).width + 1 <= maxCellX) {
+ selectedIsVisible = true;
+ }
+ }
+ rightCellX += columns.get(x).width + 1;
+ if (rightCellX >= maxCellX) {
+ break;
+ }
+ right++;
+ }
+ if (right < selectedColumn) {
+ // selectedColumn is outside the view range. Push left over,
+ // and calculate again.
+ left++;
+ } else if (left == selectedColumn) {
+ // selectedColumn doesn't fit inside the view range, but we
+ // can't go over any further either. Bail out.
+ done = true;
+ } else if (selectedIsVisible == false) {
+ // selectedColumn doesn't fit inside the view range, continue
+ // on.
+ left++;
+ } else {
+ // selectedColumn is fully visible, all done.
+ assert (selectedIsVisible == true);
+ done = true;
+ }
+
+ } // while (!done)
+
+ // We have the left/right range correct, set cell visibility and
+ // column X positions.
+ int leftCellX = showRowLabels ? ROW_LABEL_WIDTH : 0;
+ if (leftBorder != Border.NONE) {
+ leftCellX++;
+ }
+ for (int x = 0; x < columns.size(); x++) {
+ if ((x < left) || (x > right)) {
+ for (int i = 0; i < rows.size(); i++) {
+ columns.get(x).get(i).setVisible(false);
+ columns.get(x).setX(getWidth() + 1);
+ }
+ continue;
+ }
+ for (int i = 0; i < rows.size(); i++) {
+ columns.get(x).get(i).setVisible(true);
+ }
+ columns.get(x).setX(leftCellX);
+ leftCellX += columns.get(x).width + 1;
+ }
+
+ int bottom = top;
+
+ done = false;
+ while (!done) {
+ int bottomCellY = (showColumnLabels ? COLUMN_LABEL_HEIGHT : 0);
+ if (topBorder != Border.NONE) {
+ bottomCellY++;
+ }
+ int maxCellY = bottomCellY + viewRows;
+ bottom = top;
+ for (int y = top; y < rows.size(); y++) {
+ bottomCellY += rows.get(y).height;
+ if (bottomCellY >= maxCellY) {
+ break;
+ }
+ bottom++;
+ }
+ if (bottom < selectedRow) {
+ // selectedRow is outside the view range. Push top down, and
+ // calculate again.
+ top++;
+ } else {
+ // selectedRow is inside the view range, done.
+ done = true;
+ }
+ } // while (!done)
+
+ // We have the top/bottom range correct, set cell visibility and
+ // row Y positions.
+ int topCellY = showColumnLabels ? COLUMN_LABEL_HEIGHT : 0;
+ if (topBorder != Border.NONE) {
+ topCellY++;
+ }
+ for (int y = 0; y < rows.size(); y++) {
+ if ((y < top) || (y > bottom)) {
+ for (int i = 0; i < columns.size(); i++) {
+ rows.get(y).get(i).setVisible(false);
+ }
+ rows.get(y).setY(getHeight() + 1);
+ continue;
+ }
+ for (int i = 0; i < columns.size(); i++) {
+ rows.get(y).get(i).setVisible(true);
+ }
+ rows.get(y).setY(topCellY);
+ topCellY += rows.get(y).height;
+ }
+
+ }
+
+ /**
+ * Save contents to file.
+ *
+ * @param filename file to save to
+ * @throws IOException if a java.io operation throws
+ */
+ public void saveToFilename(final String filename) throws IOException {
+ // TODO
+ }
+