Project

General

Profile

Download (8.76 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.Set;
14

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

    
31
import eu.etaxonomy.cdm.model.common.TermVocabulary;
32
import eu.etaxonomy.cdm.model.location.NamedArea;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34
import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
35
import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
36
import eu.etaxonomy.cdm.vaadin.container.TaxonTreeContainer;
37
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
38
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
39

    
40
/**
41
 *
42
 * @author pplitzner
43
 *
44
 */
45
public class DistributionSettingsConfigWindow extends AbstractSettingsDialogWindow implements ValueChangeListener, ClickListener{
46

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

    
64
    protected void init() {
65
    	//init classification and taxon selection
66
        TaxonNode chosenTaxonNode = presenter.getChosenTaxonNode();
67

    
68
        classificationBox.setContainerDataSource(new TaxonNodeContainer(null));
69
        Object classificationSelection = null;
70
		if(classificationBox.getItemIds().size()==1){
71
			//only one classification exists
72
		    classificationSelection = classificationBox.getItemIds().iterator().next();
73
		}
74
		else if(chosenTaxonNode!=null){
75
			//get the classification from the selected taxon node
76
			classificationSelection = chosenTaxonNode.getClassification().getRootNode();
77
		}
78
        if(classificationSelection!=null){
79
        	classificationBox.setValue(classificationSelection);
80
        	try {
81
                taxonTree.setContainerDataSource(new TaxonTreeContainer((TaxonNode) classificationSelection));
82
                taxonTree.setVisibleColumns("Name");
83
            } catch (SQLException e) {
84
    			DistributionEditorUtil.showSqlError(e);
85
            }
86
        	if(chosenTaxonNode!=null){
87
    			taxonTree.select(new RowId(chosenTaxonNode.getId()));
88
        	}
89
        }
90
        classificationBox.addValueChangeListener(this);
91
        
92
        TermVocabulary<NamedArea> chosenArea = presenter.getChosenArea();
93
        distAreaBox.setContainerDataSource(presenter.getDistributionContainer());
94
        distAreaBox.setValue(chosenArea);
95
        distAreaBox.addValueChangeListener(this);
96

    
97
        if(chosenArea!=null){
98
        	NamedAreaContainer container = new NamedAreaContainer(chosenArea);
99
        	namedAreaList.setContainerDataSource(container);
100
        }
101
        Object selectedAreas = VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
102
        namedAreaList.setValue(selectedAreas);
103

    
104
        okButton.addClickListener(this);
105
        cancelButton.addClickListener(this);
106
        updateButtons();
107
    }
108

    
109
    protected AbstractLayout buildMainLayout() {
110

    
111
        mainLayout = new VerticalLayout();
112
        mainLayout.setSizeFull();
113

    
114
    	HorizontalLayout leftAndRightContainer = new HorizontalLayout();
115
        leftAndRightContainer.setImmediate(false);
116
        leftAndRightContainer.setSizeFull();
117
        leftAndRightContainer.setMargin(true);
118
        leftAndRightContainer.setSpacing(true);
119

    
120
        VerticalLayout leftContainer = new VerticalLayout();
121
        leftContainer.setImmediate(false);
122
        leftContainer.setSpacing(true);
123
        leftContainer.setSizeFull();
124

    
125
        VerticalLayout rightContainer = new VerticalLayout();
126
        rightContainer.setImmediate(false);
127
        rightContainer.setSpacing(true);
128
        rightContainer.setSizeFull();
129

    
130
        //classification and term
131
        classificationBox = new ComboBox("Classification");
132
        classificationBox.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
133
        classificationBox.setInputPrompt("Please select a classification...");
134
        classificationBox.setImmediate(true);
135
        classificationBox.setNewItemsAllowed(false);
136
        classificationBox.setNullSelectionAllowed(false);
137
        classificationBox.setSizeFull();
138
        classificationBox.setWidth("100%");
139

    
140
        //distribution area box
141
        distAreaBox = new ComboBox("Distribution Area:");
142
        distAreaBox.setInputPrompt("Please select a distribution area...");
143
        distAreaBox.setImmediate(true);
144
        distAreaBox.setNullSelectionAllowed(false);
145
        distAreaBox.setNewItemsAllowed(false);
146
        distAreaBox.setSizeFull();
147
        distAreaBox.setWidth("100%");
148

    
149
        // named areas
150
        namedAreaList = new ListSelect();
151
        namedAreaList.setCaption("Areas");
152
        namedAreaList.setSizeFull();
153
        namedAreaList.setMultiSelect(true);
154

    
155
        //taxonomy
156
        taxonTree = new TreeTable("Taxonomy");
157
        taxonTree.setSelectable(true);
158
        taxonTree.setSizeFull();
159
        taxonTree.setImmediate(true);
160
        taxonTree.setCacheRate(20);
161
        taxonTree.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
162

    
163
        leftContainer.addComponent(distAreaBox);
164
        leftContainer.addComponent(namedAreaList);
165
        leftContainer.setExpandRatio(distAreaBox, 0.1f);
166
        leftContainer.setExpandRatio(namedAreaList, 0.9f);
167
        leftContainer.setSizeFull();
168

    
169
        rightContainer.addComponent(classificationBox);
170
        rightContainer.addComponent(taxonTree);
171
        rightContainer.setExpandRatio(classificationBox, 0.1f);
172
        rightContainer.setExpandRatio(taxonTree, 0.9f);
173

    
174
        leftAndRightContainer.addComponent(leftContainer);
175
        leftAndRightContainer.addComponent(rightContainer);
176

    
177
        //button toolbar
178
        HorizontalLayout buttonToolBar = createOkCancelButtons();
179

    
180
        mainLayout.addComponent(leftAndRightContainer);
181
        mainLayout.addComponent(buttonToolBar);
182
        mainLayout.setExpandRatio(leftAndRightContainer, 0.9f);
183
        mainLayout.setExpandRatio(buttonToolBar, 0.1f);
184
        mainLayout.setComponentAlignment(buttonToolBar, Alignment.BOTTOM_RIGHT);
185

    
186
        return leftAndRightContainer;
187
    }
188

    
189
	@Override
190
	public void valueChange(ValueChangeEvent event) {
191
		Property property = event.getProperty();
192
		if(property==classificationBox){
193
			TaxonNode parentNode = (TaxonNode) event.getProperty().getValue();
194
			if(parentNode!=null){
195
			    try {
196
                    taxonTree.setContainerDataSource(new TaxonTreeContainer(parentNode));
197
                    taxonTree.setVisibleColumns("Name");
198
                } catch (SQLException e) {
199
        			DistributionEditorUtil.showSqlError(e);
200
                }
201
			}
202
			else{
203
				taxonTree.setContainerDataSource(null);
204
			}
205
		}
206

    
207
		else if(property==distAreaBox){
208
			TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
209
			NamedAreaContainer container = new NamedAreaContainer(vocabulary);
210
			namedAreaList.setContainerDataSource(container);
211
		}
212
		updateButtons();
213
	}
214
	
215
	@Override
216
	protected boolean isValid() {
217
		return classificationBox.getValue()!=null && distAreaBox.getValue()!=null;
218
	}
219

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

    
243
}
(2-2/5)