com.haulmont.cuba.web.gui.components.WebButton.java Source code

Java tutorial

Introduction

Here is the source code for com.haulmont.cuba.web.gui.components.WebButton.java

Source

/*
 * Copyright (c) 2008-2016 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.web.gui.components;

import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Button;
import com.haulmont.cuba.web.toolkit.ui.CubaButton;
import org.apache.commons.lang.StringUtils;

import java.beans.PropertyChangeListener;

public class WebButton extends WebAbstractComponent<CubaButton> implements Button {

    protected Action action;
    protected PropertyChangeListener actionPropertyChangeListener;

    public WebButton() {
        component = new CubaButton();
        component.addClickListener((com.vaadin.ui.Button.ClickListener) event -> {
            beforeActionPerformed();
            if (action != null) {
                performAction(action);
            }
            afterActionPerformed();
        });
        component.setDescription(null);
    }

    protected void performAction(Action action) {
        action.actionPerform(this);
    }

    // override in descendants if needed
    protected void beforeActionPerformed() {
    }

    // override in descendants if needed
    protected void afterActionPerformed() {
    }

    @Override
    public Action getAction() {
        return action;
    }

    @Override
    public void setAction(Action action) {
        if (action != this.action) {
            if (this.action != null) {
                this.action.removeOwner(this);
                this.action.removePropertyChangeListener(actionPropertyChangeListener);
            }

            this.action = action;

            if (action != null) {
                String caption = action.getCaption();
                if (caption != null && component.getCaption() == null) {
                    component.setCaption(caption);
                }

                String description = action.getDescription();
                if (description == null && action.getShortcutCombination() != null) {
                    description = action.getShortcutCombination().format();
                }
                if (description != null && component.getDescription() == null) {
                    component.setDescription(description);
                }

                component.setEnabled(action.isEnabled());
                component.setVisible(action.isVisible());

                if (action.getIcon() != null && getIcon() == null) {
                    setIcon(action.getIcon());
                }

                action.addOwner(this);

                actionPropertyChangeListener = evt -> {
                    if (Action.PROP_ICON.equals(evt.getPropertyName())) {
                        setIcon(WebButton.this.action.getIcon());
                    } else if (Action.PROP_CAPTION.equals(evt.getPropertyName())) {
                        setCaption(WebButton.this.action.getCaption());
                    } else if (Action.PROP_DESCRIPTION.equals(evt.getPropertyName())) {
                        setDescription(WebButton.this.action.getDescription());
                    } else if (Action.PROP_ENABLED.equals(evt.getPropertyName())) {
                        setEnabled(WebButton.this.action.isEnabled());
                    } else if (Action.PROP_VISIBLE.equals(evt.getPropertyName())) {
                        setVisible(WebButton.this.action.isVisible());
                    }
                };
                action.addPropertyChangeListener(actionPropertyChangeListener);

                assignAutoDebugId();
            }
        }
    }

    @Override
    public void setStyleName(String name) {
        super.setStyleName(name);
        if (getIcon() != null)
            component.addStyleName(ICON_STYLE);
    }

    @Override
    public String getStyleName() {
        if (getIcon() != null)
            return StringUtils.normalizeSpace(super.getStyleName().replace(ICON_STYLE, ""));

        return super.getStyleName();
    }

    @Override
    protected String getAlternativeDebugId() {
        if (StringUtils.isNotEmpty(id)) {
            return id;
        }
        if (action != null && StringUtils.isNotEmpty(action.getId())) {
            return action.getId();
        }

        return getClass().getSimpleName();
    }

    @Override
    public int getTabIndex() {
        return component.getTabIndex();
    }

    @Override
    public void setTabIndex(int tabIndex) {
        component.setTabIndex(tabIndex);
    }

    @Override
    public void setDisableOnClick(boolean value) {
        component.setDisableOnClick(value);
    }

    @Override
    public boolean isDisableOnClick() {
        return component.isDisableOnClick();
    }

    public boolean isUseResponsePending() {
        return component.isUseResponsePending();
    }

    public void setUseResponsePending(boolean useResponsePending) {
        component.setUseResponsePending(useResponsePending);
    }
}