|
Lines 32-37
Link Here
|
| 32 |
import org.eclipse.papyrus.core.utils.DiResourceSet; |
32 |
import org.eclipse.papyrus.core.utils.DiResourceSet; |
| 33 |
import org.eclipse.papyrus.core.utils.EditorUtils; |
33 |
import org.eclipse.papyrus.core.utils.EditorUtils; |
| 34 |
import org.eclipse.papyrus.core.utils.PapyrusTrace; |
34 |
import org.eclipse.papyrus.core.utils.PapyrusTrace; |
|
|
35 |
import org.eclipse.papyrus.resource.uml.UmlModel; |
| 35 |
import org.eclipse.ui.INewWizard; |
36 |
import org.eclipse.ui.INewWizard; |
| 36 |
import org.eclipse.ui.IWorkbench; |
37 |
import org.eclipse.ui.IWorkbench; |
| 37 |
import org.eclipse.ui.IWorkbenchPage; |
38 |
import org.eclipse.ui.IWorkbenchPage; |
|
Lines 117-140
Link Here
|
| 117 |
public void init(IWorkbench workbench, IStructuredSelection selection) { |
118 |
public void init(IWorkbench workbench, IStructuredSelection selection) { |
| 118 |
this.workbench = workbench; |
119 |
this.workbench = workbench; |
| 119 |
this.diResourceSet = new DiResourceSet(); |
120 |
this.diResourceSet = new DiResourceSet(); |
| 120 |
// set the di file name with the selected domain model |
121 |
IFile file = getSelectedFile(selection); |
| 121 |
if(selection != null && !selection.isEmpty()) { |
122 |
// builds a new Papyrus Model for an existing domain model |
| 122 |
if(selection.getFirstElement() instanceof IFile) { |
123 |
if(isSupportedDomainModelFile(file)) { |
| 123 |
IFile file = (IFile)selection.getFirstElement(); |
124 |
this.domainModelURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true); |
| 124 |
String extension = file.getFileExtension(); |
125 |
this.newModelFilePage = new NewModelFilePage("Create a new Papyrus model", "Create a new Papyrus model from an existing semantic model", selection, true); |
| 125 |
if(getModelFileExtension().equals(extension)) { |
126 |
this.newModelFilePage.setFileName(getDiagramFileName(file)); |
| 126 |
this.domainModelURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true); |
|
|
| 127 |
|
127 |
|
| 128 |
this.newModelFilePage = new NewModelFilePage("Create a new Papyrus model", "Create a new Papyrus model from an existing semantic model", selection, true); |
128 |
// I don't understand a need to load the model during initialization, |
| 129 |
String diModelFileName = (file.getLocation().removeFileExtension().lastSegment()); |
129 |
// I rather expect it to be loaded when wizard is finished and executed or |
| 130 |
diModelFileName += ".di"; |
130 |
// when the page that needs the model is initialized |
| 131 |
this.newModelFilePage.setFileName(diModelFileName); |
131 |
Resource resource = diResourceSet.loadModelResource(file); |
| 132 |
|
132 |
|
| 133 |
Resource resource = diResourceSet.loadModelResource(file); |
133 |
EObject diagramRoot = resource.getContents().get(0); |
| 134 |
EObject diagramRoot = resource.getContents().get(0); |
134 |
this.selectRootElementPage = new SelectRootElementPage("Select the root element", diagramRoot); |
| 135 |
this.selectRootElementPage = new SelectRootElementPage("Select the root element", diagramRoot); |
|
|
| 136 |
} |
| 137 |
} |
| 138 |
} |
135 |
} |
| 139 |
if(domainModelURI == null) { |
136 |
if(domainModelURI == null) { |
| 140 |
this.newModelFilePage = new NewModelFilePage("Create a new Papyrus model", "Create a new empty Papyrus model", selection, false); |
137 |
this.newModelFilePage = new NewModelFilePage("Create a new Papyrus model", "Create a new empty Papyrus model", selection, false); |
|
Lines 143-150
Link Here
|
| 143 |
// fjcano #293135 :: support model templates |
140 |
// fjcano #293135 :: support model templates |
| 144 |
selectTemplateWizardPage = new SelectTemplateWizardPage(Activator.PLUGIN_ID, null, null); |
141 |
selectTemplateWizardPage = new SelectTemplateWizardPage(Activator.PLUGIN_ID, null, null); |
| 145 |
} |
142 |
} |
|
|
143 |
|
| 144 |
/** |
| 145 |
* Suggests a name of diagram file for the domain model file |
| 146 |
*/ |
| 147 |
protected String getDiagramFileName(IFile domainModel) { |
| 148 |
String diModelFileName = (domainModel.getLocation().removeFileExtension().lastSegment()); |
| 149 |
diModelFileName += ".di"; |
| 150 |
return diModelFileName; |
| 151 |
} |
| 146 |
|
152 |
|
| 147 |
/** |
153 |
/** |
|
|
154 |
* Returns the first file from the given selection |
| 155 |
*/ |
| 156 |
private IFile getSelectedFile(IStructuredSelection selection) { |
| 157 |
if(selection != null && !selection.isEmpty() && selection.getFirstElement() instanceof IFile) { |
| 158 |
return (IFile)selection.getFirstElement(); |
| 159 |
} |
| 160 |
return null; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Returns true is the file can be served as a model model for the diagram |
| 165 |
*/ |
| 166 |
protected boolean isSupportedDomainModelFile(IFile file) { |
| 167 |
return file != null && getModelFileExtension().equals(file.getFileExtension()); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 148 |
* This method will be invoked, when the "Finish" button is pressed. |
172 |
* This method will be invoked, when the "Finish" button is pressed. |
| 149 |
* |
173 |
* |
| 150 |
* @return <code>true</code> if everything runs without problems, <code>false</code> if an exception must be caught. |
174 |
* @return <code>true</code> if everything runs without problems, <code>false</code> if an exception must be caught. |
|
Lines 155-161
Link Here
|
| 155 |
try { |
179 |
try { |
| 156 |
// create a new file, result != null if successful |
180 |
// create a new file, result != null if successful |
| 157 |
final IFile newFile = newModelFilePage.createNewFile(); |
181 |
final IFile newFile = newModelFilePage.createNewFile(); |
| 158 |
NewModelFilePage.fileCount++; |
|
|
| 159 |
|
182 |
|
| 160 |
RecordingCommand command = new RecordingCommand(diResourceSet.getTransactionalEditingDomain()) { |
183 |
RecordingCommand command = new RecordingCommand(diResourceSet.getTransactionalEditingDomain()) { |
| 161 |
|
184 |
|
|
Lines 197-208
Link Here
|
| 197 |
// SashSystem. |
220 |
// SashSystem. |
| 198 |
EditorUtils.getTransactionalIPageMngr(diResourceSet.getDiResource(), diResourceSet.getTransactionalEditingDomain()); |
221 |
EditorUtils.getTransactionalIPageMngr(diResourceSet.getDiResource(), diResourceSet.getTransactionalEditingDomain()); |
| 199 |
} else { |
222 |
} else { |
| 200 |
// Create requested diagram. |
223 |
EObject root = domainModelURI != null ? selectRootElementPage.getModelElement() : null; |
| 201 |
if(domainModelURI != null) { |
224 |
creationCommand.createDiagram(diResourceSet, root, diagramName); |
| 202 |
creationCommand.createDiagram(diResourceSet, selectRootElementPage.getModelElement(), diagramName); |
|
|
| 203 |
} else { |
| 204 |
creationCommand.createDiagram(diResourceSet, null, diagramName); |
| 205 |
} |
| 206 |
} |
225 |
} |
| 207 |
try { |
226 |
try { |
| 208 |
diResourceSet.save(new NullProgressMonitor()); |
227 |
diResourceSet.save(new NullProgressMonitor()); |
|
Lines 231-277
Link Here
|
| 231 |
protected abstract String getModelFileExtension(); |
250 |
protected abstract String getModelFileExtension(); |
| 232 |
|
251 |
|
| 233 |
// fjcano #293135 :: support model templates |
252 |
// fjcano #293135 :: support model templates |
| 234 |
protected void initializeModelResource(Resource resource, String rootElementName) { |
253 |
private void initializeModelResource(Resource resource, String rootElementName) { |
| 235 |
if(isInitializeFromTemplate()) { |
254 |
String templatePath = selectTemplateWizardPage.getTemplatePath(); |
| 236 |
Resource templateResource = loadTemplateResource(); |
255 |
boolean initializeFromTemplate = templatePath != null; |
| 237 |
List<EObject> eObjectsToAdd = new ArrayList<EObject>(); |
256 |
if(initializeFromTemplate) { |
| 238 |
for(EObject eObject : templateResource.getContents()) { |
257 |
initializeFromTemplate(resource, rootElementName, templatePath); |
| 239 |
eObjectsToAdd.add(EcoreUtil.copy(eObject)); |
|
|
| 240 |
} |
| 241 |
for(EObject eObject : eObjectsToAdd) { |
| 242 |
resource.getContents().add(eObject); |
| 243 |
} |
| 244 |
} else { |
258 |
} else { |
| 245 |
// nothing |
259 |
initializeEmptyModel(resource, rootElementName); |
| 246 |
return; |
|
|
| 247 |
} |
260 |
} |
| 248 |
} |
261 |
} |
| 249 |
|
262 |
|
| 250 |
// fjcano #293135 :: support model templates |
263 |
protected void initializeFromTemplate(Resource resource, String rootElementName, String templatePath) { |
| 251 |
protected boolean isInitializeFromTemplate() { |
264 |
Resource templateResource = loadTemplateResource(templatePath); |
| 252 |
return getTemplatePath() != null; |
265 |
List<EObject> eObjectsToAdd = new ArrayList<EObject>(); |
|
|
266 |
for(EObject eObject : templateResource.getContents()) { |
| 267 |
eObjectsToAdd.add(EcoreUtil.copy(eObject)); |
| 268 |
} |
| 269 |
for(EObject eObject : eObjectsToAdd) { |
| 270 |
resource.getContents().add(eObject); |
| 271 |
} |
| 253 |
} |
272 |
} |
| 254 |
|
273 |
|
| 255 |
// fjcano #293135 :: support model templates |
274 |
protected void initializeEmptyModel(Resource resource, String rootElementName) { |
| 256 |
protected String getTemplatePath() { |
|
|
| 257 |
String templatePath = selectTemplateWizardPage.getTemplatePath(); |
| 258 |
return templatePath; |
| 259 |
} |
275 |
} |
| 260 |
|
276 |
|
| 261 |
// fjcano #293135 :: support model templates |
277 |
private Resource loadTemplateResource(String templatePath) { |
| 262 |
protected Resource loadTemplateResource() { |
|
|
| 263 |
String templatePluginID = selectTemplateWizardPage.getTemplatePluginId(); |
278 |
String templatePluginID = selectTemplateWizardPage.getTemplatePluginId(); |
| 264 |
String templatePath = getTemplatePath(); |
|
|
| 265 |
java.net.URL templateURL = Platform.getBundle(templatePluginID).getResource(templatePath); |
279 |
java.net.URL templateURL = Platform.getBundle(templatePluginID).getResource(templatePath); |
| 266 |
if(templatePath != null) { |
280 |
String fullUri = templateURL.getPath(); |
| 267 |
String fullUri = templateURL.getPath(); |
281 |
URI uri = URI.createPlatformPluginURI(templatePluginID + fullUri, true); |
| 268 |
URI uri = URI.createPlatformPluginURI(templatePluginID + fullUri, true); |
282 |
ResourceSet resourceSet = new ResourceSetImpl(); |
| 269 |
ResourceSet resourceSet = new ResourceSetImpl(); |
283 |
Resource resource = resourceSet.getResource(uri, true); |
| 270 |
Resource resource = resourceSet.getResource(uri, true); |
284 |
if(resource.isLoaded()) { |
| 271 |
if(resource.isLoaded()) { |
285 |
return resource; |
| 272 |
return resource; |
|
|
| 273 |
} |
| 274 |
} |
286 |
} |
| 275 |
return null; |
287 |
return null; |
| 276 |
} |
288 |
} |
|
|
289 |
|
| 290 |
|
| 277 |
} |
291 |
} |