Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
27   161   17   2.7
14   69   0.63   5
10     1.7  
2    
 
  TableCellInRowByValue       Line # 46 25 0% 15 0 100% 1.0
  TableCellInRowByValue.TableCellBuilder       Line # 71 2 0% 2 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Dec 28, 2009
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2009-2010 the original author or authors.
15    */
16    package org.fest.swing.data;
17   
18    import static org.fest.swing.edt.GuiActionRunner.execute;
19    import static org.fest.swing.exception.ActionFailedException.actionFailure;
20    import static org.fest.util.Arrays.format;
21    import static org.fest.util.Objects.areEqual;
22    import static org.fest.util.Strings.concat;
23   
24    import javax.swing.JTable;
25   
26    import org.fest.swing.annotation.RunsInCurrentThread;
27    import org.fest.swing.annotation.RunsInEDT;
28    import org.fest.swing.cell.JTableCellReader;
29    import org.fest.swing.edt.GuiQuery;
30    import org.fest.swing.exception.ActionFailedException;
31   
32    /**
33    * Understands lookup of a cell in the first row in <code>{@link JTable}</code> whose values match the given ones.
34    * <p>
35    * Example:
36    * <pre>
37    * // import static org.fest.swing.data.TableCellInSelectedRow.row;
38    * <code>{@link TableCell}</code> cell = dialog.table("records").cell({@link TableCellInRowByValue#rowWithValue(String...) rowWithValue}("column1", "column2", "column3").column(2));
39    * </pre>
40    * </p>
41    *
42    * @author Alex Ruiz
43    *
44    * @since 1.2
45    */
 
46    public class TableCellInRowByValue implements TableCellFinder {
47   
48    /**
49    * Starting point for the creation of a <code>{@link TableCellInRowByValue}</code>.
50    * <p>
51    * Example:
52    * <pre>
53    * // import static org.fest.swing.data.TableCellInRowByValue.rowWithValue;
54    * TableCellByColumnId cell = rowWithValue("column1", "column2", "column3").column(3);
55    * </pre>
56    * </p>
57    * @param values the values in the cells of the row we are looking for.
58    * @return the created builder.
59    * @throws NullPointerException if the given array of values is <code>null</code>.
60    */
 
61  7 toggle public static TableCellBuilder rowWithValue(String...values) {
62  1 if (values == null) throw new NullPointerException("The array of values should not be null");
63  6 return new TableCellBuilder(values);
64    }
65   
66    /**
67    * Understands creation of <code>{@link TableCellInSelectedRow}</code>s.
68    *
69    * @author Alex Ruiz
70    */
 
71    public static class TableCellBuilder {
72    private final String[] values;
73   
74    /**
75    * Creates a new </code>{@link TableCellBuilder}</code>.
76    * @param values the values of the cells of the row to find.
77    */
 
78  6 toggle TableCellBuilder(String[] values) {
79  6 this.values = values;
80    }
81   
82    /**
83    * Creates a new table cell finder using the row cell values specified in
84    * <code>{@link TableCellInRowByValue#rowWithValue(String...)}</code>
85    * and the column index specified as the argument in this method.
86    * @param column the index of the column in the table cell to find.
87    * @return the created finder.
88    */
 
89  6 toggle public TableCellInRowByValue column(int column) {
90  6 return new TableCellInRowByValue(values, column);
91    }
92    }
93   
94    private final String[] values;
95    private final int column;
96   
97    /**
98    * Creates a new </code>{@link TableCellInRowByValue}</code>.
99    * @param values the values in the cells of the row we are looking for.
100    * @param column the index of the column in the table cell to find.
101    */
 
102  6 toggle protected TableCellInRowByValue(String[] values, int column) {
103  6 this.values = values;
104  6 this.column = column;
105    }
106   
107    /**
108    * Finds a cell in the given <code>{@link JTable}</code> that:
109    * <ol>
110    * <li>is located in the first row whose values match the given ones</li>
111    * <li>has a matching row index</li>
112    * </ol>
113    * @param table the target <code>JTable</code>.
114    * @param cellReader knows how to read the contents of a cell in a <code>JTable</code>.
115    * @return the cell found, if any.
116    * @throws IllegalStateException if the size of values to look up is not equal to the number of columns in the given
117    * <code>JTable</code>.
118    * @throws ActionFailedException if a matching cell could not be found.
119    */
 
120  5 toggle @RunsInEDT
121    public TableCell findCell(JTable table, JTableCellReader cellReader) {
122  5 int row = findRowIndex(table, cellReader, values);
123  4 if (row == -1)
124  2 throw actionFailure(concat("Unable to find a row with values:<", format(values), ">"));
125  2 return new TableCell(row, column);
126    }
127   
 
128  5 toggle @RunsInEDT
129    private static int findRowIndex(final JTable table, final JTableCellReader cellReader, final String[] values) {
130  5 return execute(new GuiQuery<Integer>() {
 
131  5 toggle protected Integer executeInEDT() {
132  5 validateEqualSize(table, values);
133  4 int rowCount = table.getRowCount();
134  19 for (int row = 0; row < rowCount; row++)
135  2 if (matchingRow(table, cellReader, values, row)) return row;
136  2 return -1;
137    }
138   
139    });
140    }
141   
 
142  5 toggle @RunsInCurrentThread
143    private static void validateEqualSize(final JTable table, final String[] values) {
144  5 int columnCount = table.getColumnCount();
145  5 if (values.length != columnCount)
146  1 throw new IllegalStateException(concat("The array of values should have size:<", columnCount,">"));
147    }
148   
 
149  17 toggle @RunsInCurrentThread
150    private static boolean matchingRow(JTable table, JTableCellReader cellReader, String[] values, int row) {
151  17 int columnCount = table.getColumnCount();
152  31 for (int col = 0; col < columnCount; col++) {
153  15 if (!areEqual(cellReader.valueAt(table, row, col), values[col])) return false;
154    }
155  2 return true;
156    }
157   
 
158  1 toggle @Override public String toString() {
159  1 return concat(getClass().getName(), "[values=", format(values), ", column=", column, "]");
160    }
161    }