KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > ecore2xml > action > GenerateEcore2XMLActionDelegate


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 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: GenerateEcore2XMLActionDelegate.java,v 1.3 2005/06/21 16:16:58 khussey Exp $
16  */

17 package org.eclipse.emf.mapping.ecore2xml.action;
18
19
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.emf.common.util.TreeIterator;
29 import org.eclipse.emf.common.util.URI;
30 import org.eclipse.emf.ecore.EClassifier;
31 import org.eclipse.emf.ecore.ENamedElement;
32 import org.eclipse.emf.ecore.EObject;
33 import org.eclipse.emf.ecore.EPackage;
34 import org.eclipse.emf.ecore.EStructuralFeature;
35 import org.eclipse.emf.ecore.resource.Resource;
36 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
37 import org.eclipse.emf.ecore.util.EcoreSwitch;
38 import org.eclipse.emf.ecore.util.EcoreUtil;
39 import org.eclipse.emf.ecore.util.ExtendedMetaData;
40 import org.eclipse.emf.ecore.xmi.XMLResource;
41 import org.eclipse.emf.mapping.Mapping;
42 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreMappingRoot;
43 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcorePackage;
44 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLFactory;
45 import org.eclipse.emf.mapping.ecore2xml.ui.Ecore2XMLUIPlugin;
46 import org.eclipse.emf.mapping.ecore2xml.util.Ecore2XMLResource;
47 import org.eclipse.jface.action.IAction;
48 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
49 import org.eclipse.jface.viewers.ISelection;
50 import org.eclipse.jface.viewers.IStructuredSelection;
51 import org.eclipse.jface.viewers.StructuredSelection;
52 import org.eclipse.ui.IWorkbenchPage;
53 import org.eclipse.ui.IWorkbenchPart;
54 import org.eclipse.ui.IWorkbenchWindow;
55 import org.eclipse.ui.PartInitException;
56 import org.eclipse.ui.PlatformUI;
57 import org.eclipse.ui.actions.ActionDelegate;
58 import org.eclipse.ui.actions.WorkspaceModifyOperation;
59 import org.eclipse.ui.part.FileEditorInput;
60 import org.eclipse.ui.part.ISetSelectionTarget;
61
62
63 /**
64  * A action that generates Ecore2XML mappings from Ecore2Ecore mappings.
65  */

66 public class GenerateEcore2XMLActionDelegate extends ActionDelegate
67 {
68   
69   private static final String JavaDoc ECORE2ECORE_FILE_EXTENSION = "ecore2ecore";
70   
71   protected Ecore2EcoreMappingRoot getMappingRoot(IStructuredSelection structuredSelection)
72   {
73     
74     if (IFile.class.isInstance(structuredSelection.getFirstElement()))
75     {
76       IFile file = (IFile)structuredSelection.getFirstElement();
77       
78       if (ECORE2ECORE_FILE_EXTENSION.equals(file.getFullPath().getFileExtension()))
79       {
80         return (Ecore2EcoreMappingRoot)EcoreUtil.getObjectByType(new ResourceSetImpl().getResource(
81           URI.createPlatformResourceURI(file.getFullPath().toString(), true),
82           true).getContents(), Ecore2EcorePackage.eINSTANCE.getEcore2EcoreMappingRoot());
83       }
84     }
85     
86     return null;
87   }
88   
89   protected static XMLResource.XMLInfo createXMLInfo(EObject eObject)
90   {
91     return (XMLResource.XMLInfo)new EcoreSwitch()
92     {
93       public Object JavaDoc caseEPackage(EPackage ePackage)
94       {
95         XMLResource.XMLInfo xmlInfo = Ecore2XMLFactory.eINSTANCE.createXMLInfo();
96         
97         xmlInfo.setName(ePackage.getName());
98         xmlInfo.setTargetNamespace(ExtendedMetaData.INSTANCE.getNamespace(ePackage));
99         
100         return xmlInfo;
101       }
102       
103       public Object JavaDoc caseEClassifier(EClassifier eClassifier)
104       {
105         XMLResource.XMLInfo xmlInfo = Ecore2XMLFactory.eINSTANCE.createXMLInfo();
106         
107         xmlInfo.setName(ExtendedMetaData.INSTANCE.getName(eClassifier));
108         xmlInfo.setTargetNamespace(ExtendedMetaData.INSTANCE.getNamespace(eClassifier));
109         
110         return xmlInfo;
111       }
112       
113       public Object JavaDoc caseEStructuralFeature(EStructuralFeature eStructuralFeature)
114       {
115         XMLResource.XMLInfo xmlInfo = Ecore2XMLFactory.eINSTANCE.createXMLInfo();
116         
117         xmlInfo.setName(ExtendedMetaData.INSTANCE.getName(eStructuralFeature));
118         xmlInfo.setTargetNamespace(ExtendedMetaData.INSTANCE.getNamespace(eStructuralFeature));
119         
120         switch (ExtendedMetaData.INSTANCE.getFeatureKind(eStructuralFeature))
121         {
122           case ExtendedMetaData.ATTRIBUTE_FEATURE:
123             xmlInfo.setXMLRepresentation(XMLResource.XMLInfo.ATTRIBUTE);
124             break;
125           case ExtendedMetaData.ELEMENT_FEATURE:
126             xmlInfo.setXMLRepresentation(XMLResource.XMLInfo.ELEMENT);
127             break;
128         }
129         
130         return xmlInfo;
131       }
132     }.doSwitch(eObject);
133   }
134   
135   protected static XMLResource.XMLMap createXMLMap(Ecore2EcoreMappingRoot mappingRoot)
136   {
137     XMLResource.XMLMap xmlMap = Ecore2XMLFactory.eINSTANCE.createXMLMap();
138     
139     for (TreeIterator mappings = mappingRoot.treeIterator(); mappings.hasNext();)
140     {
141       Mapping mapping = (Mapping)mappings.next();
142       
143       if (!mapping.getInputs().isEmpty())
144       {
145         EObject input = (EObject)mapping.getInputs().get(0);
146         
147         for (Iterator JavaDoc outputs = mapping.getOutputs().iterator(); outputs.hasNext();)
148         {
149           ENamedElement output = (ENamedElement)outputs.next();
150           XMLResource.XMLInfo xmlInfo = createXMLInfo(input);
151           if (xmlInfo != null) {
152             xmlMap.add(output, xmlInfo);
153           }
154         }
155       }
156     }
157     
158     return xmlMap;
159   }
160   
161   protected IFile getFile(Resource resource)
162   {
163     URI uri = resource.getURI();
164     uri = resource.getResourceSet().getURIConverter().normalize(uri);
165     String JavaDoc scheme = uri.scheme();
166     if ("platform".equals(scheme) && uri.segmentCount() > 1 && "resource".equals(uri.segment(0)))
167     {
168       StringBuffer JavaDoc platformResourcePath = new StringBuffer JavaDoc();
169       for (int j = 1, size = uri.segmentCount(); j < size; ++j)
170       {
171         platformResourcePath.append('/');
172         platformResourcePath.append(uri.segment(j));
173       }
174       return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourcePath.toString()));
175     }
176     
177     return null;
178   }
179   
180   /*
181    * (non-Javadoc)
182    *
183    * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
184    */

185   public void run(IAction action)
186   {
187     final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
188     
189     try
190     {
191       new ProgressMonitorDialog(workbenchWindow.getShell()).run(false, true, new WorkspaceModifyOperation()
192         {
193         
194         protected void execute(IProgressMonitor progressMonitor)
195         {
196           try
197           {
198             progressMonitor.beginTask("", IProgressMonitor.UNKNOWN);
199             
200             Ecore2EcoreMappingRoot mappingRoot = getMappingRoot((IStructuredSelection)workbenchWindow.getSelectionService().getSelection());
201             
202             if (mappingRoot != null)
203             {
204               Resource ecore2ecoreResource = mappingRoot.eResource();
205               
206               URI ecore2xmlURI = ecore2ecoreResource.getURI().trimFileExtension().appendFileExtension(Ecore2XMLResource.FILE_EXTENSION);
207               Resource ecore2xmlResource = ecore2ecoreResource.getResourceSet().createResource(ecore2xmlURI);
208               
209               ecore2xmlResource.getContents().add(createXMLMap(mappingRoot));
210               
211               try
212               {
213                 ecore2xmlResource.save(null);
214                 
215                 IFile file = getFile(ecore2xmlResource);
216                 IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
217                 
218                 final IWorkbenchPart activePart = workbenchPage.getActivePart();
219                 if (activePart instanceof ISetSelectionTarget)
220                 {
221                   final ISelection targetSelection = new StructuredSelection(file);
222                   workbenchWindow.getShell().getDisplay().asyncExec(new Runnable JavaDoc()
223                     {
224                     public void run()
225                     {
226                       ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
227                     }
228                     });
229                 }
230                 
231                 try {
232                   workbenchPage.openEditor(new FileEditorInput(file), workbenchWindow.getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId());
233                 } catch (PartInitException pie) {
234                   Ecore2XMLUIPlugin.INSTANCE.log(pie);
235                 }
236               }
237               catch (IOException JavaDoc ioe)
238               {
239                 Ecore2XMLUIPlugin.INSTANCE.log(ioe);
240               }
241             }
242           }
243           finally
244           {
245             progressMonitor.done();
246           }
247         }
248         });
249     }
250     catch (InterruptedException JavaDoc ie)
251     {
252       // ignore
253
}
254     catch (InvocationTargetException JavaDoc ite)
255     {
256       Ecore2XMLUIPlugin.INSTANCE.log(ite);
257     }
258   }
259   
260   /*
261    * (non-Javadoc)
262    *
263    * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
264    * org.eclipse.jface.viewers.ISelection)
265    */

266   public void selectionChanged(IAction action, ISelection selection)
267   {
268     action.setEnabled(IStructuredSelection.class.isInstance(selection) && getMappingRoot((IStructuredSelection)selection) != null);
269   }
270   
271 }
Popular Tags