| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2006 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.library.tester.impl.testcommands; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EStructuralFeature; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.emf.edit.command.AbstractOverrideableCommand; |
| import org.eclipse.epf.library.tester.TesterOutputUtil; |
| import org.eclipse.epf.library.tester.iface.TCExeReply; |
| import org.eclipse.epf.library.tester.impl.TestCommandImpl; |
| import org.eclipse.epf.uma.ContentElement; |
| import org.eclipse.epf.uma.ContentPackage; |
| import org.eclipse.epf.uma.MethodElement; |
| import org.eclipse.epf.uma.MethodLibrary; |
| import org.eclipse.epf.uma.MethodPackage; |
| import org.eclipse.epf.uma.UmaFactory; |
| import org.eclipse.epf.uma.UmaPackage; |
| import org.eclipse.epf.uma.util.ContentDescriptionFactory; |
| import org.eclipse.epf.uma.util.IMethodLibraryPersister; |
| import org.eclipse.swt.widgets.Display; |
| import org.w3c.dom.Element; |
| |
| /** |
| * A test command class |
| * |
| * @author Weiping Lu |
| * @since 1.0 |
| * |
| */ |
| public class TCNewMethodElement extends TestCommandImpl { |
| |
| private static Map typeClassMap = new HashMap(); |
| static { |
| UmaFactory uf = UmaFactory.eINSTANCE; |
| typeClassMap.put("MethodPlugin", uf.createMethodPlugin().eClass()); |
| typeClassMap.put("ContentPackage", uf.createContentPackage().eClass()); |
| typeClassMap.put("Role", uf.createRole().eClass()); |
| typeClassMap.put("Task", uf.createTask().eClass()); |
| } |
| |
| |
| private List referencedPlugins; |
| |
| public void parse(Element element) { |
| setAttribute(AT_Type, element.getAttribute(AT_Type)); |
| setAttribute(AT_Name, element.getAttribute(AT_Name)); |
| setAttribute(AT_ParentPath, element.getAttribute(AT_ParentPath)); |
| } |
| |
| public TCExeReply execute() { |
| MethodLibrary currLib = getOwner().getCurrentBaseLib(); |
| |
| String type = getAttribute(AT_Type); |
| String name = getAttribute(AT_Name); |
| String parentPath = getAttribute(AT_ParentPath); |
| |
| EClass eClass = getEClass(type); |
| MethodElement me = (MethodElement) UmaFactory.eINSTANCE.create(eClass); |
| me.setName(name); |
| me.setGuid(EcoreUtil.generateUUID()); |
| MethodElement parentMe = TesterOutputUtil.getMethodElement(currLib, parentPath); |
| if (parentMe == null) { |
| return new TCExeReplyImpl("parentMe == null", false); |
| } |
| EStructuralFeature feature = getContainmentFeature(parentMe, me); |
| if (feature != null) { |
| EList elist = AbstractOverrideableCommand.getOwnerList(parentMe, feature); |
| List c = new ArrayList(); |
| c.add(me); |
| elist.addAll(c); |
| } |
| |
| save(parentMe); |
| |
| return null; |
| } |
| |
| public EClass getEClass(String type) { |
| return (EClass) typeClassMap.get(type); |
| } |
| |
| private EStructuralFeature getContainmentFeature(EObject owner, EObject eobj) { |
| UmaPackage up = UmaPackage.eINSTANCE; |
| |
| if (owner instanceof MethodPackage) { |
| if (eobj instanceof ContentPackage) { |
| return up.getMethodPackage_ChildPackages(); |
| } |
| } |
| |
| if (owner instanceof ContentPackage) { |
| if (eobj instanceof ContentElement) { |
| return up.getContentPackage_ContentElements(); |
| } |
| } |
| |
| return null; |
| } |
| |
| public void save(MethodElement element) { |
| final IMethodLibraryPersister.FailSafeMethodLibraryPersister persister = ContentDescriptionFactory |
| .getMethodLibraryPersister().getFailSafePersister(); |
| final Resource res = element.eResource(); |
| Display.getDefault().syncExec(new Runnable() { |
| public void run() { |
| try { |
| persister.save(res); |
| persister.commit(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| }); |
| } |
| |
| } |