KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > ecore2ecore > action > MapToEcoreActionDelegate


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: MapToEcoreActionDelegate.java,v 1.4 2005/06/08 06:23:41 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.ecore2ecore.action;
18
19
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.emf.common.notify.AdapterFactory;
30 import org.eclipse.emf.common.util.URI;
31 import org.eclipse.emf.ecore.EPackage;
32 import org.eclipse.emf.ecore.EcorePackage;
33 import org.eclipse.emf.ecore.provider.EcoreItemProviderAdapterFactory;
34 import org.eclipse.emf.ecore.resource.Resource;
35 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
36 import org.eclipse.emf.ecore.util.EcoreUtil;
37 import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
38 import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
39 import org.eclipse.emf.mapping.domain.PluginAdapterFactoryMappingDomain;
40 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreFactory;
41 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreMappingRoot;
42 import org.eclipse.emf.mapping.ecore2ecore.presentation.Ecore2EcoreEditorPlugin;
43 import org.eclipse.emf.mapping.provider.MappingItemProviderAdapterFactory;
44 import org.eclipse.jface.action.IAction;
45 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
46 import org.eclipse.jface.viewers.ISelection;
47 import org.eclipse.jface.viewers.IStructuredSelection;
48 import org.eclipse.jface.viewers.StructuredSelection;
49 import org.eclipse.ui.IWorkbenchPage;
50 import org.eclipse.ui.IWorkbenchPart;
51 import org.eclipse.ui.IWorkbenchWindow;
52 import org.eclipse.ui.PartInitException;
53 import org.eclipse.ui.PlatformUI;
54 import org.eclipse.ui.actions.ActionDelegate;
55 import org.eclipse.ui.actions.WorkspaceModifyOperation;
56 import org.eclipse.ui.dialogs.ResourceSelectionDialog;
57 import org.eclipse.ui.part.FileEditorInput;
58 import org.eclipse.ui.part.ISetSelectionTarget;
59
60
61 /**
62  *
63  */

64 public class MapToEcoreActionDelegate extends ActionDelegate
65 {
66   private static final String JavaDoc ECORE_FILE_EXTENSION = "ecore";
67
68   private static final String JavaDoc ECORE2ECORE_FILE_EXTENSION = "ecore2ecore";
69
70   protected EPackage getInputEPackage(IStructuredSelection structuredSelection)
71   {
72     if (IFile.class.isInstance(structuredSelection.getFirstElement()))
73     {
74       IFile file = (IFile)structuredSelection.getFirstElement();
75
76       if (ECORE_FILE_EXTENSION.equals(file.getFullPath().getFileExtension()))
77       {
78         return (EPackage)EcoreUtil.getObjectByType(new ResourceSetImpl().getResource(
79           URI.createPlatformResourceURI(file.getFullPath().toString(), true),
80           true).getContents(), EcorePackage.eINSTANCE.getEPackage());
81       }
82     }
83
84     return null;
85   }
86
87   protected IFile getFile(Resource resource)
88   {
89       URI uri = resource.getURI();
90       uri = resource.getResourceSet().getURIConverter().normalize(uri);
91       String JavaDoc scheme = uri.scheme();
92       if ("platform".equals(scheme) && uri.segmentCount() > 1 && "resource".equals(uri.segment(0)))
93       {
94         StringBuffer JavaDoc platformResourcePath = new StringBuffer JavaDoc();
95         for (int j = 1, size = uri.segmentCount(); j < size; ++j)
96         {
97           platformResourcePath.append('/');
98           platformResourcePath.append(uri.segment(j));
99         }
100         return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourcePath.toString()));
101       }
102
103       return null;
104   }
105
106   /*
107    * (non-Javadoc)
108    *
109    * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
110    */

111   public void run(IAction action)
112   {
113     final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
114
115     ResourceSelectionDialog resourceSelectionDialog = new ResourceSelectionDialog(
116       workbenchWindow.getShell(),
117       ResourcesPlugin.getWorkspace().getRoot(),
118       Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_SelectOutputEcoreModels_label"));
119     resourceSelectionDialog.open();
120
121     final Object JavaDoc[] result = resourceSelectionDialog.getResult();
122
123     if (result != null)
124     {
125       final EPackage inputEPackage = getInputEPackage((IStructuredSelection)workbenchWindow.getSelectionService().getSelection());
126       final Resource inputResource = inputEPackage.eResource();
127
128       ProgressMonitorDialog dialog = new ProgressMonitorDialog(workbenchWindow.getShell());
129
130       try
131       {
132         dialog.run(false, true,
133           new WorkspaceModifyOperation()
134           {
135             protected void execute(IProgressMonitor progressMonitor)
136             {
137               try
138               {
139                 progressMonitor.beginTask("", result.length);
140
141                 for (int i = 0; i < result.length; i++)
142                 {
143                   IResource resource = (IResource)result[i];
144
145                   if (resource.getType() == IResource.FILE && ECORE_FILE_EXTENSION.equals(resource.getFullPath().getFileExtension()))
146                   {
147                     Resource outputResource = inputResource.getResourceSet().getResource(
148                       URI.createPlatformResourceURI(resource.getFullPath().toString(), true),
149                       true);
150                     EPackage outputEPackage = (EPackage)EcoreUtil.getObjectByType(
151                       outputResource.getContents(),
152                       EcorePackage.eINSTANCE.getEPackage());
153
154                     String JavaDoc base = inputResource.getURI().trimFileExtension().lastSegment() + "_2_" +
155                       outputResource.getURI().trimFileExtension().lastSegment();
156                     URI mappingURI = outputResource.getURI().trimSegments(1).appendSegment(base).appendFileExtension(ECORE2ECORE_FILE_EXTENSION);
157
158                     Resource mappingResource = inputResource.getResourceSet().createResource(mappingURI);
159
160                     progressMonitor.subTask(MessageFormat.format(
161                       Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_BuildingMappingFromTo_message"),
162                       new Object JavaDoc[] { inputResource.getURI().lastSegment(), outputResource.getURI().lastSegment() }));
163
164                     mappingResource.getContents().add(createMappingRoot(inputEPackage, outputEPackage));
165                     
166                     try
167                     {
168                       mappingResource.save(null);
169                       
170                       IFile file = getFile(mappingResource);
171                       IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
172                       
173                       final IWorkbenchPart activePart = workbenchPage.getActivePart();
174                       if (activePart instanceof ISetSelectionTarget)
175                       {
176                         final ISelection targetSelection = new StructuredSelection(file);
177                         workbenchWindow.getShell().getDisplay().asyncExec(new Runnable JavaDoc()
178                           {
179                             public void run()
180                             {
181                               ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
182                             }
183                           });
184                       }
185
186                       try {
187                         workbenchPage.openEditor(new FileEditorInput(file), workbenchWindow.getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId());
188                       } catch (PartInitException pie) {
189                         Ecore2EcoreEditorPlugin.INSTANCE.log(pie);
190                       }
191                     }
192                     catch (IOException JavaDoc ioe)
193                     {
194                       Ecore2EcoreEditorPlugin.INSTANCE.log(ioe);
195                     }
196                   }
197                 }
198               }
199               finally
200               {
201                 progressMonitor.done();
202               }
203             }
204           });
205       }
206       catch (InterruptedException JavaDoc ie)
207       {
208         // ignore
209
}
210       catch (InvocationTargetException JavaDoc ite)
211       {
212         Ecore2EcoreEditorPlugin.INSTANCE.log(ite);
213       }
214     }
215   }
216
217   protected static Ecore2EcoreMappingRoot createMappingRoot(EPackage inputEPackage, EPackage outputEPackage)
218   {
219     Ecore2EcoreMappingRoot mappingRoot = Ecore2EcoreFactory.eINSTANCE.createEcore2EcoreMappingRoot();
220     mappingRoot.setTopToBottom(true);
221
222     AdapterFactory ecoreAdapterFactory = new EcoreItemProviderAdapterFactory();
223     AdapterFactory composedAdapterFactory = new ComposedAdapterFactory(
224       new AdapterFactory []
225       {
226         new ResourceItemProviderAdapterFactory(),
227         new MappingItemProviderAdapterFactory(),
228         ecoreAdapterFactory
229       });
230     mappingRoot.setDomain(new PluginAdapterFactoryMappingDomain(composedAdapterFactory, ecoreAdapterFactory, null, null));
231     
232     mappingRoot.getInputs().add(inputEPackage);
233     mappingRoot.getOutputs().add(outputEPackage);
234     
235     return mappingRoot;
236   }
237   
238   /*
239    * (non-Javadoc)
240    *
241    * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
242    * org.eclipse.jface.viewers.ISelection)
243    */

244   public void selectionChanged(IAction action, ISelection selection)
245   {
246     action.setEnabled(IStructuredSelection.class.isInstance(selection) && getInputEPackage((IStructuredSelection)selection) != null);
247   }
248 }
Popular Tags