1
|
package eu.etaxonomy.cdm.vaadin.view.distributionStatus;
|
2
|
|
3
|
import java.sql.SQLException;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.Arrays;
|
6
|
import java.util.List;
|
7
|
import java.util.UUID;
|
8
|
|
9
|
import com.vaadin.data.Item;
|
10
|
import com.vaadin.data.Property;
|
11
|
import com.vaadin.data.Property.ValueChangeEvent;
|
12
|
import com.vaadin.data.Property.ValueChangeListener;
|
13
|
import com.vaadin.event.ItemClickEvent;
|
14
|
import com.vaadin.event.ItemClickEvent.ItemClickListener;
|
15
|
import com.vaadin.navigator.View;
|
16
|
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
|
17
|
import com.vaadin.ui.AbsoluteLayout;
|
18
|
import com.vaadin.ui.Button;
|
19
|
import com.vaadin.ui.Button.ClickEvent;
|
20
|
import com.vaadin.ui.Button.ClickListener;
|
21
|
import com.vaadin.ui.CustomComponent;
|
22
|
import com.vaadin.ui.ListSelect;
|
23
|
import com.vaadin.ui.Notification;
|
24
|
import com.vaadin.ui.Notification.Type;
|
25
|
import com.vaadin.ui.Table;
|
26
|
import com.vaadin.ui.UI;
|
27
|
import com.vaadin.ui.VerticalLayout;
|
28
|
import com.vaadin.ui.Window;
|
29
|
|
30
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
31
|
import eu.etaxonomy.cdm.model.common.Language;
|
32
|
import eu.etaxonomy.cdm.model.common.Representation;
|
33
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
34
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
36
|
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
|
37
|
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
|
38
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
39
|
import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
|
40
|
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
|
41
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
42
|
import eu.etaxonomy.cdm.vaadin.util.TermCacher;
|
43
|
|
44
|
public class DistributionTableView extends CustomComponent implements View{
|
45
|
|
46
|
private static final long serialVersionUID = 1L;
|
47
|
private HorizontalToolbar toolbar;
|
48
|
private Table table;
|
49
|
|
50
|
private DistributionTablePresenter listener;
|
51
|
|
52
|
private CdmSQLContainer container;
|
53
|
private DistributionSettingsConfigWindow distributionSettingConfigWindow;
|
54
|
|
55
|
/**
|
56
|
* The constructor should first build the main layout, set the
|
57
|
* composition root and then do any custom initialization.
|
58
|
*
|
59
|
* The constructor will not be automatically regenerated by the
|
60
|
* visual editor.
|
61
|
*/
|
62
|
public DistributionTableView() {
|
63
|
AbsoluteLayout mainLayout = initLayout();
|
64
|
setCompositionRoot(mainLayout);
|
65
|
createEditClickListener();
|
66
|
|
67
|
}
|
68
|
|
69
|
private AbsoluteLayout initLayout() {
|
70
|
AbsoluteLayout mainLayout = new AbsoluteLayout();
|
71
|
mainLayout.setImmediate(false);
|
72
|
mainLayout.setWidth("100%");
|
73
|
mainLayout.setHeight("100%");
|
74
|
|
75
|
setWidth("100.0%");
|
76
|
setHeight("100.0%");
|
77
|
|
78
|
//Horizontal Toolbar
|
79
|
toolbar = new HorizontalToolbar();
|
80
|
mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
|
81
|
|
82
|
// table + formatting
|
83
|
table = new Table(){
|
84
|
private static final long serialVersionUID = -5148756917468804385L;
|
85
|
|
86
|
@Override
|
87
|
protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
|
88
|
String formattedValue = null;
|
89
|
PresenceAbsenceTerm presenceAbsenceTerm = null;
|
90
|
Object value = property.getValue();
|
91
|
if(value instanceof String){
|
92
|
presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) value);
|
93
|
}
|
94
|
if(presenceAbsenceTerm!=null){
|
95
|
Representation representation = presenceAbsenceTerm.getRepresentation(Language.DEFAULT());
|
96
|
if(representation!=null){
|
97
|
if(DistributionEditorUtil.isAbbreviatedLabels()){
|
98
|
formattedValue = representation.getAbbreviatedLabel();
|
99
|
}
|
100
|
else{
|
101
|
formattedValue = representation.getLabel();
|
102
|
}
|
103
|
}
|
104
|
if(formattedValue==null){
|
105
|
formattedValue = presenceAbsenceTerm.getTitleCache();
|
106
|
}
|
107
|
return formattedValue;
|
108
|
}
|
109
|
return super.formatPropertyValue(rowId, colId, property);
|
110
|
}
|
111
|
};
|
112
|
table.setImmediate(false);
|
113
|
table.setWidth("100.0%");
|
114
|
table.setHeight("100.0%");
|
115
|
|
116
|
table.setColumnReorderingAllowed(true);
|
117
|
table.setSortEnabled(false);
|
118
|
|
119
|
table.setColumnCollapsingAllowed(true);
|
120
|
table.setSelectable(true);
|
121
|
table.setPageLength(20);
|
122
|
table.setFooterVisible(true);
|
123
|
table.setCacheRate(20);
|
124
|
|
125
|
table.addItemClickListener(new ItemClickListener() {
|
126
|
private static final long serialVersionUID = 2743935539139014771L;
|
127
|
@Override
|
128
|
public void itemClick(ItemClickEvent event) {
|
129
|
if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN)) && !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))){
|
130
|
final Item item = event.getItem();
|
131
|
Property<?> itemProperty = item.getItemProperty("uuid");
|
132
|
UUID uuid = UUID.fromString(itemProperty.getValue().toString());
|
133
|
final Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
|
134
|
final String areaID = (String) event.getPropertyId();
|
135
|
PresenceAbsenceTerm presenceAbsenceTerm = null;
|
136
|
Object statusValue = item.getItemProperty(areaID).getValue();
|
137
|
if(statusValue instanceof String){
|
138
|
presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) statusValue);
|
139
|
}
|
140
|
//popup window
|
141
|
final Window popup = new Window("Choose distribution status");
|
142
|
final ListSelect termSelect = new ListSelect();
|
143
|
termSelect.setSizeFull();
|
144
|
termSelect.setContainerDataSource(PresenceAbsenceTermContainer.getInstance());
|
145
|
termSelect.setNullSelectionAllowed(presenceAbsenceTerm!=null);
|
146
|
if(presenceAbsenceTerm!=null){
|
147
|
termSelect.setNullSelectionItemId("[no status]");
|
148
|
}
|
149
|
termSelect.setValue(presenceAbsenceTerm);
|
150
|
termSelect.addValueChangeListener(new ValueChangeListener() {
|
151
|
|
152
|
private static final long serialVersionUID = 1883728509174752769L;
|
153
|
|
154
|
@Override
|
155
|
public void valueChange(ValueChangeEvent event) {
|
156
|
System.out.println(event);
|
157
|
Object distributionStatus = event.getProperty().getValue();
|
158
|
listener.updateDistributionField(areaID, distributionStatus, taxon);
|
159
|
container.refresh();
|
160
|
popup.close();
|
161
|
}
|
162
|
});
|
163
|
VerticalLayout layout = new VerticalLayout(termSelect);
|
164
|
popup.setContent(layout);
|
165
|
popup.setModal(true);
|
166
|
popup.center();
|
167
|
UI.getCurrent().addWindow(popup);
|
168
|
}
|
169
|
}
|
170
|
});
|
171
|
|
172
|
|
173
|
mainLayout.addComponent(table, "top:75px;right:0.0px;");
|
174
|
return mainLayout;
|
175
|
}
|
176
|
|
177
|
public void addListener(DistributionTablePresenter listener) {
|
178
|
this.listener = listener;
|
179
|
}
|
180
|
|
181
|
@Override
|
182
|
public void enter(ViewChangeEvent event) {
|
183
|
try {
|
184
|
container = listener.getSQLContainer();
|
185
|
} catch (SQLException e) {
|
186
|
DistributionEditorUtil.showSqlError(e);
|
187
|
return;
|
188
|
}
|
189
|
if(container==null){
|
190
|
return;
|
191
|
}
|
192
|
|
193
|
table.setContainerDataSource(container);
|
194
|
|
195
|
List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
|
196
|
columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
|
197
|
columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
|
198
|
columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
|
199
|
columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
|
200
|
|
201
|
List<String> columnList = new ArrayList<String>(columnHeaders);
|
202
|
|
203
|
String[] string = new String[columnList.size()];
|
204
|
|
205
|
table.setVisibleColumns(columnList.toArray());
|
206
|
table.setColumnHeaders(columnList.toArray(string));
|
207
|
table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
|
208
|
|
209
|
|
210
|
}
|
211
|
|
212
|
private void createEditClickListener(){
|
213
|
Button detailButton = toolbar.getDetailButton();
|
214
|
detailButton.setCaption("Detail View");
|
215
|
detailButton.addClickListener(new ClickListener() {
|
216
|
|
217
|
private static final long serialVersionUID = 1479133195403139547L;
|
218
|
|
219
|
@Override
|
220
|
public void buttonClick(ClickEvent event) {
|
221
|
Object selectedItemId = DistributionTableView.this.table.getValue();
|
222
|
if(selectedItemId!=null){
|
223
|
final UUID uuid = UUID.fromString(table.getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
|
224
|
Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
|
225
|
List<DescriptionElementBase> listDescriptions = listener.listDescriptionElementsForTaxon(taxon, null);
|
226
|
DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
|
227
|
Window window = detailWindow.createWindow();
|
228
|
window.center();
|
229
|
getUI().addWindow(window);
|
230
|
}
|
231
|
else{
|
232
|
Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
|
233
|
}
|
234
|
}
|
235
|
});
|
236
|
|
237
|
Button distributionSettingsButton = toolbar.getDistributionSettingsButton();
|
238
|
distributionSettingsButton.addClickListener(new ClickListener() {
|
239
|
|
240
|
private static final long serialVersionUID = -8695281619014251132L;
|
241
|
|
242
|
@Override
|
243
|
public void buttonClick(ClickEvent event) {
|
244
|
openDistributionSettings();
|
245
|
}
|
246
|
});
|
247
|
|
248
|
Button settingsButton = toolbar.getSettingsButton();
|
249
|
settingsButton.addClickListener(new ClickListener() {
|
250
|
|
251
|
private static final long serialVersionUID = -147703680580181544L;
|
252
|
|
253
|
@Override
|
254
|
public void buttonClick(ClickEvent event) {
|
255
|
openSettings();
|
256
|
}
|
257
|
});
|
258
|
}
|
259
|
|
260
|
public void openSettings() {
|
261
|
SettingsConfigWindow cw = new SettingsConfigWindow(this);
|
262
|
Window window = cw.createWindow();
|
263
|
getUI().addWindow(window);
|
264
|
}
|
265
|
|
266
|
public void openDistributionSettings() {
|
267
|
if(distributionSettingConfigWindow==null){
|
268
|
distributionSettingConfigWindow = new DistributionSettingsConfigWindow(this);
|
269
|
}
|
270
|
Window window = distributionSettingConfigWindow.createWindow();
|
271
|
getUI().addWindow(window);
|
272
|
}
|
273
|
|
274
|
}
|