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
10   107   8   1.43
2   38   0.8   3.5
7     1.14  
2    
 
  TableCellInSelectedRow       Line # 43 9 0% 7 0 100% 1.0
  TableCellInSelectedRow.TableCellBuilder       Line # 63 1 0% 1 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Dec 25, 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 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.util.Strings.concat;
22   
23    import javax.swing.JTable;
24   
25    import org.fest.swing.annotation.RunsInEDT;
26    import org.fest.swing.cell.JTableCellReader;
27    import org.fest.swing.edt.GuiQuery;
28    import org.fest.swing.exception.ActionFailedException;
29   
30    /**
31    * Understands lookup of a cell in the first selected row of a <code>{@link JTable}</code>.
32    * <p>
33    * Example:
34    * <pre>
35    * // import static org.fest.swing.data.TableCellInSelectedRow.row;
36    * <code>{@link TableCell}</code> cell = dialog.table("records").cell({@link TableCellInSelectedRow#selectedRow() selectedRow}().column(2));
37    * </pre>
38    * </p>
39    *
40    * @author Alex Ruiz
41    *
42    */
 
43    public class TableCellInSelectedRow implements TableCellFinder {
44   
45    /**
46    * Starting point for the creation of a <code>{@link TableCellInSelectedRow}</code>.
47    * <p>
48    * Example:
49    * <pre>
50    * // import static org.fest.swing.data.TableCellInSelectedRow.row;
51    * TableCellInSelectedRow cell = selectedRow().column(2);
52    * </pre>
53    * </p>
54    * @return the created builder.
55    */
 
56  3 toggle public static TableCellBuilder selectedRow() { return new TableCellBuilder(); }
57   
58    /**
59    * Understands creation of <code>{@link TableCellInSelectedRow}</code>s.
60    *
61    * @author Alex Ruiz
62    */
 
63    public static class TableCellBuilder {
64   
65    /**
66    * Creates a new table cell finder.
67    * @param column the column index of the cell to find.
68    * @return the created finder.
69    */
 
70  3 toggle public TableCellInSelectedRow column(int column) {
71  3 return new TableCellInSelectedRow(column);
72    }
73    }
74   
75    private final int column;
76   
 
77  3 toggle protected TableCellInSelectedRow(int column) {
78  3 this.column = column;
79    }
80   
81    /**
82    * Finds a cell in the given <code>{@link JTable}</code> that belongs to the first selected row and has a matching
83    * column index.
84    * @param table the target <code>JTable</code>.
85    * @param cellReader knows how to read the contents of a cell in a <code>JTable</code>.
86    * @return the cell found, if any.
87    * @throws ActionFailedException if a matching cell could not be found.
88    */
 
89  2 toggle public TableCell findCell(JTable table, JTableCellReader cellReader) {
90  2 int selectedRow = selectedRowOf(table);
91  1 if (selectedRow == -1) throw actionFailure("The given JTable does not have any selection");
92  1 return new TableCell(selectedRow, column);
93    }
94   
 
95  2 toggle @RunsInEDT
96    private static int selectedRowOf(final JTable table) {
97  2 return execute(new GuiQuery<Integer>() {
 
98  2 toggle protected Integer executeInEDT() {
99  2 return table.getSelectedRow();
100    }
101    });
102    }
103   
 
104  1 toggle @Override public String toString() {
105  1 return concat(getClass().getName(), "[column=", valueOf(column), "]");
106    }
107    }