Project

General

Profile

Download (8.91 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.Notification;
28
import com.vaadin.ui.Table.ColumnHeaderMode;
29
import com.vaadin.ui.TreeTable;
30
import com.vaadin.ui.VerticalLayout;
31

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

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

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

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

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

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

    
105
        okButton.addClickListener(this);
106
        cancelButton.addClickListener(this);
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
	}
213

    
214
	@Override
215
	public void buttonClick(ClickEvent event) {
216
		Object source = event.getSource();
217
		if(source==okButton){
218
			TaxonNode taxonNode = null;
219
			TermVocabulary<NamedArea> term = null;
220
			//TODO use field converter
221
			if(taxonTree.getValue()!=null){
222
			    taxonNode = CdmSpringContextHelper.getTaxonNodeService().find((Integer)((RowId) taxonTree.getValue()).getId()[0]);
223
			}
224
			if(taxonNode==null){
225
				taxonNode = (TaxonNode) classificationBox.getValue();
226
			}
227
			term = (TermVocabulary<NamedArea>) distAreaBox.getValue();
228
			Set<NamedArea> selectedAreas = (Set<NamedArea>) namedAreaList.getValue();
229
			if(taxonNode==null){
230
				Notification.show("Please choose a classification and/or taxon", Notification.Type.HUMANIZED_MESSAGE);
231
				return;
232
			}
233
			if(term==null){
234
				Notification.show("Please choose a distribution area", Notification.Type.HUMANIZED_MESSAGE);
235
				return;
236
			}
237
			DistributionEditorUtil.openDistributionView(taxonNode, term, selectedAreas);
238
			window.close();
239
		}
240
		else if(source==cancelButton){
241
			window.close();
242
		}
243
	}
244

    
245
}
(2-2/5)