001    /*
002     * Created on Oct 13, 2008
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2008-2010 the original author or authors.
015     */
016    package org.fest.swing.driver;
017    
018    import static java.lang.String.valueOf;
019    import static org.fest.util.Strings.concat;
020    import static org.fest.util.Strings.quote;
021    
022    import javax.swing.JTable;
023    
024    import org.fest.swing.annotation.RunsInCurrentThread;
025    import org.fest.swing.data.TableCell;
026    
027    /**
028     * Understands validation of <code>{@link JTable}</code>-related information.
029     *
030     * @author Alex Ruiz
031     */
032    public final class JTableCellValidator {
033    
034      /**
035       * Validates that the table cell in the given coordinates is editable.
036       * <p>
037       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
038       * responsible for calling this method from the EDT.
039       * </p>
040       * @param table the target <code>JTable</code>.
041       * @param row the row index of the cell to validate.
042       * @param column the column index of the cell to validate.
043       * @throws IllegalStateException if the table cell in the given coordinates is not editable.
044       */
045      @RunsInCurrentThread
046      public static void validateCellIsEditable(JTable table, int row, int column) {
047        if (!table.isCellEditable(row, column))
048          throw new IllegalStateException(
049              concat("Expecting the cell [", valueOf(row), ",", valueOf(column), "] to be editable"));
050      }
051    
052      /**
053       * Validates that the given table cell is non <code>null</code> and its indices are not out of bounds.
054       * <p>
055       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
056       * responsible for calling this method from the EDT.
057       * </p>
058       * @param table the target <code>JTable</code>.
059       * @param cell the cell to validate.
060       * @throws NullPointerException if the cell is <code>null</code>.
061       * @throws IndexOutOfBoundsException if any of the indices (row and column) is out of bounds.
062       */
063      @RunsInCurrentThread
064      public static void validateCellIndices(JTable table, TableCell cell) {
065        validateNotNull(cell);
066        validateIndices(table, cell.row, cell.column);
067      }
068    
069      /**
070       * Validates that the given table cell is not <code>null</code>.
071       * @param cell the cell to validate.
072       * @throws NullPointerException if the cell is <code>null</code>.
073       */
074      public static void validateNotNull(TableCell cell) {
075        if (cell == null) throw new NullPointerException("Table cell cannot be null");
076      }
077    
078      /**
079       * Validates the given indices regarding the given table.
080       * <p>
081       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
082       * responsible for calling this method from the EDT.
083       * </p>
084       * @param table the <code>JTable</code> to use to validate the given indices.
085       * @param row the row index to validate.
086       * @param column the column index to validate.
087       * @throws IndexOutOfBoundsException if any of the indices is out of bounds or if the <code>JTable</code> does not
088       * have any rows.
089       */
090      @RunsInCurrentThread
091      public static void validateIndices(JTable table, int row, int column) {
092        if (table.getRowCount() == 0) throw new IndexOutOfBoundsException("Table does not contain any rows");
093        validateRowIndex(table, row);
094        validateColumnIndex(table, column);
095      }
096    
097      /**
098       * Validates that the given row index exists in the given table.
099       * <p>
100       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
101       * responsible for calling this method from the EDT.
102       * </p>
103       * @param table the table the given table.
104       * @param row the row to validate.
105       * @throws IndexOutOfBoundsException if the row index is out of bounds.
106       */
107      @RunsInCurrentThread
108      public static void validateRowIndex(JTable table, int row) {
109        validateIndex(row, table.getRowCount(), "row");
110      }
111    
112      /**
113       * Validates that the given column index exists in the given table.
114       * <p>
115       * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
116       * responsible for calling this method from the EDT.
117       * </p>
118       * @param table the table the given table.
119       * @param column the column to validate.
120       * @throws IndexOutOfBoundsException if the column index is out of bounds.
121       */
122      @RunsInCurrentThread
123      public static void validateColumnIndex(JTable table, int column) {
124        validateIndex(column, table.getColumnCount(), "column");
125      }
126    
127      @RunsInCurrentThread
128      private static void validateIndex(int index, int itemCount, String indexName) {
129        if (index >= 0 && index < itemCount) return;
130        throw new IndexOutOfBoundsException(concat(
131            indexName, " ", quote(valueOf(index)), " should be between <0> and <", valueOf(itemCount - 1), ">"));
132      }
133    
134      private JTableCellValidator() {}
135    }