1 | /** |
2 | * Copyright 2007 by SDQ, IPD, University of Karlsruhe, Germany |
3 | * |
4 | * $Id$ |
5 | */ |
6 | package de.uka.ipd.sdq.pcm.core.presentation; |
7 | |
8 | |
9 | import java.util.ArrayList; |
10 | import java.util.Arrays; |
11 | import java.util.Collection; |
12 | import java.util.Collections; |
13 | import java.util.HashMap; |
14 | import java.util.List; |
15 | import java.util.Map; |
16 | import java.util.MissingResourceException; |
17 | import java.util.StringTokenizer; |
18 | |
19 | import org.eclipse.core.resources.IContainer; |
20 | import org.eclipse.core.resources.IFile; |
21 | import org.eclipse.core.resources.IFolder; |
22 | import org.eclipse.core.resources.IProject; |
23 | import org.eclipse.core.resources.IResource; |
24 | import org.eclipse.core.resources.ResourcesPlugin; |
25 | import org.eclipse.core.runtime.IProgressMonitor; |
26 | import org.eclipse.core.runtime.Path; |
27 | import org.eclipse.emf.common.CommonPlugin; |
28 | import org.eclipse.emf.common.util.URI; |
29 | import org.eclipse.emf.ecore.EClass; |
30 | import org.eclipse.emf.ecore.EClassifier; |
31 | import org.eclipse.emf.ecore.EObject; |
32 | import org.eclipse.emf.ecore.resource.Resource; |
33 | import org.eclipse.emf.ecore.resource.ResourceSet; |
34 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
35 | import org.eclipse.emf.ecore.xmi.XMLResource; |
36 | import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; |
37 | import org.eclipse.jface.dialogs.MessageDialog; |
38 | import org.eclipse.jface.viewers.ISelection; |
39 | import org.eclipse.jface.viewers.IStructuredSelection; |
40 | import org.eclipse.jface.viewers.StructuredSelection; |
41 | import org.eclipse.jface.wizard.Wizard; |
42 | import org.eclipse.jface.wizard.WizardPage; |
43 | import org.eclipse.swt.SWT; |
44 | import org.eclipse.swt.events.ModifyEvent; |
45 | import org.eclipse.swt.events.ModifyListener; |
46 | import org.eclipse.swt.layout.GridData; |
47 | import org.eclipse.swt.layout.GridLayout; |
48 | import org.eclipse.swt.widgets.Combo; |
49 | import org.eclipse.swt.widgets.Composite; |
50 | import org.eclipse.swt.widgets.Label; |
51 | import org.eclipse.ui.INewWizard; |
52 | import org.eclipse.ui.IWorkbench; |
53 | import org.eclipse.ui.IWorkbenchPage; |
54 | import org.eclipse.ui.IWorkbenchPart; |
55 | import org.eclipse.ui.IWorkbenchWindow; |
56 | import org.eclipse.ui.PartInitException; |
57 | import org.eclipse.ui.actions.WorkspaceModifyOperation; |
58 | import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
59 | import org.eclipse.ui.part.FileEditorInput; |
60 | import org.eclipse.ui.part.ISetSelectionTarget; |
61 | |
62 | import de.uka.ipd.sdq.pcm.core.CoreFactory; |
63 | import de.uka.ipd.sdq.pcm.core.CorePackage; |
64 | import de.uka.ipd.sdq.pcm.core.provider.PalladioComponentModelEditPlugin; |
65 | |
66 | |
67 | /** |
68 | * This is a simple wizard for creating a new model file. |
69 | * <!-- begin-user-doc --> |
70 | * <!-- end-user-doc --> |
71 | * @generated |
72 | */ |
73 | public class CoreModelWizard extends Wizard implements INewWizard { |
74 | /** |
75 | * <!-- begin-user-doc --> |
76 | * <!-- end-user-doc --> |
77 | * @generated |
78 | */ |
79 | public static final String copyright = "Copyright 2005-2009 by SDQ, IPD, University of Karlsruhe, Germany"; |
80 | |
81 | /** |
82 | * The supported extensions for created files. |
83 | * <!-- begin-user-doc --> |
84 | * <!-- end-user-doc --> |
85 | * @generated |
86 | */ |
87 | public static final List<String> FILE_EXTENSIONS = |
88 | Collections.unmodifiableList(Arrays.asList(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CoreEditorFilenameExtensions").split("\\s*,\\s*"))); |
89 | |
90 | /** |
91 | * A formatted list of supported file extensions, suitable for display. |
92 | * <!-- begin-user-doc --> |
93 | * <!-- end-user-doc --> |
94 | * @generated |
95 | */ |
96 | public static final String FORMATTED_FILE_EXTENSIONS = |
97 | PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CoreEditorFilenameExtensions").replaceAll("\\s*,\\s*", ", "); |
98 | |
99 | /** |
100 | * This caches an instance of the model package. |
101 | * <!-- begin-user-doc --> |
102 | * <!-- end-user-doc --> |
103 | * @generated |
104 | */ |
105 | protected CorePackage corePackage = CorePackage.eINSTANCE; |
106 | |
107 | /** |
108 | * This caches an instance of the model factory. |
109 | * <!-- begin-user-doc --> |
110 | * <!-- end-user-doc --> |
111 | * @generated |
112 | */ |
113 | protected CoreFactory coreFactory = corePackage.getCoreFactory(); |
114 | |
115 | /** |
116 | * This is the file creation page. |
117 | * <!-- begin-user-doc --> |
118 | * <!-- end-user-doc --> |
119 | * @generated |
120 | */ |
121 | protected CoreModelWizardNewFileCreationPage newFileCreationPage; |
122 | |
123 | /** |
124 | * This is the initial object creation page. |
125 | * <!-- begin-user-doc --> |
126 | * <!-- end-user-doc --> |
127 | * @generated |
128 | */ |
129 | protected CoreModelWizardInitialObjectCreationPage initialObjectCreationPage; |
130 | |
131 | /** |
132 | * Remember the selection during initialization for populating the default container. |
133 | * <!-- begin-user-doc --> |
134 | * <!-- end-user-doc --> |
135 | * @generated |
136 | */ |
137 | protected IStructuredSelection selection; |
138 | |
139 | /** |
140 | * Remember the workbench during initialization. |
141 | * <!-- begin-user-doc --> |
142 | * <!-- end-user-doc --> |
143 | * @generated |
144 | */ |
145 | protected IWorkbench workbench; |
146 | |
147 | /** |
148 | * Caches the names of the types that can be created as the root object. |
149 | * <!-- begin-user-doc --> |
150 | * <!-- end-user-doc --> |
151 | * @generated |
152 | */ |
153 | protected List<String> initialObjectNames; |
154 | |
155 | /** |
156 | * This just records the information. |
157 | * <!-- begin-user-doc --> |
158 | * <!-- end-user-doc --> |
159 | * @generated |
160 | */ |
161 | public void init(IWorkbench workbench, IStructuredSelection selection) { |
162 | this.workbench = workbench; |
163 | this.selection = selection; |
164 | setWindowTitle(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_Wizard_label")); |
165 | setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(PalladioComponentModelEditorPlugin.INSTANCE.getImage("full/wizban/NewCore"))); |
166 | } |
167 | |
168 | /** |
169 | * Returns the names of the types that can be created as the root object. |
170 | * <!-- begin-user-doc --> |
171 | * <!-- end-user-doc --> |
172 | * @generated |
173 | */ |
174 | protected Collection<String> getInitialObjectNames() { |
175 | if (initialObjectNames == null) { |
176 | initialObjectNames = new ArrayList<String>(); |
177 | for (EClassifier eClassifier : corePackage.getEClassifiers()) { |
178 | if (eClassifier instanceof EClass) { |
179 | EClass eClass = (EClass)eClassifier; |
180 | if (!eClass.isAbstract()) { |
181 | initialObjectNames.add(eClass.getName()); |
182 | } |
183 | } |
184 | } |
185 | Collections.sort(initialObjectNames, CommonPlugin.INSTANCE.getComparator()); |
186 | } |
187 | return initialObjectNames; |
188 | } |
189 | |
190 | /** |
191 | * Create a new model. |
192 | * <!-- begin-user-doc --> |
193 | * <!-- end-user-doc --> |
194 | * @generated |
195 | */ |
196 | protected EObject createInitialModel() { |
197 | EClass eClass = (EClass)corePackage.getEClassifier(initialObjectCreationPage.getInitialObjectName()); |
198 | EObject rootObject = coreFactory.create(eClass); |
199 | return rootObject; |
200 | } |
201 | |
202 | /** |
203 | * Do the work after everything is specified. |
204 | * <!-- begin-user-doc --> |
205 | * <!-- end-user-doc --> |
206 | * @generated |
207 | */ |
208 | @Override |
209 | public boolean performFinish() { |
210 | try { |
211 | // Remember the file. |
212 | // |
213 | final IFile modelFile = getModelFile(); |
214 | |
215 | // Do the work within an operation. |
216 | // |
217 | WorkspaceModifyOperation operation = |
218 | new WorkspaceModifyOperation() { |
219 | @Override |
220 | protected void execute(IProgressMonitor progressMonitor) { |
221 | try { |
222 | // Create a resource set |
223 | // |
224 | ResourceSet resourceSet = new ResourceSetImpl(); |
225 | |
226 | // Get the URI of the model file. |
227 | // |
228 | URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true); |
229 | |
230 | // Create a resource for this file. |
231 | // |
232 | Resource resource = resourceSet.createResource(fileURI); |
233 | |
234 | // Add the initial model object to the contents. |
235 | // |
236 | EObject rootObject = createInitialModel(); |
237 | if (rootObject != null) { |
238 | resource.getContents().add(rootObject); |
239 | } |
240 | |
241 | // Save the contents of the resource to the file system. |
242 | // |
243 | Map<Object, Object> options = new HashMap<Object, Object>(); |
244 | options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding()); |
245 | resource.save(options); |
246 | } |
247 | catch (Exception exception) { |
248 | PalladioComponentModelEditorPlugin.INSTANCE.log(exception); |
249 | } |
250 | finally { |
251 | progressMonitor.done(); |
252 | } |
253 | } |
254 | }; |
255 | |
256 | getContainer().run(false, false, operation); |
257 | |
258 | // Select the new file resource in the current view. |
259 | // |
260 | IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); |
261 | IWorkbenchPage page = workbenchWindow.getActivePage(); |
262 | final IWorkbenchPart activePart = page.getActivePart(); |
263 | if (activePart instanceof ISetSelectionTarget) { |
264 | final ISelection targetSelection = new StructuredSelection(modelFile); |
265 | getShell().getDisplay().asyncExec |
266 | (new Runnable() { |
267 | public void run() { |
268 | ((ISetSelectionTarget)activePart).selectReveal(targetSelection); |
269 | } |
270 | }); |
271 | } |
272 | |
273 | // Open an editor on the new file. |
274 | // |
275 | try { |
276 | page.openEditor |
277 | (new FileEditorInput(modelFile), |
278 | workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId()); |
279 | } |
280 | catch (PartInitException exception) { |
281 | MessageDialog.openError(workbenchWindow.getShell(), PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); |
282 | return false; |
283 | } |
284 | |
285 | return true; |
286 | } |
287 | catch (Exception exception) { |
288 | PalladioComponentModelEditorPlugin.INSTANCE.log(exception); |
289 | return false; |
290 | } |
291 | } |
292 | |
293 | /** |
294 | * This is the one page of the wizard. |
295 | * <!-- begin-user-doc --> |
296 | * <!-- end-user-doc --> |
297 | * @generated |
298 | */ |
299 | public class CoreModelWizardNewFileCreationPage extends WizardNewFileCreationPage { |
300 | /** |
301 | * Pass in the selection. |
302 | * <!-- begin-user-doc --> |
303 | * <!-- end-user-doc --> |
304 | * @generated |
305 | */ |
306 | public CoreModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) { |
307 | super(pageId, selection); |
308 | } |
309 | |
310 | /** |
311 | * The framework calls this to see if the file is correct. |
312 | * <!-- begin-user-doc --> |
313 | * <!-- end-user-doc --> |
314 | * @generated |
315 | */ |
316 | @Override |
317 | protected boolean validatePage() { |
318 | if (super.validatePage()) { |
319 | String extension = new Path(getFileName()).getFileExtension(); |
320 | if (extension == null || !FILE_EXTENSIONS.contains(extension)) { |
321 | String key = FILE_EXTENSIONS.size() > 1 ? "_WARN_FilenameExtensions" : "_WARN_FilenameExtension"; |
322 | setErrorMessage(PalladioComponentModelEditorPlugin.INSTANCE.getString(key, new Object [] { FORMATTED_FILE_EXTENSIONS })); |
323 | return false; |
324 | } |
325 | return true; |
326 | } |
327 | return false; |
328 | } |
329 | |
330 | /** |
331 | * <!-- begin-user-doc --> |
332 | * <!-- end-user-doc --> |
333 | * @generated |
334 | */ |
335 | public IFile getModelFile() { |
336 | return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())); |
337 | } |
338 | } |
339 | |
340 | /** |
341 | * This is the page where the type of object to create is selected. |
342 | * <!-- begin-user-doc --> |
343 | * <!-- end-user-doc --> |
344 | * @generated |
345 | */ |
346 | public class CoreModelWizardInitialObjectCreationPage extends WizardPage { |
347 | /** |
348 | * <!-- begin-user-doc --> |
349 | * <!-- end-user-doc --> |
350 | * @generated |
351 | */ |
352 | protected Combo initialObjectField; |
353 | |
354 | /** |
355 | * @generated |
356 | * <!-- begin-user-doc --> |
357 | * <!-- end-user-doc --> |
358 | */ |
359 | protected List<String> encodings; |
360 | |
361 | /** |
362 | * <!-- begin-user-doc --> |
363 | * <!-- end-user-doc --> |
364 | * @generated |
365 | */ |
366 | protected Combo encodingField; |
367 | |
368 | /** |
369 | * Pass in the selection. |
370 | * <!-- begin-user-doc --> |
371 | * <!-- end-user-doc --> |
372 | * @generated |
373 | */ |
374 | public CoreModelWizardInitialObjectCreationPage(String pageId) { |
375 | super(pageId); |
376 | } |
377 | |
378 | /** |
379 | * <!-- begin-user-doc --> |
380 | * <!-- end-user-doc --> |
381 | * @generated |
382 | */ |
383 | public void createControl(Composite parent) { |
384 | Composite composite = new Composite(parent, SWT.NONE); { |
385 | GridLayout layout = new GridLayout(); |
386 | layout.numColumns = 1; |
387 | layout.verticalSpacing = 12; |
388 | composite.setLayout(layout); |
389 | |
390 | GridData data = new GridData(); |
391 | data.verticalAlignment = GridData.FILL; |
392 | data.grabExcessVerticalSpace = true; |
393 | data.horizontalAlignment = GridData.FILL; |
394 | composite.setLayoutData(data); |
395 | } |
396 | |
397 | Label containerLabel = new Label(composite, SWT.LEFT); |
398 | { |
399 | containerLabel.setText(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_ModelObject")); |
400 | |
401 | GridData data = new GridData(); |
402 | data.horizontalAlignment = GridData.FILL; |
403 | containerLabel.setLayoutData(data); |
404 | } |
405 | |
406 | initialObjectField = new Combo(composite, SWT.BORDER); |
407 | { |
408 | GridData data = new GridData(); |
409 | data.horizontalAlignment = GridData.FILL; |
410 | data.grabExcessHorizontalSpace = true; |
411 | initialObjectField.setLayoutData(data); |
412 | } |
413 | |
414 | for (String objectName : getInitialObjectNames()) { |
415 | initialObjectField.add(getLabel(objectName)); |
416 | } |
417 | |
418 | if (initialObjectField.getItemCount() == 1) { |
419 | initialObjectField.select(0); |
420 | } |
421 | initialObjectField.addModifyListener(validator); |
422 | |
423 | Label encodingLabel = new Label(composite, SWT.LEFT); |
424 | { |
425 | encodingLabel.setText(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_XMLEncoding")); |
426 | |
427 | GridData data = new GridData(); |
428 | data.horizontalAlignment = GridData.FILL; |
429 | encodingLabel.setLayoutData(data); |
430 | } |
431 | encodingField = new Combo(composite, SWT.BORDER); |
432 | { |
433 | GridData data = new GridData(); |
434 | data.horizontalAlignment = GridData.FILL; |
435 | data.grabExcessHorizontalSpace = true; |
436 | encodingField.setLayoutData(data); |
437 | } |
438 | |
439 | for (String encoding : getEncodings()) { |
440 | encodingField.add(encoding); |
441 | } |
442 | |
443 | encodingField.select(0); |
444 | encodingField.addModifyListener(validator); |
445 | |
446 | setPageComplete(validatePage()); |
447 | setControl(composite); |
448 | } |
449 | |
450 | /** |
451 | * <!-- begin-user-doc --> |
452 | * <!-- end-user-doc --> |
453 | * @generated |
454 | */ |
455 | protected ModifyListener validator = |
456 | new ModifyListener() { |
457 | public void modifyText(ModifyEvent e) { |
458 | setPageComplete(validatePage()); |
459 | } |
460 | }; |
461 | |
462 | /** |
463 | * <!-- begin-user-doc --> |
464 | * <!-- end-user-doc --> |
465 | * @generated |
466 | */ |
467 | protected boolean validatePage() { |
468 | return getInitialObjectName() != null && getEncodings().contains(encodingField.getText()); |
469 | } |
470 | |
471 | /** |
472 | * <!-- begin-user-doc --> |
473 | * <!-- end-user-doc --> |
474 | * @generated |
475 | */ |
476 | @Override |
477 | public void setVisible(boolean visible) { |
478 | super.setVisible(visible); |
479 | if (visible) { |
480 | if (initialObjectField.getItemCount() == 1) { |
481 | initialObjectField.clearSelection(); |
482 | encodingField.setFocus(); |
483 | } |
484 | else { |
485 | encodingField.clearSelection(); |
486 | initialObjectField.setFocus(); |
487 | } |
488 | } |
489 | } |
490 | |
491 | /** |
492 | * <!-- begin-user-doc --> |
493 | * <!-- end-user-doc --> |
494 | * @generated |
495 | */ |
496 | public String getInitialObjectName() { |
497 | String label = initialObjectField.getText(); |
498 | |
499 | for (String name : getInitialObjectNames()) { |
500 | if (getLabel(name).equals(label)) { |
501 | return name; |
502 | } |
503 | } |
504 | return null; |
505 | } |
506 | |
507 | /** |
508 | * <!-- begin-user-doc --> |
509 | * <!-- end-user-doc --> |
510 | * @generated |
511 | */ |
512 | public String getEncoding() { |
513 | return encodingField.getText(); |
514 | } |
515 | |
516 | /** |
517 | * Returns the label for the specified type name. |
518 | * <!-- begin-user-doc --> |
519 | * <!-- end-user-doc --> |
520 | * @generated |
521 | */ |
522 | protected String getLabel(String typeName) { |
523 | try { |
524 | return PalladioComponentModelEditPlugin.INSTANCE.getString("_UI_" + typeName + "_type"); |
525 | } |
526 | catch(MissingResourceException mre) { |
527 | PalladioComponentModelEditorPlugin.INSTANCE.log(mre); |
528 | } |
529 | return typeName; |
530 | } |
531 | |
532 | /** |
533 | * <!-- begin-user-doc --> |
534 | * <!-- end-user-doc --> |
535 | * @generated |
536 | */ |
537 | protected Collection<String> getEncodings() { |
538 | if (encodings == null) { |
539 | encodings = new ArrayList<String>(); |
540 | for (StringTokenizer stringTokenizer = new StringTokenizer(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) { |
541 | encodings.add(stringTokenizer.nextToken()); |
542 | } |
543 | } |
544 | return encodings; |
545 | } |
546 | } |
547 | |
548 | /** |
549 | * The framework calls this to create the contents of the wizard. |
550 | * <!-- begin-user-doc --> |
551 | * <!-- end-user-doc --> |
552 | * @generated |
553 | */ |
554 | @Override |
555 | public void addPages() { |
556 | // Create a page, set the title, and the initial model file name. |
557 | // |
558 | newFileCreationPage = new CoreModelWizardNewFileCreationPage("Whatever", selection); |
559 | newFileCreationPage.setTitle(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CoreModelWizard_label")); |
560 | newFileCreationPage.setDescription(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CoreModelWizard_description")); |
561 | newFileCreationPage.setFileName(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CoreEditorFilenameDefaultBase") + "." + FILE_EXTENSIONS.get(0)); |
562 | addPage(newFileCreationPage); |
563 | |
564 | // Try and get the resource selection to determine a current directory for the file dialog. |
565 | // |
566 | if (selection != null && !selection.isEmpty()) { |
567 | // Get the resource... |
568 | // |
569 | Object selectedElement = selection.iterator().next(); |
570 | if (selectedElement instanceof IResource) { |
571 | // Get the resource parent, if its a file. |
572 | // |
573 | IResource selectedResource = (IResource)selectedElement; |
574 | if (selectedResource.getType() == IResource.FILE) { |
575 | selectedResource = selectedResource.getParent(); |
576 | } |
577 | |
578 | // This gives us a directory... |
579 | // |
580 | if (selectedResource instanceof IFolder || selectedResource instanceof IProject) { |
581 | // Set this for the container. |
582 | // |
583 | newFileCreationPage.setContainerFullPath(selectedResource.getFullPath()); |
584 | |
585 | // Make up a unique new name here. |
586 | // |
587 | String defaultModelBaseFilename = PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CoreEditorFilenameDefaultBase"); |
588 | String defaultModelFilenameExtension = FILE_EXTENSIONS.get(0); |
589 | String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; |
590 | for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) { |
591 | modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; |
592 | } |
593 | newFileCreationPage.setFileName(modelFilename); |
594 | } |
595 | } |
596 | } |
597 | initialObjectCreationPage = new CoreModelWizardInitialObjectCreationPage("Whatever2"); |
598 | initialObjectCreationPage.setTitle(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_CoreModelWizard_label")); |
599 | initialObjectCreationPage.setDescription(PalladioComponentModelEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description")); |
600 | addPage(initialObjectCreationPage); |
601 | } |
602 | |
603 | /** |
604 | * Get the file from the page. |
605 | * <!-- begin-user-doc --> |
606 | * <!-- end-user-doc --> |
607 | * @generated |
608 | */ |
609 | public IFile getModelFile() { |
610 | return newFileCreationPage.getModelFile(); |
611 | } |
612 | |
613 | } |