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