Java tutorial
/* 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.ArrayList; 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.edit.domain.EditingDomain; import org.eclipse.emf.edit.ui.action.CopyAction; 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.FlowerCopyToClipboardCommand; import com.crispico.flower.mp.notation.Diagram; import com.crispico.flower.mp.notation.View; /** * Action for copy elements * * @author Cristina * * @flowerModelElementId _M91CMb_SEd-259cbnb9fYw */ public class FlowerCopyAction extends EditingDomainFlowerAction { private static final String EDIT_GROUP = "group.edit"; protected CopyAction copyAction = new CopyAction() { @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 sameDomain = objects.size() == 0 ? false : true; Iterator<?> it = objects.iterator(); while (sameDomain && it.hasNext()) { Object obj = it.next(); if (obj instanceof Relationship || (obj instanceof View && !(obj instanceof Diagram) && !(((View) obj).eContainer() instanceof Diagram))) sameDomain = false; else sameDomain = sameDomain && FlowerEditingDomain.getEditingDomainFor(obj) == domain; } if (sameDomain) return new FlowerCopyToClipboardCommand(domain, (List<EObject>) objects); else return UnexecutableCommand.INSTANCE; } }; public void setEditingDomain(EditingDomain domain) { copyAction.setEditingDomain(domain); } public void setEnabled(boolean enabled) { copyAction.setEnabled(enabled); } /** * @flowerModelElementId _M91COb_SEd-259cbnb9fYw */ public FlowerCopyAction() { label = copyAction.getText(); image = "action/copy_edit.gif"; group = EDIT_GROUP; } /** * @flowerModelElementId _0chLUNXREd-4l6LZuYEfyA */ @Override public boolean isVisible(List<?> selection) { return true; } /** * @flowerModelElementId _0chyYtXREd-4l6LZuYEfyA */ @Override public boolean isEnabled(List<?> selection) { update(new StructuredSelection(selection)); return copyAction.isEnabled(); } /** * @flowerModelElementId _0ciZctXREd-4l6LZuYEfyA */ @Override public void run(List<?> selection) { update(new StructuredSelection(selection)); copyAction.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(copyAction.updateSelection(selection)); } else { // editingDomain is null, reset action setEnabled(false); } } }