1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.security.app.dialog.field;
35
36 import static com.vaadin.server.Sizeable.Unit.*;
37
38 import java.util.Map;
39
40 import com.vaadin.ui.Button;
41 import com.vaadin.ui.Component;
42 import com.vaadin.v7.data.Property;
43 import com.vaadin.v7.data.fieldgroup.BeanFieldGroup;
44 import com.vaadin.v7.data.fieldgroup.FieldGroup;
45 import com.vaadin.v7.data.util.BeanItem;
46 import com.vaadin.v7.ui.CustomField;
47 import com.vaadin.v7.ui.Field;
48 import com.vaadin.v7.ui.HorizontalLayout;
49 import com.vaadin.v7.ui.NativeSelect;
50 import com.vaadin.v7.ui.TextField;
51
52
53
54
55
56
57 public class AccessControlField extends CustomField<AccessControlList.Entry> {
58
59 private NativeSelect permissionSelect = new NativeSelect();
60 private NativeSelect accessTypeSelect;
61 private TextField path = new TextField();
62 private FieldGroup fieldGroup = new BeanFieldGroup<>(AccessControlList.Entry.class);
63
64 private Map<Long, String> permissions;
65 private Map<Long, String> accessTypes;
66 private String chooseButtonCaption = "Choose";
67 private PathChooserHandler pathChooserHandler;
68 private ValueChangeListener valueChangeListener;
69
70
71
72
73
74
75 public AccessControlField(Map<Long, String> permissions) {
76 this(permissions, null);
77 }
78
79
80
81
82
83
84
85 public AccessControlField(Map<Long, String> permissions, Map<Long, String> accessTypes) {
86 if (permissions == null || permissions.isEmpty()) {
87 throw new IllegalArgumentException("AccessControlField requires a non-empty collection of permission entries.");
88 }
89 this.permissions = permissions;
90 this.accessTypes = accessTypes;
91 }
92
93 @Override
94 protected Component initContent() {
95
96
97 permissionSelect.setNullSelectionAllowed(false);
98 permissionSelect.setInvalidAllowed(false);
99 permissionSelect.setNewItemsAllowed(false);
100 for (Map.Entry<Long, String> entry : permissions.entrySet()) {
101 permissionSelect.addItem(entry.getKey());
102 permissionSelect.setItemCaption(entry.getKey(), entry.getValue());
103 }
104
105 if (accessTypes != null && !accessTypes.isEmpty()) {
106 accessTypeSelect = new NativeSelect();
107 accessTypeSelect.setNullSelectionAllowed(false);
108 accessTypeSelect.setInvalidAllowed(false);
109 accessTypeSelect.setNewItemsAllowed(false);
110 accessTypeSelect.setWidth(150, PIXELS);
111 for (Map.Entry<Long, String> entry : accessTypes.entrySet()) {
112 accessTypeSelect.addItem(entry.getKey());
113 accessTypeSelect.setItemCaption(entry.getKey(), entry.getValue());
114 }
115 }
116
117 path.setWidth(100, PERCENTAGE);
118
119
120 HorizontalLayout ruleLayout = new HorizontalLayout();
121 ruleLayout.setSpacing(true);
122 ruleLayout.setWidth("100%");
123
124 fieldGroup.bind(permissionSelect, "permissions");
125 ruleLayout.addComponent(permissionSelect);
126
127 if (accessTypeSelect != null) {
128 fieldGroup.bind(accessTypeSelect, "accessType");
129 ruleLayout.addComponent(accessTypeSelect);
130 }
131
132 fieldGroup.bind(path, "path");
133 ruleLayout.addComponent(path);
134 ruleLayout.setExpandRatio(path, 1.0f);
135
136 if (pathChooserHandler != null) {
137 Button chooseButton = new Button(chooseButtonCaption, (Button.ClickListener) event -> {
138 pathChooserHandler.openChooser(path.getPropertyDataSource());
139 event.getButton().setEnabled(true);
140 });
141 chooseButton.setDisableOnClick(true);
142 ruleLayout.addComponent(chooseButton);
143 }
144
145
146 fieldGroup.setBuffered(false);
147
148 return ruleLayout;
149 }
150
151 @Override
152 public Class<? extends AccessControlList.Entry> getType() {
153 return AccessControlList.Entry.class;
154 }
155
156 @Override
157 protected void setInternalValue(AccessControlList.Entry newValue) {
158 super.setInternalValue(newValue);
159 fieldGroup.setItemDataSource(new BeanItem<>(newValue));
160 }
161
162
163
164
165
166 @Override
167 public void setValidationVisible(boolean validateAutomatically) {
168 super.setValidationVisible(validateAutomatically);
169
170 if (validateAutomatically && valueChangeListener == null) {
171 valueChangeListener = (ValueChangeListener) event -> fireValueChange(false);
172 for (Field<?> field : fieldGroup.getFields()) {
173 field.addValueChangeListener(valueChangeListener);
174 }
175 } else if (!validateAutomatically && valueChangeListener != null) {
176 for (Field<?> field : fieldGroup.getFields()) {
177 field.removeValueChangeListener(valueChangeListener);
178 }
179 valueChangeListener = null;
180 }
181 }
182
183 public String getChooseButtonCaption() {
184 return chooseButtonCaption;
185 }
186
187 public void setChooseButtonCaption(String chooseButtonCaption) {
188 this.chooseButtonCaption = chooseButtonCaption;
189 }
190
191 public PathChooserHandler getPathChooserHandler() {
192 return pathChooserHandler;
193 }
194
195 public void setPathChooserHandler(PathChooserHandler pathChooserHandler) {
196 this.pathChooserHandler = pathChooserHandler;
197 }
198
199
200
201
202 public interface PathChooserHandler {
203
204
205
206
207
208 void openChooser(Property<String> pathProperty);
209 }
210 }