ImageButtonGroup.java 3.29 KB
package cn.csbr.app.gui.util;

import cn.csbr.app.gui.component.common.ImageButton;
import javafx.scene.image.Image;

import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import org.apache.commons.lang.StringUtils;

/**
 * 图片按钮组.
 *
 * @author fenglinz
 * @since 2018-10-31
 */
public final class ImageButtonGroup {

    /**
     * 按钮分组按钮项.
     */
    private static class Item {
        private String url;
        private String currentUrl;
        private String highlightUrl;
        @Getter
        private ImageButton button;

        /**
         * 构造方法
         *
         * @param button 按钮
         * @param url 按钮当前图像url
         */
        public Item(ImageButton button, String url) {
            this.url = url;
            this.button = button;
            this.currentUrl = url;
            this.highlightUrl = StringUtils.isNotBlank(url) ? url.replace("_o", "_s") : "";
        }

        /**
         * 构造方法
         *
         * @param button 按钮
         * @param url 按钮当前图像url
         * @param highlightUrl 按钮高亮图像url
         */
        public Item(ImageButton button, String url, String highlightUrl) {
            this.url = url;
            this.button = button;
            this.currentUrl = url;
            this.highlightUrl = highlightUrl;
        }

        /**
         * 更换按钮图像
         *
         * @param url 要更换的图像url
         */
        public void changeButtonImage(String url) {
            if (this.button != null) {
                this.currentUrl = url;
                this.button.setImage(new Image(url));
            }
        }
    }

    /** 按钮组项集合 */
    private List<Item> items = new ArrayList<>();
    public  List<Item> getItems(){
        return items;
    }
    /**
     * 清空按钮组集合
     */
    public void clear(){
        this.items.clear();
    }

    /**
     * 添加按钮分组项.
     *
     * @param button 按钮对象
     * @param url 图像url
     *
     * @return 按钮分组对象
     */
    public ImageButtonGroup addItem(ImageButton button, String url) {
        this.items.add(new Item(button, url));

        return this;
    }

    /**
     * 高亮显示第一个按钮.
     */
    public void highlightFirst() {
        if (!this.items.isEmpty()) {
            this.highlightCurrentItem(this.items.get(0).button);
        }
    }

    /**
     * 高亮显示按钮.
     *
     * @param button 当前按钮
     */
    public void highlightCurrentItem(ImageButton button) {
        if (!this.items.isEmpty()) {
            Item current = this.findCurrentItem(button);

            if (current != null) {
                current.changeButtonImage(current.highlightUrl);
            }

            for (Item item : this.items) {
                if (item == current) {
                    continue;
                }

                item.changeButtonImage(item.url);
            }
        }
    }

    /**
     * 查找按钮对应的按钮项
     *
     * @param button 按钮
     *
     * @return 按钮项
     */
    private Item findCurrentItem(ImageButton button) {
        for (Item item : this.items) {
            if (item.button == button) {
                return item;
            }
        }

        return null;
    }
}