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