/******************************************************************************* | |
* <copyright> | |
* | |
* Copyright (c) 2013, 2013 SAP AG. | |
* 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: | |
* SAP AG - initial API, implementation and documentation | |
* | |
* </copyright> | |
* | |
*******************************************************************************/ | |
package org.eclipse.fmc.blockdiagram.editor.meta.util; | |
import java.util.Collections; | |
import org.eclipse.core.runtime.Platform; | |
import org.eclipse.core.runtime.Status; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.ecore.EObject; | |
import org.eclipse.emf.ecore.resource.Resource; | |
import org.eclipse.emf.ecore.resource.ResourceSet; | |
import org.eclipse.fmc.blockdiagram.editor.meta.Activator; | |
import org.eclipse.fmc.mm.Comment; | |
import org.eclipse.fmc.mm.FMCConnection; | |
import org.eclipse.fmc.mm.FMCModel; | |
import org.eclipse.fmc.mm.FMCNode; | |
import org.eclipse.fmc.mm.FmcFactory; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.mm.pictograms.Diagram; | |
/** | |
* Utility Class for the Meta Editor. | |
* | |
* @author Patrick Jahnke | |
* | |
*/ | |
public class FMCMetaUtil { | |
private FMCMetaUtil() { | |
} | |
/** | |
* Adds the model object. | |
* | |
* @param obj | |
* the object to add | |
* @param d | |
* the diagram | |
* @param fp | |
* the featureprovider | |
*/ | |
public static void addModelObject(EObject obj, Diagram d, | |
IFeatureProvider fp) { | |
URI uri = d.eResource().getURI(); | |
uri = uri.trimFragment(); | |
uri = uri.trimFileExtension(); | |
uri = uri.appendFileExtension("block"); | |
ResourceSet rSet = d.eResource().getResourceSet(); | |
/* | |
* final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace() | |
* .getRoot(); IResource file = | |
* workspaceRoot.findMember(uri.toPlatformString(true)); Resource r = | |
* null; | |
*/ | |
FMCModel root = null; | |
Resource file = rSet.getResource(uri, false); | |
if (file == null) { | |
file = rSet.createResource(uri); | |
root = FmcFactory.eINSTANCE.createFMCModel(); | |
file.getContents().add(root); | |
try { | |
file.save(Collections.EMPTY_MAP); | |
} catch (Exception e) { | |
Platform.getLog(Platform.getBundle(Activator.PLUGIN_ID)).log( | |
new Status(Status.ERROR, Activator.PLUGIN_ID, | |
"Could not save the model file", e)); | |
} | |
fp.link(d, new EObject[] { root }); | |
} | |
else { | |
file = rSet.getResource(uri, true); | |
root = (FMCModel) file.getContents().get(0); | |
} | |
if (obj instanceof FMCNode) { | |
FMCNode node = (FMCNode) obj; | |
// Add only to top level model if not already contained somewhere | |
// else | |
// if (node.getContainer() == null) | |
root.getNodes().add(node); | |
} else if (obj instanceof FMCConnection) { | |
root.getConnections().add((FMCConnection) obj); | |
} else if (obj instanceof Comment) { | |
root.getComments().add((Comment) obj); | |
} | |
} | |
/** | |
* Get the FmcModel object from a given Diagram. | |
* | |
* @param d | |
* The diagram | |
* @return The FMCModel Object. | |
*/ | |
public static FMCModel getFMCModel(Diagram d) { | |
URI uri = d.eResource().getURI(); | |
uri = uri.trimFragment(); | |
uri = uri.trimFileExtension(); | |
uri = uri.appendFileExtension("block"); | |
ResourceSet rSet = d.eResource().getResourceSet(); | |
FMCModel root = null; | |
Resource file = rSet.getResource(uri, false); | |
file = rSet.getResource(uri, true); | |
root = (FMCModel) file.getContents().get(0); | |
return root; | |
} | |
} |