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

Java tutorial

Introduction

Here is the source code for com.crispico.flower.mp.contributions.FlowerCutAction.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.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.action.CutAction;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.uml2.uml.Relationship;

import com.crispico.flower.mp.FlowerEditingDomain;
import com.crispico.flower.mp.FlowerMPUtils;
import com.crispico.flower.mp.command.java.FlowerCutToClipboardCommand;
import com.crispico.flower.mp.notation.Diagram;
import com.crispico.flower.mp.notation.View;

/**
 * Action for cut elements
 * 
 * @author Cristina
 * @flowerModelElementId _M9-MH7_SEd-259cbnb9fYw
 */
public class FlowerCutAction extends EditingDomainFlowerAction {

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

    protected CutAction cutAction = new CutAction() {
        @Override
        public Command createCommand(Collection<?> objects) {
            // Check first if all objects are from the same editing domain and there is no relationship in the objects selected
            boolean shouldExecute = objects.size() == 0 ? false : true;
            Iterator<?> it = objects.iterator();
            while (shouldExecute && it.hasNext()) {
                Object obj = it.next();
                if (obj instanceof Relationship || (obj instanceof View && !(obj instanceof Diagram)
                        && !(((View) obj).eContainer() instanceof Diagram)))
                    shouldExecute = false;
                else
                    shouldExecute = shouldExecute && FlowerEditingDomain.getEditingDomainFor(obj) == domain;
            }
            if (shouldExecute) {
                // check that there is no Root element in the selection.
                ResourceSet rs = domain.getResourceSet();
                for (Resource res : rs.getResources())
                    if (objects.contains(FlowerMPUtils.getRootPackage(res)))
                        shouldExecute = false;
            }
            if (shouldExecute) {
                return new FlowerCutToClipboardCommand(domain, (List<EObject>) objects);
            } else
                return UnexecutableCommand.INSTANCE;
        }
    };

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

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

    public FlowerCutAction() {
        label = cutAction.getText();
        image = "action/cut_edit.gif";
        group = EDIT_GROUP;
    }

    @Override
    public boolean isVisible(List<?> selection) {
        return true;
    }

    @Override
    public boolean isEnabled(List<?> selection) {
        update(new StructuredSelection(selection));
        return cutAction.isEnabled();
    }

    @Override
    public void run(List<?> selection) {
        update(new StructuredSelection(selection));
        cutAction.run();
    }

    /**
     * Update editing domain from selection
     * @flowerModelElementId _M-H9F7_SEd-259cbnb9fYw
     */
    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(cutAction.updateSelection(selection));
        } else {
            // editingDomain is null, reset action
            setEnabled(false);
        }
    }

}