Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvlorenzo2012-11-28 10:48:42 +0000
committervlorenzo2012-11-28 10:48:42 +0000
commitdc991f97b44be9ad2da0970470fbed022a8fe4fa (patch)
treedb6e431e303cd0beaa3f106689f5c5f257544df4 /sandbox/org.eclipse.papyrus.dev.infra.emf.export/src
parent011bb489bf350005ea1f78a41063a584c0633222 (diff)
downloadorg.eclipse.papyrus-dc991f97b44be9ad2da0970470fbed022a8fe4fa.tar.gz
org.eclipse.papyrus-dc991f97b44be9ad2da0970470fbed022a8fe4fa.tar.xz
org.eclipse.papyrus-dc991f97b44be9ad2da0970470fbed022a8fe4fa.zip
Rename the plugin oep.infra.emf.export into oep.dev.infra.emf.export
Diffstat (limited to 'sandbox/org.eclipse.papyrus.dev.infra.emf.export/src')
-rw-r--r--sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/Activator.java54
-rw-r--r--sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/handler/EMFSaveAsHandler.java222
2 files changed, 276 insertions, 0 deletions
diff --git a/sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/Activator.java b/sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/Activator.java
new file mode 100644
index 00000000000..2f20ca23d43
--- /dev/null
+++ b/sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/Activator.java
@@ -0,0 +1,54 @@
+package org.eclipse.papyrus.infra.emf.export;
+
+import org.eclipse.papyrus.infra.core.log.LogHelper;
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class Activator extends AbstractUIPlugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "org.eclipse.papyrus.infra.emf.export"; //$NON-NLS-1$
+
+ // The shared instance
+ private static Activator plugin;
+
+ public static LogHelper log;
+
+ /**
+ * The constructor
+ */
+ public Activator() {
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ plugin = this;
+ log = new LogHelper(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static Activator getDefault() {
+ return plugin;
+ }
+
+}
diff --git a/sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/handler/EMFSaveAsHandler.java b/sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/handler/EMFSaveAsHandler.java
new file mode 100644
index 00000000000..474514ee0c0
--- /dev/null
+++ b/sandbox/org.eclipse.papyrus.dev.infra.emf.export/src/org/eclipse/papyrus/infra/emf/export/handler/EMFSaveAsHandler.java
@@ -0,0 +1,222 @@
+package org.eclipse.papyrus.infra.emf.export.handler;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.internal.resources.File;
+import org.eclipse.core.internal.resources.Workspace;
+import org.eclipse.core.internal.resources.WorkspaceRoot;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.emf.common.notify.AdapterFactory;
+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.emf.ecore.resource.URIConverter;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
+import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
+import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
+import org.eclipse.jface.viewers.ILabelProvider;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.window.Window;
+import org.eclipse.papyrus.infra.emf.export.Activator;
+import org.eclipse.papyrus.infra.widgets.editors.InputDialog;
+import org.eclipse.papyrus.infra.widgets.providers.WorkspaceContentProvider;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog;
+import org.eclipse.ui.dialogs.SaveAsDialog;
+
+
+public class EMFSaveAsHandler extends AbstractHandler {
+
+ private ITreeContentProvider contentProvider = null;
+
+ private ILabelProvider labelProvider = null;
+
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ ResourceSet set = new ResourceSetImpl();
+ final List<Resource> selectedResources = getSelectedResource(set);
+
+ // the dialog used to choose a file to browse
+ final CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(Display.getCurrent().getActiveShell(), getLabelProvider(), getContentProvider());
+ dialog.setTitle("Browse Model");
+ dialog.setMessage("Select the elements to export");
+ dialog.setInput(selectedResources);
+
+ if(dialog.open() == Window.OK) {
+ final Object[] selectedEObject = dialog.getResult();
+ final Collection<EObject> toExport = new ArrayList<EObject>();
+ for(final Object current : selectedEObject) {
+ assert current instanceof EObject;
+ EcoreUtil.resolveAll((EObject)current);
+ toExport.add((EObject)current);
+ }
+
+ SaveAsDialog saveAsDialog = new SaveAsDialog(Display.getCurrent().getActiveShell());
+ // saveAsDialog.setTitle("Select the destination for the exported elements"); //without effect
+ if(saveAsDialog.open() == Window.OK) {
+ Resource newResource = createNewResource(set, saveAsDialog.getResult());
+ newResource.getContents().addAll(toExport);
+ try {
+ newResource.save(Collections.emptyMap());
+ for(final Resource current : selectedResources) {
+ current.save(Collections.emptyMap());
+ }
+
+ } catch (IOException e) {
+ Activator.log.error(e);
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ *
+ * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
+ *
+ * @return
+ */
+ @Override
+ public boolean isEnabled() {
+ return getSelectedResource(new ResourceSetImpl()).size() != 0;
+ }
+
+ /**
+ * FIXME : use the label provider service
+ *
+ * @return
+ * the label provider to use
+ */
+ public ILabelProvider getLabelProvider() {
+ if(labelProvider == null) {
+ final AdapterFactory factory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
+ labelProvider = new AdapterFactoryLabelProvider(factory);
+ }
+ return labelProvider;
+ }
+
+ /**
+ * FIXME : use the content provider service
+ *
+ * @return
+ * the content provider to use
+ */
+ public ITreeContentProvider getContentProvider() {
+ final AdapterFactory factory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
+ if(contentProvider == null) {
+ contentProvider = new AdapterFactoryContentProvider(factory) {
+
+ @Override
+ public Object[] getElements(Object inputElement) {
+ if(inputElement instanceof Resource) {
+ return ((Resource)inputElement).getContents().toArray();
+
+ } else if(inputElement instanceof Collection<?>) {
+ return ((Collection)inputElement).toArray();
+ } else {
+ return super.getElements(inputElement);
+ }
+ }
+ };
+ }
+ return contentProvider;
+ }
+
+ /**
+ *
+ * @param set
+ * a resource set
+ * @return
+ * the selected resource
+ */
+ protected List<Resource> getSelectedResource(final ResourceSet set) {
+ IStructuredSelection selection = (IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
+ final List<Resource> selectedResources = new ArrayList<Resource>();
+ final List<?> selectedElement = selection.toList();
+ for(Object object : selectedElement) {
+
+ if(object instanceof IAdaptable) {
+ final File file = (File)((IAdaptable)object).getAdapter(IFile.class);
+ if(file != null) {
+
+ final String pathName = ((File)file).getFullPath().toString();
+
+
+ final URI uri = URI.createPlatformResourceURI(pathName, true);
+ final Resource res = set.getResource(uri, true);
+ EcoreUtil.resolveAll(res);
+ if(pathName.contains("notation")) {
+
+ selectedResources.add(0, res);
+ } else {
+ selectedResources.add(res);
+ }
+ }
+ }
+
+ }
+ // final Object selectedElement = ((IStructuredSelection)selection).getFirstElement();
+ // String pathName = "";
+ // Resource res = null;
+ // if(selectedElement instanceof IAdaptable) {
+ // final File file = (File)((IAdaptable)selectedElement).getAdapter(IFile.class);
+ // if(file!=null){
+ //
+ // pathName = ((File)file).getFullPath().toString();
+ //
+ // final URI uri = URI.createPlatformResourceURI(pathName, true);
+ // res = set.getResource(uri, true);
+ // }
+ // }
+ // return res;
+ // System.out.println(object);
+ return selectedResources;
+ }
+
+ /**
+ *
+ * @param set
+ * the resourceset to use
+ * @param iPath
+ * a path
+ * @return
+ * the created resource with the wanted path
+ */
+ protected Resource createNewResource(final ResourceSet set, final IPath iPath) {
+ // EcoreResourceUtil.
+ // URIConverter converter = set.getURIConverter();
+ // converter.
+ // IPath wsPath = ResourcesPlugin.getWorkspace().getDescription()
+ // java.io.File file = iPath.toFile();
+ // file.
+ // URI.createPlatformResourceURI(pathName)
+
+ //FIXME : the references to the new files are stored with OS path (F:/....)
+ IPath fullPath = ResourcesPlugin.getWorkspace().getRoot().getFile(iPath).getLocation();
+ // fullPath.get
+ final URI uri = URI.createPlatformResourceURI(iPath.toString(), true);//FileURI(fullPath.toFile().getAbsolutePath());
+ final URI uri2 = URI.createPlatformResourceURI(iPath.toFile().getPath(), true);
+ String device = iPath.getDevice();
+ final Resource newResource = set.createResource(uri);
+ return newResource;
+ }
+
+}

Back to the top