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

Java tutorial

Introduction

Here is the source code for com.crispico.flower.mp.contributions.FlowerPasteAction.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.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.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.action.PasteAction;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;

import com.crispico.flower.mp.FlowerMPUtils;
import com.crispico.flower.mp.command.java.FlowerPasteFromClipboardCommand;

/**
 * Action for paste elements
 * 
 * @author Cristina
 * @flowerModelElementId _M-a4AL_SEd-259cbnb9fYw
 */
public class FlowerPasteAction extends EditingDomainFlowerAction {

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

    protected PasteAction pasteAction = new PasteAction() {
        @Override
        public Command createCommand(Collection<?> objects) {
            if (objects.size() == 1)
                return new FlowerPasteFromClipboardCommand(domain, (EObject) objects.toArray()[0], null, 0);
            else
                return UnexecutableCommand.INSTANCE;
        }
    };

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

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

    /**
     * @flowerModelElementId _M-a4CL_SEd-259cbnb9fYw
     */
    public FlowerPasteAction() {
        label = pasteAction.getText();
        image = "action/paste_edit.gif";
        group = EDIT_GROUP;
    }

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

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

    /**
     * @flowerModelElementId _0dbxU9XREd-4l6LZuYEfyA
     */
    @Override
    public void run(List<?> selection) {
        update(new StructuredSelection(selection));
        pasteAction.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(pasteAction.updateSelection(selection));
        } else {
            // editingDomain is null, reset action
            setEnabled(false);
        }
    }

}