1 | /** |
2 | * <copyright> |
3 | * </copyright> |
4 | * |
5 | * $Id$ |
6 | */ |
7 | package de.uka.ipd.sdq.sensitivity.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.sensitivity.SensitivityFactory; |
77 | import de.uka.ipd.sdq.sensitivity.SensitivityPackage; |
78 | import de.uka.ipd.sdq.sensitivity.provider.SensitivityEditPlugin; |
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 SensitivityModelWizard 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(SensitivityEditorPlugin.INSTANCE.getString("_UI_SensitivityEditorFilenameExtensions").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 | SensitivityEditorPlugin.INSTANCE.getString("_UI_SensitivityEditorFilenameExtensions").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 SensitivityPackage sensitivityPackage = SensitivityPackage.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 SensitivityFactory sensitivityFactory = sensitivityPackage.getSensitivityFactory(); |
132 | |
133 | /** |
134 | * This is the file creation page. |
135 | * <!-- begin-user-doc --> |
136 | * <!-- end-user-doc --> |
137 | * @generated |
138 | */ |
139 | protected SensitivityModelWizardNewFileCreationPage newFileCreationPage; |
140 | |
141 | /** |
142 | * This is the initial object creation page. |
143 | * <!-- begin-user-doc --> |
144 | * <!-- end-user-doc --> |
145 | * @generated |
146 | */ |
147 | protected SensitivityModelWizardInitialObjectCreationPage 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(SensitivityEditorPlugin.INSTANCE.getString("_UI_Wizard_label")); |
183 | setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(SensitivityEditorPlugin.INSTANCE.getImage("full/wizban/NewSensitivity"))); |
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 : sensitivityPackage.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)sensitivityPackage.getEClassifier(initialObjectCreationPage.getInitialObjectName()); |
216 | EObject rootObject = sensitivityFactory.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 | SensitivityEditorPlugin.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(), SensitivityEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); |
300 | return false; |
301 | } |
302 | |
303 | return true; |
304 | } |
305 | catch (Exception exception) { |
306 | SensitivityEditorPlugin.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 SensitivityModelWizardNewFileCreationPage extends WizardNewFileCreationPage { |
318 | /** |
319 | * Pass in the selection. |
320 | * <!-- begin-user-doc --> |
321 | * <!-- end-user-doc --> |
322 | * @generated |
323 | */ |
324 | public SensitivityModelWizardNewFileCreationPage(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(SensitivityEditorPlugin.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 SensitivityModelWizardInitialObjectCreationPage 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 SensitivityModelWizardInitialObjectCreationPage(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 | GridLayout layout = new GridLayout(); |
404 | layout.numColumns = 1; |
405 | layout.verticalSpacing = 12; |
406 | composite.setLayout(layout); |
407 | |
408 | GridData data = new GridData(); |
409 | data.verticalAlignment = GridData.FILL; |
410 | data.grabExcessVerticalSpace = true; |
411 | data.horizontalAlignment = GridData.FILL; |
412 | composite.setLayoutData(data); |
413 | } |
414 | |
415 | Label containerLabel = new Label(composite, SWT.LEFT); |
416 | { |
417 | containerLabel.setText(SensitivityEditorPlugin.INSTANCE.getString("_UI_ModelObject")); |
418 | |
419 | GridData data = new GridData(); |
420 | data.horizontalAlignment = GridData.FILL; |
421 | containerLabel.setLayoutData(data); |
422 | } |
423 | |
424 | initialObjectField = new Combo(composite, SWT.BORDER); |
425 | { |
426 | GridData data = new GridData(); |
427 | data.horizontalAlignment = GridData.FILL; |
428 | data.grabExcessHorizontalSpace = true; |
429 | initialObjectField.setLayoutData(data); |
430 | } |
431 | |
432 | for (String objectName : getInitialObjectNames()) { |
433 | initialObjectField.add(getLabel(objectName)); |
434 | } |
435 | |
436 | if (initialObjectField.getItemCount() == 1) { |
437 | initialObjectField.select(0); |
438 | } |
439 | initialObjectField.addModifyListener(validator); |
440 | |
441 | Label encodingLabel = new Label(composite, SWT.LEFT); |
442 | { |
443 | encodingLabel.setText(SensitivityEditorPlugin.INSTANCE.getString("_UI_XMLEncoding")); |
444 | |
445 | GridData data = new GridData(); |
446 | data.horizontalAlignment = GridData.FILL; |
447 | encodingLabel.setLayoutData(data); |
448 | } |
449 | encodingField = new Combo(composite, SWT.BORDER); |
450 | { |
451 | GridData data = new GridData(); |
452 | data.horizontalAlignment = GridData.FILL; |
453 | data.grabExcessHorizontalSpace = true; |
454 | encodingField.setLayoutData(data); |
455 | } |
456 | |
457 | for (String encoding : getEncodings()) { |
458 | encodingField.add(encoding); |
459 | } |
460 | |
461 | encodingField.select(0); |
462 | encodingField.addModifyListener(validator); |
463 | |
464 | setPageComplete(validatePage()); |
465 | setControl(composite); |
466 | } |
467 | |
468 | /** |
469 | * <!-- begin-user-doc --> |
470 | * <!-- end-user-doc --> |
471 | * @generated |
472 | */ |
473 | protected ModifyListener validator = |
474 | new ModifyListener() { |
475 | public void modifyText(ModifyEvent e) { |
476 | setPageComplete(validatePage()); |
477 | } |
478 | }; |
479 | |
480 | /** |
481 | * <!-- begin-user-doc --> |
482 | * <!-- end-user-doc --> |
483 | * @generated |
484 | */ |
485 | protected boolean validatePage() { |
486 | return getInitialObjectName() != null && getEncodings().contains(encodingField.getText()); |
487 | } |
488 | |
489 | /** |
490 | * <!-- begin-user-doc --> |
491 | * <!-- end-user-doc --> |
492 | * @generated |
493 | */ |
494 | @Override |
495 | public void setVisible(boolean visible) { |
496 | super.setVisible(visible); |
497 | if (visible) { |
498 | if (initialObjectField.getItemCount() == 1) { |
499 | initialObjectField.clearSelection(); |
500 | encodingField.setFocus(); |
501 | } |
502 | else { |
503 | encodingField.clearSelection(); |
504 | initialObjectField.setFocus(); |
505 | } |
506 | } |
507 | } |
508 | |
509 | /** |
510 | * <!-- begin-user-doc --> |
511 | * <!-- end-user-doc --> |
512 | * @generated |
513 | */ |
514 | public String getInitialObjectName() { |
515 | String label = initialObjectField.getText(); |
516 | |
517 | for (String name : getInitialObjectNames()) { |
518 | if (getLabel(name).equals(label)) { |
519 | return name; |
520 | } |
521 | } |
522 | return null; |
523 | } |
524 | |
525 | /** |
526 | * <!-- begin-user-doc --> |
527 | * <!-- end-user-doc --> |
528 | * @generated |
529 | */ |
530 | public String getEncoding() { |
531 | return encodingField.getText(); |
532 | } |
533 | |
534 | /** |
535 | * Returns the label for the specified type name. |
536 | * <!-- begin-user-doc --> |
537 | * <!-- end-user-doc --> |
538 | * @generated |
539 | */ |
540 | protected String getLabel(String typeName) { |
541 | try { |
542 | return SensitivityEditPlugin.INSTANCE.getString("_UI_" + typeName + "_type"); |
543 | } |
544 | catch(MissingResourceException mre) { |
545 | SensitivityEditorPlugin.INSTANCE.log(mre); |
546 | } |
547 | return typeName; |
548 | } |
549 | |
550 | /** |
551 | * <!-- begin-user-doc --> |
552 | * <!-- end-user-doc --> |
553 | * @generated |
554 | */ |
555 | protected Collection<String> getEncodings() { |
556 | if (encodings == null) { |
557 | encodings = new ArrayList<String>(); |
558 | for (StringTokenizer stringTokenizer = new StringTokenizer(SensitivityEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) { |
559 | encodings.add(stringTokenizer.nextToken()); |
560 | } |
561 | } |
562 | return encodings; |
563 | } |
564 | } |
565 | |
566 | /** |
567 | * The framework calls this to create the contents of the wizard. |
568 | * <!-- begin-user-doc --> |
569 | * <!-- end-user-doc --> |
570 | * @generated |
571 | */ |
572 | @Override |
573 | public void addPages() { |
574 | // Create a page, set the title, and the initial model file name. |
575 | // |
576 | newFileCreationPage = new SensitivityModelWizardNewFileCreationPage("Whatever", selection); |
577 | newFileCreationPage.setTitle(SensitivityEditorPlugin.INSTANCE.getString("_UI_SensitivityModelWizard_label")); |
578 | newFileCreationPage.setDescription(SensitivityEditorPlugin.INSTANCE.getString("_UI_SensitivityModelWizard_description")); |
579 | newFileCreationPage.setFileName(SensitivityEditorPlugin.INSTANCE.getString("_UI_SensitivityEditorFilenameDefaultBase") + "." + FILE_EXTENSIONS.get(0)); |
580 | addPage(newFileCreationPage); |
581 | |
582 | // Try and get the resource selection to determine a current directory for the file dialog. |
583 | // |
584 | if (selection != null && !selection.isEmpty()) { |
585 | // Get the resource... |
586 | // |
587 | Object selectedElement = selection.iterator().next(); |
588 | if (selectedElement instanceof IResource) { |
589 | // Get the resource parent, if its a file. |
590 | // |
591 | IResource selectedResource = (IResource)selectedElement; |
592 | if (selectedResource.getType() == IResource.FILE) { |
593 | selectedResource = selectedResource.getParent(); |
594 | } |
595 | |
596 | // This gives us a directory... |
597 | // |
598 | if (selectedResource instanceof IFolder || selectedResource instanceof IProject) { |
599 | // Set this for the container. |
600 | // |
601 | newFileCreationPage.setContainerFullPath(selectedResource.getFullPath()); |
602 | |
603 | // Make up a unique new name here. |
604 | // |
605 | String defaultModelBaseFilename = SensitivityEditorPlugin.INSTANCE.getString("_UI_SensitivityEditorFilenameDefaultBase"); |
606 | String defaultModelFilenameExtension = FILE_EXTENSIONS.get(0); |
607 | String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; |
608 | for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) { |
609 | modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; |
610 | } |
611 | newFileCreationPage.setFileName(modelFilename); |
612 | } |
613 | } |
614 | } |
615 | initialObjectCreationPage = new SensitivityModelWizardInitialObjectCreationPage("Whatever2"); |
616 | initialObjectCreationPage.setTitle(SensitivityEditorPlugin.INSTANCE.getString("_UI_SensitivityModelWizard_label")); |
617 | initialObjectCreationPage.setDescription(SensitivityEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description")); |
618 | addPage(initialObjectCreationPage); |
619 | } |
620 | |
621 | /** |
622 | * Get the file from the page. |
623 | * <!-- begin-user-doc --> |
624 | * <!-- end-user-doc --> |
625 | * @generated |
626 | */ |
627 | public IFile getModelFile() { |
628 | return newFileCreationPage.getModelFile(); |
629 | } |
630 | |
631 | } |