Project

General

Profile

Download (11.9 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.VaadinSession;
25
import com.vaadin.ui.AbstractLayout;
26
import com.vaadin.ui.Alignment;
27
import com.vaadin.ui.Button.ClickEvent;
28
import com.vaadin.ui.Button.ClickListener;
29
import com.vaadin.ui.ComboBox;
30
import com.vaadin.ui.HorizontalLayout;
31
import com.vaadin.ui.ListSelect;
32
import com.vaadin.ui.Table.ColumnHeaderMode;
33
import com.vaadin.ui.TextField;
34
import com.vaadin.ui.Tree.ExpandEvent;
35
import com.vaadin.ui.Tree.ExpandListener;
36
import com.vaadin.ui.TreeTable;
37
import com.vaadin.ui.VerticalLayout;
38

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

    
52
/**
53
 *
54
 * @author pplitzner
55
 *
56
 */
57
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener, ExpandListener{
58

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

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

    
104
        TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
105
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
106
        distAreaBox.setValue(chosenArea);
107
        distAreaBox.addValueChangeListener(this);
108

    
109
        if(chosenArea!=null){
110
            NamedAreaContainer container = new NamedAreaContainer(chosenArea);
111
            namedAreaList.setContainerDataSource(container);
112
        }
113
        Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
114
        namedAreaList.setValue(selectedAreas);
115

    
116
        okButton.addClickListener(this);
117
        cancelButton.addClickListener(this);
118
        updateButtons();
119
    }
120

    
121
    @Override
122
    protected AbstractLayout buildMainLayout() {
123

    
124
        mainLayout = new VerticalLayout();
125
        mainLayout.setSizeFull();
126

    
127
        HorizontalLayout leftAndRightContainer = new HorizontalLayout();
128
        leftAndRightContainer.setImmediate(false);
129
        leftAndRightContainer.setSizeFull();
130
        leftAndRightContainer.setMargin(true);
131
        leftAndRightContainer.setSpacing(true);
132

    
133
        VerticalLayout leftContainer = new VerticalLayout();
134
        leftContainer.setImmediate(false);
135
        leftContainer.setSpacing(true);
136
        leftContainer.setSizeFull();
137

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

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

    
153
        //taxonFilter
154
        taxonFilter = new TextField("Filter");
155
        taxonFilter.setInputPrompt("Filter taxa by name...");
156
        taxonFilter.setSizeFull();
157
        taxonFilter.setImmediate(true);
158

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

    
168
        // named areas
169
        namedAreaList = new ListSelect();
170
        namedAreaList.setCaption("Areas");
171
        namedAreaList.setSizeFull();
172
        namedAreaList.setMultiSelect(true);
173

    
174
        //taxonomy
175
        taxonTree = new TreeTable("Taxonomy");
176
        taxonTree.setSelectable(true);
177
        taxonTree.setSizeFull();
178
        taxonTree.setImmediate(true);
179
        taxonTree.setCacheRate(20);
180
        taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
181
        taxonTree.setMultiSelect(true);
182

    
183
        leftContainer.addComponent(distAreaBox);
184
        leftContainer.addComponent(namedAreaList);
185
        leftContainer.setExpandRatio(distAreaBox, 0.1f);
186
        leftContainer.setExpandRatio(namedAreaList, 0.9f);
187
        leftContainer.setSizeFull();
188

    
189
        rightContainer.addComponent(classificationBox);
190
        rightContainer.addComponent(taxonFilter);
191
        rightContainer.addComponent(taxonTree);
192
        rightContainer.setExpandRatio(classificationBox, 0.1f);
193
        rightContainer.setExpandRatio(taxonFilter, 0.1f);
194
        rightContainer.setExpandRatio(taxonTree, 0.8f);
195

    
196
        leftAndRightContainer.addComponent(leftContainer);
197
        leftAndRightContainer.addComponent(rightContainer);
198

    
199
        //button toolbar
200
        HorizontalLayout buttonToolBar = createOkCancelButtons();
201

    
202
        mainLayout.addComponent(leftAndRightContainer);
203
        mainLayout.addComponent(buttonToolBar);
204
        mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
205
        mainLayout.setExpandRatio(buttonToolBar, 0.1f);
206
        mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
207

    
208
        return leftAndRightContainer;
209
    }
210

    
211
    @Override
212
    public void valueChange(ValueChangeEvent event) {
213
        Property property = event.getProperty();
214
        if(property==classificationBox){
215
        	UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
216
            showClassificationTaxa(parent);
217
        }
218
        else if(property==taxonFilter){
219
            String filterText = taxonFilter.getValue();
220
            if(CdmUtils.isNotBlank(filterText)){
221
                List<UuidAndTitleCache<TaxonNode>> taxa = CdmSpringContextHelper.getTaxonNodeService().getUuidAndTitleCache(null, filterText);
222
                if(!taxa.isEmpty()){
223
                    BeanItemContainer<UuidAndTitleCache<TaxonNode>> container = new BeanItemContainer<>(UuidAndTitleCache.class);
224
                    taxonTree.setContainerDataSource(container);
225
                    for (UuidAndTitleCache<TaxonNode> taxon : taxa) {
226
                        container.addItem(taxon);
227
                        taxonTree.setChildrenAllowed(taxon, false);
228
                    }
229
                    taxonTree.setVisibleColumns("titleCache");
230
                }
231
            }
232
            else{
233
                UuidAndTitleCache<TaxonNode> parent = getUuidAndTitleCacheFromRowId(classificationBox.getValue());
234
                showClassificationTaxa(parent);
235
            }
236
        }
237
        else if(property==distAreaBox){
238
            TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
239
            NamedAreaContainer container = new NamedAreaContainer(vocabulary);
240
            namedAreaList.setContainerDataSource(container);
241
        }
242
        updateButtons();
243
    }
244

    
245
    @Override
246
    protected boolean isValid() {
247
        return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
248
    }
249

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

    
274
    @Override
275
    public void nodeExpand(ExpandEvent event) {
276
        UuidAndTitleCache<TaxonNode> parent = (UuidAndTitleCache<TaxonNode>) event.getItemId();
277
        ((TaxonNodeContainer) taxonTree.getContainerDataSource()).addChildItems(parent);
278
    }
279

    
280
    private void showClassificationTaxa(UuidAndTitleCache<TaxonNode> parent) {
281
        Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(parent);
282
        taxonTree.setContainerDataSource(new TaxonNodeContainer(children));
283
    }
284

    
285
    private UuidAndTitleCache<TaxonNode> getUuidAndTitleCacheFromRowId(Object classificationSelection) {
286
        String uuidString = (String) classificationBox.getContainerProperty(classificationSelection, "uuid").getValue();
287
        int id = (int) classificationBox.getContainerProperty(classificationSelection, "id").getValue();
288
        String titleCache = (String) classificationBox.getContainerProperty(classificationSelection, "titleCache").getValue();
289
        UUID uuid = UUID.fromString(uuidString);
290
        UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
291
        return parent;
292
    }
293
}
(2-2/5)