/*
 * Copyright (c) 2008-2018 Haulmont.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.haulmont.cuba.gui.actions.list;

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.Security;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.ListComponent;
import com.haulmont.cuba.gui.components.actions.ListAction;
import com.haulmont.cuba.security.entity.ConstraintOperationType;

import javax.inject.Inject;

/**
 * Standard action that changes enabled property depending on selection of a bound {@link ListComponent}.
 * <br>
 * You can use fluent API to create instances of SecuredListAction and assign handlers to them:
 * <pre>{@code
 *     Action action = new SecuredListAction("moveToTrash")
 *             .withCaption("Move to trash")
 *             .withIcon("icons/trash.png")
 *             .withHandler(event -> {
 *                 // action logic here
 *             });
 *     docsTable.addAction(action);
 * }</pre>
 */
public abstract class SecuredListAction extends ListAction implements Action.HasSecurityConstraint {

    protected ConstraintOperationType constraintOperationType;
    protected String constraintCode;

    protected Security security;

    protected SecuredListAction(String id) {
        super(id);
    }

    @Inject
    protected void setSecurity(Security security) {
        this.security = security;
    }

    @Override
    protected boolean isApplicable() {
        return target != null
                && target.getSingleSelected() != null
                && super.isApplicable();
    }

    @Override
    protected boolean isPermitted() {
        if (target == null) {
            return false;
        }

        Entity singleSelected = target.getSingleSelected();
        if (singleSelected == null) {
            return false;
        }

        if (constraintOperationType != null) {
            boolean isPermitted;
            if (constraintCode != null) {
                isPermitted = security.isPermitted(singleSelected, constraintCode);
            } else {
                isPermitted = security.isPermitted(singleSelected, constraintOperationType);
            }
            if (!isPermitted) {
                return false;
            }
        }

        return super.isPermitted();
    }

    @Override
    public void setConstraintOperationType(ConstraintOperationType constraintOperationType) {
        this.constraintOperationType = constraintOperationType;
    }

    @Override
    public ConstraintOperationType getConstraintOperationType() {
        return constraintOperationType;
    }

    @Override
    public void setConstraintCode(String constraintCode) {
        this.constraintCode = constraintCode;
    }

    @Override
    public String getConstraintCode() {
        return constraintCode;
    }
}
@@ -1003,18 +1003,9 @@
 %0A * 
-Standard a
+A
 ctio
@@ -1023,15 +1023,27 @@
 ges 
+its %7B@code 
 enabled
+%7D
  pro
@@ -1065,464 +1065,57 @@
  on 
+a 
 selec
-tion of a bound %7B@link ListComponent%7D.%0A * %3Cbr%3E%0A * You can use fluent API to crea
 te
+d
  i
-nstances of SecuredListAction and assign handlers to them:%0A * %3Cpre%3E%7B@code%0A *     Action action = new SecuredListAction(%22moveToTrash%22)%0A *             .withCaption(%22Move to trash%22)%0A *             .withIcon(%22icons/trash.png%22)%0A *             .withHandler(event -%3E %7B%0A *                 // action logic here%0A *             %7D);%0A *     docsTable.addAction(action);%0A * %7D%3C/pre%3E
+tem in a bound %7B@link ListComponent%7D.
 %0A */

Fix Javadoc