1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.vaadin.view.dbstatus;
|
11
|
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import com.vaadin.data.Property;
|
16
|
import com.vaadin.data.Property.ValueChangeEvent;
|
17
|
import com.vaadin.data.Property.ValueChangeListener;
|
18
|
import com.vaadin.data.util.sqlcontainer.RowId;
|
19
|
import com.vaadin.server.VaadinSession;
|
20
|
import com.vaadin.ui.AbstractLayout;
|
21
|
import com.vaadin.ui.Alignment;
|
22
|
import com.vaadin.ui.Button.ClickEvent;
|
23
|
import com.vaadin.ui.Button.ClickListener;
|
24
|
import com.vaadin.ui.ComboBox;
|
25
|
import com.vaadin.ui.HorizontalLayout;
|
26
|
import com.vaadin.ui.ListSelect;
|
27
|
import com.vaadin.ui.Notification;
|
28
|
import com.vaadin.ui.Table.ColumnHeaderMode;
|
29
|
import com.vaadin.ui.TreeTable;
|
30
|
import com.vaadin.ui.VerticalLayout;
|
31
|
|
32
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
33
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
35
|
import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
|
36
|
import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
|
37
|
import eu.etaxonomy.cdm.vaadin.container.TaxonTreeContainer;
|
38
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
39
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
40
|
|
41
|
/**
|
42
|
*
|
43
|
* @author pplitzner
|
44
|
*
|
45
|
*/
|
46
|
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener{
|
47
|
|
48
|
private static final long serialVersionUID = 1439411115014088780L;
|
49
|
private ComboBox classificationBox;
|
50
|
private ComboBox distAreaBox;
|
51
|
private ListSelect namedAreaList;
|
52
|
private TreeTable taxonTree;
|
53
|
/**
|
54
|
* The constructor should first build the main layout, set the
|
55
|
* composition root and then do any custom initialization.
|
56
|
*
|
57
|
* The constructor will not be automatically regenerated by the
|
58
|
* visual editor.
|
59
|
* @param distributionTableView
|
60
|
*/
|
61
|
public DistributionSettingsConfigWindow(DistributionTableView distributionTableView) {
|
62
|
super();
|
63
|
}
|
64
|
|
65
|
protected void init() {
|
66
|
//init classification and taxon selection
|
67
|
TaxonNode chosenTaxonNode = presenter.getChosenTaxonNode();
|
68
|
|
69
|
classificationBox.setContainerDataSource(new TaxonNodeContainer(null));
|
70
|
Object classificationSelection = null;
|
71
|
if(classificationBox.getItemIds().size()==1){
|
72
|
//only one classification exists
|
73
|
classificationSelection = classificationBox.getItemIds().iterator().next();
|
74
|
}
|
75
|
else if(chosenTaxonNode!=null){
|
76
|
//get the classification from the selected taxon node
|
77
|
classificationSelection = chosenTaxonNode.getClassification().getRootNode();
|
78
|
}
|
79
|
if(classificationSelection!=null){
|
80
|
classificationBox.setValue(classificationSelection);
|
81
|
try {
|
82
|
taxonTree.setContainerDataSource(new TaxonTreeContainer((TaxonNode) classificationSelection));
|
83
|
taxonTree.setVisibleColumns("Name");
|
84
|
} catch (SQLException e) {
|
85
|
// TODO Auto-generated catch block
|
86
|
e.printStackTrace();
|
87
|
}
|
88
|
if(chosenTaxonNode!=null){
|
89
|
taxonTree.select(new RowId(chosenTaxonNode.getId()));
|
90
|
}
|
91
|
}
|
92
|
classificationBox.addValueChangeListener(this);
|
93
|
|
94
|
TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
|
95
|
distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
|
96
|
distAreaBox.setValue(chosenArea);
|
97
|
distAreaBox.addValueChangeListener(this);
|
98
|
|
99
|
if(chosenArea!=null){
|
100
|
NamedAreaContainer container = new NamedAreaContainer(chosenArea);
|
101
|
namedAreaList.setContainerDataSource(container);
|
102
|
}
|
103
|
Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
|
104
|
namedAreaList.setValue(selectedAreas);
|
105
|
|
106
|
okButton.addClickListener(this);
|
107
|
cancelButton.addClickListener(this);
|
108
|
}
|
109
|
|
110
|
protected AbstractLayout buildMainLayout() {
|
111
|
|
112
|
mainLayout = new VerticalLayout();
|
113
|
mainLayout.setSizeFull();
|
114
|
|
115
|
HorizontalLayout leftAndRightContainer = new HorizontalLayout();
|
116
|
leftAndRightContainer.setImmediate(false);
|
117
|
leftAndRightContainer.setSizeFull();
|
118
|
leftAndRightContainer.setMargin(true);
|
119
|
leftAndRightContainer.setSpacing(true);
|
120
|
|
121
|
VerticalLayout leftContainer = new VerticalLayout();
|
122
|
leftContainer.setImmediate(false);
|
123
|
leftContainer.setSpacing(true);
|
124
|
leftContainer.setSizeFull();
|
125
|
|
126
|
VerticalLayout rightContainer = new VerticalLayout();
|
127
|
rightContainer.setImmediate(false);
|
128
|
rightContainer.setSpacing(true);
|
129
|
rightContainer.setSizeFull();
|
130
|
|
131
|
//classification and term
|
132
|
classificationBox = new ComboBox("Classification");
|
133
|
classificationBox.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
|
134
|
classificationBox.setInputPrompt("Please select a classification...");
|
135
|
classificationBox.setImmediate(true);
|
136
|
classificationBox.setNewItemsAllowed(false);
|
137
|
classificationBox.setNullSelectionAllowed(false);
|
138
|
classificationBox.setSizeFull();
|
139
|
classificationBox.setWidth("100%");
|
140
|
|
141
|
//distribution area box
|
142
|
distAreaBox = new ComboBox("Distribution Area:");
|
143
|
distAreaBox.setInputPrompt("Please select a distribution area...");
|
144
|
distAreaBox.setImmediate(true);
|
145
|
distAreaBox.setNullSelectionAllowed(false);
|
146
|
distAreaBox.setNewItemsAllowed(false);
|
147
|
distAreaBox.setSizeFull();
|
148
|
distAreaBox.setWidth("100%");
|
149
|
|
150
|
// named areas
|
151
|
namedAreaList = new ListSelect();
|
152
|
namedAreaList.setCaption("Areas");
|
153
|
namedAreaList.setSizeFull();
|
154
|
namedAreaList.setMultiSelect(true);
|
155
|
|
156
|
//taxonomy
|
157
|
taxonTree = new TreeTable("Taxonomy");
|
158
|
taxonTree.setSelectable(true);
|
159
|
taxonTree.setSizeFull();
|
160
|
taxonTree.setImmediate(true);
|
161
|
taxonTree.setCacheRate(20);
|
162
|
taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
|
163
|
|
164
|
leftContainer.addComponent(distAreaBox);
|
165
|
leftContainer.addComponent(namedAreaList);
|
166
|
leftContainer.setExpandRatio(distAreaBox, 0.1f);
|
167
|
leftContainer.setExpandRatio(namedAreaList, 0.9f);
|
168
|
leftContainer.setSizeFull();
|
169
|
|
170
|
rightContainer.addComponent(classificationBox);
|
171
|
rightContainer.addComponent(taxonTree);
|
172
|
rightContainer.setExpandRatio(classificationBox, 0.1f);
|
173
|
rightContainer.setExpandRatio(taxonTree, 0.9f);
|
174
|
|
175
|
leftAndRightContainer.addComponent(leftContainer);
|
176
|
leftAndRightContainer.addComponent(rightContainer);
|
177
|
|
178
|
//button toolbar
|
179
|
HorizontalLayout buttonToolBar = createOkCancelButtons();
|
180
|
|
181
|
mainLayout.addComponent(leftAndRightContainer);
|
182
|
mainLayout.addComponent(buttonToolBar);
|
183
|
mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
|
184
|
mainLayout.setExpandRatio(buttonToolBar, 0.1f);
|
185
|
mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
|
186
|
|
187
|
return leftAndRightContainer;
|
188
|
}
|
189
|
|
190
|
@Override
|
191
|
public void valueChange(ValueChangeEvent event) {
|
192
|
Property property = event.getProperty();
|
193
|
if(property==classificationBox){
|
194
|
TaxonNode parentNode = (TaxonNode) event.getProperty().getValue();
|
195
|
if(parentNode!=null){
|
196
|
try {
|
197
|
taxonTree.setContainerDataSource(new TaxonTreeContainer(parentNode));
|
198
|
taxonTree.setVisibleColumns("Name");
|
199
|
} catch (SQLException e) {
|
200
|
// TODO Auto-generated catch block
|
201
|
e.printStackTrace();
|
202
|
}
|
203
|
}
|
204
|
else{
|
205
|
taxonTree.setContainerDataSource(null);
|
206
|
}
|
207
|
}
|
208
|
|
209
|
else if(property==distAreaBox){
|
210
|
TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
|
211
|
NamedAreaContainer container = new NamedAreaContainer(vocabulary);
|
212
|
namedAreaList.setContainerDataSource(container);
|
213
|
}
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
public void buttonClick(ClickEvent event) {
|
218
|
Object source = event.getSource();
|
219
|
if(source==okButton){
|
220
|
TaxonNode taxonNode = null;
|
221
|
TermVocabulary<NamedArea> term = null;
|
222
|
//TODO use field converter
|
223
|
if(taxonTree.getValue()!=null){
|
224
|
taxonNode = CdmSpringContextHelper.getTaxonNodeService().find((Integer)((RowId) taxonTree.getValue()).getId()[0]);
|
225
|
}
|
226
|
if(taxonNode==null){
|
227
|
taxonNode = (TaxonNode) classificationBox.getValue();
|
228
|
}
|
229
|
term = (TermVocabulary<NamedArea>) distAreaBox.getValue();
|
230
|
Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
|
231
|
if(taxonNode==null){
|
232
|
Notification.show("Please choose a classification and/or taxon", Notification.Type.HUMANIZED_MESSAGE);
|
233
|
return;
|
234
|
}
|
235
|
if(term==null){
|
236
|
Notification.show("Please choose a distribution area", Notification.Type.HUMANIZED_MESSAGE);
|
237
|
return;
|
238
|
}
|
239
|
DistributionEditorUtil.openDistributionView(taxonNode, term, selectedAreas);
|
240
|
window.close();
|
241
|
}
|
242
|
else if(source==cancelButton){
|
243
|
window.close();
|
244
|
}
|
245
|
}
|
246
|
|
247
|
}
|