Project

General

Profile

Download (13.1 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.vaadin.view.dbstatus;
11

    
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import com.vaadin.data.Property;
20
import com.vaadin.data.Property.ValueChangeEvent;
21
import com.vaadin.data.Property.ValueChangeListener;
22
import com.vaadin.data.util.BeanItemContainer;
23
import com.vaadin.data.util.sqlcontainer.RowId;
24
import com.vaadin.server.Page;
25
import com.vaadin.server.VaadinSession;
26
import com.vaadin.ui.AbstractLayout;
27
import com.vaadin.ui.Alignment;
28
import com.vaadin.ui.Button.ClickEvent;
29
import com.vaadin.ui.Button.ClickListener;
30
import com.vaadin.ui.ComboBox;
31
import com.vaadin.ui.HorizontalLayout;
32
import com.vaadin.ui.ListSelect;
33
import com.vaadin.ui.Notification;
34
import com.vaadin.ui.ProgressBar;
35
import com.vaadin.ui.Table.ColumnHeaderMode;
36
import com.vaadin.ui.TextField;
37
import com.vaadin.ui.Tree.ExpandEvent;
38
import com.vaadin.ui.Tree.ExpandListener;
39
import com.vaadin.ui.TreeTable;
40
import com.vaadin.ui.UI;
41
import com.vaadin.ui.VerticalLayout;
42

    
43
import eu.etaxonomy.cdm.common.CdmUtils;
44
import eu.etaxonomy.cdm.model.common.TermVocabulary;
45
import eu.etaxonomy.cdm.model.location.NamedArea;
46
import eu.etaxonomy.cdm.model.taxon.Classification;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
49
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
50
import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
51
import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
52
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
53
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
54
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
55

    
56
/**
57
 *
58
 * @author pplitzner
59
 *
60
 */
61
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener, ExpandListener{
62

    
63
    private static final long serialVersionUID = 1439411115014088780L;
64
    private ComboBox classificationBox;
65
    private TextField taxonFilter;
66
    private ComboBox distAreaBox;
67
    private ListSelect namedAreaList;
68
    private TreeTable taxonTree;
69
    
70
    /**
71
     * The constructor should first build the main layout, set the
72
     * composition root and then do any custom initialization.
73
     *
74
     * The constructor will not be automatically regenerated by the
75
     * visual editor.
76
     * @param distributionTableView
77
     */
78
    public DistributionSettingsConfigWindow(DistributionTableView distributionTableView) {
79
        super();
80
    }
81

    
82
    @Override
83
    protected void init() {
84
        //init classification
85
        Classification classification = presenter.getChosenClassification();
86
        try {
87
            classificationBox.setContainerDataSource(new CdmSQLContainer(CdmQueryFactory.generateTableQuery("Classification")));
88
        } catch (SQLException e) {
89
            DistributionEditorUtil.showSqlError(e);
90
        }
91
        RowId parent = null;
92
        if(classification!=null){
93
        	parent = new RowId(classification.getRootNode().getId());
94
        }
95
        else if(classificationBox.getItemIds().size()==1){
96
            //only one classification exists
97
            parent = (RowId) classificationBox.getItemIds().iterator().next();
98
        }
99
        if(parent!=null){
100
            classificationBox.setValue(new RowId(parent.getId()));
101
            showClassificationTaxa(getUuidAndTitleCacheFromRowId(parent));
102
        }
103
        
104
        classificationBox.addValueChangeListener(this);
105
        taxonFilter.addValueChangeListener(this);
106
        taxonTree.addExpandListener(this);
107

    
108
        TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
109
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
110
        distAreaBox.setValue(chosenArea);
111
        distAreaBox.addValueChangeListener(this);
112

    
113
        if(chosenArea!=null){
114
            NamedAreaContainer container = new NamedAreaContainer(chosenArea);
115
            namedAreaList.setContainerDataSource(container);
116
        }
117
        Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
118
        namedAreaList.setValue(selectedAreas);
119

    
120
        okButton.addClickListener(this);
121
        cancelButton.addClickListener(this);
122
        updateButtons();
123
    }
124

    
125
    @Override
126
    protected AbstractLayout buildMainLayout() {
127

    
128
        mainLayout = new VerticalLayout();
129
        mainLayout.setSizeFull();
130

    
131
        HorizontalLayout leftAndRightContainer = new HorizontalLayout();
132
        leftAndRightContainer.setImmediate(false);
133
        leftAndRightContainer.setSizeFull();
134
        leftAndRightContainer.setMargin(true);
135
        leftAndRightContainer.setSpacing(true);
136

    
137
        VerticalLayout leftContainer = new VerticalLayout();
138
        leftContainer.setImmediate(false);
139
        leftContainer.setSpacing(true);
140
        leftContainer.setSizeFull();
141

    
142
        VerticalLayout rightContainer = new VerticalLayout();
143
        rightContainer.setImmediate(false);
144
        rightContainer.setSpacing(true);
145
        rightContainer.setSizeFull();
146

    
147
        //classification
148
        classificationBox = new ComboBox("Classification");
149
        classificationBox.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
150
        classificationBox.setInputPrompt("Please select a classification...");
151
        classificationBox.setImmediate(true);
152
        classificationBox.setNewItemsAllowed(false);
153
        classificationBox.setNullSelectionAllowed(false);
154
        classificationBox.setSizeFull();
155
        classificationBox.setWidth("100%");
156

    
157
        //taxonFilter
158
        taxonFilter = new TextField("Filter");
159
        taxonFilter.setInputPrompt("Filter taxa by name...");
160
        taxonFilter.setSizeFull();
161
        taxonFilter.setImmediate(true);
162

    
163
        //distribution area box
164
        distAreaBox = new ComboBox("Distribution Area:");
165
        distAreaBox.setInputPrompt("Please select a distribution area...");
166
        distAreaBox.setImmediate(true);
167
        distAreaBox.setNullSelectionAllowed(false);
168
        distAreaBox.setNewItemsAllowed(false);
169
        distAreaBox.setSizeFull();
170
        distAreaBox.setWidth("100%");
171

    
172
        // named areas
173
        namedAreaList = new ListSelect();
174
        namedAreaList.setCaption("Areas");
175
        namedAreaList.setSizeFull();
176
        namedAreaList.setMultiSelect(true);
177

    
178
        //taxonomy
179
        taxonTree = new TreeTable("Taxonomy");
180
        taxonTree.setSelectable(true);
181
        taxonTree.setSizeFull();
182
        taxonTree.setImmediate(true);
183
        taxonTree.setCacheRate(20);
184
        taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
185
        taxonTree.setMultiSelect(true);
186

    
187
        leftContainer.addComponent(distAreaBox);
188
        leftContainer.addComponent(namedAreaList);
189
        leftContainer.setExpandRatio(distAreaBox, 0.1f);
190
        leftContainer.setExpandRatio(namedAreaList, 0.9f);
191
        leftContainer.setSizeFull();
192

    
193
        rightContainer.addComponent(classificationBox);
194
        rightContainer.addComponent(taxonFilter);
195
        rightContainer.addComponent(taxonTree);
196
        rightContainer.setExpandRatio(classificationBox, 0.1f);
197
        rightContainer.setExpandRatio(taxonFilter, 0.1f);
198
        rightContainer.setExpandRatio(taxonTree, 0.8f);
199

    
200
        leftAndRightContainer.addComponent(leftContainer);
201
        leftAndRightContainer.addComponent(rightContainer);
202

    
203
        //button toolbar
204
        HorizontalLayout buttonToolBar = createOkCancelButtons();
205

    
206
        mainLayout.addComponent(leftAndRightContainer);
207
        mainLayout.addComponent(buttonToolBar);
208
        mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
209
        mainLayout.setExpandRatio(buttonToolBar, 0.1f);
210
        mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
211

    
212
        return leftAndRightContainer;
213
    }
214

    
215
    @Override
216
    public void valueChange(ValueChangeEvent event) {
217
        Property property = event.getProperty();
218
        if(property==classificationBox){
219
        	UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
220
            showClassificationTaxa(parent);
221
        }
222
        else if(property==taxonFilter){
223
            String filterText = taxonFilter.getValue();
224
            Property uuidProperty = classificationBox.getContainerProperty(classificationBox.getValue(),"uuid");
225
            if(uuidProperty==null){
226
            	Notification.show("Please select a classification");
227
            }
228
            else{
229
            	if(CdmUtils.isNotBlank(filterText)){
230
            		UUID classificationUuid = UUID.fromString((String) uuidProperty.getValue());
231
            		List<UuidAndTitleCache<TaxonNode>> taxa = CdmSpringContextHelper.getTaxonNodeService().getUuidAndTitleCache(null, filterText, classificationUuid);
232
            		BeanItemContainer<UuidAndTitleCache<TaxonNode>> container = new BeanItemContainer<>(UuidAndTitleCache.class);
233
            		taxonTree.setContainerDataSource(container);
234
            		for (UuidAndTitleCache<TaxonNode> taxon : taxa) {
235
            			container.addItem(taxon);
236
            			taxonTree.setChildrenAllowed(taxon, false);
237
            		}
238
            		taxonTree.setVisibleColumns("titleCache");
239
            	}
240
            	else{
241
            		UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
242
            		showClassificationTaxa(parent);
243
            	}
244
            }
245
        }
246
        else if(property==distAreaBox){
247
            TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
248
            NamedAreaContainer container = new NamedAreaContainer(vocabulary);
249
            namedAreaList.setContainerDataSource(container);
250
        }
251
        updateButtons();
252
    }
253

    
254
    @Override
255
    protected boolean isValid() {
256
        return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
257
    }
258

    
259
    @Override
260
    public void buttonClick(ClickEvent event) {
261
        Object source = event.getSource();
262
        if(source==okButton){
263
            List<UUID> taxonNodes = new ArrayList<>();
264
            TermVocabulary<NamedArea> term = null;
265
            String uuidString = (String) classificationBox.getContainerProperty(classificationBox.getValue(),"uuid").getValue();
266
            UUID classificationUuid = UUID.fromString(uuidString);
267
            Set<UuidAndTitleCache<TaxonNode>> treeSelection = (Set<UuidAndTitleCache<TaxonNode>>) taxonTree.getValue();
268
			if(!treeSelection.isEmpty()){
269
				for (UuidAndTitleCache<TaxonNode> uuidAndTitleCache : treeSelection) {
270
					taxonNodes.add(uuidAndTitleCache.getUuid());
271
				}
272
            }
273
            term = (TermVocabulary<NamedArea>) distAreaBox.getValue();
274
            Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
275
            DistributionEditorUtil.openDistributionView(taxonNodes, term, selectedAreas, classificationUuid);
276
            window.close();
277
        }
278
        else if(source==cancelButton){
279
            window.close();
280
        }
281
    }
282

    
283
    @Override
284
    public void nodeExpand(ExpandEvent event) {
285
        UuidAndTitleCache<TaxonNode> parent = (UuidAndTitleCache<TaxonNode>) event.getItemId();
286
        ((TaxonNodeContainer) taxonTree.getContainerDataSource()).addChildItems(parent);
287
    }
288

    
289
    private void showClassificationTaxa(UuidAndTitleCache<TaxonNode> parent) {
290
        final Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(parent);
291
        // Enable polling and set frequency to 0.5 seconds
292
        UI.getCurrent().setPollInterval(500);
293
        taxonTree.setEnabled(false);
294
        Notification.show("Loading taxa...");
295

    
296
        new TreeUpdater(children).start();
297
    }
298

    
299
    private UuidAndTitleCache<TaxonNode> getUuidAndTitleCacheFromRowId(Object classificationSelection) {
300
        String uuidString = (String) classificationBox.getContainerProperty(classificationSelection, "uuid").getValue();
301
        int id = (int) classificationBox.getContainerProperty(classificationSelection, "id").getValue();
302
        String titleCache = (String) classificationBox.getContainerProperty(classificationSelection, "titleCache").getValue();
303
        UUID uuid = UUID.fromString(uuidString);
304
        UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
305
        return parent;
306
    }
307
    
308
    private class TreeUpdater extends Thread{
309
    	
310
    	private Collection<UuidAndTitleCache<TaxonNode>> children;
311

    
312
    	
313
		public TreeUpdater(Collection<UuidAndTitleCache<TaxonNode>> children) {
314
			this.children = children;
315
		}
316

    
317
		@Override
318
    	public void run() {
319
			UI.getCurrent().access(new Runnable() {
320
				@Override
321
				public void run() {
322
					taxonTree.setContainerDataSource(new TaxonNodeContainer(children));
323

    
324
			        Notification notification = new Notification("Loading complete");
325
			        notification.setDelayMsec(500);
326
			        notification.show(Page.getCurrent());
327
			        taxonTree.setEnabled(true);
328

    
329
			        //disable polling when all taxa are loaded
330
			        UI.getCurrent().setPollInterval(-1);
331
				}
332
			});
333
    	}
334
    }
335
}
(2-2/5)