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