1 | /** |
2 | * <copyright> |
3 | * </copyright> |
4 | * |
5 | * $Id$ |
6 | */ |
7 | package de.uka.ipd.sdq.pcm.cost.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.cost.costFactory; |
77 | import de.uka.ipd.sdq.pcm.cost.costPackage; |
78 | import de.uka.ipd.sdq.pcm.cost.provider.CostModel3EditPlugin; |
79 | import de.uka.ipd.sdq.pcm.cost.provider.CostModelEditPlugin; |
80 | |
81 | |
82 | import org.eclipse.core.runtime.Path; |
83 | |
84 | import org.eclipse.jface.viewers.ISelection; |
85 | import org.eclipse.jface.viewers.StructuredSelection; |
86 | |
87 | import org.eclipse.ui.IWorkbenchPage; |
88 | import org.eclipse.ui.IWorkbenchPart; |
89 | import org.eclipse.ui.IWorkbenchWindow; |
90 | import org.eclipse.ui.PartInitException; |
91 | |
92 | |
93 | /** |
94 | * This is a simple wizard for creating a new model file. |
95 | * <!-- begin-user-doc --> |
96 | * <!-- end-user-doc --> |
97 | * @generated |
98 | */ |
99 | public class costModelWizard extends Wizard implements INewWizard { |
100 | /** |
101 | * The supported extensions for created files. |
102 | * <!-- begin-user-doc --> |
103 | * <!-- end-user-doc --> |
104 | * @generated |
105 | */ |
106 | public static final List<String> FILE_EXTENSIONS = |
107 | Collections.unmodifiableList(Arrays.asList(CostModelEditorPlugin.INSTANCE.getString("_UI_costEditorFilenameExtensions").split("\\s*,\\s*"))); |
108 | |
109 | /** |
110 | * A formatted list of supported file extensions, suitable for display. |
111 | * <!-- begin-user-doc --> |
112 | * <!-- end-user-doc --> |
113 | * @generated |
114 | */ |
115 | public static final String FORMATTED_FILE_EXTENSIONS = |
116 | CostModelEditorPlugin.INSTANCE.getString("_UI_costEditorFilenameExtensions").replaceAll("\\s*,\\s*", ", "); |
117 | |
118 | /** |
119 | * This caches an instance of the model package. |
120 | * <!-- begin-user-doc --> |
121 | * <!-- end-user-doc --> |
122 | * @generated |
123 | */ |
124 | protected costPackage _costPackage = costPackage.eINSTANCE; |
125 | |
126 | /** |
127 | * This caches an instance of the model factory. |
128 | * <!-- begin-user-doc --> |
129 | * <!-- end-user-doc --> |
130 | * @generated |
131 | */ |
132 | protected costFactory _costFactory = _costPackage.getcostFactory(); |
133 | |
134 | /** |
135 | * This is the file creation page. |
136 | * <!-- begin-user-doc --> |
137 | * <!-- end-user-doc --> |
138 | * @generated |
139 | */ |
140 | protected costModelWizardNewFileCreationPage newFileCreationPage; |
141 | |
142 | /** |
143 | * This is the initial object creation page. |
144 | * <!-- begin-user-doc --> |
145 | * <!-- end-user-doc --> |
146 | * @generated |
147 | */ |
148 | protected costModelWizardInitialObjectCreationPage initialObjectCreationPage; |
149 | |
150 | /** |
151 | * Remember the selection during initialization for populating the default container. |
152 | * <!-- begin-user-doc --> |
153 | * <!-- end-user-doc --> |
154 | * @generated |
155 | */ |
156 | protected IStructuredSelection selection; |
157 | |
158 | /** |
159 | * Remember the workbench during initialization. |
160 | * <!-- begin-user-doc --> |
161 | * <!-- end-user-doc --> |
162 | * @generated |
163 | */ |
164 | protected IWorkbench workbench; |
165 | |
166 | /** |
167 | * Caches the names of the types that can be created as the root object. |
168 | * <!-- begin-user-doc --> |
169 | * <!-- end-user-doc --> |
170 | * @generated |
171 | */ |
172 | protected List<String> initialObjectNames; |
173 | |
174 | /** |
175 | * This just records the information. |
176 | * <!-- begin-user-doc --> |
177 | * <!-- end-user-doc --> |
178 | * @generated |
179 | */ |
180 | public void init(IWorkbench workbench, IStructuredSelection selection) { |
181 | this.workbench = workbench; |
182 | this.selection = selection; |
183 | setWindowTitle(CostModelEditorPlugin.INSTANCE.getString("_UI_Wizard_label")); |
184 | setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(CostModelEditorPlugin.INSTANCE.getImage("full/wizban/Newcost"))); |
185 | } |
186 | |
187 | /** |
188 | * Returns the names of the types that can be created as the root object. |
189 | * <!-- begin-user-doc --> |
190 | * <!-- end-user-doc --> |
191 | * @generated |
192 | */ |
193 | protected Collection<String> getInitialObjectNames() { |
194 | if (initialObjectNames == null) { |
195 | initialObjectNames = new ArrayList<String>(); |
196 | for (EClassifier eClassifier : _costPackage.getEClassifiers()) { |
197 | if (eClassifier instanceof EClass) { |
198 | EClass eClass = (EClass)eClassifier; |
199 | if (!eClass.isAbstract()) { |
200 | initialObjectNames.add(eClass.getName()); |
201 | } |
202 | } |
203 | } |
204 | Collections.sort(initialObjectNames, CommonPlugin.INSTANCE.getComparator()); |
205 | } |
206 | return initialObjectNames; |
207 | } |
208 | |
209 | /** |
210 | * Create a new model. |
211 | * <!-- begin-user-doc --> |
212 | * <!-- end-user-doc --> |
213 | * @generated |
214 | */ |
215 | protected EObject createInitialModel() { |
216 | EClass eClass = (EClass)_costPackage.getEClassifier(initialObjectCreationPage.getInitialObjectName()); |
217 | EObject rootObject = _costFactory.create(eClass); |
218 | return rootObject; |
219 | } |
220 | |
221 | /** |
222 | * Do the work after everything is specified. |
223 | * <!-- begin-user-doc --> |
224 | * <!-- end-user-doc --> |
225 | * @generated |
226 | */ |
227 | @Override |
228 | public boolean performFinish() { |
229 | try { |
230 | // Remember the file. |
231 | // |
232 | final IFile modelFile = getModelFile(); |
233 | |
234 | // Do the work within an operation. |
235 | // |
236 | WorkspaceModifyOperation operation = |
237 | new WorkspaceModifyOperation() { |
238 | @Override |
239 | protected void execute(IProgressMonitor progressMonitor) { |
240 | try { |
241 | // Create a resource set |
242 | // |
243 | ResourceSet resourceSet = new ResourceSetImpl(); |
244 | |
245 | // Get the URI of the model file. |
246 | // |
247 | URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true); |
248 | |
249 | // Create a resource for this file. |
250 | // |
251 | Resource resource = resourceSet.createResource(fileURI); |
252 | |
253 | // Add the initial model object to the contents. |
254 | // |
255 | EObject rootObject = createInitialModel(); |
256 | if (rootObject != null) { |
257 | resource.getContents().add(rootObject); |
258 | } |
259 | |
260 | // Save the contents of the resource to the file system. |
261 | // |
262 | Map<Object, Object> options = new HashMap<Object, Object>(); |
263 | options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding()); |
264 | resource.save(options); |
265 | } |
266 | catch (Exception exception) { |
267 | CostModelEditorPlugin.INSTANCE.log(exception); |
268 | } |
269 | finally { |
270 | progressMonitor.done(); |
271 | } |
272 | } |
273 | }; |
274 | |
275 | getContainer().run(false, false, operation); |
276 | |
277 | // Select the new file resource in the current view. |
278 | // |
279 | IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); |
280 | IWorkbenchPage page = workbenchWindow.getActivePage(); |
281 | final IWorkbenchPart activePart = page.getActivePart(); |
282 | if (activePart instanceof ISetSelectionTarget) { |
283 | final ISelection targetSelection = new StructuredSelection(modelFile); |
284 | getShell().getDisplay().asyncExec |
285 | (new Runnable() { |
286 | public void run() { |
287 | ((ISetSelectionTarget)activePart).selectReveal(targetSelection); |
288 | } |
289 | }); |
290 | } |
291 | |
292 | // Open an editor on the new file. |
293 | // |
294 | try { |
295 | page.openEditor |
296 | (new FileEditorInput(modelFile), |
297 | workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId()); |
298 | } |
299 | catch (PartInitException exception) { |
300 | MessageDialog.openError(workbenchWindow.getShell(), CostModelEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage()); |
301 | return false; |
302 | } |
303 | |
304 | return true; |
305 | } |
306 | catch (Exception exception) { |
307 | CostModelEditorPlugin.INSTANCE.log(exception); |
308 | return false; |
309 | } |
310 | } |
311 | |
312 | /** |
313 | * This is the one page of the wizard. |
314 | * <!-- begin-user-doc --> |
315 | * <!-- end-user-doc --> |
316 | * @generated |
317 | */ |
318 | public class costModelWizardNewFileCreationPage extends WizardNewFileCreationPage { |
319 | /** |
320 | * Pass in the selection. |
321 | * <!-- begin-user-doc --> |
322 | * <!-- end-user-doc --> |
323 | * @generated |
324 | */ |
325 | public costModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) { |
326 | super(pageId, selection); |
327 | } |
328 | |
329 | /** |
330 | * The framework calls this to see if the file is correct. |
331 | * <!-- begin-user-doc --> |
332 | * <!-- end-user-doc --> |
333 | * @generated |
334 | */ |
335 | @Override |
336 | protected boolean validatePage() { |
337 | if (super.validatePage()) { |
338 | String extension = new Path(getFileName()).getFileExtension(); |
339 | if (extension == null || !FILE_EXTENSIONS.contains(extension)) { |
340 | String key = FILE_EXTENSIONS.size() > 1 ? "_WARN_FilenameExtensions" : "_WARN_FilenameExtension"; |
341 | setErrorMessage(CostModelEditorPlugin.INSTANCE.getString(key, new Object [] { FORMATTED_FILE_EXTENSIONS })); |
342 | return false; |
343 | } |
344 | return true; |
345 | } |
346 | return false; |
347 | } |
348 | |
349 | /** |
350 | * <!-- begin-user-doc --> |
351 | * <!-- end-user-doc --> |
352 | * @generated |
353 | */ |
354 | public IFile getModelFile() { |
355 | return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())); |
356 | } |
357 | } |
358 | |
359 | /** |
360 | * This is the page where the type of object to create is selected. |
361 | * <!-- begin-user-doc --> |
362 | * <!-- end-user-doc --> |
363 | * @generated |
364 | */ |
365 | public class costModelWizardInitialObjectCreationPage extends WizardPage { |
366 | /** |
367 | * <!-- begin-user-doc --> |
368 | * <!-- end-user-doc --> |
369 | * @generated |
370 | */ |
371 | protected Combo initialObjectField; |
372 | |
373 | /** |
374 | * @generated |
375 | * <!-- begin-user-doc --> |
376 | * <!-- end-user-doc --> |
377 | */ |
378 | protected List<String> encodings; |
379 | |
380 | /** |
381 | * <!-- begin-user-doc --> |
382 | * <!-- end-user-doc --> |
383 | * @generated |
384 | */ |
385 | protected Combo encodingField; |
386 | |
387 | /** |
388 | * Pass in the selection. |
389 | * <!-- begin-user-doc --> |
390 | * <!-- end-user-doc --> |
391 | * @generated |
392 | */ |
393 | public costModelWizardInitialObjectCreationPage(String pageId) { |
394 | super(pageId); |
395 | } |
396 | |
397 | /** |
398 | * <!-- begin-user-doc --> |
399 | * <!-- end-user-doc --> |
400 | * @generated |
401 | */ |
402 | public void createControl(Composite parent) { |
403 | Composite composite = new Composite(parent, SWT.NONE); { |
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(CostModelEditorPlugin.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(CostModelEditorPlugin.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 CostModelEditPlugin.INSTANCE.getString("_UI_" + typeName + "_type"); |
544 | } |
545 | catch(MissingResourceException mre) { |
546 | CostModelEditorPlugin.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(CostModelEditorPlugin.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 costModelWizardNewFileCreationPage("Whatever", selection); |
578 | newFileCreationPage.setTitle(CostModelEditorPlugin.INSTANCE.getString("_UI_costModelWizard_label")); |
579 | newFileCreationPage.setDescription(CostModelEditorPlugin.INSTANCE.getString("_UI_costModelWizard_description")); |
580 | newFileCreationPage.setFileName(CostModelEditorPlugin.INSTANCE.getString("_UI_costEditorFilenameDefaultBase") + "." + 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 = CostModelEditorPlugin.INSTANCE.getString("_UI_costEditorFilenameDefaultBase"); |
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 costModelWizardInitialObjectCreationPage("Whatever2"); |
617 | initialObjectCreationPage.setTitle(CostModelEditorPlugin.INSTANCE.getString("_UI_costModelWizard_label")); |
618 | initialObjectCreationPage.setDescription(CostModelEditorPlugin.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 | } |