Project

General

Profile

Download (12.3 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.context.annotation.Scope;
15
import org.springframework.security.core.GrantedAuthority;
16
import org.vaadin.viritin.fields.ElementCollectionField;
17

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

    
27
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
28
import eu.etaxonomy.cdm.vaadin.component.CollectionRowRepresentative;
29
import eu.etaxonomy.cdm.vaadin.component.PartialDateField;
30
import eu.etaxonomy.cdm.vaadin.component.common.GeoLocationField;
31
import eu.etaxonomy.cdm.vaadin.component.common.MinMaxTextField;
32
import eu.etaxonomy.cdm.vaadin.component.common.TeamOrPersonField;
33
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
34
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
35
import eu.etaxonomy.cdm.vaadin.permission.AccessRestrictedView;
36
import eu.etaxonomy.cdm.vaadin.util.TeamOrPersonBaseCaptionGenerator;
37
import eu.etaxonomy.cdm.vaadin.util.converter.DoubleConverter;
38
import eu.etaxonomy.cdm.vaadin.util.converter.IntegerConverter;
39
import eu.etaxonomy.cdm.vaadin.view.PerEntityAuthorityGrantingEditor;
40
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
41

    
42
/**
43
 * @author a.kohlbecker
44
 * @since May 15, 2017
45
 *
46
 */
47
@SpringComponent
48
@Scope("prototype")
49
public class SpecimenTypeDesignationWorkingsetPopupEditor
50
    extends AbstractPopupEditor<SpecimenTypeDesignationWorkingSetDTO, SpecimenTypeDesignationWorkingsetEditorPresenter>
51
    implements SpecimenTypeDesignationWorkingsetPopupEditorView, AccessRestrictedView, PerEntityAuthorityGrantingEditor {
52
    /**
53
     *
54
     */
55
    private static final String CAN_T_SAVE_AS_LONG_AS_TYPE_DESIGNATIONS_ARE_MISSING = "Can't save as long as type designations are missing.";
56

    
57
    /**
58
     * @param layout
59
     * @param dtoType
60
     */
61
    public SpecimenTypeDesignationWorkingsetPopupEditor() {
62
        super(new GridLayout(), SpecimenTypeDesignationWorkingSetDTO.class);
63
        GridLayout grid = (GridLayout) getFieldLayout();
64
        grid.setMargin(true);
65
        grid.setSpacing(true);
66
    }
67

    
68
    private static final long serialVersionUID = 5418275817834009509L;
69

    
70
    private ListSelect countrySelectField;
71

    
72
    private ElementCollectionField<SpecimenTypeDesignationDTO> typeDesignationsCollectionField;
73

    
74
    private EnumSet<CRUD> crud;
75

    
76
    private TeamOrPersonField collectorField;
77

    
78
    /**
79
     * @return the countrySelectField
80
     */
81
    @Override
82
    public ListSelect getCountrySelectField() {
83
        return countrySelectField;
84
    }
85

    
86
    /**
87
     * {@inheritDoc}
88
     */
89
    @Override
90
    protected void initContent() {
91

    
92
        GridLayout grid = (GridLayout)getFieldLayout();
93
        grid.setSpacing(true);
94
        grid.setMargin(true);
95
        grid.setColumns(3);
96
        grid.setRows(9);
97

    
98
        //TODO typifyingAuthors
99

    
100
        // FieldUnit + GatheringEvent
101

    
102
        int row = 0;
103
        countrySelectField = new ListSelect("Country");
104
        addField(countrySelectField, "country", 1, row , 2, row);
105
        countrySelectField.setWidth("100%");
106
        countrySelectField.setItemCaptionMode(ItemCaptionMode.PROPERTY);
107
        countrySelectField.setItemCaptionPropertyId("label");
108
        countrySelectField.setRows(1);
109

    
110
        row++;
111
        TextArea localityField = new TextArea("Locality");
112
        localityField.setNullRepresentation("");
113
        addField(localityField, "locality", 0, row , 2, row);
114
        localityField.setWidth("100%");
115
        // NOTE: setRows and SetCold breaks he width setting,
116
        //        see https://github.com/vaadin/framework/issues/3617
117

    
118
        row++;
119
        GeoLocationField exactLocation = new GeoLocationField("Geo location");
120
        addField(exactLocation, "exactLocation", 0, row, 2, row);
121
        exactLocation.setWidth("100%");
122

    
123
        row++;
124
        MinMaxTextField absElevationMinMax = new MinMaxTextField("Altitude", "m");
125
        absElevationMinMax.setWidth("100%");
126
        absElevationMinMax.addSubComponentsStyleName(getDefaultComponentStyles());
127
        grid.addComponent(absElevationMinMax, 0, row, 2, row);
128

    
129
        bindField(absElevationMinMax.getMinField(), "absoluteElevation");
130
        bindField(absElevationMinMax.getMaxField(), "absoluteElevationMax");
131
        bindField(absElevationMinMax.getTextField(), "absoluteElevationText");
132

    
133
        absElevationMinMax.getMaxField().setConverter(new IntegerConverter());
134
        absElevationMinMax.getMinField().setConverter(new IntegerConverter());
135

    
136
        row++;
137
        MinMaxTextField distanceToWaterSurfaceMinMax = new MinMaxTextField("Distance to water surface", "m");
138
        distanceToWaterSurfaceMinMax.setWidth("100%");
139
        distanceToWaterSurfaceMinMax.addSubComponentsStyleName(getDefaultComponentStyles());
140
        grid.addComponent(distanceToWaterSurfaceMinMax, 0, row, 2, row);
141

    
142
        bindField(distanceToWaterSurfaceMinMax.getMinField(), "distanceToWaterSurface");
143
        bindField(distanceToWaterSurfaceMinMax.getMaxField(), "distanceToWaterSurfaceMax");
144
        bindField(distanceToWaterSurfaceMinMax.getTextField(), "distanceToWaterSurfaceText");
145
        distanceToWaterSurfaceMinMax.getMaxField().setConverter(new DoubleConverter());
146
        distanceToWaterSurfaceMinMax.getMinField().setConverter(new DoubleConverter());
147
        distanceToWaterSurfaceMinMax.getMaxField().addValidator(new DoubleRangeValidator("Negative values are not allowed here.", 0.0, Double.MAX_VALUE));
148
        distanceToWaterSurfaceMinMax.getMinField().addValidator(new DoubleRangeValidator("Negative values are not allowed here.", 0.0, Double.MAX_VALUE));
149

    
150
        row++;
151
        MinMaxTextField distanceToGroundMinMax = new MinMaxTextField("Distance to substrate", "m");
152
        distanceToGroundMinMax.setWidth("100%");
153
        distanceToGroundMinMax.addSubComponentsStyleName(getDefaultComponentStyles());
154
        grid.addComponent(distanceToGroundMinMax, 0, row, 2, row);
155

    
156
        bindField(distanceToGroundMinMax.getMinField(), "distanceToGround");
157
        bindField(distanceToGroundMinMax.getMaxField(), "distanceToGroundMax");
158
        bindField(distanceToGroundMinMax.getTextField(), "distanceToGroundText");
159
        distanceToGroundMinMax.getMaxField().setConverter(new DoubleConverter());
160
        distanceToGroundMinMax.getMinField().setConverter(new DoubleConverter());
161

    
162
        row++;
163
        collectorField = new TeamOrPersonField("Collector", TeamOrPersonBaseCaptionGenerator.CacheType.NOMENCLATURAL_TITLE);
164
        addField(collectorField, "collector", 0, row, 2, row);
165

    
166
        row++;
167
        PartialDateField collectionDateField = new PartialDateField("Collection date");
168
        collectionDateField.setInputPrompt("dd.mm.yyyy");
169
        addField(collectionDateField, "gatheringDate", 0, row, 1, row);
170
        addTextField("Field number", "fieldNumber", 2, row);
171

    
172

    
173
        row++;
174

    
175
        // FIXME: can we use the Grid instead?
176
        typeDesignationsCollectionField = new ElementCollectionField<>(
177
                SpecimenTypeDesignationDTO.class,
178
                SpecimenTypeDesignationDTORow.class
179
                );
180
        typeDesignationsCollectionField.withCaption("Types");
181
        typeDesignationsCollectionField.getLayout().setSpacing(false);
182
        typeDesignationsCollectionField.getLayout().setColumns(3);
183

    
184
        typeDesignationsCollectionField.setVisibleProperties(SpecimenTypeDesignationDTORow.visibleFields());
185

    
186
        typeDesignationsCollectionField.setPropertyHeader("accessionNumber", "Access. num.");
187
        typeDesignationsCollectionField.setPropertyHeader("preferredStableUri", "Stable URI");
188
        typeDesignationsCollectionField.setPropertyHeader("mediaSpecimenReference", "Image reference");
189
        typeDesignationsCollectionField.setPropertyHeader("mediaSpecimenReferenceDetail", "Reference detail");
190
        typeDesignationsCollectionField.addElementAddedListener( e -> updateAllowSave());
191
        typeDesignationsCollectionField.addElementRemovedListener( e -> updateAllowSave());
192

    
193
        // typeDesignationsCollectionField.getLayout().setMargin(false);
194
        // typeDesignationsCollectionField.addStyleName("composite-field-wrapper");
195
        // addField(typeDesignationsCollectionField, "specimenTypeDesignationDTOs", 0, row, 2, row);
196

    
197
        Panel scrollPanel = new Panel(typeDesignationsCollectionField.getLayout());
198
        scrollPanel.setCaption("Types");
199
        scrollPanel.setWidth(800, Unit.PIXELS);
200

    
201
        bindField(typeDesignationsCollectionField, "specimenTypeDesignationDTOs");
202
        addComponent(scrollPanel, 0, row, 2, row);
203

    
204
     }
205

    
206

    
207
    /**
208
     * {@inheritDoc}
209
     */
210
    @Override
211
    public String getWindowCaption() {
212
        return "Specimen typedesignations editor";
213
    }
214

    
215
    /**
216
     * {@inheritDoc}
217
     */
218
    @Override
219
    public void focusFirst() {
220
        // none
221
    }
222

    
223
    /**
224
     * {@inheritDoc}
225
     */
226
    @Override
227
    protected String getDefaultComponentStyles() {
228
        return "tiny";
229
    }
230

    
231
    /**
232
     * {@inheritDoc}
233
     */
234
    @Override
235
    public boolean allowAnonymousAccess() {
236
        return false;
237
    }
238

    
239
    /**
240
     * {@inheritDoc}
241
     */
242
    @Override
243
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
244
        return null;
245
    }
246

    
247
    /**
248
     * {@inheritDoc}
249
     */
250
    @Override
251
    public boolean isResizable() {
252
        return true;
253
    }
254

    
255

    
256
    // ------- SpecimenTypeDesignationWorkingsetPopupEditorView methods ---- //
257
    @Override
258
    public ElementCollectionField<SpecimenTypeDesignationDTO> getTypeDesignationsCollectionField() {
259
        return typeDesignationsCollectionField;
260
    }
261

    
262
    @Override
263
    public void applyDefaultComponentStyle(Component ... components){
264
        for(int i = 0; i <components.length; i++){
265
            components[i].setStyleName(getDefaultComponentStyles());
266
        }
267
    }
268

    
269
    /**
270
     * {@inheritDoc}
271
     */
272
    @Override
273
    public void grantToCurrentUser(EnumSet<CRUD> crud) {
274
        getPresenter().setGrantsForCurrentUser(crud);
275

    
276
    }
277

    
278
    /**
279
     * {@inheritDoc}
280
     */
281
    @Override
282
    protected void afterItemDataSourceSet() {
283
        super.afterItemDataSourceSet();
284
        GridLayout gridLayout = this.typeDesignationsCollectionField.getLayout();
285
        for(int rowIndex = 1; rowIndex < gridLayout.getRows(); rowIndex++){ // first row is header
286
            Component item = gridLayout.getComponent(SpecimenTypeDesignationDTORow.rowListSelectColumn(), rowIndex);
287
            ((CollectionRowRepresentative)item).updateRowItemsEnabledStates();
288
        }
289
        updateAllowDelete();
290
        updateAllowSave();
291
    }
292

    
293
    /**
294
     * {@inheritDoc}
295
     */
296
    @Override
297
    public void updateAllowDelete(){
298
        // disable the delete button if there is only one typeDesignation
299
        // if this typeDesignation is deleted the fieldUnit would become orphan in the
300
        // TypeDesignationWorkingSet
301
        GridLayout gridLayout = this.typeDesignationsCollectionField.getLayout();
302
        if(gridLayout.getRows() == 3){ // first row is header, last row is next new item
303
            gridLayout.getComponent(gridLayout.getColumns() - 1, 1).setEnabled(false);
304
        }
305
    }
306

    
307
    public void updateAllowSave(){
308
        boolean hasTypeDesignations = getBean().getSpecimenTypeDesignationDTOs().size() > 0;
309
        setSaveButtonEnabled(hasTypeDesignations);
310
        if(!hasTypeDesignations){
311
            addStatusMessage(CAN_T_SAVE_AS_LONG_AS_TYPE_DESIGNATIONS_ARE_MISSING);
312
        } else {
313
            removeStatusMessage(CAN_T_SAVE_AS_LONG_AS_TYPE_DESIGNATIONS_ARE_MISSING);
314
        }
315

    
316
    }
317

    
318
    /**
319
     * {@inheritDoc}
320
     */
321
    @Override
322
    public void setReadOnly(boolean readOnly) {
323
        super.setReadOnly(readOnly);
324
        getFieldLayout().iterator().forEachRemaining(c -> c.setReadOnly(readOnly));
325
        typeDesignationsCollectionField.getLayout().iterator().forEachRemaining(c -> c.setReadOnly(readOnly));
326

    
327
    }
328

    
329
    /**
330
     * @return the collectorField
331
     */
332
    @Override
333
    public TeamOrPersonField getCollectorField() {
334
        return collectorField;
335
    }
336

    
337

    
338

    
339
}
(7-7/13)