-
Notifications
You must be signed in to change notification settings - Fork 2
/
DlgAddColumn.java
306 lines (284 loc) · 11.8 KB
/
DlgAddColumn.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tappas;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.stage.Window;
import tappas.DataApp.EnumData;
import java.util.*;
/**
*
* @author Hector del Risco - [email protected] & Pedro Salguero - [email protected]
*/
public class DlgAddColumn extends DlgBase {
TextField txtColName;
CheckBox chkAutoName, chkPrefix;
TreeViewFeatures tvFeatures;
TreeView tvColumns;
ArrayList<String> lstColNames;
ArrayList<String> lstColIDSels = new ArrayList<>();
ArrayList<String> lstColumns = new ArrayList<>();
HashMap<String, HashMap<String, Object>> hmFeatureSels = new HashMap<>();
public DlgAddColumn(Project project, Window window) {
super(project, window);
}
public Params showAndWait(ArrayList<String> lstColNames, HashMap<String, String> dfltValues) {
if(createDialog("AddColumn.fxml", "Add Annotation Features Column(s)", true, "Help_Dlg_AddColumn.html")) {
this.lstColNames = lstColNames;
// create parameters object
Params params = new Params(dfltValues);
// get control objects
txtColName = (TextField) scene.lookup("#txtColName");
chkAutoName = (CheckBox) scene.lookup("#chkAutoName");
chkAutoName.setOnAction((event) -> { if(chkAutoName.isSelected()) annotationFeaturesOnSelectionChanged(); });
txtColName.disableProperty().bind(chkAutoName.selectedProperty());
tvColumns = (TreeView) scene.lookup("#tvColumns");
tvColumns.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
chkPrefix = (CheckBox) scene.lookup("#chkPrefix");
tvFeatures = new TreeViewFeatures(project, this);
// populate dialog
setDefaultValues(params);
tvFeatures.initialize((TreeView) scene.lookup("#tvFeatures"), hmFeatureSels, true);
annotationFeaturesOnSelectionChanged();
// setup dialog event handlers
dialog.setOnCloseRequest((DialogEvent event) -> {
if(dialog.getResult() != null && dialog.getResult().containsKey("ERRMSG")) {
showDlgMsg((String)dialog.getResult().get("ERRMSG"));
dialog.setResult(null);
event.consume();
}
});
dialog.setResultConverter((ButtonType b) -> {
HashMap<String, String> dlgParams = null;
System.out.println(b.getButtonData().toString());
if (b.getButtonData() == ButtonBar.ButtonData.OK_DONE) {
dlgParams = validate(dialog);
}
return dlgParams;
});
// process dialog
Optional<HashMap> result = dialog.showAndWait();
if(result.isPresent())
return (new Params(result.get()));
}
return null;
}
private void setDefaultValues(Params params) {
// set default values
txtColName.setText(params.name);
chkAutoName.setSelected(params.autoName);
chkPrefix.setSelected(params.prefix);
// default to add ID if none set
if(params.lstColumnSels.isEmpty())
params.lstColumnSels.add(Params.ColType.ID.name());
initColumns(params);
hmFeatureSels.clear();
if(!params.features.isEmpty()) {
// we don't currently have default values passed for selected features since we don't save the params
// to support it - just change Params and code to be like in DlgFEAnalysis (also single source features)
System.out.println("Default features provided - not being currently handled in code!");
}
}
private void initColumns(Params params) {
TreeItem<String> rootItem = new TreeItem<> ("Columns");
HashMap<String, Object> hm;
for(EnumData ed : Params.lstColTypes) {
CheckBoxTreeItem<String> item = new CheckBoxTreeItem<> (ed.name);
if(params.lstColumnSels.contains(ed.id))
item.setSelected(true);
rootItem.getChildren().add(item);
}
rootItem.setExpanded(true);
tvColumns.setShowRoot(false);
tvColumns.setRoot(rootItem);
}
@Override
protected void annotationFeaturesOnSelectionChanged() {
if(chkAutoName.isSelected()) {
String feature = "";
TreeItem<String> rootItem = tvFeatures.tv.getRoot();
if(rootItem != null) {
ObservableList<TreeItem<String>> lst = rootItem.getChildren();
for(TreeItem ti : lst) {
CheckBoxTreeItem<String> item = (CheckBoxTreeItem<String>) ti;
if(item.isSelected() || item.isIndeterminate()) {
feature = item.getValue();
break;
}
}
}
if(feature.isEmpty())
feature = "<SelectedFeature>";
String name = feature;
if(name.length() > Params.MAX_NAME_LENGTH)
txtColName.setText(name.substring(0, Params.MAX_NAME_LENGTH));
else
txtColName.setText(name);
}
}
//
// Dialog Validation
//
private HashMap<String, String> validate(Dialog dialog) {
HashMap<String, String> results = new HashMap<>();
String errmsg = "";
String txt;
System.out.println("Validate dialog");
results.put(Params.AUTONAME_PARAM, chkAutoName.isSelected()? "true" : "false");
results.put(Params.PREFIX_PARAM, chkPrefix.isSelected()? "true" : "false");
txt = txtColName.getText().trim();
if(txt.isEmpty())
errmsg = "You must specify a column name.";
if(errmsg.isEmpty()) {
errmsg = validateColumns(results);
if(errmsg.isEmpty()) {
String baseName = txt.toLowerCase();
boolean dup = false;
for(String colId : lstColIDSels) {
String colIdName = Params.getColName(colId, baseName);
for(String colName : lstColNames) {
if(colName.toLowerCase().equals(colIdName.toLowerCase())) {
dup = true;
break;
}
}
if(dup) {
errmsg = "Specified column name is already in use.";
break;
}
}
if(errmsg.isEmpty()) {
results.put(Params.NAME_PARAM, txt);
errmsg = tvFeatures.validate(Params.FEATURE_PARAM, results);
}
else
txtColName.requestFocus();
}
}
else
txtColName.requestFocus();
if(!errmsg.isEmpty())
results.put("ERRMSG", errmsg);
return results;
}
private String validateColumns(HashMap<String, String> results) {
String errmsg = "";
String columns = "";
ObservableList<TreeItem<String>> lst = tvColumns.getRoot().getChildren();
String col;
lstColIDSels.clear();
for(TreeItem ti : lst) {
CheckBoxTreeItem<String> item = (CheckBoxTreeItem<String>) ti;
if(item.isSelected()) {
col = item.getValue();
EnumData ed = Params.getColTypeDef(col);
lstColIDSels.add(ed.id);
if(!columns.isEmpty())
columns += ";";
columns += ed.id;
}
}
if(!columns.isEmpty())
results.put(Params.COLUMNS_PARAM, columns);
else {
tvColumns.requestFocus();
errmsg = "You must select at least one column to add.";
}
return errmsg;
}
//
// Data Classes
//
public static class Params {
public static int MAX_NAME_LENGTH = 30;
// parameters, always specified
public static final String NAME_PARAM = "name";
public static final String AUTONAME_PARAM = "autoName";
public static final String COLUMNS_PARAM = "columns";
public static final String PREFIX_PARAM = "prefix";
public static final String FEATURE_PARAM = "feature1";
static public enum ColType {
COUNT, ID, DESCRIPTION
}
private static final List<EnumData> lstColTypes = Arrays.asList(
new EnumData(ColType.COUNT.name(), "Number of unique features (\"#ColumnTitle\")"),
new EnumData(ColType.ID.name(), "Feature ID (\"ColumnTitle ID\")"),
new EnumData(ColType.DESCRIPTION.name(), "Feature description (\"ColumnTitle Description\")")
);
String name, columns, features;
boolean autoName, prefix;
HashMap<String, String> hmParams;
ArrayList<String> lstColumnSels = new ArrayList<>();
public Params(HashMap<String, String> hmParams) {
this.hmParams = hmParams;
name = "";
columns = "";
features = "";
autoName = true;
prefix = false;
// set parameter values
if(hmParams.containsKey(NAME_PARAM))
name = hmParams.get(NAME_PARAM);
if(hmParams.containsKey(AUTONAME_PARAM))
autoName = Boolean.valueOf(hmParams.get(AUTONAME_PARAM));
if(hmParams.containsKey(COLUMNS_PARAM))
columns = hmParams.get(COLUMNS_PARAM);
if(!columns.isEmpty()) {
String[] fields = columns.split(";");
for(String col : fields)
lstColumnSels.add(col);
}
if(hmParams.containsKey(PREFIX_PARAM))
prefix = Boolean.valueOf(hmParams.get(PREFIX_PARAM));
if(hmParams.containsKey(FEATURE_PARAM))
features = hmParams.get(FEATURE_PARAM);
}
public ArrayList<String> getSelectedColNames() {
ArrayList<String> lstColNames = new ArrayList<>();
for(String id : lstColumnSels)
lstColNames.add(getColName(id, name));
return lstColNames;
}
public HashMap<String, HashMap<String, Object>> getFeatures() {
// NOTE: single source features
HashMap<String, HashMap<String, Object>> hmFeatures = new HashMap<>();
String[] fields = features.split("\t");
HashMap<String, Object> hm = new HashMap<>();
for(int i = 0; i < fields.length; i++) {
if(i == 0)
hmFeatures.put(fields[0], hm);
else
hm.put(fields[i], null);
}
return hmFeatures;
}
//
// Static Functions
//
private static EnumData getColTypeDef(String name) {
int idx = 0;
for(EnumData entry : lstColTypes) {
if(entry.name.equals(name))
break;
idx++;
}
if(idx >= lstColTypes.size())
idx = 0;
return lstColTypes.get(idx);
}
public static String getColName(String colId, String baseName) {
String colName = baseName;
if(colId.equals(ColType.COUNT.name()))
colName = "#" + baseName;
else if(colId.equals(ColType.ID.name()))
colName = baseName + " ID";
else if(colId.equals(ColType.DESCRIPTION.name()))
colName = baseName + " Description";
return colName;
}
}
}