1
|
/**
|
2
|
* Copyright (C) 2021 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.util.fields;
|
10
|
|
11
|
import java.awt.Point;
|
12
|
import java.util.List;
|
13
|
import java.util.Objects;
|
14
|
|
15
|
import org.vaadin.viritin.fields.AbstractElementCollection;
|
16
|
import org.vaadin.viritin.fields.ElementCollectionField;
|
17
|
import org.vaadin.viritin.fields.ElementCollectionTable;
|
18
|
|
19
|
import com.vaadin.ui.Component;
|
20
|
import com.vaadin.ui.GridLayout;
|
21
|
import com.vaadin.ui.Layout;
|
22
|
|
23
|
/**
|
24
|
* @author a.kohlbecker
|
25
|
* @since Feb 25, 2021
|
26
|
*/
|
27
|
public class ElementCollectionHelper<T extends AbstractElementCollection<?>> {
|
28
|
|
29
|
private T elementColection;
|
30
|
|
31
|
public ElementCollectionHelper(T elementColection) {
|
32
|
this.elementColection = elementColection;
|
33
|
}
|
34
|
|
35
|
private Layout getLayout() {
|
36
|
if(elementColection instanceof ElementCollectionField) {
|
37
|
return ((ElementCollectionField)elementColection).getLayout();
|
38
|
}
|
39
|
if(elementColection instanceof ElementCollectionTable) {
|
40
|
return ((ElementCollectionTable)elementColection).getLayout();
|
41
|
}
|
42
|
throw new RuntimeException("Unsuppoerted type " + elementColection.getClass());
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Provides the Property name of the field to which the <code>component</code> is mapped.
|
47
|
*/
|
48
|
public String properyFor(Component component) {
|
49
|
Point xy = locateComponent(component);
|
50
|
List<String> visProps = elementColection.getVisibleProperties();
|
51
|
return visProps.get(xy.x);
|
52
|
}
|
53
|
|
54
|
protected Point locateComponent(Component component) {
|
55
|
if(elementColection instanceof ElementCollectionField) {
|
56
|
GridLayout gridLayout = ((ElementCollectionField)elementColection).getLayout();
|
57
|
for(int x = 0; x < gridLayout.getColumns(); x++) {
|
58
|
for(int y = 0; y < gridLayout.getRows(); y++) {
|
59
|
Component gridComponent = gridLayout.getComponent(x, y);
|
60
|
if(Objects.equals(gridComponent, component)) {
|
61
|
return new Point(x, y);
|
62
|
}
|
63
|
}
|
64
|
}
|
65
|
return null;
|
66
|
} else {
|
67
|
throw new RuntimeException("Unsuppoerted type " + elementColection.getClass());
|
68
|
}
|
69
|
}
|
70
|
|
71
|
}
|