Project

General

Profile

Download (12.8 KB) Statistics
| Branch: | Tag: | Revision:
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.HashSet;
7
import java.util.List;
8
import java.util.UUID;
9

    
10
import com.vaadin.annotations.AutoGenerated;
11
import com.vaadin.data.Container;
12
import com.vaadin.data.Item;
13
import com.vaadin.data.Property;
14
import com.vaadin.data.Property.ValueChangeEvent;
15
import com.vaadin.data.Property.ValueChangeListener;
16
import com.vaadin.event.ItemClickEvent;
17
import com.vaadin.event.ItemClickEvent.ItemClickListener;
18
import com.vaadin.event.ShortcutAction.KeyCode;
19
import com.vaadin.event.ShortcutAction.ModifierKey;
20
import com.vaadin.navigator.View;
21
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
22
import com.vaadin.server.VaadinSession;
23
import com.vaadin.ui.AbsoluteLayout;
24
import com.vaadin.ui.Button;
25
import com.vaadin.ui.Button.ClickEvent;
26
import com.vaadin.ui.Button.ClickListener;
27
import com.vaadin.ui.ComboBox;
28
import com.vaadin.ui.Component;
29
import com.vaadin.ui.CustomComponent;
30
import com.vaadin.ui.Field;
31
import com.vaadin.ui.Notification;
32
import com.vaadin.ui.PopupView;
33
import com.vaadin.ui.PopupView.PopupVisibilityEvent;
34
import com.vaadin.ui.PopupView.PopupVisibilityListener;
35
import com.vaadin.ui.Table;
36
import com.vaadin.ui.Table.ColumnGenerator;
37
import com.vaadin.ui.Window;
38

    
39
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
40
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41
import eu.etaxonomy.cdm.model.description.Feature;
42
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
43
import eu.etaxonomy.cdm.model.taxon.Taxon;
44
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
45
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
46
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
47
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
48

    
49
public class DistributionTableView<E> extends CustomComponent implements IDistributionTableComponent, View{
50

    
51
    private static final long serialVersionUID = 1L;
52
    @AutoGenerated
53
	private AbsoluteLayout mainLayout;
54
    private HorizontalToolbar toolbar;
55
	@AutoGenerated
56
	private Table table;
57

    
58
	private Taxon currentTaxon;
59

    
60
	private ArrayList<Object> propertyList = new ArrayList<Object>();
61

    
62
	private DistributionTableComponentListener listener;
63

    
64
	protected List<Field> fields = new ArrayList<Field>();
65

    
66
	List<String> columnList;
67
	ArrayList<String> headerList;
68

    
69
	/**
70
	 * The constructor should first build the main layout, set the
71
	 * composition root and then do any custom initialization.
72
	 *
73
	 * The constructor will not be automatically regenerated by the
74
	 * visual editor.
75
	 */
76
	public DistributionTableView() {
77
		buildMainLayout();
78
		setCompositionRoot(mainLayout);
79
		createEditClickListener();
80

    
81
	}
82

    
83
	@Override
84
	public void addListener(DistributionTableComponentListener listener) {
85
	   this.listener = listener;
86
	}
87

    
88
	@AutoGenerated
89
	private AbsoluteLayout buildMainLayout() {
90
		// common part: create layout
91
		mainLayout = new AbsoluteLayout();
92
		mainLayout.setImmediate(false);
93
		mainLayout.setWidth("100%");
94
		mainLayout.setHeight("100%");
95

    
96
		// top-level component properties
97
		setWidth("100.0%");
98
		setHeight("100.0%");
99
		//Horizontal Toolbar
100
		toolbar = new HorizontalToolbar();
101
		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
102

    
103
		// table_1
104
		table = new Table();
105
		table.setImmediate(false);
106
		table.setWidth("100.0%");
107
		table.setHeight("100.0%");
108
		mainLayout.addComponent(table, "top:75px;right:0.0px;");
109

    
110
		return mainLayout;
111
	}
112

    
113
    /* (non-Javadoc)
114
     * @see com.vaadin.navigator.View#enter(com.vaadin.navigator.ViewChangeListener.ViewChangeEvent)
115
     */
116
    @Override
117
    public void enter(ViewChangeEvent event) {
118
        // TODO Auto-generated method stub
119

    
120
    }
121

    
122

    
123

    
124
	public void dataBinding() throws SQLException{
125
		CdmSQLContainer container = listener.getSQLContainer();
126

    
127
		table.setContainerDataSource(container);
128
		table.setColumnReorderingAllowed(true);
129
		table.setSortEnabled(true);
130

    
131
		columnList = new ArrayList<String>(Arrays.asList(new String[]{"Taxon","Rank"}));
132
		columnList.addAll(listener.getTermList());
133
		Object[] visibleColumns = columnList.toArray();
134
		table.setVisibleColumns(visibleColumns);
135

    
136
		headerList = new ArrayList<String>(Arrays.asList(new String[]{"Taxon","Rang"}));
137
		headerList.addAll(listener.getAbbreviatedTermList());
138
//		table.setColumnHeaders(headerList.toArray(new String[headerList.size()]));//new String[]{"Taxon", "Rang"});// ,"Deutschland"
139

    
140
		table.setColumnCollapsingAllowed(true);
141
		table.setSelectable(true);
142
		table.setPageLength(20);
143
		table.setFooterVisible(true);
144
		table.setColumnFooter("Taxon", "Total amount of Taxa displayed: " + container.size());
145

    
146
		table.setCacheRate(20);
147
	}
148

    
149

    
150
	private void createEditClickListener(){
151
		Button detailButton = toolbar.getDetailButton();
152
		detailButton.setCaption("Detail View");
153
		detailButton.addClickListener(new ClickListener() {
154

    
155
			@Override
156
			public void buttonClick(ClickEvent event) {
157
				try{
158
					HashSet<Feature> featureSet = new HashSet<Feature>(Arrays.asList(Feature.DESCRIPTION(), Feature.DISTRIBUTION()));
159
				if(currentTaxon != null){
160
					List<DescriptionElementBase> listDescriptions = listener.listDescriptionElementsForTaxon(currentTaxon, null);
161
					DetailWindow dw = new DetailWindow(currentTaxon, listDescriptions);
162
					Window window = dw.createWindow();
163
					getUI().addWindow(window);
164
				}else{
165
					Notification.show("Please select a Taxon.", Notification.Type.HUMANIZED_MESSAGE);
166
				}
167
				}catch(Exception e){
168
					Notification.show("Unexpected Error, \n\n Please log in again!", Notification.Type.WARNING_MESSAGE);
169
				}
170
			}
171
		});
172

    
173
		Button settingsButton = toolbar.getSettingsButton();
174
		settingsButton.addClickListener(new ClickListener() {
175

    
176
            @Override
177
            public void buttonClick(ClickEvent event) {
178
                SettingsConfigWindow cw = new SettingsConfigWindow();
179
                Window window  = cw.createWindow();
180
                getUI().addWindow(window);
181
            }
182
        });
183

    
184
		Button saveButton = toolbar.getSaveButton();
185
		saveButton.setClickShortcut(KeyCode.S, ModifierKey.CTRL);
186
		saveButton.setDescription("Shortcut: CTRL+S");
187
		saveButton.setCaption("Save Data");
188
		saveButton.addClickListener(new ClickListener() {
189
			private static final long serialVersionUID = 1L;
190
			@Override
191
			public void buttonClick(ClickEvent event) {
192
				ConversationHolder conversationHolder = (ConversationHolder) VaadinSession.getCurrent().getAttribute("conversation");
193
				try{
194
					conversationHolder.commit();
195
				}catch(Exception stateException){
196
					//TODO create Table without DTO
197
				}
198
				if(propertyList != null){
199
					for(Object propertyId:propertyList){
200
						table.removeGeneratedColumn(propertyId);
201
					}
202
					redrawTable();
203
				}
204
				Notification.show("Data saved", Notification.Type.HUMANIZED_MESSAGE);
205
				propertyList = null;
206
				propertyList = new ArrayList<Object>();
207
				table.setEditable(false);
208
				toolbar.getSaveButton().setCaption("Save Data");
209
			}
210
		});
211

    
212

    
213
		//FIXME: Due lack of time needs to be properly done
214

    
215
//		Button editButton = toolbar.getEditButton();
216
//		editButton.setClickShortcut(KeyCode.E, ModifierKey.CTRL);
217
//		editButton.setDescription("Shortcut: CTRL+e");
218
//		editButton.addClickListener(new ClickListener() {
219
//			private static final long serialVersionUID = 1L;
220
//			@Override
221
//			public void buttonClick(ClickEvent event) {
222
//			    //				if(table.isEditable() == false){
223
//			    //					table.setEditable(true);
224
//			    for(Object prop:table.getContainerPropertyIds()){
225
//			            if(!prop.equals("Taxon")&&!prop.equals("Rank")){
226
//			                table.addGeneratedColumn(prop, createTableColumnGenerator());
227
//			            }
228
//			    }
229
////				}//else{
230
////					table.setEditable(false);
231
////					table.refreshRowCache();
232
////				}
233
//			}
234
//		});
235

    
236
		/**Double Click listener for Table*/
237
		table.addItemClickListener(new ItemClickListener() {
238
		    private static final long serialVersionUID = 1L;
239

    
240
		    @Override
241
		    public void itemClick(ItemClickEvent event) {
242
		        TaxonBase taxonBase = loadTaxonFromSelection(event);
243
		        currentTaxon = (Taxon)taxonBase;
244
		        if(event.isDoubleClick()){
245
		            if(!(event.getPropertyId().toString().equalsIgnoreCase("Taxon")) && !(event.getPropertyId().toString().equalsIgnoreCase("Rank"))){
246
		                if(!table.removeGeneratedColumn(event.getPropertyId())){
247
		                    table.addGeneratedColumn(event.getPropertyId(), createTableColumnGenerator());
248
		                    propertyList.add(event.getPropertyId());
249
		                }else{
250
		                    table.removeGeneratedColumn(event.getPropertyId());
251
		                    propertyList.remove(event.getPropertyId());
252
		                    redrawTable();
253
		                }
254
		            }
255
		        }
256
		    }
257
		});
258
	}
259

    
260
	private TaxonBase loadTaxonFromSelection(ItemClickEvent event) {
261
	    Item item = event.getItem();
262
	    Property itemProperty = item.getItemProperty("uuid");
263
	    UUID uuid = UUID.fromString(itemProperty.getValue().toString());
264
	    TaxonBase taxonBase = listener.getTaxonService().load(uuid);
265
	    return taxonBase;
266
	}
267

    
268
	private String refreshValue(ComboBox box, Object value){
269
	    if(box.getValue() == null){
270
	        if(value != null){
271
	            return value.toString();
272
	        }else{
273
	            return "click me for new Distribution Status";
274
	        }
275
	    }else{
276
	        return box.getValue().toString();
277
	    }
278
	}
279

    
280
	private ColumnGenerator createTableColumnGenerator(){
281

    
282
	    ColumnGenerator generator = new ColumnGenerator() {
283

    
284
	        private static final long serialVersionUID = 1L;
285

    
286
	        @Override
287
	        public Object generateCell(Table source, Object itemId, Object columnId) {
288
	            Property containerProperty = source.getContainerProperty(itemId, columnId);
289
	            Object item = itemId;
290
	            Object value = null;
291
	            if(containerProperty != null){
292
	                value = containerProperty.getValue();
293
	            }
294
	            Container containerDataSource = source.getContainerDataSource();
295
	            final UUID uuid = UUID.fromString(table.getItem(itemId).getItemProperty("uuid").getValue().toString());
296
	            final ComboBox box = new ComboBox("Occurrence Status: ", listener.getPresenceAbsenceContainer());
297
	            final String area = columnId.toString();
298
	            box.setImmediate(true);
299
	            final Object value1 = value;
300
	            box.addValueChangeListener(new ValueChangeListener() {
301
	                @Override
302
	                public void valueChange(ValueChangeEvent event) {
303
	                    Taxon taxon = (Taxon)listener.getTaxonService().load(uuid);
304

    
305
	                    if(value1==null){
306
	                        listener.createDistributionField(taxon, box.getValue(), area);
307
	                        Notification.show("Create Status", Notification.Type.TRAY_NOTIFICATION);
308
	                    }else{
309
	                        int result = listener.updateDistributionField(area, box.getValue(), taxon);
310
	                        if(result == 1){
311
	                            Notification.show("Delete Status", Notification.Type.TRAY_NOTIFICATION);
312
	                        }else if(result == 0){
313
	                            Notification.show("DescriptionService wrote", Notification.Type.TRAY_NOTIFICATION);
314
	                        }
315
	                    }
316
	                }
317
	            });
318
	            final PopupView popup = new PopupView(new PopupView.Content() {
319
	                private static final long serialVersionUID = 1L;
320
	                @Override
321
	                public String getMinimizedValueAsHTML() {
322
	                    return refreshValue(box, value1);
323
	                }
324
	                @Override
325
	                public Component getPopupComponent() {
326
	                    //FIXME: find a better solution
327
	                    box.setValue(compareObjectToPAT(value1));
328
	                    box.setBuffered(true);
329
	                    return box;
330
	                }
331
	            });
332
	            popup.addPopupVisibilityListener(new PopupVisibilityListener() {
333

    
334
	                @Override
335
	                public void popupVisibilityChange(PopupVisibilityEvent event) {
336

    
337
	                }
338
	            });
339
	            popup.setHideOnMouseOut(true);
340
	            return popup;
341
	        }
342
	    };
343
	    return generator;
344
	}
345

    
346
	private PresenceAbsenceTerm compareObjectToPAT(Object object){
347
	    List<PresenceAbsenceTerm> presenceAbsenceTerms = listener.getPresenceAbsenceTerms();
348
	    for(PresenceAbsenceTerm term:presenceAbsenceTerms){
349
	        if(term.getTitleCache().equals(object)){
350
	            return term;
351
	        }
352
	    }
353
	    return null;
354
	}
355

    
356
	private void redrawTable(){
357
	    try {
358
	        CdmSQLContainer sqlContainer = listener.getSQLContainer();
359
	        sqlContainer.refresh();
360
	        table.setContainerDataSource(sqlContainer);
361
	        table.setVisibleColumns(columnList.toArray());
362
	    } catch (SQLException e) {
363
	        e.printStackTrace();
364
	    }
365
	}
366

    
367

    
368
}
(2-2/5)