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
14   120   10   1.56
2   48   0.71   4.5
9     1.11  
2    
 
  TableCellByColumnId       Line # 44 12 0% 8 0 100% 1.0
  TableCellByColumnId.TableCellBuilder       Line # 68 2 0% 2 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Oct 13, 2007
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 @2008-2010 the original author or authors.
15    */
16    package org.fest.swing.data;
17   
18    import static java.lang.String.valueOf;
19    import static org.fest.swing.edt.GuiActionRunner.execute;
20    import static org.fest.swing.exception.ActionFailedException.actionFailure;
21    import static org.fest.swing.query.JTableColumnByIdentifierQuery.columnIndexByIdentifier;
22    import static org.fest.util.Strings.concat;
23    import static org.fest.util.Strings.quote;
24   
25    import javax.swing.JTable;
26   
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 a <code>{@link JTable}</code> by column identifier.
34    * <p>
35    * Example:
36    * <pre>
37    * // import static org.fest.swing.data.TableCellByColumnId.row;
38    * <code>{@link TableCell}</code> cell = dialog.table("records").cell({@link TableCellByColumnId#row(int) row}(3).columnId("firstColumn"));
39    * </pre>
40    * </p>
41    *
42    * @author Alex Ruiz
43    */
 
44    public class TableCellByColumnId implements TableCellFinder {
45   
46    public final int row;
47    public final Object columnId;
48   
49    /**
50    * Starting point for the creation of a <code>{@link TableCellByColumnId}</code>.
51    * <p>
52    * Example:
53    * <pre>
54    * // import static org.fest.swing.data.TableCellByColumnId.row;
55    * TableCellByColumnId cell = row(5).columnId("hobbyColumn");
56    * </pre>
57    * </p>
58    * @param row the row index of the table cell to find.
59    * @return the created builder.
60    */
 
61  8 toggle public static TableCellBuilder row(int row) { return new TableCellBuilder(row); }
62   
63    /**
64    * Understands creation of <code>{@link TableCellByColumnId}</code>s.
65    *
66    * @author Alex Ruiz
67    */
 
68    public static class TableCellBuilder {
69    private final int row;
70   
 
71  8 toggle TableCellBuilder(int row) { this.row = row; }
72   
73    /**
74    * Creates a new table cell finder using the row index specified in
75    * <code>{@link TableCellByColumnId#row(int)}</code> and the column id specified as the argument in this method.
76    * @param columnId the name of the column in the table cell to find.
77    * @return the created finder.
78    */
 
79  8 toggle public TableCellByColumnId columnId(Object columnId) {
80  8 return new TableCellByColumnId(row, columnId);
81    }
82    }
83   
 
84  8 toggle protected TableCellByColumnId(int row, Object columnId) {
85  8 this.row = row;
86  8 this.columnId = columnId;
87    }
88   
89    /**
90    * Finds a cell in the given <code>{@link JTable}</code> that has a matching row index and column id.
91    * @param table the target <code>JTable</code>.
92    * @param cellReader knows how to read the contents of a cell in a <code>JTable</code>.
93    * @return the cell found, if any.
94    * @throws ActionFailedException if a matching cell could not be found.
95    */
 
96  6 toggle @RunsInEDT
97    public TableCell findCell(JTable table, JTableCellReader cellReader) {
98  6 return findCell(table, row, columnId);
99    }
100   
 
101  6 toggle @RunsInEDT
102    private static TableCell findCell(final JTable table, final int row, final Object columnId) {
103  6 return execute(new GuiQuery<TableCell>() {
 
104  6 toggle protected TableCell executeInEDT() {
105  6 int column = columnIndexByIdentifier(table, columnId);
106  1 if (column == -1) failColumnIndexNotFound(columnId);
107  5 table.convertColumnIndexToView(table.getColumn(columnId).getModelIndex());
108  5 return new TableCell(row, column);
109    }
110    });
111    }
112   
 
113  1 toggle private static ActionFailedException failColumnIndexNotFound(Object columnId) {
114  1 throw actionFailure(concat("Unable to find a column with id ", quote(columnId)));
115    }
116   
 
117  1 toggle @Override public String toString() {
118  1 return concat(getClass().getName(), "[row=", valueOf(row), ", columnId=", quote(columnId), "]");
119    }
120    }