Project

General

Profile

Revision 6d29f629

ID6d29f6295eb9a17c0378c164d6915edb9c518a69
Parent 7000c7bf
Child fbae3d00

Added by Andreas Kohlbecker about 1 year ago

ref #7366 TeamOrPersonField read only if user lacks editing permissions

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/component/common/TeamOrPersonField.java
8 8
*/
9 9
package eu.etaxonomy.cdm.vaadin.component.common;
10 10

  
11
import java.util.Arrays;
11 12
import java.util.EnumSet;
12 13
import java.util.List;
13 14

  
......
203 204
                titleField.bindTo(fieldGroup, "titleCache", "protectedTitleCache");
204 205
                nomenclaturalTitleField.bindTo(fieldGroup, "nomenclaturalTitle", "protectedNomenclaturalTitleCache");
205 206
                fieldGroup.setItemDataSource(new BeanItem<Team>((Team)newValue));
207
                boolean readonlyState = personsListEditor.isReadOnly();
206 208
                fieldGroup.bind(personsListEditor, "teamMembers"); // here personField is set readonly since setTeamMembers does not exist
207
                personsListEditor.setReadOnly(false); // fixing the readonly state
209
                personsListEditor.setReadOnly(readonlyState); // fixing the readonly state
208 210

  
209 211
                personsListEditor.registerParentFieldGroup(fieldGroup);
210 212

  
......
213 215
            }
214 216

  
215 217
        }
216

  
218
        adaptToUserPermissions(newValue);
217 219
        updateToolBarButtonStates();
218 220
    }
219 221

  
222

  
223
    private void adaptToUserPermissions(TeamOrPersonBase teamOrPerson) {
224

  
225
        if(teamOrPerson == null){
226
            return;
227
        }
228

  
229
        UserHelper userHelper = UserHelper.fromSession();
230
        boolean canEdit = userHelper.userHasPermission(teamOrPerson, CRUD.UPDATE);
231
        if(!canEdit){
232
            setContentReadOnly(true);
233
        }
234
    }
235

  
220 236
    private void checkUserPermissions(TeamOrPersonBase<?> newValue) {
221 237
        boolean userCanEdit = UserHelper.fromSession().userHasPermission(newValue, "DELETE", "UPDATE");
222 238
        setEnabled(userCanEdit);
......
346 362
    @Override
347 363
    public void setReadOnly(boolean readOnly) {
348 364
        super.setReadOnly(readOnly);
349
        setDeepReadOnly(readOnly, getContent());
365
        setContentReadOnly(readOnly);
366
    }
367

  
368
    /**
369
     * Set the nested team or person to read only but keep the state of the <code>TeamOrPersonField</code> untouched so
370
     * that the <code>removeButton</code>, <code>personButton</code> and <code>teamButton</code> can stay operational.
371
     *
372
     * @param readOnly
373
     */
374
    public void setContentReadOnly(boolean readOnly) {
375
        setDeepReadOnly(readOnly, getContent(), Arrays.asList(removeButton, personButton, teamButton));
376
        updateCaptionReadonlyNotice();
350 377
    }
351 378

  
352 379

  
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/TaxonNamePopupEditorView.java
10 10

  
11 11
import java.util.EnumSet;
12 12

  
13
import org.apache.commons.lang3.BooleanUtils;
14

  
15 13
import com.vaadin.ui.CheckBox;
16 14
import com.vaadin.ui.ListSelect;
17 15

  
18
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
19 16
import eu.etaxonomy.cdm.model.name.TaxonName;
20 17
import eu.etaxonomy.cdm.model.reference.Reference;
21 18
import eu.etaxonomy.cdm.vaadin.component.common.TeamOrPersonField;
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationWorkingsetPresenter.java
347 347
        popup.withDeleteButton(true);
348 348
        configureTaxonNameEditor(popup);
349 349
        popup.loadInEditor(event.getEntityUuid());
350
        if(event.getSourceComponent() != null){
351
            popup.setReadOnly(event.getSourceComponent().isReadOnly());
350
        if(event.getSourceComponent() != null && event.getSourceComponent().isReadOnly()){
351
            // avoid resetting readonly to false
352
            popup.setReadOnly(true);
352 353
        }
353 354

  
354 355
    }
src/main/java/eu/etaxonomy/vaadin/component/CompositeCustomField.java
21 21
import com.vaadin.ui.Component;
22 22
import com.vaadin.ui.CustomField;
23 23
import com.vaadin.ui.Field;
24
import com.vaadin.ui.Layout;
24
import com.vaadin.ui.HasComponents;
25 25

  
26 26
/**
27 27
 * TODO implement height methods for full component size support
......
35 35
@SuppressWarnings("serial")
36 36
public abstract class CompositeCustomField<T> extends CustomField<T> implements NestedFieldGroup {
37 37

  
38
    protected static final String READ_ONLY_CAPTION_SUFFIX = " (read only)";
39

  
38 40
    private List<Component> styledComponents = new ArrayList<>();
39 41

  
40 42
    private List<Component> sizedComponents = new ArrayList<>();
......
217 219
    /**
218 220
     * @param readOnly
219 221
     */
220
    protected void setDeepReadOnly(boolean readOnly, Component component) {
222
    protected void setDeepReadOnly(boolean readOnly, Component component, Collection<Component> ignore) {
223

  
224
        if(ignore != null && ignore.contains(component)){
225
            return;
226
        }
221 227

  
222 228
        component.setReadOnly(readOnly);
223 229
        if(Button.class.isAssignableFrom(component.getClass())){
224 230
            component.setEnabled(!readOnly);
225 231
        }
226
        if(Layout.class.isAssignableFrom(component.getClass())){
227
            for(Component nestedComponent : ((Layout)component)){
228
                setDeepReadOnly(readOnly, nestedComponent);
232
        if(HasComponents.class.isAssignableFrom(component.getClass())){
233
            for(Component nestedComponent : ((HasComponents)component)){
234
                setDeepReadOnly(readOnly, nestedComponent, ignore);
229 235
            }
230 236
        }
231 237
    }
......
236 242
                ( getValue() != null ? getValue() : "null");
237 243
    }
238 244

  
245
    protected void updateCaptionReadonlyNotice() {
246
        if(isReadOnly()){
247
            setCaption(getCaption() + READ_ONLY_CAPTION_SUFFIX);
248
        } else {
249
            setCaption(getCaption().replace(READ_ONLY_CAPTION_SUFFIX, ""));
250
        }
251
    }
252

  
239 253
}
src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesListSelect.java
625 625
    @Override
626 626
    public void setReadOnly(boolean readOnly) {
627 627
        super.setReadOnly(readOnly);
628
        setDeepReadOnly(readOnly, getContent());
628
        setDeepReadOnly(readOnly, getContent(), null);
629 629
        updateButtonStates();
630 630
    }
631 631

  
src/main/java/eu/etaxonomy/vaadin/component/ToOneRelatedEntityCombobox.java
199 199
    @Override
200 200
    public void setReadOnly(boolean readOnly) {
201 201
        super.setReadOnly(readOnly);
202
        setDeepReadOnly(readOnly, getContent());
202
        setDeepReadOnly(readOnly, getContent(), null);
203 203
    }
204 204

  
205 205

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)