Project

General

Profile

« Previous | Next » 

Revision b2063ede

Added by Patrick Plitzner over 7 years ago

ref #5458 Cleanup and stability for distribution editor

  • Extract string constants for DB columns
  • renaming methods and variables
  • fix session handling for CRUD operations on NamedAreas

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionTableView.java
3 3
import java.sql.SQLException;
4 4
import java.util.ArrayList;
5 5
import java.util.Arrays;
6
import java.util.Collection;
6 7
import java.util.List;
7 8
import java.util.UUID;
8 9

  
9
import com.vaadin.data.Item;
10 10
import com.vaadin.data.Property;
11 11
import com.vaadin.data.Property.ValueChangeEvent;
12 12
import com.vaadin.data.Property.ValueChangeListener;
13
import com.vaadin.event.ItemClickEvent;
14
import com.vaadin.event.ItemClickEvent.ItemClickListener;
15 13
import com.vaadin.event.ShortcutAction.KeyCode;
16 14
import com.vaadin.event.ShortcutAction.ModifierKey;
17 15
import com.vaadin.navigator.View;
......
22 20
import com.vaadin.ui.Button.ClickEvent;
23 21
import com.vaadin.ui.Button.ClickListener;
24 22
import com.vaadin.ui.ComboBox;
25
import com.vaadin.ui.Component;
26 23
import com.vaadin.ui.CustomComponent;
27 24
import com.vaadin.ui.Notification;
28
import com.vaadin.ui.PopupView;
29
import com.vaadin.ui.PopupView.PopupVisibilityEvent;
30
import com.vaadin.ui.PopupView.PopupVisibilityListener;
31 25
import com.vaadin.ui.Table;
32 26
import com.vaadin.ui.Table.ColumnGenerator;
33 27
import com.vaadin.ui.Window;
......
36 30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37 31
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
38 32
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
40 33
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
41 34
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
42 35
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
36
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
43 37

  
44 38
public class DistributionTableView<E> extends CustomComponent implements IDistributionTableComponent, View{
45 39

  
46
    private static final long serialVersionUID = 1L;
40
    private final class AreaColumnGenerator implements ColumnGenerator {
41
		private static final long serialVersionUID = 1L;
42

  
43
		@Override
44
		public Object generateCell(Table source, Object itemId, Object columnId) {
45
		    Property containerProperty = source.getContainerProperty(itemId, columnId);
46
		    Object value = null;
47
		    if(containerProperty != null){
48
		        value = containerProperty.getValue();
49
		    }
50
		    final UUID uuid = UUID.fromString(table.getItem(itemId).getItemProperty("uuid").getValue().toString());
51
		    final ComboBox box = new ComboBox("Occurrence Status: ", listener.getPresenceAbsenceContainer());
52
		    final String area = columnId.toString();
53
		    box.setImmediate(true);
54
		    box.setBuffered(true);
55
		    box.setValue(compareObjectToPAT(value));
56
		    final Object value1 = value;
57
		    box.addValueChangeListener(new ValueChangeListener() {
58
		        @Override
59
		        public void valueChange(ValueChangeEvent event) {
60
		            Taxon taxon = (Taxon)listener.getTaxonService().load(uuid);
61
		            listener.updateDistributionField(area, box.getValue(), taxon);
62
		        }
63
		    });
64
		    return box;
65
		}
66
	}
67

  
68
	private static final long serialVersionUID = 1L;
47 69
    private HorizontalToolbar toolbar;
48 70
	private Table table;
49 71

  
50 72
	private Taxon currentTaxon;
51 73

  
52
	private ArrayList<Object> propertyList = new ArrayList<Object>();
53

  
54 74
	private DistributionTableComponentListener listener;
55 75

  
56 76
	private List<String> columnList;
......
99 119

  
100 120
    @Override
101 121
    public void enter(ViewChangeEvent event) {
102
        // TODO Auto-generated method stub
103

  
104 122
    }
105 123

  
106

  
107

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

  
......
112 128
		table.setColumnReorderingAllowed(true);
113 129
		table.setSortEnabled(true);
114 130

  
115
		columnList = new ArrayList<String>(Arrays.asList(new String[]{"Taxon","Rank"}));
116
		columnList.addAll(listener.getTermList());
131
		columnList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,CdmQueryFactory.RANK_COLUMN}));
132
		List<String> termList = listener.getTermList();
133
		columnList.addAll(termList);
117 134
		Object[] visibleColumns = columnList.toArray();
118 135
		table.setVisibleColumns(visibleColumns);
119 136

  
120
		headerList = new ArrayList<String>(Arrays.asList(new String[]{"Taxon","Rang"}));
137
		headerList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,"Rang"}));
121 138
		headerList.addAll(listener.getAbbreviatedTermList());
122 139

  
123 140
		table.setColumnCollapsingAllowed(true);
124 141
		table.setSelectable(true);
125 142
		table.setPageLength(20);
126 143
		table.setFooterVisible(true);
127
		table.setColumnFooter("Taxon", "Total amount of Taxa displayed: " + container.size());
144
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
128 145

  
129 146
		table.setCacheRate(20);
147
		
148
		//add generated columns for NamedAreas
149
		Collection<?> containerPropertyIds = table.getContainerPropertyIds();
150
		for (Object object : containerPropertyIds) {
151
			if(termList.contains(object)){
152
				  table.addGeneratedColumn(object, new AreaColumnGenerator());
153
			}
154
		}
130 155
	}
131 156

  
132 157

  
......
177 202
				}catch(Exception stateException){
178 203
					//TODO create Table without DTO
179 204
				}
180
				if(propertyList != null){
181
					for(Object propertyId:propertyList){
182
						table.removeGeneratedColumn(propertyId);
183
					}
184
					redrawTable();
185
				}
186 205
				Notification.show("Data saved", Notification.Type.HUMANIZED_MESSAGE);
187
				propertyList = null;
188
				propertyList = new ArrayList<Object>();
189
				table.setEditable(false);
190
				toolbar.getSaveButton().setCaption("Save Data");
191 206
			}
192 207
		});
193 208

  
194

  
195
		//FIXME: Due lack of time needs to be properly done
196

  
197
//		Button editButton = toolbar.getEditButton();
198
//		editButton.setClickShortcut(KeyCode.E, ModifierKey.CTRL);
199
//		editButton.setDescription("Shortcut: CTRL+e");
200
//		editButton.addClickListener(new ClickListener() {
201
//			private static final long serialVersionUID = 1L;
202
//			@Override
203
//			public void buttonClick(ClickEvent event) {
204
//			    //				if(table.isEditable() == false){
205
//			    //					table.setEditable(true);
206
//			    for(Object prop:table.getContainerPropertyIds()){
207
//			            if(!prop.equals("Taxon")&&!prop.equals("Rank")){
208
//			                table.addGeneratedColumn(prop, createTableColumnGenerator());
209
//			            }
210
//			    }
211
////				}//else{
212
////					table.setEditable(false);
213
////					table.refreshRowCache();
214
////				}
215
//			}
216
//		});
217

  
218
		/**Double Click listener for Table*/
219
		table.addItemClickListener(new ItemClickListener() {
220
		    private static final long serialVersionUID = 1L;
221

  
222
		    @Override
223
		    public void itemClick(ItemClickEvent event) {
224
		        TaxonBase taxonBase = loadTaxonFromSelection(event);
225
		        currentTaxon = (Taxon)taxonBase;
226
		        if(event.isDoubleClick()){
227
		            if(!(event.getPropertyId().toString().equalsIgnoreCase("Taxon")) && !(event.getPropertyId().toString().equalsIgnoreCase("Rank"))){
228
		                if(!table.removeGeneratedColumn(event.getPropertyId())){
229
		                    table.addGeneratedColumn(event.getPropertyId(), createTableColumnGenerator());
230
		                    propertyList.add(event.getPropertyId());
231
		                }else{
232
		                    table.removeGeneratedColumn(event.getPropertyId());
233
		                    propertyList.remove(event.getPropertyId());
234
		                    redrawTable();
235
		                }
236
		            }
237
		        }
238
		    }
239
		});
240
	}
241

  
242
	private TaxonBase loadTaxonFromSelection(ItemClickEvent event) {
243
	    Item item = event.getItem();
244
	    Property itemProperty = item.getItemProperty("uuid");
245
	    UUID uuid = UUID.fromString(itemProperty.getValue().toString());
246
	    TaxonBase taxonBase = listener.getTaxonService().load(uuid);
247
	    return taxonBase;
248
	}
249

  
250
	private String refreshValue(ComboBox box, Object value){
251
	    if(box.getValue() == null){
252
	        if(value != null){
253
	            return value.toString();
254
	        }else{
255
	            return "click me for new Distribution Status";
256
	        }
257
	    }else{
258
	        return box.getValue().toString();
259
	    }
260
	}
261

  
262
	private ColumnGenerator createTableColumnGenerator(){
263

  
264
	    ColumnGenerator generator = new ColumnGenerator() {
265

  
266
	        private static final long serialVersionUID = 1L;
267

  
268
	        @Override
269
	        public Object generateCell(Table source, Object itemId, Object columnId) {
270
	            Property containerProperty = source.getContainerProperty(itemId, columnId);
271
	            Object value = null;
272
	            if(containerProperty != null){
273
	                value = containerProperty.getValue();
274
	            }
275
	            final UUID uuid = UUID.fromString(table.getItem(itemId).getItemProperty("uuid").getValue().toString());
276
	            final ComboBox box = new ComboBox("Occurrence Status: ", listener.getPresenceAbsenceContainer());
277
	            final String area = columnId.toString();
278
	            box.setImmediate(true);
279
	            final Object value1 = value;
280
	            box.addValueChangeListener(new ValueChangeListener() {
281
	                @Override
282
	                public void valueChange(ValueChangeEvent event) {
283
	                    Taxon taxon = (Taxon)listener.getTaxonService().load(uuid);
284

  
285
	                    if(value1==null){
286
	                        listener.createDistributionField(taxon, box.getValue(), area);
287
	                        Notification.show("Create Status", Notification.Type.TRAY_NOTIFICATION);
288
	                    }else{
289
	                        int result = listener.updateDistributionField(area, box.getValue(), taxon);
290
	                        if(result == 1){
291
	                            Notification.show("Delete Status", Notification.Type.TRAY_NOTIFICATION);
292
	                        }else if(result == 0){
293
	                            Notification.show("DescriptionService wrote", Notification.Type.TRAY_NOTIFICATION);
294
	                        }
295
	                    }
296
	                }
297
	            });
298
	            final PopupView popup = new PopupView(new PopupView.Content() {
299
	                private static final long serialVersionUID = 1L;
300
	                @Override
301
	                public String getMinimizedValueAsHTML() {
302
	                    return refreshValue(box, value1);
303
	                }
304
	                @Override
305
	                public Component getPopupComponent() {
306
	                    //FIXME: find a better solution
307
	                    box.setValue(compareObjectToPAT(value1));
308
	                    box.setBuffered(true);
309
	                    return box;
310
	                }
311
	            });
312
	            popup.addPopupVisibilityListener(new PopupVisibilityListener() {
313

  
314
	                @Override
315
	                public void popupVisibilityChange(PopupVisibilityEvent event) {
316

  
317
	                }
318
	            });
319
	            popup.setHideOnMouseOut(true);
320
	            return popup;
321
	        }
322
	    };
323
	    return generator;
324 209
	}
325 210

  
326 211
	private PresenceAbsenceTerm compareObjectToPAT(Object object){
......
333 218
	    return null;
334 219
	}
335 220

  
336
	private void redrawTable(){
337
	    try {
338
	        CdmSQLContainer sqlContainer = listener.getSQLContainer();
339
	        sqlContainer.refresh();
340
	        table.setContainerDataSource(sqlContainer);
341
	        table.setVisibleColumns(columnList.toArray());
342
	    } catch (SQLException e) {
343
	        e.printStackTrace();
344
	    }
345
	}
346

  
347

  
348 221
}

Also available in: Unified diff