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