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.Collection;
|
7
|
import java.util.List;
|
8
|
import java.util.UUID;
|
9
|
|
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.ContextClickEvent;
|
14
|
import com.vaadin.event.ContextClickEvent.ContextClickListener;
|
15
|
import com.vaadin.event.ItemClickEvent;
|
16
|
import com.vaadin.event.ItemClickEvent.ItemClickListener;
|
17
|
import com.vaadin.event.ShortcutAction.KeyCode;
|
18
|
import com.vaadin.event.ShortcutAction.ModifierKey;
|
19
|
import com.vaadin.navigator.View;
|
20
|
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
|
21
|
import com.vaadin.server.VaadinSession;
|
22
|
import com.vaadin.ui.AbsoluteLayout;
|
23
|
import com.vaadin.ui.Button;
|
24
|
import com.vaadin.ui.Button.ClickEvent;
|
25
|
import com.vaadin.ui.Button.ClickListener;
|
26
|
import com.vaadin.ui.ComboBox;
|
27
|
import com.vaadin.ui.CustomComponent;
|
28
|
import com.vaadin.ui.Notification;
|
29
|
import com.vaadin.ui.Notification.Type;
|
30
|
import com.vaadin.ui.Table;
|
31
|
import com.vaadin.ui.Table.ColumnGenerator;
|
32
|
import com.vaadin.ui.Window;
|
33
|
|
34
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
35
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
36
|
import eu.etaxonomy.cdm.model.common.Language;
|
37
|
import eu.etaxonomy.cdm.model.common.Representation;
|
38
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
39
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
40
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
41
|
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
|
42
|
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
|
43
|
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
|
44
|
import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
|
45
|
import eu.etaxonomy.cdm.vaadin.presenter.dbstatus.DistributionTablePresenter;
|
46
|
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
|
47
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
48
|
import eu.etaxonomy.cdm.vaadin.util.TermCacher;
|
49
|
|
50
|
public class DistributionTableView extends CustomComponent implements View{
|
51
|
|
52
|
private static final long serialVersionUID = 1L;
|
53
|
private HorizontalToolbar toolbar;
|
54
|
private Table table;
|
55
|
|
56
|
private DistributionTablePresenter listener;
|
57
|
|
58
|
private List<String> columnList;
|
59
|
private ArrayList<String> headerList;
|
60
|
|
61
|
/**
|
62
|
* The constructor should first build the main layout, set the
|
63
|
* composition root and then do any custom initialization.
|
64
|
*
|
65
|
* The constructor will not be automatically regenerated by the
|
66
|
* visual editor.
|
67
|
*/
|
68
|
public DistributionTableView() {
|
69
|
AbsoluteLayout mainLayout = initLayout();
|
70
|
setCompositionRoot(mainLayout);
|
71
|
createEditClickListener();
|
72
|
|
73
|
}
|
74
|
|
75
|
private AbsoluteLayout initLayout() {
|
76
|
AbsoluteLayout mainLayout = new AbsoluteLayout();
|
77
|
mainLayout.setImmediate(false);
|
78
|
mainLayout.setWidth("100%");
|
79
|
mainLayout.setHeight("100%");
|
80
|
|
81
|
setWidth("100.0%");
|
82
|
setHeight("100.0%");
|
83
|
|
84
|
//Horizontal Toolbar
|
85
|
toolbar = new HorizontalToolbar();
|
86
|
mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
|
87
|
|
88
|
// table
|
89
|
table = new Table();
|
90
|
table.setImmediate(false);
|
91
|
table.setWidth("100.0%");
|
92
|
table.setHeight("100.0%");
|
93
|
mainLayout.addComponent(table, "top:75px;right:0.0px;");
|
94
|
return mainLayout;
|
95
|
}
|
96
|
|
97
|
public void addListener(DistributionTablePresenter listener) {
|
98
|
this.listener = listener;
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
public void enter(ViewChangeEvent event) {
|
103
|
CdmSQLContainer container = null;
|
104
|
try {
|
105
|
container = listener.getSQLContainer();
|
106
|
} catch (SQLException e) {
|
107
|
Notification.show("Error while accessing data base", Type.ERROR_MESSAGE);
|
108
|
e.printStackTrace();
|
109
|
return;
|
110
|
}
|
111
|
|
112
|
table.setContainerDataSource(container);
|
113
|
table.setColumnReorderingAllowed(true);
|
114
|
table.setSortEnabled(true);
|
115
|
|
116
|
columnList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,CdmQueryFactory.RANK_COLUMN}));
|
117
|
List<String> namedAreas = listener.getNamedAreasLabels();
|
118
|
columnList.addAll(namedAreas);
|
119
|
table.setVisibleColumns(columnList.toArray());
|
120
|
|
121
|
headerList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,"Rang"}));
|
122
|
headerList.addAll(listener.getNamedAreasLabels());
|
123
|
String[] string = new String[headerList.size()];
|
124
|
table.setColumnHeaders(headerList.toArray(string));
|
125
|
|
126
|
// table.setColumnExpandRatio(propertyId, expandRatio);
|
127
|
table.setColumnCollapsingAllowed(true);
|
128
|
table.setSelectable(true);
|
129
|
table.setPageLength(20);
|
130
|
table.setFooterVisible(true);
|
131
|
table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
|
132
|
|
133
|
table.setCacheRate(20);
|
134
|
|
135
|
//add generated columns for NamedAreas
|
136
|
Collection<?> containerPropertyIds = table.getContainerPropertyIds();
|
137
|
for (Object object : containerPropertyIds) {
|
138
|
if(namedAreas.contains(object)){
|
139
|
table.removeGeneratedColumn(object);
|
140
|
table.addGeneratedColumn(object, new AreaColumnGenerator());
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
private void createEditClickListener(){
|
146
|
Button detailButton = toolbar.getDetailButton();
|
147
|
detailButton.setCaption("Detail View");
|
148
|
detailButton.addClickListener(new ClickListener() {
|
149
|
|
150
|
private static final long serialVersionUID = 1479133195403139547L;
|
151
|
|
152
|
@Override
|
153
|
public void buttonClick(ClickEvent event) {
|
154
|
Object selectedItemId = DistributionTableView.this.table.getValue();
|
155
|
if(selectedItemId!=null){
|
156
|
final UUID uuid = UUID.fromString(table.getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
|
157
|
Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
|
158
|
List<DescriptionElementBase> listDescriptions = listener.listDescriptionElementsForTaxon(taxon, null);
|
159
|
DetailWindow dw = new DetailWindow(taxon, listDescriptions);
|
160
|
Window window = dw.createWindow();
|
161
|
getUI().addWindow(window);
|
162
|
}
|
163
|
else{
|
164
|
Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
|
165
|
}
|
166
|
}
|
167
|
});
|
168
|
|
169
|
Button settingsButton = toolbar.getSettingsButton();
|
170
|
settingsButton.addClickListener(new ClickListener() {
|
171
|
|
172
|
private static final long serialVersionUID = 3834048719431837966L;
|
173
|
|
174
|
@Override
|
175
|
public void buttonClick(ClickEvent event) {
|
176
|
SettingsConfigWindow cw = new SettingsConfigWindow();
|
177
|
Window window = cw.createWindow();
|
178
|
getUI().addWindow(window);
|
179
|
}
|
180
|
});
|
181
|
|
182
|
Button saveButton = toolbar.getSaveButton();
|
183
|
saveButton.setClickShortcut(KeyCode.S, ModifierKey.CTRL);
|
184
|
saveButton.setDescription("Shortcut: CTRL+S");
|
185
|
saveButton.setCaption("Save Data");
|
186
|
saveButton.addClickListener(new ClickListener() {
|
187
|
private static final long serialVersionUID = 1L;
|
188
|
@Override
|
189
|
public void buttonClick(ClickEvent event) {
|
190
|
ConversationHolder conversationHolder = (ConversationHolder) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_CONVERSATION);
|
191
|
try{
|
192
|
conversationHolder.commit();
|
193
|
}catch(Exception stateException){
|
194
|
//TODO create Table without DTO
|
195
|
}
|
196
|
Notification.show("Data saved", Notification.Type.HUMANIZED_MESSAGE);
|
197
|
}
|
198
|
});
|
199
|
|
200
|
}
|
201
|
|
202
|
private final class AreaColumnGenerator implements ColumnGenerator {
|
203
|
private static final long serialVersionUID = 1L;
|
204
|
|
205
|
@Override
|
206
|
public Object generateCell(Table source, Object itemId, Object columnId) {
|
207
|
Property<?> containerProperty = source.getContainerProperty(itemId, columnId);
|
208
|
Object value = null;
|
209
|
if(containerProperty != null){
|
210
|
value = containerProperty.getValue();
|
211
|
}
|
212
|
final UUID uuid = UUID.fromString(table.getItem(itemId).getItemProperty("uuid").getValue().toString());
|
213
|
final ComboBox box = new ComboBox("Occurrence Status: ", PresenceAbsenceTermContainer.getInstance());
|
214
|
final String area = columnId.toString();
|
215
|
box.setImmediate(true);
|
216
|
box.setBuffered(true);
|
217
|
box.setSizeFull();
|
218
|
PresenceAbsenceTerm presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String)value);
|
219
|
box.setValue(presenceAbsenceTerm);
|
220
|
if(presenceAbsenceTerm!=null){
|
221
|
String itemCaption = null;
|
222
|
Representation representation = presenceAbsenceTerm.getRepresentation(Language.DEFAULT());
|
223
|
if(representation!=null){
|
224
|
if(DistributionEditorUtil.isAbbreviatedLabels()){
|
225
|
itemCaption = representation.getAbbreviatedLabel();
|
226
|
}
|
227
|
else{
|
228
|
itemCaption = representation.getLabel();
|
229
|
}
|
230
|
}
|
231
|
if(itemCaption==null){
|
232
|
itemCaption = presenceAbsenceTerm.getTitleCache();
|
233
|
}
|
234
|
box.setItemCaption(presenceAbsenceTerm, itemCaption);
|
235
|
}
|
236
|
box.addValueChangeListener(new ValueChangeListener() {
|
237
|
private static final long serialVersionUID = 6221534597911674067L;
|
238
|
|
239
|
@Override
|
240
|
public void valueChange(ValueChangeEvent event) {
|
241
|
Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
|
242
|
listener.updateDistributionField(area, box.getValue(), taxon);
|
243
|
}
|
244
|
});
|
245
|
return box;
|
246
|
}
|
247
|
}
|
248
|
|
249
|
}
|