Project

General

Profile

« Previous | Next » 

Revision 1746c51d

Added by Alexander Oppermann over 9 years ago

finalize vaadin app to seperate to prepare the next step for separating vaadin from cdmlib-remote

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionTableView.java
1 1
package eu.etaxonomy.cdm.vaadin.view.dbstatus;
2 2

  
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
import java.util.SortedSet;
11

  
3 12
import com.vaadin.annotations.AutoGenerated;
4
import com.vaadin.data.Container;
13
import com.vaadin.data.Property.ValueChangeListener;
5 14
import com.vaadin.data.util.BeanItemContainer;
6
import com.vaadin.data.util.IndexedContainer;
7 15
import com.vaadin.navigator.View;
8 16
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
9 17
import com.vaadin.ui.AbsoluteLayout;
10 18
import com.vaadin.ui.Button.ClickEvent;
11 19
import com.vaadin.ui.Button.ClickListener;
12
import com.vaadin.ui.Component;
20
import com.vaadin.ui.Table.ColumnGenerator;
21
import com.vaadin.ui.ComboBox;
13 22
import com.vaadin.ui.CustomComponent;
14
import com.vaadin.ui.Field;
23
import com.vaadin.ui.Label;
24
import com.vaadin.ui.Notification;
15 25
import com.vaadin.ui.Table;
16
import com.vaadin.ui.TableFieldFactory;
17 26

  
27
import eu.etaxonomy.cdm.model.common.CdmBase;
18 28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
19
import eu.etaxonomy.cdm.vaadin.model.DistributionDTO;
20
import eu.etaxonomy.cdm.vaadin.model.Users;
21
import eu.etaxonomy.cdm.vaadin.model.taxonDTO;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30
import eu.etaxonomy.cdm.model.description.Distribution;
31
import eu.etaxonomy.cdm.model.description.Feature;
32
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
33
import eu.etaxonomy.cdm.model.description.TaxonDescription;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
38
import eu.etaxonomy.cdm.vaadin.model.LazyLoadedContainer;
22 39

  
23 40
public class DistributionTableView extends CustomComponent implements IDistributionTableComponent, View, ClickListener{
24 41

  
......
35 52
	
36 53
	private DistributionTableComponentListener listener;
37 54
	
55
	ArrayList<String> columnList;
56
	ArrayList<String> headerList;
57
	
38 58
	/**
39 59
	 * The constructor should first build the main layout, set the
40 60
	 * composition root and then do any custom initialization.
......
48 68
		
49 69
		
50 70
	}
51

  
52
	
53
	public void dataBinding(){
54
//		LazyLoadedContainer container = new LazyLoadedContainer(DbTableDTO.class);
55
		BeanItemContainer<taxonDTO> taxonContainer = new BeanItemContainer<taxonDTO>(taxonDTO.class);
56
		taxonContainer.addNestedContainerProperty("distribution.name");
57
		taxonContainer.addNestedContainerProperty("distribution.status");
58
		BeanItemContainer<DistributionDTO> distContainer = new BeanItemContainer<DistributionDTO>(DistributionDTO.class);
59
		for(int i = 0; i<100; i++){
60
			distContainer.addBean(new DistributionDTO("Area"+i, "Status"+i));
61
		}
62
		for(int i = 0; i<100; i++){
63
			taxonContainer.addBean(new taxonDTO("taxon"+i, new DistributionDTO("Area"+i, "Status"+i)));
64
		}
65

  
66
		Users user = new Users();
67
		
68
		table_1.setContainerDataSource(user);
69
		
70
//		container.addNestedContainerProperty("dDTO.status");
71
	}
72 71
	
73 72
	@Override
74 73
	public void addListener(DistributionTableComponentListener listener) {
......
86 85
		// top-level component properties
87 86
		setWidth("100.0%");
88 87
		setHeight("100.0%");
89

  
88
		//Horizontal Toolbar
89
		HorizontalToolbar bar = new HorizontalToolbar();
90
		mainLayout.addComponent(bar, "top:0.0px;right:0.0px;");
91
		
90 92
		// table_1
91 93
		table_1 = new Table();
92 94
		table_1.setImmediate(false);
93 95
		table_1.setWidth("100.0%");
94 96
		table_1.setHeight("100.0%");
95
		mainLayout.addComponent(table_1, "top:0.0px;right:0.0px;");
97
		mainLayout.addComponent(table_1, "top:100px;right:0.0px;");
96 98

  
97 99
		return mainLayout;
98 100
	}
......
115 117

  
116 118
    }
117 119

  
120
    
121
	public void dataBinding(){
122

  
123
//		container.addNestedContainerProperty("dDTO.status");
124
		final LazyLoadedContainer container = listener.getLazyLoadedContainer();
125
		Set<DefinedTermBase> chosenTerms = listener.getChosenTerms();
126
		generateDsitributionColumn(chosenTerms);
127
		
128
		table_1.setContainerDataSource(container);
129
		table_1.setColumnReorderingAllowed(true);
130
		table_1.setSortEnabled(false);
131
//		setVisibleColumns(columns);
132
		Object[] visibleColumns = columnList.toArray();
133
		table_1.setVisibleColumns(visibleColumns);
134
		table_1.setColumnHeaders(headerList.toArray(new String[headerList.size()]));//new String[]{"Taxon", "Rang"});// ,"Deutschland"
135
		table_1.setColumnCollapsingAllowed(true);
136
		table_1.setSelectable(true);
137
//		table_1.setSizeUndefined();
138
//		setSizeFull();
139
		table_1.setPageLength(20);
140
		table_1.setFooterVisible(true);
141
		table_1.setColumnFooter("fullTitleCache", "Total amount of Taxa displayed: " + container.size());
142

  
143
		table_1.setCacheRate(10);
144
	}
145
    
146
    
147
    void generateDsitributionColumn(Set<DefinedTermBase> terms){
148
		columnList = new ArrayList<String>(Arrays.asList(new String[]{"fullTitleCache","rank"}));
149
		headerList = new ArrayList<String>(Arrays.asList(new String[]{"Taxon","Rang"}));
150
		for(final DefinedTermBase dt : terms){
151
			columnList.add(dt.getTitleCache());
152
			headerList.add(dt.getTitleCache());
153
//			container.addContainerProperty(dt.getTitleCache(), String.class, null);
154
			
155
			table_1.addContainerProperty(dt.getTitleCache(), String.class, null);
156
			try{
157
				table_1.addGeneratedColumn(dt.getTitleCache(), new ColumnGenerator() {
158
					public Object generateCell(Table source, Object itemId, Object columnId) {
159
						Label tf = new Label();
160
						ComboBox box = null;
161
						if(itemId instanceof TaxonNode){
162
							TaxonNode tn = CdmBase.deproxy((TaxonNode) itemId, TaxonNode.class);
163
							Taxon taxon = CdmBase.deproxy(tn.getTaxon(), Taxon.class);
164
							taxon = (Taxon) listener.getTaxonService().load(taxon.getUuid());
165
							HashMap<DescriptionElementBase, Distribution> map = getDistribution(taxon);
166
							
167
							List<PresenceAbsenceTerm> listTerm = listener.getPresenceAbsenceTerms();
168
							BeanItemContainer<PresenceAbsenceTerm> termContainer = new BeanItemContainer<PresenceAbsenceTerm>(PresenceAbsenceTerm.class);
169
							termContainer.addAll(listTerm);
170
							box = new ComboBox("Occurrence Status: ", termContainer);
171
							box.setImmediate(true);
172
							
173
							if(map != null){
174
								/** update field **/
175
								DescriptionElementBase deb = null; 
176
								Distribution db = null;
177
								for(Map.Entry<DescriptionElementBase, Distribution> entry : map.entrySet()){
178
									deb = entry.getKey();
179
									db = entry.getValue();
180
								}
181
								if(table_1.isEditable()){
182
									box = updateDistributionField(deb, db, termContainer, box, taxon);
183
								}else{
184
									if(db.getStatus() != null){
185
									tf.setValue(db.getStatus().toString());
186
									}else{
187
										//FIXME: case for delete?
188
										Notification.show("Possible Error for " +taxon.getTitleCache() + " for Distribution: " + db.getArea().getTitleCache(), Notification.Type.TRAY_NOTIFICATION);
189
										tf.setValue("-");
190
									}
191
								}
192
							}else{
193
								/** create distribution if it does not exist and set the status **/
194
								if(table_1.isEditable()){
195
									box = createDistributionField(taxon, termContainer, box);
196
								}else{
197
									tf.setValue("-");
198
								}
199
							}
200
							
201
						}
202
						
203
						if(table_1.isEditable()){
204
							return box;
205
						}else{
206
							return tf;
207
						}
208
					}
209

  
210
					private ComboBox createDistributionField(
211
							final Taxon taxon,
212
							BeanItemContainer<PresenceAbsenceTerm> termContainer,ComboBox box) {
213
						final ComboBox box2 = box;
214
						box.addValueChangeListener(new ValueChangeListener() {
215

  
216
							@Override
217
							public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
218
								NamedArea area = (NamedArea) dt;
219
								Distribution db = Distribution.NewInstance(area, (PresenceAbsenceTerm) box2.getValue());
220
					               Set<TaxonDescription> descriptions = taxon.getDescriptions();
221
				                    if (descriptions != null) {
222
				                        for (TaxonDescription desc : descriptions) {
223
				                            // add to first taxon description
224
				                            desc.addElement(db);
225
											listener.getDescriptionService().saveOrUpdate(desc);
226
				                            break;
227
				                        }
228
				                    } else {// there are no TaxonDescription yet.
229
				                        TaxonDescription td = TaxonDescription.NewInstance(taxon);
230
				                        td.addElement(db);
231
				                        taxon.addDescription(td);
232
				                        listener.getTaxonService().saveOrUpdate(taxon);
233
				                    }
234
							}
235
						
236
						});
237
						
238
						
239
						return box;
240
					}
241

  
242
					private HashMap<DescriptionElementBase, Distribution> getDistribution(Taxon taxon){
243
						Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
244
						List<DescriptionElementBase> listTaxonDescription = listener.listDescriptionElementsForTaxon(taxon, setFeature);
245
						for(DescriptionElementBase deb : listTaxonDescription){
246
							if(deb instanceof Distribution){
247
								Distribution db = (Distribution)deb;
248
								String titleCache = dt.getTitleCache();
249
								if(db.getArea().getTitleCache().equalsIgnoreCase(titleCache)){
250
									HashMap<DescriptionElementBase, Distribution> map = new HashMap<DescriptionElementBase, Distribution>();
251
									map.put(deb, db);
252
									return map;
253
								}
254
							}
255
						}
256
						return null;
257
					}
258
					
259
					private ComboBox updateDistributionField(DescriptionElementBase deb, Distribution db, BeanItemContainer<PresenceAbsenceTerm> termContainer, ComboBox box, Taxon taxon) {
260
						final Distribution db2 = db;
261
						final DescriptionElementBase deb2 = deb;
262
						box.setValue(db.getStatus());
263
						final ComboBox box2 = box;
264
						final Taxon taxon2 = taxon;
265
						box.addValueChangeListener(new ValueChangeListener() {
266
							
267
							private static final long serialVersionUID = 1L;
268

  
269
							@Override
270
							public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
271
								if(box2.getValue() == null){//delete descriptionElementBase
272
									listener.getDescriptionService().deleteDescriptionElement(deb2);
273
									listener.getTaxonService().saveOrUpdate(taxon2);
274
									Notification.show("Delete Status", Notification.Type.TRAY_NOTIFICATION);
275
								}else{
276
									db2.setStatus((PresenceAbsenceTerm)box2.getValue());
277
									listener.getDescriptionService().saveDescriptionElement(deb2);
278
									Notification.show("DescriptionService wrote", Notification.Type.TRAY_NOTIFICATION);
279
								}
280
							}
281
						});
282
						return box;
283
					}
284
				});
285
			}catch(IllegalArgumentException e){
286
				e.printStackTrace();
287
			}
288
		}	
289
    }
118 290
}

Also available in: Unified diff