KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Olj > presentation > OljModelWizard


1 /**
2  * <copyright>
3  * </copyright>
4  *
5  * $Id$
6  */

7 package Olj.presentation;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.MissingResourceException JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 import org.eclipse.emf.common.util.URI;
20
21 import org.eclipse.emf.ecore.EClass;
22 import org.eclipse.emf.ecore.EClassifier;
23
24 import org.eclipse.emf.ecore.resource.Resource;
25 import org.eclipse.emf.ecore.resource.ResourceSet;
26
27 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
28
29 import org.eclipse.emf.ecore.EObject;
30
31 import org.eclipse.emf.ecore.xmi.XMLResource;
32
33 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
34
35 import org.eclipse.core.resources.IContainer;
36 import org.eclipse.core.resources.IFile;
37 import org.eclipse.core.resources.IFolder;
38 import org.eclipse.core.resources.IProject;
39 import org.eclipse.core.resources.IResource;
40 import org.eclipse.core.resources.ResourcesPlugin;
41
42 import org.eclipse.core.runtime.IProgressMonitor;
43
44 import org.eclipse.jface.dialogs.MessageDialog;
45
46 import org.eclipse.jface.viewers.IStructuredSelection;
47
48 import org.eclipse.jface.wizard.Wizard;
49 import org.eclipse.jface.wizard.WizardPage;
50
51 import org.eclipse.swt.SWT;
52
53 import org.eclipse.swt.events.SelectionAdapter;
54 import org.eclipse.swt.events.SelectionEvent;
55
56 import org.eclipse.swt.layout.GridData;
57 import org.eclipse.swt.layout.GridLayout;
58
59 import org.eclipse.swt.widgets.Combo;
60 import org.eclipse.swt.widgets.Composite;
61 import org.eclipse.swt.widgets.Label;
62
63 import org.eclipse.ui.INewWizard;
64 import org.eclipse.ui.IWorkbench;
65
66 import org.eclipse.ui.actions.WorkspaceModifyOperation;
67
68 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
69
70 import org.eclipse.ui.part.FileEditorInput;
71 import org.eclipse.ui.part.ISetSelectionTarget;
72
73 import Olj.OljFactory;
74 import Olj.OljPackage;
75 import Olj.provider.OljEditPlugin;
76
77
78 import org.eclipse.core.runtime.Path;
79
80 import org.eclipse.emf.ecore.EStructuralFeature;
81
82 import org.eclipse.emf.ecore.util.EcoreUtil;
83 import org.eclipse.emf.ecore.util.ExtendedMetaData;
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 OljModelWizard extends Wizard implements INewWizard {
101     /**
102      * This caches an instance of the model package.
103      * <!-- begin-user-doc -->
104      * <!-- end-user-doc -->
105      * @generated
106      */

107     protected OljPackage oljPackage = OljPackage.eINSTANCE;
108
109     /**
110      * This caches an instance of the model factory.
111      * <!-- begin-user-doc -->
112      * <!-- end-user-doc -->
113      * @generated
114      */

115     protected OljFactory oljFactory = oljPackage.getOljFactory();
116
117     /**
118      * This is the file creation page.
119      * <!-- begin-user-doc -->
120      * <!-- end-user-doc -->
121      * @generated
122      */

123     protected OljModelWizardNewFileCreationPage newFileCreationPage;
124
125     /**
126      * This is the file creation page.
127      * <!-- begin-user-doc -->
128      * <!-- end-user-doc -->
129      * @generated
130      */

131     protected OljModelWizardInitialObjectCreationPage initialObjectCreationPage;
132
133     /**
134      * Remember the selection during initialization for populating the default container.
135      * <!-- begin-user-doc -->
136      * <!-- end-user-doc -->
137      * @generated
138      */

139     protected IStructuredSelection selection;
140
141     /**
142      * Remember the workbench during initialization.
143      * <!-- begin-user-doc -->
144      * <!-- end-user-doc -->
145      * @generated
146      */

147     protected IWorkbench workbench;
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(OljEditorPlugin.INSTANCE.getString("_UI_Wizard_label"));
159         setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(OljEditorPlugin.INSTANCE.getImage("full/wizban/NewOlj")));
160     }
161
162     /**
163      * Create a new model.
164      * <!-- begin-user-doc -->
165      * <!-- end-user-doc -->
166      * @generated
167      */

168     EObject createInitialModel() {
169         EClass eClass = ExtendedMetaData.INSTANCE.getDocumentRoot(oljPackage);
170         EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(initialObjectCreationPage.getInitialObjectName());
171         EObject rootObject = oljFactory.create(eClass);
172         rootObject.eSet(eStructuralFeature, EcoreUtil.create((EClass)eStructuralFeature.getEType()));
173         return rootObject;
174     }
175
176     /**
177      * Do the work after everything is specified.
178      * <!-- begin-user-doc -->
179      * <!-- end-user-doc -->
180      * @generated
181      */

182     public boolean performFinish() {
183         try {
184             // Remember the file.
185
//
186
final IFile modelFile = getModelFile();
187
188             // Do the work within an operation.
189
//
190
WorkspaceModifyOperation operation =
191                 new WorkspaceModifyOperation() {
192                     protected void execute(IProgressMonitor progressMonitor) {
193                         try {
194                             // Create a resource set
195
//
196
ResourceSet resourceSet = new ResourceSetImpl();
197
198                             // Get the URI of the model file.
199
//
200
URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString());
201
202                             // Create a resource for this file.
203
//
204
Resource resource = resourceSet.createResource(fileURI);
205
206                             // Add the initial model object to the contents.
207
//
208
EObject rootObject = createInitialModel();
209                             if (rootObject != null) {
210                                 resource.getContents().add(rootObject);
211                             }
212
213                             // Save the contents of the resource to the file system.
214
//
215
Map JavaDoc options = new HashMap JavaDoc();
216                             options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding());
217                             resource.save(options);
218                         }
219                         catch (Exception JavaDoc exception) {
220                             OljEditorPlugin.INSTANCE.log(exception);
221                         }
222                         finally {
223                             progressMonitor.done();
224                         }
225                     }
226                 };
227
228             getContainer().run(false, false, operation);
229
230             // Select the new file resource in the current view.
231
//
232
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
233             IWorkbenchPage page = workbenchWindow.getActivePage();
234             final IWorkbenchPart activePart = page.getActivePart();
235             if (activePart instanceof ISetSelectionTarget) {
236                 final ISelection targetSelection = new StructuredSelection(modelFile);
237                 getShell().getDisplay().asyncExec
238                     (new Runnable JavaDoc() {
239                          public void run() {
240                              ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
241                          }
242                      });
243             }
244
245             // Open an editor on the new file.
246
//
247
try {
248                 page.openEditor
249                     (new FileEditorInput(modelFile),
250                      workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
251             }
252             catch (PartInitException exception) {
253                 MessageDialog.openError(workbenchWindow.getShell(), OljEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
254                 return false;
255             }
256
257             return true;
258         }
259         catch (Exception JavaDoc exception) {
260             OljEditorPlugin.INSTANCE.log(exception);
261             return false;
262         }
263     }
264
265     /**
266      * This is the one page of the wizard.
267      * <!-- begin-user-doc -->
268      * <!-- end-user-doc -->
269      * @generated
270      */

271     public class OljModelWizardNewFileCreationPage extends WizardNewFileCreationPage {
272         /**
273          * Remember the model file.
274          * <!-- begin-user-doc -->
275          * <!-- end-user-doc -->
276          * @generated
277          */

278         protected IFile modelFile;
279
280         /**
281          * Pass in the selection.
282          * <!-- begin-user-doc -->
283          * <!-- end-user-doc -->
284          * @generated
285          */

286         public OljModelWizardNewFileCreationPage(String JavaDoc pageId, IStructuredSelection selection) {
287             super(pageId, selection);
288         }
289
290         /**
291          * The framework calls this to see if the file is correct.
292          * <!-- begin-user-doc -->
293          * <!-- end-user-doc -->
294          * @generated
295          */

296         protected boolean validatePage() {
297             if (super.validatePage()) {
298                 // Make sure the file ends in ".olj".
299
//
300
String JavaDoc requiredExt = OljEditorPlugin.INSTANCE.getString("_UI_OljEditorFilenameExtension");
301                 String JavaDoc enteredExt = new Path(getFileName()).getFileExtension();
302                 if (enteredExt == null || !enteredExt.equals(requiredExt)) {
303                     setErrorMessage(OljEditorPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object JavaDoc [] { requiredExt }));
304                     return false;
305                 }
306                 else {
307                     return true;
308                 }
309             }
310             else {
311                 return false;
312             }
313         }
314
315         /**
316          * Store the dialog field settings upon completion.
317          * <!-- begin-user-doc -->
318          * <!-- end-user-doc -->
319          * @generated
320          */

321         public boolean performFinish() {
322             modelFile = getModelFile();
323             return true;
324         }
325
326         /**
327          * <!-- begin-user-doc -->
328          * <!-- end-user-doc -->
329          * @generated
330          */

331         public IFile getModelFile() {
332             return
333                 modelFile == null ?
334                     ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())) :
335                     modelFile;
336         }
337     }
338
339     /**
340      * This is the page where the type of object to create is selected.
341      * <!-- begin-user-doc -->
342      * <!-- end-user-doc -->
343      * @generated
344      */

345     public class OljModelWizardInitialObjectCreationPage extends WizardPage {
346         /**
347          * <!-- begin-user-doc -->
348          * <!-- end-user-doc -->
349          * @generated
350          */

351         protected String JavaDoc initialObjectName;
352
353         /**
354          * <!-- begin-user-doc -->
355          * <!-- end-user-doc -->
356          * @generated
357          */

358         protected Combo initialObjectField;
359
360         /**
361          * @generated
362          * <!-- begin-user-doc -->
363          * <!-- end-user-doc -->
364          */

365         protected String JavaDoc encoding;
366
367         /**
368          * <!-- begin-user-doc -->
369          * <!-- end-user-doc -->
370          * @generated
371          */

372         protected Combo encodingField;
373
374         /**
375          * Pass in the selection.
376          * <!-- begin-user-doc -->
377          * <!-- end-user-doc -->
378          * @generated
379          */

380         public OljModelWizardInitialObjectCreationPage(String JavaDoc pageId) {
381             super(pageId);
382         }
383
384         /**
385          * <!-- begin-user-doc -->
386          * <!-- end-user-doc -->
387          * @generated
388          */

389         public void createControl(Composite parent) {
390             Composite composite = new Composite(parent, SWT.NONE);
391             {
392                 GridLayout layout = new GridLayout();
393                 layout.numColumns = 1;
394                 layout.verticalSpacing = 12;
395                 composite.setLayout(layout);
396
397                 GridData data = new GridData();
398                 data.verticalAlignment = GridData.FILL;
399                 data.grabExcessVerticalSpace = true;
400                 data.horizontalAlignment = GridData.FILL;
401                 composite.setLayoutData(data);
402             }
403
404             Label containerLabel = new Label(composite, SWT.LEFT);
405             {
406                 containerLabel.setText(OljEditorPlugin.INSTANCE.getString("_UI_ModelObject"));
407
408                 GridData data = new GridData();
409                 data.horizontalAlignment = GridData.FILL;
410                 containerLabel.setLayoutData(data);
411             }
412
413             initialObjectField = new Combo(composite, SWT.BORDER);
414             {
415                 GridData data = new GridData();
416                 data.horizontalAlignment = GridData.FILL;
417                 data.grabExcessHorizontalSpace = true;
418                 initialObjectField.setLayoutData(data);
419             }
420
421             List JavaDoc objectNames = new ArrayList JavaDoc();
422             for (Iterator JavaDoc elements = ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE.getDocumentRoot(oljPackage)).iterator(); elements.hasNext(); ) {
423                 EStructuralFeature eStructuralFeature = (EStructuralFeature)elements.next();
424                 EClassifier eClassifier = eStructuralFeature.getEType();
425                 if (eClassifier instanceof EClass) {
426                     EClass eClass = (EClass)eClassifier;
427                     if (!eClass.isAbstract()) {
428                         objectNames.add(getLabel(eStructuralFeature));
429                     }
430                 }
431             }
432
433             Collections.sort(objectNames, java.text.Collator.getInstance());
434             for (Iterator JavaDoc i = objectNames.iterator(); i.hasNext(); ) {
435                 String JavaDoc objectName = (String JavaDoc)i.next();
436                 initialObjectField.add(objectName);
437             }
438
439             initialObjectField.addSelectionListener
440                 (new SelectionAdapter() {
441                      public void widgetSelected(SelectionEvent e) {
442                          setPageComplete(isPageComplete());
443                      }
444                  });
445
446             Label encodingLabel = new Label(composite, SWT.LEFT);
447             {
448                 encodingLabel.setText(OljEditorPlugin.INSTANCE.getString("_UI_XMLEncoding"));
449
450                 GridData data = new GridData();
451                 data.horizontalAlignment = GridData.FILL;
452                 encodingLabel.setLayoutData(data);
453             }
454             encodingField = new Combo(composite, SWT.BORDER);
455             {
456                 GridData data = new GridData();
457                 data.horizontalAlignment = GridData.FILL;
458                 data.grabExcessHorizontalSpace = true;
459                 encodingField.setLayoutData(data);
460             }
461
462             for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(OljEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) {
463                 encodingField.add(stringTokenizer.nextToken());
464             }
465             encodingField.select(0);
466
467             setControl(composite);
468         }
469
470         /**
471          * The framework calls this to see if the file is correct.
472          * <!-- begin-user-doc -->
473          * <!-- end-user-doc -->
474          * @generated
475          */

476         public boolean isPageComplete() {
477             if (super.isPageComplete()) {
478                 return initialObjectField.getSelectionIndex() != -1;
479             }
480             else {
481                 return false;
482             }
483         }
484
485         /**
486          * Store the dialog field settings upon completion.
487          * <!-- begin-user-doc -->
488          * <!-- end-user-doc -->
489          * @generated
490          */

491         public boolean performFinish() {
492             initialObjectName = getInitialObjectName();
493             encoding = getEncoding();
494             return true;
495         }
496
497         /**
498          * <!-- begin-user-doc -->
499          * <!-- end-user-doc -->
500          * @generated
501          */

502         public String JavaDoc getInitialObjectName() {
503             if (initialObjectName != null) {
504                 return initialObjectName;
505             }
506             else {
507                 String JavaDoc label = initialObjectField.getText();
508                 for (Iterator JavaDoc elements = ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE.getDocumentRoot(oljPackage)).iterator(); elements.hasNext(); ) {
509                     EStructuralFeature eStructuralFeature = (EStructuralFeature)elements.next();
510                     EClassifier eClassifier = eStructuralFeature.getEType();
511                     if (eClassifier instanceof EClass) {
512                         EClass eClass = (EClass)eClassifier;
513                         if (!eClass.isAbstract() && getLabel(eStructuralFeature).equals(label)) {
514                             return eStructuralFeature.getName();
515                         }
516                     }
517                 }
518                 return label;
519             }
520         }
521
522         /**
523          * <!-- begin-user-doc -->
524          * <!-- end-user-doc -->
525          * @generated
526          */

527         public String JavaDoc getEncoding() {
528             return
529                 encoding == null ?
530                     encodingField.getText() :
531                     encoding;
532         }
533         /**
534          * Returns the label of the specified element.
535          * <!-- begin-user-doc -->
536          * <!-- end-user-doc -->
537          * @generated
538          */

539         protected String JavaDoc getLabel(EStructuralFeature eStructuralFeature) {
540             String JavaDoc name = eStructuralFeature.getName();
541             try {
542                 return OljEditPlugin.INSTANCE.getString("_UI_" + name + "_feature");
543             }
544             catch(MissingResourceException JavaDoc mre) {
545             }
546             return name;
547         }
548     }
549
550     /**
551      * The framework calls this to create the contents of the wizard.
552      * <!-- begin-user-doc -->
553      * <!-- end-user-doc -->
554      * @generated
555      */

556     public void addPages() {
557         // Create a page, set the title, and the initial model file name.
558
//
559
newFileCreationPage = new OljModelWizardNewFileCreationPage("Whatever", selection);
560         newFileCreationPage.setTitle(OljEditorPlugin.INSTANCE.getString("_UI_OljModelWizard_label"));
561         newFileCreationPage.setDescription(OljEditorPlugin.INSTANCE.getString("_UI_OljModelWizard_description"));
562         newFileCreationPage.setFileName(OljEditorPlugin.INSTANCE.getString("_UI_OljEditorFilenameDefaultBase") + "." + OljEditorPlugin.INSTANCE.getString("_UI_OljEditorFilenameExtension"));
563         addPage(newFileCreationPage);
564
565         // Try and get the resource selection to determine a current directory for the file dialog.
566
//
567
if (selection != null && !selection.isEmpty()) {
568             // Get the resource...
569
//
570
Object JavaDoc selectedElement = selection.iterator().next();
571             if (selectedElement instanceof IResource) {
572                 // Get the resource parent, if its a file.
573
//
574
IResource selectedResource = (IResource)selectedElement;
575                 if (selectedResource.getType() == IResource.FILE) {
576                     selectedResource = selectedResource.getParent();
577                 }
578
579                 // This gives us a directory...
580
//
581
if (selectedResource instanceof IFolder || selectedResource instanceof IProject) {
582                     // Set this for the container.
583
//
584
String JavaDoc currentDirectory = selectedResource.getLocation().toOSString();
585                     newFileCreationPage.setContainerFullPath(selectedResource.getFullPath());
586
587                     // Make up a unique new name here.
588
//
589
String JavaDoc defaultModelBaseFilename = OljEditorPlugin.INSTANCE.getString("_UI_OljEditorFilenameDefaultBase");
590                     String JavaDoc defaultModelFilenameExtension = OljEditorPlugin.INSTANCE.getString("_UI_OljEditorFilenameExtension");
591                     String JavaDoc modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension;
592                     for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) {
593                         modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension;
594                     }
595                     newFileCreationPage.setFileName(modelFilename);
596                 }
597             }
598         }
599         initialObjectCreationPage = new OljModelWizardInitialObjectCreationPage("Whatever2");
600         initialObjectCreationPage.setTitle(OljEditorPlugin.INSTANCE.getString("_UI_OljModelWizard_label"));
601         initialObjectCreationPage.setDescription(OljEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description"));
602         addPage(initialObjectCreationPage);
603     }
604
605     /**
606      * Get the file from the page.
607      * <!-- begin-user-doc -->
608      * <!-- end-user-doc -->
609      * @generated
610      */

611     public IFile getModelFile() {
612         return newFileCreationPage.getModelFile();
613     }
614
615 }
616
Popular Tags