| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 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 |
|
@link |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
@link@link |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
@author |
| 43 |
|
|
| 44 |
|
@since |
| 45 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (47) |
Complexity: 15 |
Complexity Density: 0.6 |
|
| 46 |
|
public class TableCellInRowByValue implements TableCellFinder { |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@link |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
@param |
| 58 |
|
@return |
| 59 |
|
@throws |
| 60 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 61 |
7
|
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 |
|
@link |
| 68 |
|
|
| 69 |
|
@author |
| 70 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 71 |
|
public static class TableCellBuilder { |
| 72 |
|
private final String[] values; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@link |
| 76 |
|
@param |
| 77 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 78 |
6
|
TableCellBuilder(String[] values) {... |
| 79 |
6
|
this.values = values; |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@link |
| 85 |
|
|
| 86 |
|
@param |
| 87 |
|
@return |
| 88 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 89 |
6
|
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 |
|
@link |
| 99 |
|
@param |
| 100 |
|
@param |
| 101 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 102 |
6
|
protected TableCellInRowByValue(String[] values, int column) {... |
| 103 |
6
|
this.values = values; |
| 104 |
6
|
this.column = column; |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
@link |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
@param |
| 114 |
|
@param |
| 115 |
|
@return |
| 116 |
|
@throws |
| 117 |
|
|
| 118 |
|
@throws |
| 119 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 120 |
5
|
@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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 128 |
5
|
@RunsInEDT... |
| 129 |
|
private static int findRowIndex(final JTable table, final JTableCellReader cellReader, final String[] values) { |
| 130 |
5
|
return execute(new GuiQuery<Integer>() { |
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 131 |
5
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 142 |
5
|
@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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 149 |
17
|
@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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 158 |
1
|
@Override public String toString() {... |
| 159 |
1
|
return concat(getClass().getName(), "[values=", format(values), ", column=", column, "]"); |
| 160 |
|
} |
| 161 |
|
} |