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