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.description.DescriptionElementBase;
|
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.presenter.dbstatus.DistributionTablePresenter;
|
41
|
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
|
42
|
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
|
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 List<String> columnList;
|
53
|
private ArrayList<String> headerList;
|
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
|
83
|
table = new Table();
|
84
|
table.setImmediate(false);
|
85
|
table.setWidth("100.0%");
|
86
|
table.setHeight("100.0%");
|
87
|
mainLayout.addComponent(table, "top:75px;right:0.0px;");
|
88
|
return mainLayout;
|
89
|
}
|
90
|
|
91
|
public void addListener(DistributionTablePresenter listener) {
|
92
|
this.listener = listener;
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
public void enter(ViewChangeEvent event) {
|
97
|
final CdmSQLContainer container;
|
98
|
try {
|
99
|
container = listener.getSQLContainer();
|
100
|
} catch (SQLException e) {
|
101
|
Notification.show("Error while accessing data base", Type.ERROR_MESSAGE);
|
102
|
e.printStackTrace();
|
103
|
return;
|
104
|
}
|
105
|
if(container==null){
|
106
|
return;
|
107
|
}
|
108
|
|
109
|
table.setContainerDataSource(container);
|
110
|
table.setColumnReorderingAllowed(true);
|
111
|
table.setSortEnabled(true);
|
112
|
|
113
|
columnList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,CdmQueryFactory.RANK_COLUMN}));
|
114
|
List<String> namedAreas = listener.getNamedAreasLabels();
|
115
|
columnList.addAll(namedAreas);
|
116
|
table.setVisibleColumns(columnList.toArray());
|
117
|
|
118
|
headerList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,"Rang"}));
|
119
|
headerList.addAll(listener.getNamedAreasLabels());
|
120
|
String[] string = new String[headerList.size()];
|
121
|
table.setColumnHeaders(headerList.toArray(string));
|
122
|
|
123
|
// table.setColumnExpandRatio(propertyId, expandRatio);
|
124
|
table.setColumnCollapsingAllowed(true);
|
125
|
table.setSelectable(true);
|
126
|
table.setPageLength(20);
|
127
|
table.setFooterVisible(true);
|
128
|
table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
|
129
|
table.setCacheRate(20);
|
130
|
|
131
|
table.addItemClickListener(new ItemClickListener() {
|
132
|
private static final long serialVersionUID = 2743935539139014771L;
|
133
|
@Override
|
134
|
public void itemClick(ItemClickEvent event) {
|
135
|
if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN)) && !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))){
|
136
|
final Item item = event.getItem();
|
137
|
Property<?> itemProperty = item.getItemProperty("uuid");
|
138
|
UUID uuid = UUID.fromString(itemProperty.getValue().toString());
|
139
|
final Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
|
140
|
final String areaID = (String) event.getPropertyId();
|
141
|
|
142
|
//popup window
|
143
|
final Window popup = new Window("Choose distribution status");
|
144
|
final ListSelect termSelect = new ListSelect();
|
145
|
termSelect.setContainerDataSource(PresenceAbsenceTermContainer.getInstance());
|
146
|
Button btnOk = new Button("OK", new ClickListener() {
|
147
|
private static final long serialVersionUID = -3732219609337335697L;
|
148
|
@Override
|
149
|
public void buttonClick(ClickEvent event) {
|
150
|
Object distributionStatus = termSelect.getValue();
|
151
|
listener.updateDistributionField(areaID, distributionStatus, taxon);
|
152
|
container.refresh();
|
153
|
popup.close();
|
154
|
}
|
155
|
});
|
156
|
VerticalLayout layout = new VerticalLayout(termSelect, btnOk);
|
157
|
layout.setComponentAlignment(btnOk, Alignment.BOTTOM_RIGHT);
|
158
|
popup.setContent(layout);
|
159
|
popup.setModal(true);
|
160
|
popup.center();
|
161
|
UI.getCurrent().addWindow(popup);
|
162
|
}
|
163
|
}
|
164
|
});
|
165
|
}
|
166
|
|
167
|
private void createEditClickListener(){
|
168
|
Button detailButton = toolbar.getDetailButton();
|
169
|
detailButton.setCaption("Detail View");
|
170
|
detailButton.addClickListener(new ClickListener() {
|
171
|
|
172
|
private static final long serialVersionUID = 1479133195403139547L;
|
173
|
|
174
|
@Override
|
175
|
public void buttonClick(ClickEvent event) {
|
176
|
Object selectedItemId = DistributionTableView.this.table.getValue();
|
177
|
if(selectedItemId!=null){
|
178
|
final UUID uuid = UUID.fromString(table.getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
|
179
|
Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
|
180
|
List<DescriptionElementBase> listDescriptions = listener.listDescriptionElementsForTaxon(taxon, null);
|
181
|
DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
|
182
|
Window window = detailWindow.createWindow();
|
183
|
window.center();
|
184
|
getUI().addWindow(window);
|
185
|
}
|
186
|
else{
|
187
|
Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
|
188
|
}
|
189
|
}
|
190
|
});
|
191
|
|
192
|
Button settingsButton = toolbar.getSettingsButton();
|
193
|
settingsButton.addClickListener(new ClickListener() {
|
194
|
|
195
|
private static final long serialVersionUID = 3834048719431837966L;
|
196
|
|
197
|
@Override
|
198
|
public void buttonClick(ClickEvent event) {
|
199
|
openSettings();
|
200
|
}
|
201
|
});
|
202
|
|
203
|
Button saveButton = toolbar.getSaveButton();
|
204
|
saveButton.setClickShortcut(KeyCode.S, ModifierKey.CTRL);
|
205
|
saveButton.setDescription("Shortcut: CTRL+S");
|
206
|
saveButton.setCaption("Save Data");
|
207
|
saveButton.addClickListener(new ClickListener() {
|
208
|
private static final long serialVersionUID = 1L;
|
209
|
@Override
|
210
|
public void buttonClick(ClickEvent event) {
|
211
|
ConversationHolder conversationHolder = (ConversationHolder) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_CONVERSATION);
|
212
|
try{
|
213
|
conversationHolder.commit();
|
214
|
}catch(Exception stateException){
|
215
|
//TODO create Table without DTO
|
216
|
}
|
217
|
Notification.show("Data saved", Notification.Type.HUMANIZED_MESSAGE);
|
218
|
}
|
219
|
});
|
220
|
|
221
|
}
|
222
|
|
223
|
public void openSettings() {
|
224
|
SettingsConfigWindow cw = new SettingsConfigWindow();
|
225
|
Window window = cw.createWindow();
|
226
|
getUI().addWindow(window);
|
227
|
}
|
228
|
|
229
|
}
|