| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2007 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.diagram.core.services; |
| |
| import java.io.File; |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.emf.common.command.Command; |
| import org.eclipse.emf.ecore.EModelElement; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.emf.edit.command.CopyCommand; |
| import org.eclipse.emf.edit.domain.EditingDomain; |
| import org.eclipse.emf.transaction.Transaction; |
| import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain; |
| import org.eclipse.epf.common.CommonPlugin; |
| import org.eclipse.epf.diagram.core.DiagramCorePlugin; |
| import org.eclipse.epf.diagram.core.DiagramCoreResources; |
| import org.eclipse.epf.diagram.core.bridge.BridgeHelper; |
| import org.eclipse.epf.diagram.core.bridge.DiagramAdapter; |
| import org.eclipse.epf.library.edit.util.IDiagramManager; |
| import org.eclipse.epf.library.edit.util.ProcessUtil; |
| import org.eclipse.epf.library.edit.util.TngUtil; |
| import org.eclipse.epf.persistence.FileManager; |
| import org.eclipse.epf.services.Services; |
| import org.eclipse.epf.services.ILibraryPersister.FailSafeMethodLibraryPersister; |
| import org.eclipse.epf.uma.Activity; |
| import org.eclipse.epf.uma.MethodElement; |
| import org.eclipse.epf.uma.Process; |
| import org.eclipse.gmf.runtime.diagram.core.DiagramEditingDomainFactory; |
| import org.eclipse.gmf.runtime.notation.Diagram; |
| import org.eclipse.gmf.runtime.notation.Node; |
| import org.eclipse.gmf.runtime.notation.View; |
| import org.eclipse.uml2.uml.ActivityNode; |
| |
| /** |
| * @author Shashidhar Kannoori |
| * @author Phong Nguyen Le |
| * @since 1.2 |
| */ |
| public class DiagramHelper { |
| |
| public static final int[] DIAGRAM_TYPES = { IDiagramManager.ACTIVITY_DIAGRAM, |
| IDiagramManager.ACTIVITY_DETAIL_DIAGRAM, IDiagramManager.WORK_PRODUCT_DEPENDENCY_DIAGRAM }; |
| |
| |
| public static int getDiagramType(Diagram diagram){ |
| String typeStr = diagram.getType(); |
| if(typeStr.equals(DiagramManager.AD_kind)){ |
| return IDiagramManager.ACTIVITY_DIAGRAM; |
| } |
| if(typeStr.equals(DiagramManager.ADD_kind)){ |
| return IDiagramManager.ACTIVITY_DETAIL_DIAGRAM; |
| } |
| if(typeStr.equals(DiagramManager.WPD_kind)){ |
| return IDiagramManager.WORK_PRODUCT_DEPENDENCY_DIAGRAM; |
| } |
| return -1; |
| } |
| |
| public static Node findNode(Diagram diagram, Object object) { |
| if(object == null) return null; |
| for (Iterator iter = diagram.getChildren().iterator(); iter.hasNext();) { |
| View view = (View) iter.next(); |
| if(view instanceof Node){ |
| Object e = null; |
| if(object instanceof MethodElement){ |
| e = BridgeHelper.getMethodElement((ActivityNode)view.getElement()); |
| }else{ |
| e = (ActivityNode)view.getElement(); |
| } |
| if(object.equals(e)){ |
| return (Node)view; |
| } |
| } |
| } |
| return null; |
| } |
| |
| public static Node findNodeInADD(Diagram diagram, MethodElement e){ |
| for (Iterator iter = diagram.getChildren().iterator(); iter.hasNext();) { |
| View view = (View) iter.next(); |
| if(view instanceof Node){ |
| Object ex = null; |
| EObject model = view.getElement(); |
| if(model instanceof org.eclipse.epf.diagram.model.Node){ |
| ex = ((org.eclipse.epf.diagram.model.Node)model).getLinkedElement(); |
| if(ex != null && ex.equals(e)){ |
| return (Node)view; |
| } |
| } |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * Returns base diagram. |
| * @param act |
| * @param type |
| * @return |
| */ |
| |
| public static Diagram getBaseDiagram(Activity act, int type){ |
| |
| if (ProcessUtil.isExtendingOrLocallyContributing(act)) { |
| DiagramManager mgr = null; |
| try{ |
| Activity baseAct = (Activity) act.getVariabilityBasedOnElement(); |
| mgr = DiagramManager.getInstance(TngUtil.getOwningProcess(baseAct)); |
| List baseDiagrams = mgr.getDiagrams(baseAct, type); |
| return (Diagram)baseDiagrams.get(0); |
| }catch(Exception e){ |
| DiagramCorePlugin.getDefault().getLogger().logError("Error in getting base diagram: ", e); //$NON-NLS-1$ |
| } |
| finally{ |
| if(mgr != null){ |
| mgr.release(); |
| } |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * Soft copy returns a copy of diagram object with copied references. |
| * @param diagram |
| * @param act |
| * @return |
| */ |
| public static Diagram getSoftCopy(Diagram diagram, Activity act){ |
| Diagram copy = (Diagram)EcoreUtil.copy(diagram); |
| if(act != null){ |
| try{ |
| Transaction tx = getEditingDomain().startTransaction(false, Collections.EMPTY_MAP); |
| BridgeHelper.addEAnnotation((EModelElement)copy.getElement(), act); |
| tx.commit(); |
| }catch(Exception e){ |
| DiagramCorePlugin.getDefault().getLogger().logError("Error in getting soft copy", e); //$NON-NLS-1$ |
| } |
| } |
| if(copy != null){ |
| return copy; |
| } |
| return null; |
| } |
| |
| /** |
| * Deletes the diagram and saves immediately. |
| * @param diagram |
| * @throws Exception |
| */ |
| public static void deleteDiagram(Diagram diagram, boolean save)throws Exception{ |
| if(diagram == null )return; |
| |
| boolean fileExist = true; |
| File file = null; |
| Object linkedObject = null; |
| if(diagram.getElement() instanceof org.eclipse.uml2.uml.Activity){ |
| DiagramAdapter adapter = BridgeHelper.getDiagramAdapter(diagram.getElement()); |
| if(adapter != null) { |
| linkedObject = adapter.getWrapper(); |
| if(linkedObject == null){ |
| linkedObject = adapter.getElement(); |
| } |
| } |
| }else{ |
| org.eclipse.epf.diagram.model.Diagram diagramModel = |
| (org.eclipse.epf.diagram.model.Diagram)diagram.getElement(); |
| linkedObject = diagramModel.getLinkedElement(); |
| } |
| |
| if(linkedObject != null){ |
| Process process = TngUtil.getOwningProcess(linkedObject); |
| String filePath = DiagramManager.getDiagramFilePath(process); |
| file = new File(filePath); |
| if(!file.exists()) fileExist = false; |
| } |
| if(file == null){ |
| file = new File(FileManager.toFileString(diagram.eResource().getURI())); |
| if(!file.exists()) fileExist = false; |
| } |
| |
| Resource resource = diagram.eResource(); |
| if(resource != null){ |
| Transaction tx = ((InternalTransactionalEditingDomain)DiagramEditingDomainFactory.getInstance().getEditingDomain(resource.getResourceSet())) |
| .startTransaction(false, Collections.EMPTY_MAP); |
| DiagramEditingDomainFactory.getInstance().getEditingDomain(resource.getResourceSet()); |
| resource.getContents().remove(diagram.getElement()); |
| resource.getContents().remove(diagram); |
| tx.commit(); |
| if(!fileExist) return; |
| |
| if(save){ |
| FailSafeMethodLibraryPersister persister = Services.getLibraryPersister(Services.XMI_PERSISTENCE_TYPE).getFailSafePersister(); |
| try { |
| // persister.save(resource); |
| // persister.commit(); |
| resource.save(new HashMap<Object, Object>()); |
| if(resource.getContents().isEmpty()){ |
| if(file != null){ |
| file.delete(); |
| } |
| } |
| } catch (Exception e) { |
| CommonPlugin.getDefault().getLogger().logError(e); |
| persister.rollback(); |
| throw new CoreException(new Status(IStatus.ERROR, DiagramCorePlugin.PLUGIN_ID |
| , 1, e.getLocalizedMessage(), null)); |
| } |
| } |
| } |
| } |
| |
| /** |
| * Convenient method to get editing domain |
| * @return |
| */ |
| public static InternalTransactionalEditingDomain getEditingDomain(){ |
| return (InternalTransactionalEditingDomain) DiagramEditingDomainFactory.getInstance() |
| .createEditingDomain(); |
| } |
| |
| public static InternalTransactionalEditingDomain getEditingDomain(Diagram diagram){ |
| if(diagram == null) return getEditingDomain(); |
| Resource resource = diagram.eResource(); |
| if(resource == null) return getEditingDomain(); |
| return (InternalTransactionalEditingDomain)DiagramEditingDomainFactory.getInstance().getEditingDomain(resource.getResourceSet()); |
| } |
| |
| public static Transaction getTransaction(Diagram diagram){ |
| try{ |
| return getEditingDomain(diagram).startTransaction(false, Collections.EMPTY_MAP); |
| }catch(Exception e){ |
| return null; |
| } |
| } |
| |
| public static void commitTransaction(Transaction tx){ |
| try{ |
| tx.commit(); |
| }catch(Exception e){ |
| DiagramCorePlugin.getDefault().getLogger().logError("Error in committing transaction: ", e); //$NON-NLS-1$ |
| } |
| } |
| |
| /** |
| * |
| * @param act |
| * @return |
| */ |
| public static Collection getDiagrams(Activity act) { |
| DiagramManager mgr = DiagramManager.getInstance(TngUtil.getOwningProcess(act)); |
| try{ |
| ArrayList diagrams = new ArrayList(); |
| for (int i = 0; i < DIAGRAM_TYPES.length; i++) { |
| List list = mgr.getDiagrams(act, DIAGRAM_TYPES[i]); |
| if (!list.isEmpty()) { |
| diagrams.addAll(list); |
| } |
| } |
| return diagrams; |
| }catch(Exception e){ |
| return Collections.EMPTY_LIST; |
| } |
| finally{ |
| mgr.release(); |
| } |
| } |
| |
| public Diagram getDiagram(Activity e, int type){ |
| // TODO: consolidate with getDiagrams(Activity) |
| DiagramManager dMgr = DiagramManager.getInstance(TngUtil.getOwningProcess(e)); |
| Diagram diagram = null; |
| try{ |
| List list = dMgr.getDiagrams(e, type); |
| if(!list.isEmpty()){ |
| diagram = (Diagram)list.get(0); |
| } |
| }catch(Exception ex){ |
| DiagramCorePlugin.getDefault().getLogger().logError("Error in getDiagram()", ex); //$NON-NLS-1$ |
| } |
| finally{ |
| if(dMgr != null){ |
| dMgr.release(); |
| } |
| } |
| return diagram; |
| } |
| |
| public static String getDiagramTypeString(Diagram diagram){ |
| String typeStr = diagram.getType(); |
| if(typeStr.equals(DiagramManager.AD_kind)){ |
| return DiagramCoreResources.ActivityDiagram_kind; |
| } |
| if(typeStr.equals(DiagramManager.ADD_kind)){ |
| return DiagramCoreResources.ActivityDetail_Diagram_kind; |
| } |
| if(typeStr.equals(DiagramManager.WPD_kind)){ |
| return DiagramCoreResources.WorkProductDependency_Diagram_kind; |
| } |
| return ""; //$NON-NLS-1$ |
| } |
| |
| public static Diagram copyDiagram(EditingDomain domain, Diagram diagram) { |
| ArrayList list = new ArrayList(); |
| list.add(diagram.getElement()); |
| list.add(diagram); |
| Command cmd = CopyCommand.create(domain, list); |
| cmd.execute(); |
| for (Iterator iter = cmd.getResult().iterator(); iter.hasNext();) { |
| Object element = (Object) iter.next(); |
| if(element instanceof Diagram) { |
| return (Diagram) element; |
| } |
| } |
| return null; |
| } |
| |
| } |