Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.view.name;
10

    
11
import java.util.Collection;
12
import java.util.EnumSet;
13

    
14
import org.springframework.security.core.GrantedAuthority;
15
import org.vaadin.viritin.fields.ElementCollectionField;
16

    
17
import com.vaadin.data.validator.DoubleRangeValidator;
18
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
19
import com.vaadin.ui.Component;
20
import com.vaadin.ui.GridLayout;
21
import com.vaadin.ui.ListSelect;
22
import com.vaadin.ui.Panel;
23
import com.vaadin.ui.TextArea;
24
import com.vaadin.ui.TextField;
25

    
26
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
27
import eu.etaxonomy.cdm.vaadin.component.CollectionRowRepresentative;
28
import eu.etaxonomy.cdm.vaadin.component.PartialDateField;
29
import eu.etaxonomy.cdm.vaadin.component.common.GeoLocationField;
30
import eu.etaxonomy.cdm.vaadin.component.common.MinMaxTextField;
31
import eu.etaxonomy.cdm.vaadin.component.common.TeamOrPersonField;
32
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
33
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
34
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
35
import eu.etaxonomy.cdm.vaadin.view.PerEntityAuthorityGrantingEditor;
36
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
37

    
38
/**
39
 * @author a.kohlbecker
40
 * @since May 15, 2017
41
 *
42
 */
43
public class SpecimenTypeDesignationWorkingsetPopupEditor
44
    extends AbstractPopupEditor<SpecimenTypeDesignationWorkingSetDTO, SpecimenTypeDesignationWorkingsetEditorPresenter>
45
    implements SpecimenTypeDesignationWorkingsetPopupEditorView, AccessRestrictedView, PerEntityAuthorityGrantingEditor {
46
    /**
47
     * @param layout
48
     * @param dtoType
49
     */
50
    public SpecimenTypeDesignationWorkingsetPopupEditor() {
51
        super(new GridLayout(), SpecimenTypeDesignationWorkingSetDTO.class);
52
        GridLayout grid = (GridLayout) getFieldLayout();
53
        grid.setMargin(true);
54
        grid.setSpacing(true);
55
    }
56

    
57
    private static final long serialVersionUID = 5418275817834009509L;
58

    
59
    private ListSelect countrySelectField;
60

    
61
    private ElementCollectionField<SpecimenTypeDesignationDTO> typeDesignationsCollectionField;
62

    
63
    private EnumSet<CRUD> crud;
64

    
65
    /**
66
     * @return the countrySelectField
67
     */
68
    @Override
69
    public ListSelect getCountrySelectField() {
70
        return countrySelectField;
71
    }
72

    
73
    /**
74
     * {@inheritDoc}
75
     */
76
    @Override
77
    protected void initContent() {
78

    
79
        GridLayout grid = (GridLayout)getFieldLayout();
80
        grid.setSpacing(true);
81
        grid.setMargin(true);
82
        grid.setColumns(3);
83
        grid.setRows(9);
84

    
85
        //TODO typifyingAuthors
86

    
87
        // FieldUnit + GatheringEvent
88

    
89
        int row = 0;
90
        countrySelectField = new ListSelect("Country");
91
        addField(countrySelectField, "country", 1, row , 2, row);
92
        countrySelectField.setWidth("100%");
93
        countrySelectField.setItemCaptionMode(ItemCaptionMode.PROPERTY);
94
        countrySelectField.setItemCaptionPropertyId("label");
95
        countrySelectField.setRows(1);
96

    
97
        row++;
98
        TextArea localityField = new TextArea("Locality");
99
        addField(localityField, "locality", 0, row , 2, row);
100
        localityField.setWidth("100%");
101
        // NOTE: setRows and SetCold breaks he width setting,
102
        //        see https://github.com/vaadin/framework/issues/3617
103

    
104
        row++;
105
        GeoLocationField exactLocation = new GeoLocationField("Geo location");
106
        addField(exactLocation, "exactLocation", 0, row, 2, row);
107
        exactLocation.setWidth("100%");
108

    
109
        row++;
110
        MinMaxTextField absElevationMinMax = new MinMaxTextField("Altitude", "m");
111
        absElevationMinMax.setWidth("100%");
112
        absElevationMinMax.addSubComponentsStyleName(getDefaultComponentStyles());
113
        grid.addComponent(absElevationMinMax, 0, row, 2, row);
114

    
115
        bindField(absElevationMinMax.getMinField(), "absoluteElevation");
116
        bindField(absElevationMinMax.getMaxField(), "absoluteElevationMax");
117
        bindField(absElevationMinMax.getTextField(), "absoluteElevationText");
118

    
119
        row++;
120
        MinMaxTextField distanceToWaterSurfaceMinMax = new MinMaxTextField("Distance to water surface", "m");
121
        distanceToWaterSurfaceMinMax.setWidth("100%");
122
        distanceToWaterSurfaceMinMax.addSubComponentsStyleName(getDefaultComponentStyles());
123
        grid.addComponent(distanceToWaterSurfaceMinMax, 0, row, 2, row);
124

    
125
        bindField(distanceToWaterSurfaceMinMax.getMinField(), "distanceToWaterSurface");
126
        bindField(distanceToWaterSurfaceMinMax.getMaxField(), "distanceToWaterSurfaceMax");
127
        bindField(distanceToWaterSurfaceMinMax.getTextField(), "distanceToWaterSurfaceText");
128
        distanceToWaterSurfaceMinMax.getMaxField().addValidator(new DoubleRangeValidator("Negative values are not allowed here.", 0.0, Double.MAX_VALUE));
129
        distanceToWaterSurfaceMinMax.getMinField().addValidator(new DoubleRangeValidator("Negative values are not allowed here.", 0.0, Double.MAX_VALUE));
130

    
131
        row++;
132
        MinMaxTextField distanceToGroundMinMax = new MinMaxTextField("Distance to substrate", "m");
133
        distanceToGroundMinMax.setWidth("100%");
134
        distanceToGroundMinMax.addSubComponentsStyleName(getDefaultComponentStyles());
135
        grid.addComponent(distanceToGroundMinMax, 0, row, 2, row);
136

    
137
        bindField(distanceToGroundMinMax.getMinField(), "distanceToGround");
138
        bindField(distanceToGroundMinMax.getMaxField(), "distanceToGroundMax");
139
        bindField(distanceToGroundMinMax.getTextField(), "distanceToGroundText");
140

    
141
        row++;
142
        TeamOrPersonField collectorField = new TeamOrPersonField("Collector");
143
        addField(collectorField, "collector", 0, row, 2, row);
144

    
145
        row++;
146
        PartialDateField collectionDateField = new PartialDateField("Collection date");
147
        collectionDateField.setInputPrompt("dd.mm.yyyy");
148
        addField(collectionDateField, "gatheringDate", 0, row);
149
        TextField fieldNumberField = addTextField("Field number", "fieldNumber", 2, row);
150

    
151
        row++;
152

    
153
        // FIXME: can we use the Grid instead?
154
        typeDesignationsCollectionField = new ElementCollectionField<>(
155
                SpecimenTypeDesignationDTO.class,
156
                //getPresenter().specimenTypeDesignationDTOInstantiator(getBean());
157
                SpecimenTypeDesignationDTORow.class
158
                );
159
        typeDesignationsCollectionField.withCaption("Types");
160
        typeDesignationsCollectionField.getLayout().setSpacing(false);
161
        typeDesignationsCollectionField.getLayout().setColumns(3);
162

    
163
        typeDesignationsCollectionField.setPropertyHeader("accessionNumber", "Access. num.");
164
        typeDesignationsCollectionField.setPropertyHeader("preferredStableUri", "Stable URI");
165
        typeDesignationsCollectionField.setPropertyHeader("mediaSpecimenReference", "Image reference");
166
        typeDesignationsCollectionField.setPropertyHeader("mediaSpecimenReferenceDetail", "Reference detail");
167

    
168
        // typeDesignationsCollectionField.getLayout().setMargin(false);
169
        // typeDesignationsCollectionField.addStyleName("composite-field-wrapper");
170
        // addField(typeDesignationsCollectionField, "specimenTypeDesignationDTOs", 0, row, 2, row);
171

    
172
        Panel scrollPanel = new Panel(typeDesignationsCollectionField.getLayout());
173
        scrollPanel.setCaption("Types");
174
        scrollPanel.setWidth(800, Unit.PIXELS);
175

    
176
        bindField(typeDesignationsCollectionField, "specimenTypeDesignationDTOs");
177
        addComponent(scrollPanel, 0, row, 2, row);
178

    
179
     }
180

    
181

    
182
    /**
183
     * {@inheritDoc}
184
     */
185
    @Override
186
    public String getWindowCaption() {
187
        return "Specimen typedesignations editor";
188
    }
189

    
190
    /**
191
     * {@inheritDoc}
192
     */
193
    @Override
194
    public void focusFirst() {
195
        // none
196
    }
197

    
198
    /**
199
     * {@inheritDoc}
200
     */
201
    @Override
202
    protected String getDefaultComponentStyles() {
203
        return "tiny";
204
    }
205

    
206
    /**
207
     * {@inheritDoc}
208
     */
209
    @Override
210
    public boolean allowAnonymousAccess() {
211
        return false;
212
    }
213

    
214
    /**
215
     * {@inheritDoc}
216
     */
217
    @Override
218
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
219
        return null;
220
    }
221

    
222
    /**
223
     * {@inheritDoc}
224
     */
225
    @Override
226
    public boolean isResizable() {
227
        return true;
228
    }
229

    
230

    
231
    // ------- SpecimenTypeDesignationWorkingsetPopupEditorView methods ---- //
232
    @Override
233
    public ElementCollectionField<SpecimenTypeDesignationDTO> getTypeDesignationsCollectionField() {
234
        return typeDesignationsCollectionField;
235
    }
236

    
237
    @Override
238
    public void applyDefaultComponentStyle(Component ... components){
239
        for(int i = 0; i <components.length; i++){
240
            components[i].setStyleName(getDefaultComponentStyles());
241
        }
242
    }
243

    
244
    /**
245
     * {@inheritDoc}
246
     */
247
    @Override
248
    public void grantToCurrentUser(EnumSet<CRUD> crud) {
249
        getPresenter().setGrantsForCurrentUser(crud);
250

    
251
    }
252

    
253
    /**
254
     * {@inheritDoc}
255
     */
256
    @Override
257
    protected void afterItemDataSourceSet() {
258
        super.afterItemDataSourceSet();
259
        GridLayout gridLayout = this.typeDesignationsCollectionField.getLayout();
260
        for(int rowIndex = 1; rowIndex < gridLayout.getRows(); rowIndex++){ // first row is header
261
            Component item = gridLayout.getComponent(0, rowIndex);
262
            ((CollectionRowRepresentative)item).updateRowItemsEnabledStates();
263
        }
264
        updateAllowDelete();
265
    }
266

    
267
    /**
268
     * {@inheritDoc}
269
     */
270
    @Override
271
    public void updateAllowDelete(){
272
        // disable the delete button if there is only one typeDesignation
273
        // if this typeDesignation is deleted the fieldUnit would become orphan in the
274
        // TypeDesignationWorkingSet
275
        GridLayout gridLayout = this.typeDesignationsCollectionField.getLayout();
276
        if(gridLayout.getRows() == 3){ // first row is header, last row is next new item
277
            gridLayout.getComponent(gridLayout.getColumns() - 1, 1).setEnabled(false);
278
        }
279
    }
280

    
281
    /**
282
     * {@inheritDoc}
283
     */
284
    @Override
285
    public void setReadOnly(boolean readOnly) {
286
        super.setReadOnly(readOnly);
287
        getFieldLayout().iterator().forEachRemaining(c -> c.setReadOnly(readOnly));
288
        typeDesignationsCollectionField.getLayout().iterator().forEachRemaining(c -> c.setReadOnly(readOnly));
289

    
290
    }
291

    
292

    
293

    
294
}
(4-4/9)