1
|
package eu.etaxonomy.cdm.vaadin.view.dbstatus;
|
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.event.ItemClickEvent;
|
12
|
import com.vaadin.event.ItemClickEvent.ItemClickListener;
|
13
|
import com.vaadin.navigator.View;
|
14
|
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
|
15
|
import com.vaadin.ui.AbsoluteLayout;
|
16
|
import com.vaadin.ui.Alignment;
|
17
|
import com.vaadin.ui.Button;
|
18
|
import com.vaadin.ui.Button.ClickEvent;
|
19
|
import com.vaadin.ui.Button.ClickListener;
|
20
|
import com.vaadin.ui.CustomComponent;
|
21
|
import com.vaadin.ui.ListSelect;
|
22
|
import com.vaadin.ui.Notification;
|
23
|
import com.vaadin.ui.Notification.Type;
|
24
|
import com.vaadin.ui.Table;
|
25
|
import com.vaadin.ui.UI;
|
26
|
import com.vaadin.ui.VerticalLayout;
|
27
|
import com.vaadin.ui.Window;
|
28
|
|
29
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
30
|
import eu.etaxonomy.cdm.model.common.Language;
|
31
|
import eu.etaxonomy.cdm.model.common.Representation;
|
32
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
33
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
35
|
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
|
36
|
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
|
37
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
38
|
import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
|
39
|
import eu.etaxonomy.cdm.vaadin.presenter.dbstatus.DistributionTablePresenter;
|
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
|
|
136
|
//popup window
|
137
|
final Window popup = new Window("Choose distribution status");
|
138
|
final ListSelect termSelect = new ListSelect();
|
139
|
termSelect.setSizeFull();
|
140
|
termSelect.setNullSelectionAllowed(false);
|
141
|
termSelect.setContainerDataSource(PresenceAbsenceTermContainer.getInstance());
|
142
|
Button btnOk = new Button("OK", new ClickListener() {
|
143
|
private static final long serialVersionUID = -3732219609337335697L;
|
144
|
@Override
|
145
|
public void buttonClick(ClickEvent event) {
|
146
|
Object distributionStatus = termSelect.getValue();
|
147
|
listener.updateDistributionField(areaID, distributionStatus, taxon);
|
148
|
container.refresh();
|
149
|
popup.close();
|
150
|
}
|
151
|
});
|
152
|
VerticalLayout layout = new VerticalLayout(termSelect, btnOk);
|
153
|
layout.setComponentAlignment(btnOk, Alignment.BOTTOM_RIGHT);
|
154
|
popup.setContent(layout);
|
155
|
popup.setModal(true);
|
156
|
popup.center();
|
157
|
UI.getCurrent().addWindow(popup);
|
158
|
}
|
159
|
}
|
160
|
});
|
161
|
|
162
|
|
163
|
mainLayout.addComponent(table, "top:75px;right:0.0px;");
|
164
|
return mainLayout;
|
165
|
}
|
166
|
|
167
|
public void addListener(DistributionTablePresenter listener) {
|
168
|
this.listener = listener;
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public void enter(ViewChangeEvent event) {
|
173
|
try {
|
174
|
container = listener.getSQLContainer();
|
175
|
} catch (SQLException e) {
|
176
|
DistributionEditorUtil.showSqlError(e);
|
177
|
return;
|
178
|
}
|
179
|
if(container==null){
|
180
|
return;
|
181
|
}
|
182
|
|
183
|
table.setContainerDataSource(container);
|
184
|
|
185
|
List<String> columnHeaders = new ArrayList<>(Arrays.asList(table.getColumnHeaders()));
|
186
|
columnHeaders.remove(CdmQueryFactory.DTYPE_COLUMN);
|
187
|
columnHeaders.remove(CdmQueryFactory.ID_COLUMN);
|
188
|
columnHeaders.remove(CdmQueryFactory.UUID_COLUMN);
|
189
|
columnHeaders.remove(CdmQueryFactory.CLASSIFICATION_COLUMN);
|
190
|
|
191
|
List<String> columnList = new ArrayList<String>(columnHeaders);
|
192
|
|
193
|
String[] string = new String[columnList.size()];
|
194
|
|
195
|
table.setVisibleColumns(columnList.toArray());
|
196
|
table.setColumnHeaders(columnList.toArray(string));
|
197
|
table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
|
198
|
|
199
|
|
200
|
}
|
201
|
|
202
|
private void createEditClickListener(){
|
203
|
Button detailButton = toolbar.getDetailButton();
|
204
|
detailButton.setCaption("Detail View");
|
205
|
detailButton.addClickListener(new ClickListener() {
|
206
|
|
207
|
private static final long serialVersionUID = 1479133195403139547L;
|
208
|
|
209
|
@Override
|
210
|
public void buttonClick(ClickEvent event) {
|
211
|
Object selectedItemId = DistributionTableView.this.table.getValue();
|
212
|
if(selectedItemId!=null){
|
213
|
final UUID uuid = UUID.fromString(table.getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
|
214
|
Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
|
215
|
List<DescriptionElementBase> listDescriptions = listener.listDescriptionElementsForTaxon(taxon, null);
|
216
|
DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
|
217
|
Window window = detailWindow.createWindow();
|
218
|
window.center();
|
219
|
getUI().addWindow(window);
|
220
|
}
|
221
|
else{
|
222
|
Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
|
223
|
}
|
224
|
}
|
225
|
});
|
226
|
|
227
|
Button distributionSettingsButton = toolbar.getDistributionSettingsButton();
|
228
|
distributionSettingsButton.addClickListener(new ClickListener() {
|
229
|
|
230
|
private static final long serialVersionUID = -8695281619014251132L;
|
231
|
|
232
|
@Override
|
233
|
public void buttonClick(ClickEvent event) {
|
234
|
openDistributionSettings();
|
235
|
}
|
236
|
});
|
237
|
|
238
|
Button settingsButton = toolbar.getSettingsButton();
|
239
|
settingsButton.addClickListener(new ClickListener() {
|
240
|
|
241
|
private static final long serialVersionUID = -147703680580181544L;
|
242
|
|
243
|
@Override
|
244
|
public void buttonClick(ClickEvent event) {
|
245
|
openSettings();
|
246
|
}
|
247
|
});
|
248
|
}
|
249
|
|
250
|
public void openSettings() {
|
251
|
SettingsConfigWindow cw = new SettingsConfigWindow(this);
|
252
|
Window window = cw.createWindow();
|
253
|
getUI().addWindow(window);
|
254
|
}
|
255
|
|
256
|
public void openDistributionSettings() {
|
257
|
if(distributionSettingConfigWindow==null){
|
258
|
distributionSettingConfigWindow = new DistributionSettingsConfigWindow(this);
|
259
|
}
|
260
|
Window window = distributionSettingConfigWindow.createWindow();
|
261
|
getUI().addWindow(window);
|
262
|
}
|
263
|
|
264
|
}
|