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