1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.view.distributionStatus;
|
10
|
|
11
|
import java.sql.SQLException;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collection;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import com.vaadin.data.Property;
|
19
|
import com.vaadin.data.Property.ValueChangeEvent;
|
20
|
import com.vaadin.data.Property.ValueChangeListener;
|
21
|
import com.vaadin.data.util.BeanItemContainer;
|
22
|
import com.vaadin.data.util.sqlcontainer.RowId;
|
23
|
import com.vaadin.server.Page;
|
24
|
import com.vaadin.server.VaadinSession;
|
25
|
import com.vaadin.ui.AbstractLayout;
|
26
|
import com.vaadin.ui.Alignment;
|
27
|
import com.vaadin.ui.Button.ClickEvent;
|
28
|
import com.vaadin.ui.Button.ClickListener;
|
29
|
import com.vaadin.ui.ComboBox;
|
30
|
import com.vaadin.ui.HorizontalLayout;
|
31
|
import com.vaadin.ui.ListSelect;
|
32
|
import com.vaadin.ui.Notification;
|
33
|
import com.vaadin.ui.Table.ColumnHeaderMode;
|
34
|
import com.vaadin.ui.TextField;
|
35
|
import com.vaadin.ui.Tree.ExpandEvent;
|
36
|
import com.vaadin.ui.Tree.ExpandListener;
|
37
|
import com.vaadin.ui.TreeTable;
|
38
|
import com.vaadin.ui.UI;
|
39
|
import com.vaadin.ui.VerticalLayout;
|
40
|
|
41
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
42
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
43
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
45
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
46
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
47
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
48
|
import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
|
49
|
import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
|
50
|
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
|
51
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
52
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
53
|
import eu.etaxonomy.cdm.vaadin.view.distributionStatus.settings.AreaAndTaxonSettingsPresenter;
|
54
|
|
55
|
/**
|
56
|
*
|
57
|
* @author pplitzner
|
58
|
*
|
59
|
*/
|
60
|
public class AreaAndTaxonSettingsConfigWindow
|
61
|
extends SettingsDialogWindowBase<AreaAndTaxonSettingsPresenter>
|
62
|
implements ValueChangeListener, ClickListener, ExpandListener{
|
63
|
|
64
|
private static final long serialVersionUID = 1439411115014088780L;
|
65
|
private ComboBox classificationBox;
|
66
|
private TextField taxonFilter;
|
67
|
private ComboBox distAreaBox;
|
68
|
private ListSelect namedAreaList;
|
69
|
private TreeTable taxonTree;
|
70
|
IDistributionTableView distributionTableView;
|
71
|
|
72
|
/**
|
73
|
* The constructor should first build the main layout, set the
|
74
|
* composition root and then do any custom initialization.
|
75
|
*
|
76
|
* The constructor will not be automatically regenerated by the
|
77
|
* visual editor.
|
78
|
* @param distributionTableView
|
79
|
*/
|
80
|
public AreaAndTaxonSettingsConfigWindow(IDistributionTableView distributionTableView) {
|
81
|
super();
|
82
|
this.distributionTableView = distributionTableView;
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
protected void init() {
|
87
|
//init classification
|
88
|
Classification classification = presenter.getChosenClassification();
|
89
|
try {
|
90
|
classificationBox.setContainerDataSource(new CdmSQLContainer(CdmQueryFactory.generateTableQuery("Classification")));
|
91
|
} catch (SQLException e) {
|
92
|
DistributionEditorUtil.showSqlError(e);
|
93
|
}
|
94
|
RowId parent = null;
|
95
|
if(classification!=null){
|
96
|
parent = new RowId(classification.getRootNode().getId());
|
97
|
}
|
98
|
else if(classificationBox.getItemIds().size()==1){
|
99
|
//only one classification exists
|
100
|
parent = (RowId) classificationBox.getItemIds().iterator().next();
|
101
|
}
|
102
|
if(parent!=null){
|
103
|
classificationBox.setValue(new RowId(parent.getId()));
|
104
|
showClassificationTaxa(getUuidAndTitleCacheFromRowId(parent));
|
105
|
}
|
106
|
|
107
|
classificationBox.addValueChangeListener(this);
|
108
|
taxonFilter.addValueChangeListener(this);
|
109
|
taxonTree.addExpandListener(this);
|
110
|
|
111
|
//init areas
|
112
|
TermVocabulary<NamedArea> chosenAreaVoc = presenter.getChosenAreaVoc();
|
113
|
distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
|
114
|
distAreaBox.setValue(chosenAreaVoc);
|
115
|
distAreaBox.addValueChangeListener(this);
|
116
|
|
117
|
if(chosenAreaVoc!=null){
|
118
|
NamedAreaContainer container = new NamedAreaContainer(chosenAreaVoc);
|
119
|
namedAreaList.setContainerDataSource(container);
|
120
|
}
|
121
|
Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
|
122
|
namedAreaList.setValue(selectedAreas);
|
123
|
|
124
|
okButton.addClickListener(this);
|
125
|
cancelButton.addClickListener(this);
|
126
|
updateButtons();
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
protected AbstractLayout buildMainLayout() {
|
131
|
|
132
|
mainLayout = new VerticalLayout();
|
133
|
mainLayout.setSizeFull();
|
134
|
|
135
|
HorizontalLayout leftAndRightContainer = new HorizontalLayout();
|
136
|
leftAndRightContainer.setImmediate(false);
|
137
|
leftAndRightContainer.setSizeFull();
|
138
|
leftAndRightContainer.setMargin(true);
|
139
|
leftAndRightContainer.setSpacing(true);
|
140
|
|
141
|
VerticalLayout leftContainer = new VerticalLayout();
|
142
|
leftContainer.setImmediate(false);
|
143
|
leftContainer.setSpacing(true);
|
144
|
leftContainer.setSizeFull();
|
145
|
|
146
|
VerticalLayout rightContainer = new VerticalLayout();
|
147
|
rightContainer.setImmediate(false);
|
148
|
rightContainer.setSpacing(true);
|
149
|
rightContainer.setSizeFull();
|
150
|
|
151
|
//classification
|
152
|
classificationBox = new ComboBox("Classification");
|
153
|
classificationBox.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
|
154
|
classificationBox.setInputPrompt("Please select a classification...");
|
155
|
classificationBox.setImmediate(true);
|
156
|
classificationBox.setNewItemsAllowed(false);
|
157
|
classificationBox.setNullSelectionAllowed(false);
|
158
|
classificationBox.setSizeFull();
|
159
|
classificationBox.setWidth("100%");
|
160
|
|
161
|
//taxonFilter
|
162
|
taxonFilter = new TextField("Filter");
|
163
|
taxonFilter.setInputPrompt("Filter taxa by name...");
|
164
|
taxonFilter.setSizeFull();
|
165
|
taxonFilter.setImmediate(true);
|
166
|
|
167
|
//distribution area box
|
168
|
distAreaBox = new ComboBox("Distribution Area:");
|
169
|
distAreaBox.setInputPrompt("Please select a distribution area...");
|
170
|
distAreaBox.setImmediate(true);
|
171
|
distAreaBox.setNullSelectionAllowed(false);
|
172
|
distAreaBox.setNewItemsAllowed(false);
|
173
|
distAreaBox.setSizeFull();
|
174
|
distAreaBox.setWidth("100%");
|
175
|
|
176
|
// named areas
|
177
|
namedAreaList = new ListSelect();
|
178
|
namedAreaList.setCaption("Areas");
|
179
|
namedAreaList.setSizeFull();
|
180
|
namedAreaList.setMultiSelect(true);
|
181
|
|
182
|
//taxonomy
|
183
|
taxonTree = new TreeTable("Taxonomy");
|
184
|
taxonTree.setSelectable(true);
|
185
|
taxonTree.setSizeFull();
|
186
|
taxonTree.setImmediate(true);
|
187
|
taxonTree.setCacheRate(20);
|
188
|
taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
|
189
|
taxonTree.setMultiSelect(true);
|
190
|
|
191
|
leftContainer.addComponent(distAreaBox);
|
192
|
leftContainer.addComponent(namedAreaList);
|
193
|
leftContainer.setExpandRatio(distAreaBox, 0.1f);
|
194
|
leftContainer.setExpandRatio(namedAreaList, 0.9f);
|
195
|
leftContainer.setSizeFull();
|
196
|
|
197
|
rightContainer.addComponent(classificationBox);
|
198
|
rightContainer.addComponent(taxonFilter);
|
199
|
rightContainer.addComponent(taxonTree);
|
200
|
rightContainer.setExpandRatio(classificationBox, 0.1f);
|
201
|
rightContainer.setExpandRatio(taxonFilter, 0.1f);
|
202
|
rightContainer.setExpandRatio(taxonTree, 0.8f);
|
203
|
|
204
|
leftAndRightContainer.addComponent(leftContainer);
|
205
|
leftAndRightContainer.addComponent(rightContainer);
|
206
|
|
207
|
//button toolbar
|
208
|
HorizontalLayout buttonToolBar = createOkCancelButtons();
|
209
|
|
210
|
mainLayout.addComponent(leftAndRightContainer);
|
211
|
mainLayout.addComponent(buttonToolBar);
|
212
|
mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
|
213
|
mainLayout.setExpandRatio(buttonToolBar, 0.1f);
|
214
|
mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
|
215
|
|
216
|
return leftAndRightContainer;
|
217
|
}
|
218
|
|
219
|
@Override
|
220
|
public void valueChange(ValueChangeEvent event) {
|
221
|
Property<?> property = event.getProperty();
|
222
|
if(property==classificationBox){
|
223
|
UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
|
224
|
showClassificationTaxa(parent);
|
225
|
}
|
226
|
else if(property==taxonFilter){
|
227
|
String filterText = taxonFilter.getValue();
|
228
|
Property<?> uuidProperty = classificationBox.getContainerProperty(classificationBox.getValue(),"uuid");
|
229
|
if(uuidProperty==null){
|
230
|
Notification.show("Please select a classification");
|
231
|
}
|
232
|
else{
|
233
|
if(CdmUtils.isNotBlank(filterText)){
|
234
|
UUID classificationUuid = UUID.fromString((String) uuidProperty.getValue());
|
235
|
List<UuidAndTitleCache<TaxonNode>> taxa = CdmSpringContextHelper.getTaxonNodeService().getUuidAndTitleCache(null, filterText, classificationUuid);
|
236
|
BeanItemContainer<UuidAndTitleCache<TaxonNode>> container = new BeanItemContainer<>(UuidAndTitleCache.class);
|
237
|
taxonTree.setContainerDataSource(container);
|
238
|
for (UuidAndTitleCache<TaxonNode> taxon : taxa) {
|
239
|
container.addItem(taxon);
|
240
|
taxonTree.setChildrenAllowed(taxon, false);
|
241
|
}
|
242
|
taxonTree.setVisibleColumns("titleCache");
|
243
|
}
|
244
|
else{
|
245
|
UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
|
246
|
showClassificationTaxa(parent);
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
else if(property==distAreaBox){
|
251
|
TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
|
252
|
NamedAreaContainer container = new NamedAreaContainer(vocabulary);
|
253
|
namedAreaList.setContainerDataSource(container);
|
254
|
}
|
255
|
updateButtons();
|
256
|
}
|
257
|
|
258
|
@Override
|
259
|
protected boolean isValid() {
|
260
|
return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
|
261
|
}
|
262
|
|
263
|
@Override
|
264
|
public void buttonClick(ClickEvent event) {
|
265
|
Object source = event.getSource();
|
266
|
if(source==okButton){
|
267
|
List<UUID> taxonNodes = new ArrayList<>();
|
268
|
TermVocabulary<NamedArea> areaVoc = null;
|
269
|
String uuidString = (String) classificationBox.getContainerProperty(classificationBox.getValue(),"uuid").getValue();
|
270
|
UUID classificationUuid = UUID.fromString(uuidString);
|
271
|
Set<UuidAndTitleCache<TaxonNode>> treeSelection = (Set<UuidAndTitleCache<TaxonNode>>) taxonTree.getValue();
|
272
|
if(!treeSelection.isEmpty()){
|
273
|
for (UuidAndTitleCache<TaxonNode> uuidAndTitleCache : treeSelection) {
|
274
|
taxonNodes.add(uuidAndTitleCache.getUuid());
|
275
|
}
|
276
|
}
|
277
|
areaVoc = (TermVocabulary<NamedArea>) distAreaBox.getValue();
|
278
|
Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
|
279
|
DistributionEditorUtil.updateDistributionView(distributionTableView, taxonNodes, areaVoc, selectedAreas, classificationUuid);
|
280
|
window.close();
|
281
|
}
|
282
|
else if(source==cancelButton){
|
283
|
window.close();
|
284
|
}
|
285
|
}
|
286
|
|
287
|
@Override
|
288
|
public void nodeExpand(ExpandEvent event) {
|
289
|
UuidAndTitleCache<TaxonNode> parent = (UuidAndTitleCache<TaxonNode>) event.getItemId();
|
290
|
((TaxonNodeContainer) taxonTree.getContainerDataSource()).addChildItems(parent);
|
291
|
}
|
292
|
|
293
|
private void showClassificationTaxa(UuidAndTitleCache<TaxonNode> parent) {
|
294
|
final Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(parent);
|
295
|
// Enable polling and set frequency to 0.5 seconds
|
296
|
UI.getCurrent().setPollInterval(500);
|
297
|
taxonTree.setEnabled(false);
|
298
|
taxonTree.removeAllItems();
|
299
|
Notification.show("Loading taxa...");
|
300
|
|
301
|
new TreeUpdater(children).start();
|
302
|
}
|
303
|
|
304
|
private UuidAndTitleCache<TaxonNode> getUuidAndTitleCacheFromRowId(Object classificationSelection) {
|
305
|
String uuidString = (String) classificationBox.getContainerProperty(classificationSelection, "uuid").getValue();
|
306
|
Property<Integer> rootNodeContainerProperty = null;
|
307
|
|
308
|
Collection<?> ids = classificationBox.getContainerPropertyIds();
|
309
|
//use for loop here because the case of the root node id columns differs between some DBs
|
310
|
for (Object id : ids) {
|
311
|
if(id instanceof String && ((String) id).toLowerCase().equals("rootnode_id")){
|
312
|
rootNodeContainerProperty = classificationBox.getContainerProperty(classificationSelection, id);
|
313
|
break;
|
314
|
}
|
315
|
}
|
316
|
int id = rootNodeContainerProperty.getValue();
|
317
|
String titleCache = (String) classificationBox.getContainerProperty(classificationSelection, "titleCache").getValue();
|
318
|
UUID uuid = UUID.fromString(uuidString);
|
319
|
UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
|
320
|
return parent;
|
321
|
}
|
322
|
|
323
|
|
324
|
/**
|
325
|
* {@inheritDoc}
|
326
|
*/
|
327
|
@Override
|
328
|
protected AreaAndTaxonSettingsPresenter getPresenter() {
|
329
|
return new AreaAndTaxonSettingsPresenter();
|
330
|
}
|
331
|
|
332
|
private class TreeUpdater extends Thread{
|
333
|
|
334
|
private Collection<UuidAndTitleCache<TaxonNode>> children;
|
335
|
|
336
|
|
337
|
public TreeUpdater(Collection<UuidAndTitleCache<TaxonNode>> children) {
|
338
|
this.children = children;
|
339
|
}
|
340
|
|
341
|
@Override
|
342
|
public void run() {
|
343
|
UI.getCurrent().access(new Runnable() {
|
344
|
@Override
|
345
|
public void run() {
|
346
|
taxonTree.setContainerDataSource(new TaxonNodeContainer(children));
|
347
|
|
348
|
Notification notification = new Notification("Loading complete");
|
349
|
notification.setDelayMsec(500);
|
350
|
notification.show(Page.getCurrent());
|
351
|
taxonTree.setEnabled(true);
|
352
|
|
353
|
//disable polling when all taxa are loaded
|
354
|
UI.getCurrent().setPollInterval(-1);
|
355
|
}
|
356
|
});
|
357
|
}
|
358
|
}
|
359
|
|
360
|
}
|