/**
 * Copyright(C) 2019 Patrik Dufresne Service Logiciel <info@patrikdufresne.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.planimod.ui.views.admin;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jface.action.Action;

import com.patrikdufresne.util.Localized;
import com.planimod.ui.databinding.MotifBean;
import com.planimod.ui.theme.Resources;

/**
 * This action is used to add a MotifBean to an PointChart. Notice: this action does not persist the modification.
 * 
 * @author Karl St-Cyr
 * 
 */
public class AddMotifEntry extends Action {

    /**
     * Motif property.
     */
    public static final String MOTIFS = "motifs";//$NON-NLS-1$

    /**
     * Used for localization.
     */
    private Localized localized = Localized.load(AddMotifEntry.class);

    /**
     * The current collection of motifs
     */
    Set<MotifBean> motifs;

    /**
     * Create a new action to add an entry to a motif chart.
     */
    public AddMotifEntry() {
        setText(this.localized.get("AddMotifEntry.text")); //$NON-NLS-1$
        setImageDescriptor(Resources.getImageDescriptor(Resources.ICON_LIST_ADD_16));
        setEnabled(canRun());
        setToolTipText(this.localized.get("AddMotifEntry.tooltip")); //$NON-NLS-1$
    }

    /**
     * Check if this action can be run.
     * 
     * @return True if the action can be run.
     */
    protected boolean canRun() {
        return this.motifs != null;
    }

    /**
     * Returns the set of MotifBean
     * 
     * @return Set<MotifBean>
     */
    public Set<MotifBean> getMotifs() {
        return motifs;
    }

    /**
     * This implementation add a new entry to a MotifBean object
     */
    @Override
    public void run() {
        if (!canRun()) {
            return;
        }
        // Create a new instance of the Motifs
        Set<MotifBean> newMotifs = new HashSet<MotifBean>(this.motifs);
        // Add a new entry
        MotifBean r = new MotifBean();
        r.setName(this.localized.get("AddMotifEntry.newMotif")); //$NON-NLS-1$
        newMotifs.add(r);
        // Fire a property change
        firePropertyChange(MOTIFS, this.motifs, this.motifs = newMotifs);
    }

    /**
     * Sets the point chart object to be updated or null if not set.
     * 
     * @param set
     *            <MotifBean>
     */
    public void setMotifs(Set<MotifBean> motifs) {
        if (this.motifs != motifs) {
            firePropertyChange(MOTIFS, this.motifs, this.motifs = motifs);
            setEnabled(canRun());
        }
    }
}
