Project

General

Profile

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

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

    
18
import com.vaadin.data.Container;
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.Table.ColumnHeaderMode;
35
import com.vaadin.ui.TextField;
36
import com.vaadin.ui.Tree.ExpandEvent;
37
import com.vaadin.ui.Tree.ExpandListener;
38
import com.vaadin.ui.TreeTable;
39
import com.vaadin.ui.UI;
40
import com.vaadin.ui.VerticalLayout;
41

    
42
import eu.etaxonomy.cdm.common.CdmUtils;
43
import eu.etaxonomy.cdm.i18n.Messages;
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
import eu.etaxonomy.cdm.vaadin.view.distributionStatus.IDistributionTableView;
56
import eu.etaxonomy.cdm.vaadin.view.distributionStatus.settings.AreaAndTaxonSettingsPresenter;
57

    
58
/**
59
 *
60
 * @author pplitzner
61
 *
62
 */
63
public class AreaAndTaxonSettingsConfigWindow
64
            extends SettingsDialogWindowBase<AreaAndTaxonSettingsPresenter>
65
            implements ValueChangeListener, ClickListener, ExpandListener{
66

    
67
    private static final long serialVersionUID = 1439411115014088780L;
68
    private ComboBox classificationBox;
69
    private TextField taxonFilter;
70
    private ComboBox distAreaBox;
71
    private ListSelect namedAreaList;
72
    private TreeTable taxonTree;
73
    private IDistributionTableView distributionTableView;
74

    
75
    /**
76
     * The constructor should first build the main layout, set the
77
     * composition root and then do any custom initialization.
78
     *
79
     * The constructor will not be automatically regenerated by the
80
     * visual editor.
81
     * @param distributionTableView
82
     */
83
    public AreaAndTaxonSettingsConfigWindow(IDistributionTableView distributionTableView) {
84
        super();
85
        this.distributionTableView = distributionTableView;
86
    }
87

    
88
    @Override
89
    protected void init() {
90
        //init classification
91
        Classification classification = presenter.getChosenClassification();
92
        try {
93
            CdmSQLContainer classificationContainer = new CdmSQLContainer(CdmQueryFactory.generateTableQuery("Classification")); //$NON-NLS-1$
94
            classificationContainer.sort(new Object[] {"titleCache"}, new boolean[] {true}); //$NON-NLS-1$
95
            classificationBox.setContainerDataSource(classificationContainer);
96
        } catch (SQLException e) {
97
            DistributionEditorUtil.showSqlError(e);
98
        }
99
        RowId classificationRow = null;
100
        if(classification!=null){
101
            classificationRow = new RowId(classification.getId());
102
        }
103
        else if(classificationBox.getItemIds().size()==1){
104
            //only one classification exists
105
            classificationRow = (RowId) classificationBox.getItemIds().iterator().next();
106
        }
107
        if(classificationRow!=null){
108
            classificationBox.setValue(classificationRow);
109
            showClassificationTaxa(getUuidAndTitleCacheFromRowId(classificationRow));
110
        }
111

    
112
        classificationBox.addValueChangeListener(this);
113
        taxonFilter.addValueChangeListener(this);
114
        taxonTree.addExpandListener(this);
115

    
116
        //init areas
117
        TermVocabulary<NamedArea> chosenAreaVoc = presenter.getChosenAreaVoc();
118
        Container areaVocContainer = presenter.getAreaContainer();
119
        if (areaVocContainer.size() == 1){
120
            chosenAreaVoc = (TermVocabulary<NamedArea>)areaVocContainer.getItemIds().iterator().next();
121
        }
122
        distAreaBox.setContainerDataSource(areaVocContainer);
123
        distAreaBox.setValue(chosenAreaVoc);
124
        distAreaBox.addValueChangeListener(this);
125

    
126
        if(chosenAreaVoc!=null){
127
            NamedAreaContainer areaContainer = new NamedAreaContainer(chosenAreaVoc);
128
            namedAreaList.setContainerDataSource(areaContainer);
129
        }
130
        Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
131
        namedAreaList.setValue(selectedAreas);
132

    
133
        okButton.addClickListener(this);
134
        cancelButton.addClickListener(this);
135
        updateButtons();
136
    }
137

    
138
    @Override
139
    protected AbstractLayout buildMainLayout() {
140

    
141
        mainLayout = new VerticalLayout();
142
        mainLayout.setSizeFull();
143

    
144
        HorizontalLayout leftAndRightContainer = new HorizontalLayout();
145
        leftAndRightContainer.setImmediate(false);
146
        leftAndRightContainer.setSizeFull();
147
        leftAndRightContainer.setMargin(true);
148
        leftAndRightContainer.setSpacing(true);
149

    
150
        VerticalLayout leftContainer = new VerticalLayout();
151
        leftContainer.setImmediate(false);
152
        leftContainer.setSpacing(true);
153
        leftContainer.setSizeFull();
154

    
155
        VerticalLayout rightContainer = new VerticalLayout();
156
        rightContainer.setImmediate(false);
157
        rightContainer.setSpacing(true);
158
        rightContainer.setSizeFull();
159

    
160
        //classification
161
        classificationBox = new ComboBox(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_CLASSIFICATION));
162
        classificationBox.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
163
        classificationBox.setInputPrompt(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_SELECT_CLASSIFICATION));
164
        classificationBox.setImmediate(true);
165
        classificationBox.setNewItemsAllowed(false);
166
        classificationBox.setNullSelectionAllowed(false);
167
        classificationBox.setSizeFull();
168
        classificationBox.setWidth("100%"); //$NON-NLS-1$
169

    
170
        //taxonFilter
171
        taxonFilter = new TextField(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_FILTER));
172
        taxonFilter.setInputPrompt(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_FILTER_TAXA_BY_NAME));
173
        taxonFilter.setSizeFull();
174
        taxonFilter.setImmediate(true);
175

    
176
        //distribution area box
177
        distAreaBox = new ComboBox(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_DISTRIBUTION_AREA));
178
        distAreaBox.setInputPrompt(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_SELECT_DISTRIBUTION_AREA));
179
        distAreaBox.setImmediate(true);
180
        distAreaBox.setNullSelectionAllowed(false);
181
        distAreaBox.setNewItemsAllowed(false);
182
        distAreaBox.setSizeFull();
183
        distAreaBox.setWidth("100%"); //$NON-NLS-1$
184

    
185
        // named areas
186
        namedAreaList = new ListSelect();
187
        namedAreaList.setCaption(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_AREAS));
188
        namedAreaList.setSizeFull();
189
        namedAreaList.setMultiSelect(true);
190

    
191
        //taxonomy
192
        taxonTree = new TreeTable(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_TAXONOMY));
193
        taxonTree.setSelectable(true);
194
        taxonTree.setSizeFull();
195
        taxonTree.setImmediate(true);
196
        taxonTree.setCacheRate(20);
197
        taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
198
        taxonTree.setMultiSelect(true);
199

    
200
        leftContainer.addComponent(distAreaBox);
201
        leftContainer.addComponent(namedAreaList);
202
        leftContainer.setExpandRatio(distAreaBox, 0.1f);
203
        leftContainer.setExpandRatio(namedAreaList, 0.9f);
204
        leftContainer.setSizeFull();
205

    
206
        rightContainer.addComponent(classificationBox);
207
        rightContainer.addComponent(taxonFilter);
208
        rightContainer.addComponent(taxonTree);
209
        rightContainer.setExpandRatio(classificationBox, 0.1f);
210
        rightContainer.setExpandRatio(taxonFilter, 0.1f);
211
        rightContainer.setExpandRatio(taxonTree, 0.8f);
212

    
213
        leftAndRightContainer.addComponent(leftContainer);
214
        leftAndRightContainer.addComponent(rightContainer);
215

    
216
        //button toolbar
217
        HorizontalLayout buttonToolBar = createOkCancelButtons();
218

    
219
        mainLayout.addComponent(leftAndRightContainer);
220
        mainLayout.addComponent(buttonToolBar);
221
        mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
222
        mainLayout.setExpandRatio(buttonToolBar, 0.1f);
223
        mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
224

    
225
        return leftAndRightContainer;
226
    }
227

    
228
    @Override
229
    public void valueChange(ValueChangeEvent event) {
230
        Property<?> property = event.getProperty();
231
        if(property==classificationBox){
232
        	UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
233
            showClassificationTaxa(parent);
234
        }
235
        else if(property==taxonFilter){
236
            String filterText = taxonFilter.getValue();
237
            Property<?> uuidProperty = classificationBox.getContainerProperty(classificationBox.getValue(),"uuid"); //$NON-NLS-1$
238
            if(uuidProperty==null){
239
            	Notification.show(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_SELECT_CLASSIFICATION));
240
            }
241
            else{
242
            	if(CdmUtils.isNotBlank(filterText)){
243
            		UUID classificationUuid = UUID.fromString((String) uuidProperty.getValue());
244
            		List<UuidAndTitleCache<TaxonNode>> taxa = CdmSpringContextHelper.getTaxonNodeService().getUuidAndTitleCache(null, filterText, classificationUuid);
245
            		BeanItemContainer<UuidAndTitleCache<TaxonNode>> container = new BeanItemContainer<>(UuidAndTitleCache.class);
246
            		taxonTree.setContainerDataSource(container);
247
            		for (UuidAndTitleCache<TaxonNode> taxon : taxa) {
248
            			container.addItem(taxon);
249
            			taxonTree.setChildrenAllowed(taxon, false);
250
            		}
251
            		taxonTree.setVisibleColumns("titleCache"); //$NON-NLS-1$
252
            	}
253
            	else{
254
            		UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
255
            		showClassificationTaxa(parent);
256
            	}
257
            }
258
        }
259
        else if(property==distAreaBox){
260
            TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
261
            NamedAreaContainer container = new NamedAreaContainer(vocabulary);
262
            namedAreaList.setContainerDataSource(container);
263
        }
264
        updateButtons();
265
    }
266

    
267
    @Override
268
    protected boolean isValid() {
269
        return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
270
    }
271

    
272
    @Override
273
    public void buttonClick(ClickEvent event) {
274
        Object source = event.getSource();
275
        if(source==okButton){
276
            List<UUID> taxonNodes = new ArrayList<>();
277
            TermVocabulary<NamedArea> areaVoc = null;
278
            String uuidString = (String) classificationBox.getContainerProperty(classificationBox.getValue(),"uuid").getValue(); //$NON-NLS-1$
279
            UUID classificationUuid = UUID.fromString(uuidString);
280
            Set<UuidAndTitleCache<TaxonNode>> treeSelection = (Set<UuidAndTitleCache<TaxonNode>>) taxonTree.getValue();
281
			if(!treeSelection.isEmpty()){
282
				for (UuidAndTitleCache<TaxonNode> uuidAndTitleCache : treeSelection) {
283
					taxonNodes.add(uuidAndTitleCache.getUuid());
284
				}
285
            }
286
			if(treeSelection.isEmpty() && CdmUtils.isNotBlank(taxonFilter.getValue())) {
287
                for (UuidAndTitleCache<TaxonNode> uuidAndTitleCache : (Collection<UuidAndTitleCache<TaxonNode>>) taxonTree.getItemIds()) {
288
                    taxonNodes.add(uuidAndTitleCache.getUuid());
289
                }
290
			}
291
            areaVoc = (TermVocabulary<NamedArea>) distAreaBox.getValue();
292
            List<NamedArea> selectedAreas = new ArrayList<>((Set<NamedArea>)namedAreaList.getValue()); //getValue is LinkedHashSet and therefore sorted
293
            DistributionEditorUtil.updateDistributionView(distributionTableView, taxonNodes, areaVoc, selectedAreas, classificationUuid);
294
            window.close();
295
        }
296
        else if(source==cancelButton){
297
            window.close();
298
        }
299
    }
300

    
301
    @Override
302
    public void nodeExpand(ExpandEvent event) {
303
        UuidAndTitleCache<TaxonNode> parent = (UuidAndTitleCache<TaxonNode>) event.getItemId();
304
        ((TaxonNodeContainer) taxonTree.getContainerDataSource()).addChildItems(parent);
305
    }
306

    
307
    private void showClassificationTaxa(UuidAndTitleCache<TaxonNode> rootNode) {
308
        final Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(rootNode);
309
        // Enable polling and set frequency to 0.5 seconds
310
        UI.getCurrent().setPollInterval(500);
311
        taxonTree.setEnabled(false);
312
        taxonTree.removeAllItems();
313
        Notification.show(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_LOADING_TAXA));
314

    
315
        new TreeUpdater(children).start();
316
    }
317

    
318
    private UuidAndTitleCache<TaxonNode> getUuidAndTitleCacheFromRowId(Object classificationSelection) {
319
        String uuidString = (String) classificationBox.getContainerProperty(classificationSelection, "uuid").getValue(); //$NON-NLS-1$
320
        Property<Integer> rootNodeContainerProperty = null;
321

    
322
        Collection<?> ids = classificationBox.getContainerPropertyIds();
323
        //use for loop here because the case of the root node id columns differs between some DBs
324
        for (Object id : ids) {
325
			if(id instanceof String && ((String) id).toLowerCase().equals("rootnode_id")){ //$NON-NLS-1$
326
				rootNodeContainerProperty = classificationBox.getContainerProperty(classificationSelection, id);
327
				break;
328
			}
329
		}
330
		int id = rootNodeContainerProperty.getValue();
331
        String titleCache = (String) classificationBox.getContainerProperty(classificationSelection, "titleCache").getValue(); //$NON-NLS-1$
332
        UUID uuid = UUID.fromString(uuidString);
333
        UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
334
        return parent;
335
    }
336

    
337

    
338
    /**
339
     * {@inheritDoc}
340
     */
341
    @Override
342
    protected AreaAndTaxonSettingsPresenter getPresenter() {
343
        return new AreaAndTaxonSettingsPresenter();
344
    }
345

    
346
    private class TreeUpdater extends Thread{
347

    
348
    	private Collection<UuidAndTitleCache<TaxonNode>> children;
349

    
350

    
351
		public TreeUpdater(Collection<UuidAndTitleCache<TaxonNode>> children) {
352
			this.children = children;
353
		}
354

    
355
		@Override
356
    	public void run() {
357
			UI.getCurrent().access(new Runnable() {
358
				@Override
359
				public void run() {
360
					taxonTree.setContainerDataSource(new TaxonNodeContainer(children));
361

    
362
			        Notification notification = new Notification(Messages.getLocalizedString(Messages.AreaAndTaxonSettingsConfigWindow_LOADING_COMPLETE));
363
			        notification.setDelayMsec(500);
364
			        notification.show(Page.getCurrent());
365
			        taxonTree.setEnabled(true);
366
			        taxonTree.setSortContainerPropertyId("titleCache"); //$NON-NLS-1$
367
			        taxonTree.sort();
368

    
369
			        //disable polling when all taxa are loaded
370
			        UI.getCurrent().setPollInterval(-1);
371
				}
372
			});
373
    	}
374
    }
375

    
376
}
(1-1/6)