-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathMappingTablePanel.java
More file actions
367 lines (327 loc) · 12.2 KB
/
Copy pathMappingTablePanel.java
File metadata and controls
367 lines (327 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*******************************************************************************
* Copyright 2019 Observational Health Data Sciences and Informatics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.ohdsi.usagi.ui;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
import org.ohdsi.usagi.CodeMapping;
import org.ohdsi.usagi.Equivalence;
import org.ohdsi.usagi.CodeMapping.MappingStatus;
import org.ohdsi.usagi.Concept;
import static org.ohdsi.usagi.ui.DataChangeEvent.*;
public class MappingTablePanel extends JPanel implements DataChangeListener {
private static final long serialVersionUID = -5862314086097240860L;
private UsagiTable table;
private CodeMapTableModel tableModel;
private List<CodeSelectedListener> listeners = new ArrayList<>();
private boolean ignoreSelection = false;
public MappingTablePanel() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
tableModel = new CodeMapTableModel();
table = new UsagiTable(tableModel);
table.setRowSorter(new TableRowSorter<>(tableModel));
table.setPreferredScrollableViewportSize(new Dimension(1200, 200));
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.getSelectionModel().addListSelectionListener(event -> {
if (!ignoreSelection) {
int primaryViewRow = table.getSelectedRow();
if (primaryViewRow != -1) {
int primaryModelRow = table.convertRowIndexToModel(primaryViewRow);
for (CodeSelectedListener listener : listeners) {
listener.codeSelected(tableModel.getCodeMapping(primaryModelRow));
listener.clearCodeMultiSelected();
}
Global.googleSearchAction.setEnabled(true);
Global.googleSearchAction.setSourceTerm(tableModel.getCodeMapping(primaryModelRow).getSourceCode().sourceName);
Global.approveAction.setEnabled(true);
Global.clearSelectedAction.setEnabled(true);
if (tableModel.getCodeMapping(primaryModelRow).getTargetConcepts().size() > 0) {
Concept firstConcept = tableModel.getCodeMapping(primaryModelRow).getTargetConcepts().get(0).getConcept();
Global.conceptInfoAction.setEnabled(true);
Global.conceptInformationDialog.setActiveConcept(firstConcept);
Global.athenaAction.setEnabled(true);
Global.athenaAction.setConcept(firstConcept);
}
// Store all other co-selected rows
for (int viewRow : table.getSelectedRows()) {
if (viewRow != -1 && viewRow != primaryViewRow) {
int modelRow = table.convertRowIndexToModel(viewRow);
for (CodeSelectedListener listener : listeners) {
listener.addCodeMultiSelected(tableModel.getCodeMapping(modelRow));
}
}
}
} else {
Global.approveAction.setEnabled(false);
Global.clearSelectedAction.setEnabled(false);
}
}
});
// Hide some less-informative columns:
table.hideColumn("Valid start date");
table.hideColumn("Valid end date");
table.hideColumn("Invalid reason");
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
Global.mapping.addListener(this);
}
class CodeMapTableModel extends AbstractTableModel {
private static final long serialVersionUID = 169286268154988911L;
private String[] defaultColumnNames = { "Status", "Source code", "Source term", "Frequency", "Match score", "Concept ID", "Concept name",
"Domain", "Concept class", "Vocabulary", "Concept code", "Valid start date", "Valid end date", "Invalid reason", "Standard concept", "Parents",
"Children", "Assigned To", "Equivalence", "Comment", "Status Provenance" };
private String[] columnNames = defaultColumnNames;
private int addInfoColCount = 0;
private final int ADD_INFO_START_COL = 4;
private static final int ASSIGNED_REVIEWER_COL = 17; // special meaning, as
public int getColumnCount() {
return columnNames.length;
}
public CodeMapping getCodeMapping(int modelRow) {
return Global.mapping.get(modelRow);
}
public void restructure() {
columnNames = defaultColumnNames;
addInfoColCount = 0;
if (Global.mapping.size() != 0) {
List<String> additionalColumns = Global.mapping.getAdditionalColumnNames();
addInfoColCount = additionalColumns.size();
columnNames = new String[defaultColumnNames.length + addInfoColCount];
for (int i = 0; i < ADD_INFO_START_COL; i++)
columnNames[i] = defaultColumnNames[i];
for (int i = 0; i < addInfoColCount; i++)
columnNames[i + ADD_INFO_START_COL] = additionalColumns.get(i);
for (int i = ADD_INFO_START_COL; i < defaultColumnNames.length; i++)
columnNames[i + addInfoColCount] = defaultColumnNames[i];
}
fireTableStructureChanged();
table.setRowSelectionInterval(0, 0);
}
public int getRowCount() {
return Global.mapping.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
CodeMapping codeMapping = Global.mapping.get(row);
if (col >= ADD_INFO_START_COL && col < ADD_INFO_START_COL + addInfoColCount) {
return codeMapping.getSourceCode().sourceAdditionalInfo.get(col - ADD_INFO_START_COL).getItem2();
} else {
col = resolveColumnIndex(col);
Concept targetConcept;
if (codeMapping.getTargetConcepts().size() > 0)
targetConcept = codeMapping.getTargetConcepts().get(0).getConcept();
else
targetConcept = Concept.EMPTY_CONCEPT;
switch (col) {
case 0:
return codeMapping.getMappingStatus();
case 1:
return codeMapping.getSourceCode().sourceCode;
case 2:
return codeMapping.getSourceCode().sourceName;
case 3:
return codeMapping.getSourceCode().sourceFrequency == -1 ? "" : codeMapping.getSourceCode().sourceFrequency;
case 4:
return codeMapping.getMatchScore();
case 5:
return targetConcept.conceptId;
case 6:
return targetConcept.conceptName;
case 7:
return targetConcept.domainId;
case 8:
return targetConcept.conceptClassId;
case 9:
return targetConcept.vocabularyId;
case 10:
return targetConcept.conceptCode;
case 11:
return targetConcept.validStartDate;
case 12:
return targetConcept.validEndDate;
case 13:
return targetConcept.invalidReason;
case 14:
return targetConcept.standardConcept;
case 15:
return targetConcept.parentCount;
case 16:
return targetConcept.childCount;
case ASSIGNED_REVIEWER_COL:
return codeMapping.getAssignedReviewer();
case 18:
if (codeMapping.getEquivalence() != Equivalence.UNREVIEWED) {
return codeMapping.getEquivalence();
} else {
return null;
}
case 19:
return codeMapping.getComment();
case 20:
if (codeMapping.getStatusSetOn() != 0L) {
return String.format("%s (%tF)", codeMapping.getStatusSetBy(), codeMapping.getStatusSetOn());
}
default:
return "";
}
}
}
public Class<?> getColumnClass(int col) {
if (col >= ADD_INFO_START_COL && col < ADD_INFO_START_COL + addInfoColCount) {
return String.class;
} else {
switch (resolveColumnIndex(col)) {
case 0:
return MappingStatus.class;
case 3:
case 5:
case 7:
case 15:
case 16:
return Integer.class;
case 4:
return Double.class;
case 18:
return Equivalence.class;
default:
return String.class;
}
}
}
public boolean isCellEditable(int row, int col) {
col = resolveColumnIndex(col);
if (col == ASSIGNED_REVIEWER_COL) {
return true;
}
return false;
}
public void setValueAt(Object value, int row, int col) {
col = resolveColumnIndex(col);
if (col == ASSIGNED_REVIEWER_COL) {
CodeMapping codeMapping = Global.mapping.get(row);
codeMapping.setAssignedReviewer((String) value);
}
}
private int resolveColumnIndex(int col) {
if (col >= ADD_INFO_START_COL) {
return col - addInfoColCount;
}
return col;
}
}
public void addCodeSelectedListener(CodeSelectedListener listener) {
listeners.add(listener);
}
@Override
public void dataChanged(DataChangeEvent event) {
if (event.approved) {
int row = table.getSelectedRow();
ignoreSelection = true;
tableModel.fireTableDataChanged();
ignoreSelection = false;
if (row < table.getRowCount() - 1) {
table.setRowSelectionInterval(row + 1, row + 1);
table.scrollRectToVisible(new Rectangle(table.getCellRect(row + 1, row + 1, true)));
}
} else if (event.structureChange) {
tableModel.restructure();
table.setRowSelectionInterval(0, 0);
} else if (event.multiUpdate) {
tableModel.fireTableDataChanged();
} else {
tableModel.fireTableRowsUpdated(table.getSelectedRow(), table.getSelectedRow());
}
// Multi selection is lost
for (CodeSelectedListener listener : listeners) {
listener.clearCodeMultiSelected();
}
}
private void fireUpdateEventAll(DataChangeEvent event) {
Global.mapping.fireDataChanged(event);
int viewRow = table.getSelectedRow();
if (viewRow != -1) {
int modelRow = table.convertRowIndexToModel(viewRow);
for (CodeSelectedListener listener : listeners) {
listener.codeSelected(tableModel.getCodeMapping(modelRow));
}
}
}
public void clearSelected() {
for (int viewRow : table.getSelectedRows()) {
int modelRow = table.convertRowIndexToModel(viewRow);
tableModel.getCodeMapping(modelRow).getTargetConcepts().clear();
tableModel.getCodeMapping(modelRow).setUnchecked();
}
fireUpdateEventAll(MULTI_UPDATE_EVENT);
}
public List<CodeMapping> getSelectedCodeMappings() {
List<CodeMapping> selectedCodeMappings = new ArrayList<>();
for (int viewRow : table.getSelectedRows()) {
int modelRow = table.convertRowIndexToModel(viewRow);
selectedCodeMappings.add(tableModel.getCodeMapping(modelRow));
}
return selectedCodeMappings;
}
public void assignReviewersRandomly(String[] reviewers) {
// Randomly assign code mappings to given reviewers
ThreadLocalRandom randomGenerator = ThreadLocalRandom.current();
for (CodeMapping codeMapping : Global.mapping) {
int random = randomGenerator.nextInt(reviewers.length);
codeMapping.setAssignedReviewer(reviewers[random]);
}
fireUpdateEventAll(APPROVE_EVENT);
}
public void assignReviewersEqually(String[] reviewers) {
// Shuffle the code mapping array, then assign reviewers one by one,
// dividing the code mappings equally between reviewers.
// If the number of code mappings is not a multiple of the number of reviewers,
// then the first, second, etc. reviewer get one mapping more assigned.
int nReviewers = reviewers.length;
List<CodeMapping> codesToAssign = new ArrayList<>();
if (table.getSelectedRows().length == 1) {
// If only one row selected, assign all rows to the given reviewers
codesToAssign = Global.mapping;
} else {
// If a multi selection is made, only assign the select rows.
for (int viewRow : table.getSelectedRows()) {
int modelRow = table.convertRowIndexToModel(viewRow);
codesToAssign.add(tableModel.getCodeMapping(modelRow));
}
}
List<Integer> codeMappingIndex = IntStream.range(0, codesToAssign.size())
.boxed().collect(Collectors.toList());
Collections.shuffle(codeMappingIndex);
for (int i = 0; i < codeMappingIndex.size(); i++) {
CodeMapping codeMapping = codesToAssign.get(codeMappingIndex.get(i));
codeMapping.setAssignedReviewer(reviewers[i % nReviewers]);
}
fireUpdateEventAll(APPROVE_EVENT);
}
}