com.crispico.flower.mp.contributions.FlowerDeleteAction.java Source code

Java tutorial

Introduction

Here is the source code for com.crispico.flower.mp.contributions.FlowerDeleteAction.java

Source

/* license-start
 * 
 * Copyright (C) 2008 - 2013 Crispico, <http://www.crispico.com/>.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 3.
 * 
 * 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 General Public License for more details, at <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *   Crispico - Initial API and implementation
 *
 * license-end
 */
package com.crispico.flower.mp.contributions;

import java.util.List;

import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.action.DeleteAction;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;

import com.crispico.flower.mp.FlowerMPUtils;
import com.crispico.flower.mp.RelationVirtualElement;

/**
 * @author Cristina
 * @flowerModelElementId _M-H9Hr_SEd-259cbnb9fYw
 */
public class FlowerDeleteAction extends EditingDomainFlowerAction {

    private static final String EDIT_GROUP = "group.edit";

    protected DeleteAction deleteAction = new DeleteAction();

    public void setEditingDomain(EditingDomain domain) {
        deleteAction.setEditingDomain(domain);
    }

    public void setEnabled(boolean enabled) {
        deleteAction.setEnabled(enabled);
    }

    /**
     * @flowerModelElementId _M-H9Jr_SEd-259cbnb9fYw
     */
    public FlowerDeleteAction() {
        label = deleteAction.getText();
        image = "action/delete_edit.gif";
        group = EDIT_GROUP;
    }

    /**
     * @flowerModelElementId _0dL5sNXREd-4l6LZuYEfyA
     */
    @Override
    public boolean isVisible(List<?> selection) {
        return true;
    }

    /**
     * @flowerModelElementId _0dMgw9XREd-4l6LZuYEfyA
     */
    @Override
    public boolean isEnabled(List<?> selection) {
        update(new StructuredSelection(selection));
        return deleteAction.isEnabled();
    }

    /**
     * @flowerModelElementId _0dNH09XREd-4l6LZuYEfyA
     */
    @Override
    public void run(List<?> selection) {
        update(new StructuredSelection(selection));
        deleteAction.run();
    }

    /**
     * Update editing domain from selection
     */
    public void update(IStructuredSelection selection) {
        // get editing domain
        EditingDomain editingDomain = FlowerMPUtils.getEditingDomainFromSelection(selection.toList());

        // if editingDomain not null, update action
        if (editingDomain != null) {
            setEditingDomain(editingDomain);
            setEnabled(deleteAction.updateSelection(selection));
        } else {
            // editingDomain is null, reset action
            setEnabled(false);

            // check to see if we have Relations elements inside the selection
            boolean containsVirtualElements = false;
            for (Object obj : selection.toList()) {
                if (obj instanceof RelationVirtualElement) {
                    containsVirtualElements = true;
                    break;
                }
            }
            if (containsVirtualElements) {
                IStructuredSelection newSelection = replaceVirtualElements(selection);

                // get the editingDomain from new selection
                EditingDomain domain = FlowerMPUtils.getEditingDomainFromSelection(selection.toList());
                // update only if the found domain is not null 
                if (domain != null) {
                    setEditingDomain(domain);
                    setEnabled(deleteAction.updateSelection(newSelection));
                }
            }
        }
    }

}