Link Here
|
1 |
/* |
2 |
* Copyright (c) 2007 Borland Software Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Borland Software Corporation - initial API and implementation |
10 |
*/ |
11 |
package org.eclipse.gmf.internal.codegen.popup.actions; |
12 |
|
13 |
import java.io.IOException; |
14 |
import java.lang.reflect.InvocationTargetException; |
15 |
import java.util.HashMap; |
16 |
import java.util.List; |
17 |
import java.util.Map; |
18 |
|
19 |
import org.eclipse.core.resources.IFile; |
20 |
import org.eclipse.core.runtime.IProgressMonitor; |
21 |
import org.eclipse.emf.common.util.URI; |
22 |
import org.eclipse.emf.ecore.EClass; |
23 |
import org.eclipse.emf.ecore.EObject; |
24 |
import org.eclipse.emf.ecore.EcorePackage; |
25 |
import org.eclipse.emf.ecore.resource.Resource; |
26 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
27 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
28 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
29 |
import org.eclipse.gmf.internal.bridge.ui.Plugin; |
30 |
import org.eclipse.gmf.internal.bridge.wizards.WizardUtil; |
31 |
import org.eclipse.gmf.internal.common.ui.ElementSelectorExtension; |
32 |
import org.eclipse.gmf.internal.common.ui.ExtensibleModelSelectionPage; |
33 |
import org.eclipse.gmf.internal.common.ui.ListElementSelectorExtension; |
34 |
import org.eclipse.gmf.internal.common.ui.PredefinedModelExtension; |
35 |
import org.eclipse.gmf.internal.common.ui.ResourceLocationProvider; |
36 |
import org.eclipse.jface.viewers.IStructuredSelection; |
37 |
import org.eclipse.jface.viewers.StructuredSelection; |
38 |
import org.eclipse.jface.wizard.Wizard; |
39 |
import org.eclipse.ui.IWorkbench; |
40 |
import org.eclipse.ui.IWorkbenchWizard; |
41 |
import org.eclipse.ui.PartInitException; |
42 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
43 |
|
44 |
public class GMFGenExtensionModelWizard extends Wizard implements IWorkbenchWizard { |
45 |
private static String GMFGEN_FILE_EXTENSION = "gmfgen"; //$NON-NLS-1$ |
46 |
private static String ECORE_FILE_EXTENSION = "ecore"; //$NON-NLS-1$ |
47 |
|
48 |
private IWorkbench myWorkbench; |
49 |
private IStructuredSelection mySelection; |
50 |
private ElementSelectorExtension myRootElementSelectorExtension; |
51 |
private ResourceSet myResourceSet; |
52 |
private ResourceLocationProvider myResourceLocationProvider; |
53 |
private Map<String, Object> myGmfgenResourceSaveOptions; |
54 |
|
55 |
public GMFGenExtensionModelWizard() { |
56 |
super(); |
57 |
} |
58 |
|
59 |
public void init(IWorkbench workbench, IStructuredSelection selection) { |
60 |
myWorkbench = workbench; |
61 |
mySelection = selection; |
62 |
setWindowTitle(Messages.GMFGenExtensionModelWizard_windowTitle); |
63 |
} |
64 |
|
65 |
public IWorkbench getWorkbench() { |
66 |
return myWorkbench; |
67 |
} |
68 |
|
69 |
public IStructuredSelection getSelection() { |
70 |
return mySelection; |
71 |
} |
72 |
|
73 |
protected ResourceSet getResourceSet() { |
74 |
if (myResourceSet == null) { |
75 |
myResourceSet = new ResourceSetImpl(); |
76 |
} |
77 |
return myResourceSet; |
78 |
} |
79 |
|
80 |
private Map<String, Object> getGmfgenResourceSaveOptions() { |
81 |
if (myGmfgenResourceSaveOptions == null) { |
82 |
myGmfgenResourceSaveOptions = new HashMap<String, Object>(); |
83 |
myGmfgenResourceSaveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$ |
84 |
} |
85 |
return myGmfgenResourceSaveOptions; |
86 |
} |
87 |
|
88 |
public ElementSelectorExtension getRootElementSelectorExtension() { |
89 |
return myRootElementSelectorExtension; |
90 |
} |
91 |
|
92 |
public ResourceLocationProvider getResourceLocationProvider() { |
93 |
return myResourceLocationProvider; |
94 |
} |
95 |
|
96 |
@Override |
97 |
public void addPages() { |
98 |
myResourceLocationProvider = new ResourceLocationProvider(getSelection()); |
99 |
ExtensibleModelSelectionPage domainModelSelectionPage = new ExtensibleModelSelectionPage("domain", myResourceLocationProvider, getResourceSet()) { //$NON-NLS-1$ |
100 |
|
101 |
protected String getModelFileExtension() { |
102 |
return ECORE_FILE_EXTENSION; |
103 |
} |
104 |
}; |
105 |
domainModelSelectionPage.setTitle(Messages.GMFGenExtensionModelWizard_pageTitle); |
106 |
domainModelSelectionPage.setDescription(Messages.GMFGenExtensionModelWizard_pageDescription); |
107 |
List<URI> uris = myResourceLocationProvider.getSelectedURIs(ECORE_FILE_EXTENSION); |
108 |
uris.add(URI.createURI("platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore")); //$NON-NLS-1$ |
109 |
domainModelSelectionPage.addExtension("prem", new PredefinedModelExtension(domainModelSelectionPage, uris)); //$NON-NLS-1$ |
110 |
myRootElementSelectorExtension = new ListElementSelectorExtension() { |
111 |
|
112 |
protected String getModelElementName() { |
113 |
return Messages.GMFGenExtensionModelWizard_classChooserLabel; |
114 |
} |
115 |
|
116 |
protected String getModelElementLabel(EObject modelElement) { |
117 |
String name = ((EClass) modelElement).getName(); |
118 |
if (name == null || name.trim().length() == 0) { |
119 |
name = Messages.GMFGenExtensionModelWizard_unnamedClassName; |
120 |
} |
121 |
return name; |
122 |
} |
123 |
|
124 |
protected boolean isApplicable(EObject element) { |
125 |
EClass clazz = (EClass) element; |
126 |
return !clazz.isAbstract(); |
127 |
} |
128 |
|
129 |
protected EClass getModelElementClass() { |
130 |
return EcorePackage.eINSTANCE.getEClass(); |
131 |
} |
132 |
}; |
133 |
domainModelSelectionPage.addExtension("domainElement", myRootElementSelectorExtension); //$NON-NLS-1$ |
134 |
addPage(domainModelSelectionPage); |
135 |
super.addPages(); |
136 |
} |
137 |
|
138 |
@Override |
139 |
public boolean performFinish() { |
140 |
WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { |
141 |
|
142 |
protected void execute(IProgressMonitor progressMonitor) { |
143 |
try { |
144 |
ElementSelectorExtension extension = getRootElementSelectorExtension(); |
145 |
EClass rootClass = (EClass) extension.getModelElement(); |
146 |
EObject instantiated = rootClass.getEPackage().getEFactoryInstance().create(rootClass); |
147 |
List<URI> gmfgenURIs = getResourceLocationProvider().getSelectedURIs(GMFGEN_FILE_EXTENSION); |
148 |
for (int i=0; i<gmfgenURIs.size(); i++) { |
149 |
URI nextGmfgenURI = gmfgenURIs.get(i); |
150 |
addExtensionMetamodelRootToGmfgen(nextGmfgenURI, instantiated); |
151 |
} |
152 |
} catch (Exception ex) { |
153 |
Plugin.log(ex); |
154 |
} finally { |
155 |
progressMonitor.done(); |
156 |
} |
157 |
} |
158 |
}; |
159 |
|
160 |
try { |
161 |
getContainer().run(false, false, operation); |
162 |
List<IFile> selectedGmfgenFiles = getResourceLocationProvider().getSelectedFiles(GMFGEN_FILE_EXTENSION, false); |
163 |
WizardUtil.selectReveal(getWorkbench(), new StructuredSelection(selectedGmfgenFiles)); |
164 |
if (!selectedGmfgenFiles.isEmpty()) { |
165 |
WizardUtil.openInEditor(getWorkbench(), selectedGmfgenFiles.get(0)); |
166 |
} |
167 |
} catch (InvocationTargetException ex) { |
168 |
return false; |
169 |
} catch (InterruptedException ex) { |
170 |
return false; |
171 |
} catch (PartInitException ex) { |
172 |
Plugin.log(ex); |
173 |
} |
174 |
return true; |
175 |
} |
176 |
|
177 |
protected void addExtensionMetamodelRootToGmfgen(URI nextGmfgenURI, EObject instantiated) throws IOException { |
178 |
Resource gmfgenModel = getResourceSet().createResource(nextGmfgenURI); |
179 |
gmfgenModel.load(getResourceSet().getLoadOptions()); |
180 |
gmfgenModel.getContents().add(instantiated); |
181 |
gmfgenModel.save(getGmfgenResourceSaveOptions()); |
182 |
gmfgenModel.unload(); |
183 |
} |
184 |
|
185 |
} |