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