Project

General

Profile

Download (10.6 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.util.Collection;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import com.vaadin.data.Item;
17
import com.vaadin.data.Property;
18
import com.vaadin.data.Property.ValueChangeEvent;
19
import com.vaadin.data.Property.ValueChangeListener;
20
import com.vaadin.data.util.sqlcontainer.RowId;
21
import com.vaadin.server.VaadinSession;
22
import com.vaadin.ui.AbstractLayout;
23
import com.vaadin.ui.Alignment;
24
import com.vaadin.ui.Button.ClickEvent;
25
import com.vaadin.ui.Button.ClickListener;
26
import com.vaadin.ui.ComboBox;
27
import com.vaadin.ui.HorizontalLayout;
28
import com.vaadin.ui.ListSelect;
29
import com.vaadin.ui.Table.ColumnHeaderMode;
30
import com.vaadin.ui.Tree.ExpandEvent;
31
import com.vaadin.ui.Tree.ExpandListener;
32
import com.vaadin.ui.TreeTable;
33
import com.vaadin.ui.VerticalLayout;
34

    
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.TermVocabulary;
37
import eu.etaxonomy.cdm.model.location.NamedArea;
38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
40
import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
41
import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
42
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
43
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
44

    
45
/**
46
 *
47
 * @author pplitzner
48
 *
49
 */
50
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener, ExpandListener{
51

    
52
    private static final long serialVersionUID = 1439411115014088780L;
53
    private ComboBox classificationBox;
54
    private ComboBox distAreaBox;
55
    private ListSelect namedAreaList;
56
    private TreeTable taxonTree;
57
    /**
58
     * The constructor should first build the main layout, set the
59
     * composition root and then do any custom initialization.
60
     *
61
     * The constructor will not be automatically regenerated by the
62
     * visual editor.
63
     * @param distributionTableView
64
     */
65
    public DistributionSettingsConfigWindow(DistributionTableView distributionTableView) {
66
        super();
67
    }
68

    
69
    @Override
70
    protected void init() {
71
        //init classification and taxon selection
72
        TaxonNode chosenTaxonNode = presenter.getChosenTaxonNode();
73

    
74
        classificationBox.setContainerDataSource(new TaxonNodeContainer(null));
75
        Object classificationSelection = null;
76
        if(classificationBox.getItemIds().size()==1){
77
            //only one classification exists
78
            classificationSelection = classificationBox.getItemIds().iterator().next();
79
        }
80
        else if(chosenTaxonNode!=null){
81
            //get the classification from the selected taxon node
82
            classificationSelection = chosenTaxonNode.getClassification().getRootNode();
83
        }
84
        if(classificationSelection!=null){
85
            classificationBox.setValue(classificationSelection);
86
            taxonTree.addExpandListener(this);
87

    
88
            UUID uuid = ((CdmBase) classificationSelection).getUuid();
89
            int id = ((CdmBase) classificationSelection).getId();
90
            String titleCache = ((TaxonNode) classificationSelection).getClassification().getTitleCache();
91
            UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
92
            taxonTree.setContainerDataSource(new TaxonNodeContainer(parent));
93
            if(chosenTaxonNode!=null){
94
                taxonTree.select(new RowId(chosenTaxonNode.getId()));
95
            }
96
        }
97
        classificationBox.addValueChangeListener(this);
98

    
99
        TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
100
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
101
        distAreaBox.setValue(chosenArea);
102
        distAreaBox.addValueChangeListener(this);
103

    
104
        if(chosenArea!=null){
105
            NamedAreaContainer container = new NamedAreaContainer(chosenArea);
106
            namedAreaList.setContainerDataSource(container);
107
        }
108
        Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
109
        namedAreaList.setValue(selectedAreas);
110

    
111
        okButton.addClickListener(this);
112
        cancelButton.addClickListener(this);
113
        updateButtons();
114
    }
115

    
116
    @Override
117
    protected AbstractLayout buildMainLayout() {
118

    
119
        mainLayout = new VerticalLayout();
120
        mainLayout.setSizeFull();
121

    
122
        HorizontalLayout leftAndRightContainer = new HorizontalLayout();
123
        leftAndRightContainer.setImmediate(false);
124
        leftAndRightContainer.setSizeFull();
125
        leftAndRightContainer.setMargin(true);
126
        leftAndRightContainer.setSpacing(true);
127

    
128
        VerticalLayout leftContainer = new VerticalLayout();
129
        leftContainer.setImmediate(false);
130
        leftContainer.setSpacing(true);
131
        leftContainer.setSizeFull();
132

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

    
138
        //classification and term
139
        classificationBox = new ComboBox("Classification");
140
        classificationBox.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
141
        classificationBox.setInputPrompt("Please select a classification...");
142
        classificationBox.setImmediate(true);
143
        classificationBox.setNewItemsAllowed(false);
144
        classificationBox.setNullSelectionAllowed(false);
145
        classificationBox.setSizeFull();
146
        classificationBox.setWidth("100%");
147

    
148
        //distribution area box
149
        distAreaBox = new ComboBox("Distribution Area:");
150
        distAreaBox.setInputPrompt("Please select a distribution area...");
151
        distAreaBox.setImmediate(true);
152
        distAreaBox.setNullSelectionAllowed(false);
153
        distAreaBox.setNewItemsAllowed(false);
154
        distAreaBox.setSizeFull();
155
        distAreaBox.setWidth("100%");
156

    
157
        // named areas
158
        namedAreaList = new ListSelect();
159
        namedAreaList.setCaption("Areas");
160
        namedAreaList.setSizeFull();
161
        namedAreaList.setMultiSelect(true);
162

    
163
        //taxonomy
164
        taxonTree = new TreeTable("Taxonomy");
165
        taxonTree.setSelectable(true);
166
        taxonTree.setSizeFull();
167
        taxonTree.setImmediate(true);
168
        taxonTree.setCacheRate(20);
169
        taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
170

    
171
        leftContainer.addComponent(distAreaBox);
172
        leftContainer.addComponent(namedAreaList);
173
        leftContainer.setExpandRatio(distAreaBox, 0.1f);
174
        leftContainer.setExpandRatio(namedAreaList, 0.9f);
175
        leftContainer.setSizeFull();
176

    
177
        rightContainer.addComponent(classificationBox);
178
        rightContainer.addComponent(taxonTree);
179
        rightContainer.setExpandRatio(classificationBox, 0.1f);
180
        rightContainer.setExpandRatio(taxonTree, 0.9f);
181

    
182
        leftAndRightContainer.addComponent(leftContainer);
183
        leftAndRightContainer.addComponent(rightContainer);
184

    
185
        //button toolbar
186
        HorizontalLayout buttonToolBar = createOkCancelButtons();
187

    
188
        mainLayout.addComponent(leftAndRightContainer);
189
        mainLayout.addComponent(buttonToolBar);
190
        mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
191
        mainLayout.setExpandRatio(buttonToolBar, 0.1f);
192
        mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
193

    
194
        return leftAndRightContainer;
195
    }
196

    
197
    @Override
198
    public void valueChange(ValueChangeEvent event) {
199
        Property property = event.getProperty();
200
        if(property==classificationBox){
201
            TaxonNode parentNode = (TaxonNode) event.getProperty().getValue();
202
            if(parentNode!=null){
203
                UUID uuid = parentNode.getUuid();
204
                int id = parentNode.getId();
205
                String titleCache = parentNode.getClassification().getTitleCache();
206
                UuidAndTitleCache<TaxonNode> parent = new UuidAndTitleCache<>(uuid, id, titleCache);
207
                taxonTree.setContainerDataSource(new TaxonNodeContainer(parent));
208
            }
209
            else{
210
                taxonTree.setContainerDataSource(null);
211
            }
212
        }
213

    
214
        else if(property==distAreaBox){
215
            TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
216
            NamedAreaContainer container = new NamedAreaContainer(vocabulary);
217
            namedAreaList.setContainerDataSource(container);
218
        }
219
        updateButtons();
220
    }
221

    
222
    @Override
223
    protected boolean isValid() {
224
        return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
225
    }
226

    
227
    @Override
228
    public void buttonClick(ClickEvent event) {
229
        Object source = event.getSource();
230
        if(source==okButton){
231
            TaxonNode taxonNode = null;
232
            TermVocabulary<NamedArea> term = null;
233
            //TODO use field converter
234
            if(taxonTree.getValue()!=null){
235
                taxonNode = CdmSpringContextHelper.getTaxonNodeService().find((Integer)((RowId) taxonTree.getValue()).getId()[0]);
236
            }
237
            if(taxonNode==null){
238
                taxonNode = (TaxonNode) classificationBox.getValue();
239
            }
240
            term = (TermVocabulary<NamedArea>) distAreaBox.getValue();
241
            Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
242
            DistributionEditorUtil.openDistributionView(taxonNode, term, selectedAreas);
243
            window.close();
244
        }
245
        else if(source==cancelButton){
246
            window.close();
247
        }
248
    }
249

    
250
    @Override
251
    public void nodeExpand(ExpandEvent event) {
252
        UuidAndTitleCache<TaxonNode> parent = (UuidAndTitleCache<TaxonNode>) event.getItemId();
253
        Collection<UuidAndTitleCache<TaxonNode>> children = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(parent);
254
        taxonTree.setChildrenAllowed(parent, !children.isEmpty());
255
        for (UuidAndTitleCache<TaxonNode> child : children) {
256
            Item childItem = taxonTree.addItem(child);
257
            if(childItem!=null){
258
                taxonTree.setParent(child, parent);
259
                childItem.getItemProperty(TaxonNodeContainer.LABEL).setValue(child.getTitleCache());
260
            }
261
            Collection<UuidAndTitleCache<TaxonNode>> grandChildren = CdmSpringContextHelper.getTaxonNodeService().listChildNodesAsUuidAndTitleCache(child);
262
            taxonTree.setChildrenAllowed(child, !grandChildren.isEmpty());
263
        }
264
    }
265

    
266
}
(2-2/5)