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