KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > ecore2ecore > presentation > Ecore2EcoreModelWizard


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: Ecore2EcoreModelWizard.java,v 1.10 2005/07/06 19:45:16 davidms Exp $
16  */

17 package org.eclipse.emf.mapping.ecore2ecore.presentation;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.MissingResourceException JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.eclipse.emf.common.util.URI;
31
32 import org.eclipse.emf.ecore.EClass;
33 import org.eclipse.emf.ecore.EClassifier;
34
35 import org.eclipse.emf.ecore.resource.Resource;
36 import org.eclipse.emf.ecore.resource.ResourceSet;
37
38 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
39
40 import org.eclipse.emf.ecore.EObject;
41
42 import org.eclipse.emf.ecore.xmi.XMLResource;
43
44 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
45
46 import org.eclipse.core.resources.IContainer;
47 import org.eclipse.core.resources.IFile;
48 import org.eclipse.core.resources.IFolder;
49 import org.eclipse.core.resources.IProject;
50 import org.eclipse.core.resources.IResource;
51 import org.eclipse.core.resources.ResourcesPlugin;
52
53 import org.eclipse.core.runtime.IProgressMonitor;
54 import org.eclipse.core.runtime.Path;
55
56 import org.eclipse.jface.dialogs.MessageDialog;
57
58 import org.eclipse.jface.viewers.ISelection;
59 import org.eclipse.jface.viewers.IStructuredSelection;
60 import org.eclipse.jface.viewers.StructuredSelection;
61
62 import org.eclipse.jface.wizard.Wizard;
63 import org.eclipse.jface.wizard.WizardPage;
64
65 import org.eclipse.swt.SWT;
66
67 import org.eclipse.swt.events.ModifyListener;
68 import org.eclipse.swt.events.ModifyEvent;
69
70 import org.eclipse.swt.layout.GridData;
71 import org.eclipse.swt.layout.GridLayout;
72
73 import org.eclipse.swt.widgets.Composite;
74 import org.eclipse.swt.widgets.Combo;
75 import org.eclipse.swt.widgets.Label;
76
77 import org.eclipse.ui.INewWizard;
78 import org.eclipse.ui.IWorkbench;
79 import org.eclipse.ui.IWorkbenchPage;
80 import org.eclipse.ui.IWorkbenchPart;
81 import org.eclipse.ui.IWorkbenchWindow;
82 import org.eclipse.ui.PartInitException;
83
84 import org.eclipse.ui.actions.WorkspaceModifyOperation;
85
86 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
87
88 import org.eclipse.ui.part.FileEditorInput;
89 import org.eclipse.ui.part.ISetSelectionTarget;
90
91 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreFactory;
92 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreMappingRoot;
93 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcorePackage;
94
95
96 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcorePlugin;
97
98
99 /**
100  * This is a simple wizard for creating a new model file.
101  * <!-- begin-user-doc -->
102  * <!-- end-user-doc -->
103  * @generated
104  */

105 public class Ecore2EcoreModelWizard extends Wizard implements INewWizard
106 {
107   /**
108    * This caches an instance of the model package.
109    * <!-- begin-user-doc -->
110    * <!-- end-user-doc -->
111    * @generated
112    */

113   protected Ecore2EcorePackage ecore2EcorePackage = Ecore2EcorePackage.eINSTANCE;
114
115   /**
116    * This caches an instance of the model factory.
117    * <!-- begin-user-doc -->
118    * <!-- end-user-doc -->
119    * @generated
120    */

121   protected Ecore2EcoreFactory ecore2EcoreFactory = ecore2EcorePackage.getEcore2EcoreFactory();
122
123   /**
124    * This is the file creation page.
125    * <!-- begin-user-doc -->
126    * <!-- end-user-doc -->
127    * @generated
128    */

129   protected Ecore2EcoreModelWizardNewFileCreationPage newFileCreationPage;
130
131   /**
132    * This is the initial object creation page.
133    * <!-- begin-user-doc -->
134    * <!-- end-user-doc -->
135    * @generated
136    */

137   protected Ecore2EcoreModelWizardInitialObjectCreationPage initialObjectCreationPage;
138
139   /**
140    * Remember the selection during initialization for populating the default container.
141    * <!-- begin-user-doc -->
142    * <!-- end-user-doc -->
143    * @generated
144    */

145   protected IStructuredSelection selection;
146
147   /**
148    * Remember the workbench during initialization.
149    * <!-- begin-user-doc -->
150    * <!-- end-user-doc -->
151    * @generated
152    */

153   protected IWorkbench workbench;
154
155   /**
156    * Caches the names of the types that can be created as the root object.
157    * <!-- begin-user-doc -->
158    * <!-- end-user-doc -->
159    * @generated
160    */

161   protected List JavaDoc initialObjectNames;
162
163   /**
164    * This just records the information.
165    * <!-- begin-user-doc -->
166    * <!-- end-user-doc -->
167    * @generated
168    */

169   public void init(IWorkbench workbench, IStructuredSelection selection)
170   {
171     this.workbench = workbench;
172     this.selection = selection;
173     setWindowTitle(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Wizard_label"));
174     setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(Ecore2EcoreEditorPlugin.INSTANCE.getImage("full/wizban/NewEcore2Ecore")));
175   }
176
177   /**
178    * Returns the names of the types that can be created as the root object.
179    * <!-- begin-user-doc -->
180    * <!-- end-user-doc -->
181    * @generated
182    */

183   protected Collection JavaDoc getInitialObjectNames()
184   {
185     if (initialObjectNames == null)
186     {
187       initialObjectNames = new ArrayList JavaDoc();
188       for (Iterator JavaDoc classifiers = ecore2EcorePackage.getEClassifiers().iterator(); classifiers.hasNext(); )
189       {
190         EClassifier eClassifier = (EClassifier)classifiers.next();
191         if (eClassifier instanceof EClass)
192         {
193           EClass eClass = (EClass)eClassifier;
194           if (!eClass.isAbstract())
195           {
196             initialObjectNames.add(eClass.getName());
197           }
198         }
199       }
200       Collections.sort(initialObjectNames, java.text.Collator.getInstance());
201     }
202     return initialObjectNames;
203   }
204
205   /**
206    * Create a new model.
207    * <!-- begin-user-doc -->
208    * <!-- end-user-doc -->
209    * @generated
210    */

211   protected EObject createInitialModelGen()
212   {
213     EClass eClass = (EClass)ecore2EcorePackage.getEClassifier(initialObjectCreationPage.getInitialObjectName());
214     EObject rootObject = ecore2EcoreFactory.create(eClass);
215     return rootObject;
216   }
217
218   EObject createInitialModel()
219   {
220     EObject rootObject = createInitialModelGen();
221
222     if (Ecore2EcoreMappingRoot.class.isInstance(rootObject))
223     {
224       ((Ecore2EcoreMappingRoot)rootObject).setTopToBottom(true);
225     }
226
227     return rootObject;
228   }
229
230   /**
231    * Do the work after everything is specified.
232    * <!-- begin-user-doc -->
233    * <!-- end-user-doc -->
234    * @generated
235    */

236   public boolean performFinish()
237   {
238     try
239     {
240       // Remember the file.
241
//
242
final IFile modelFile = getModelFile();
243
244       // Do the work within an operation.
245
//
246
WorkspaceModifyOperation operation =
247         new WorkspaceModifyOperation()
248         {
249           protected void execute(IProgressMonitor progressMonitor)
250           {
251             try
252             {
253               // Create a resource set
254
//
255
ResourceSet resourceSet = new ResourceSetImpl();
256
257               // Get the URI of the model file.
258
//
259
URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString());
260
261               // Create a resource for this file.
262
//
263
Resource resource = resourceSet.createResource(fileURI);
264
265               // Add the initial model object to the contents.
266
//
267
EObject rootObject = createInitialModel();
268               if (rootObject != null)
269               {
270                 resource.getContents().add(rootObject);
271               }
272
273               // Save the contents of the resource to the file system.
274
//
275
Map JavaDoc options = new HashMap JavaDoc();
276               options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding());
277               resource.save(options);
278             }
279             catch (Exception JavaDoc exception)
280             {
281               Ecore2EcoreEditorPlugin.INSTANCE.log(exception);
282             }
283             finally
284             {
285               progressMonitor.done();
286             }
287           }
288         };
289
290       getContainer().run(false, false, operation);
291
292       // Select the new file resource in the current view.
293
//
294
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
295       IWorkbenchPage page = workbenchWindow.getActivePage();
296       final IWorkbenchPart activePart = page.getActivePart();
297       if (activePart instanceof ISetSelectionTarget)
298       {
299         final ISelection targetSelection = new StructuredSelection(modelFile);
300         getShell().getDisplay().asyncExec
301           (new Runnable JavaDoc()
302            {
303              public void run()
304              {
305                ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
306              }
307            });
308       }
309
310       // Open an editor on the new file.
311
//
312
try
313       {
314         page.openEditor
315           (new FileEditorInput(modelFile),
316            workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
317       }
318       catch (PartInitException exception)
319       {
320         MessageDialog.openError(workbenchWindow.getShell(), Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
321         return false;
322       }
323
324       return true;
325     }
326     catch (Exception JavaDoc exception)
327     {
328       Ecore2EcoreEditorPlugin.INSTANCE.log(exception);
329       return false;
330     }
331   }
332
333   /**
334    * This is the one page of the wizard.
335    * <!-- begin-user-doc -->
336    * <!-- end-user-doc -->
337    * @generated
338    */

339   public class Ecore2EcoreModelWizardNewFileCreationPage extends WizardNewFileCreationPage
340   {
341     /**
342      * Pass in the selection.
343      * <!-- begin-user-doc -->
344      * <!-- end-user-doc -->
345      * @generated
346      */

347     public Ecore2EcoreModelWizardNewFileCreationPage(String JavaDoc pageId, IStructuredSelection selection)
348     {
349       super(pageId, selection);
350     }
351
352     /**
353      * The framework calls this to see if the file is correct.
354      * <!-- begin-user-doc -->
355      * <!-- end-user-doc -->
356      * @generated
357      */

358     protected boolean validatePage()
359     {
360       if (super.validatePage())
361       {
362         // Make sure the file ends in ".ecore2ecore".
363
//
364
String JavaDoc requiredExt = Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameExtension");
365         String JavaDoc enteredExt = new Path(getFileName()).getFileExtension();
366         if (enteredExt == null || !enteredExt.equals(requiredExt))
367         {
368           setErrorMessage(Ecore2EcoreEditorPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object JavaDoc [] { requiredExt }));
369           return false;
370         }
371         else
372         {
373           return true;
374         }
375       }
376       else
377       {
378         return false;
379       }
380     }
381
382     /**
383      * <!-- begin-user-doc -->
384      * <!-- end-user-doc -->
385      * @generated
386      */

387     public IFile getModelFile()
388     {
389       return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName()));
390     }
391   }
392
393   /**
394    * This is the page where the type of object to create is selected.
395    * <!-- begin-user-doc -->
396    * <!-- end-user-doc -->
397    * @generated
398    */

399   public class Ecore2EcoreModelWizardInitialObjectCreationPage extends WizardPage
400   {
401     /**
402      * <!-- begin-user-doc -->
403      * <!-- end-user-doc -->
404      * @generated
405      */

406     protected Combo initialObjectField;
407
408     /**
409      * @generated
410      * <!-- begin-user-doc -->
411      * <!-- end-user-doc -->
412      */

413     protected List JavaDoc encodings;
414
415     /**
416      * <!-- begin-user-doc -->
417      * <!-- end-user-doc -->
418      * @generated
419      */

420     protected Combo encodingField;
421
422     /**
423      * Pass in the selection.
424      * <!-- begin-user-doc -->
425      * <!-- end-user-doc -->
426      * @generated
427      */

428     public Ecore2EcoreModelWizardInitialObjectCreationPage(String JavaDoc pageId)
429     {
430       super(pageId);
431     }
432
433     /**
434      * <!-- begin-user-doc -->
435      * <!-- end-user-doc -->
436      * @generated
437      */

438     public void createControl(Composite parent)
439     {
440       Composite composite = new Composite(parent, SWT.NONE);
441       {
442         GridLayout layout = new GridLayout();
443         layout.numColumns = 1;
444         layout.verticalSpacing = 12;
445         composite.setLayout(layout);
446
447         GridData data = new GridData();
448         data.verticalAlignment = GridData.FILL;
449         data.grabExcessVerticalSpace = true;
450         data.horizontalAlignment = GridData.FILL;
451         composite.setLayoutData(data);
452       }
453
454       Label containerLabel = new Label(composite, SWT.LEFT);
455       {
456         containerLabel.setText(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_ModelObject"));
457
458         GridData data = new GridData();
459         data.horizontalAlignment = GridData.FILL;
460         containerLabel.setLayoutData(data);
461       }
462
463       initialObjectField = new Combo(composite, SWT.BORDER);
464       {
465         GridData data = new GridData();
466         data.horizontalAlignment = GridData.FILL;
467         data.grabExcessHorizontalSpace = true;
468         initialObjectField.setLayoutData(data);
469       }
470
471       for (Iterator JavaDoc i = getInitialObjectNames().iterator(); i.hasNext(); )
472       {
473         initialObjectField.add(getLabel((String JavaDoc)i.next()));
474       }
475
476       if (initialObjectField.getItemCount() == 1)
477       {
478         initialObjectField.select(0);
479       }
480       initialObjectField.addModifyListener(validator);
481
482       Label encodingLabel = new Label(composite, SWT.LEFT);
483       {
484         encodingLabel.setText(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_XMLEncoding"));
485
486         GridData data = new GridData();
487         data.horizontalAlignment = GridData.FILL;
488         encodingLabel.setLayoutData(data);
489       }
490       encodingField = new Combo(composite, SWT.BORDER);
491       {
492         GridData data = new GridData();
493         data.horizontalAlignment = GridData.FILL;
494         data.grabExcessHorizontalSpace = true;
495         encodingField.setLayoutData(data);
496       }
497
498       for (Iterator JavaDoc i = getEncodings().iterator(); i.hasNext(); )
499       {
500         encodingField.add((String JavaDoc)i.next());
501       }
502
503       encodingField.select(0);
504       encodingField.addModifyListener(validator);
505
506       setPageComplete(validatePage());
507       setControl(composite);
508     }
509
510     /**
511      * <!-- begin-user-doc -->
512      * <!-- end-user-doc -->
513      * @generated
514      */

515     protected ModifyListener validator =
516       new ModifyListener()
517       {
518         public void modifyText(ModifyEvent e)
519         {
520           setPageComplete(validatePage());
521         }
522       };
523
524     /**
525      * <!-- begin-user-doc -->
526      * <!-- end-user-doc -->
527      * @generated
528      */

529     protected boolean validatePage()
530     {
531       return getInitialObjectName() != null && getEncodings().contains(encodingField.getText());
532     }
533
534     /**
535      * <!-- begin-user-doc -->
536      * <!-- end-user-doc -->
537      * @generated
538      */

539     public void setVisible(boolean visible)
540     {
541       super.setVisible(visible);
542       if (visible)
543       {
544         if (initialObjectField.getItemCount() == 1)
545         {
546           initialObjectField.clearSelection();
547           encodingField.setFocus();
548         }
549         else
550         {
551           encodingField.clearSelection();
552           initialObjectField.setFocus();
553         }
554       }
555     }
556
557     /**
558      * <!-- begin-user-doc -->
559      * <!-- end-user-doc -->
560      * @generated
561      */

562     public String JavaDoc getInitialObjectName()
563     {
564       String JavaDoc label = initialObjectField.getText();
565
566       for (Iterator JavaDoc i = getInitialObjectNames().iterator(); i.hasNext(); )
567       {
568         String JavaDoc name = (String JavaDoc)i.next();
569         if (getLabel(name).equals(label))
570         {
571           return name;
572         }
573       }
574       return null;
575     }
576
577     /**
578      * <!-- begin-user-doc -->
579      * <!-- end-user-doc -->
580      * @generated
581      */

582     public String JavaDoc getEncoding()
583     {
584       return encodingField.getText();
585     }
586
587     /**
588      * Returns the label for the specified type name.
589      * <!-- begin-user-doc -->
590      * <!-- end-user-doc -->
591      * @generated
592      */

593     protected String JavaDoc getLabel(String JavaDoc typeName)
594     {
595       try
596       {
597         return Ecore2EcorePlugin.INSTANCE.getString("_UI_" + typeName + "_type");
598       }
599       catch(MissingResourceException JavaDoc mre)
600       {
601         Ecore2EcoreEditorPlugin.INSTANCE.log(mre);
602       }
603       return typeName;
604     }
605
606     /**
607      * <!-- begin-user-doc -->
608      * <!-- end-user-doc -->
609      * @generated
610      */

611     protected Collection JavaDoc getEncodings()
612     {
613       if (encodings == null)
614       {
615         encodings = new ArrayList JavaDoc();
616         for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); )
617         {
618           encodings.add(stringTokenizer.nextToken());
619         }
620       }
621       return encodings;
622     }
623   }
624
625   /**
626    * The framework calls this to create the contents of the wizard.
627    * <!-- begin-user-doc -->
628    * <!-- end-user-doc -->
629    * @generated
630    */

631   public void addPages()
632   {
633     // Create a page, set the title, and the initial model file name.
634
//
635
newFileCreationPage = new Ecore2EcoreModelWizardNewFileCreationPage("Whatever", selection);
636     newFileCreationPage.setTitle(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreModelWizard_label"));
637     newFileCreationPage.setDescription(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreModelWizard_description"));
638     newFileCreationPage.setFileName(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameDefaultBase") + "." + Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameExtension"));
639     addPage(newFileCreationPage);
640
641     // Try and get the resource selection to determine a current directory for the file dialog.
642
//
643
if (selection != null && !selection.isEmpty())
644     {
645       // Get the resource...
646
//
647
Object JavaDoc selectedElement = selection.iterator().next();
648       if (selectedElement instanceof IResource)
649       {
650         // Get the resource parent, if its a file.
651
//
652
IResource selectedResource = (IResource)selectedElement;
653         if (selectedResource.getType() == IResource.FILE)
654         {
655           selectedResource = selectedResource.getParent();
656         }
657
658         // This gives us a directory...
659
//
660
if (selectedResource instanceof IFolder || selectedResource instanceof IProject)
661         {
662           // Set this for the container.
663
//
664
newFileCreationPage.setContainerFullPath(selectedResource.getFullPath());
665
666           // Make up a unique new name here.
667
//
668
String JavaDoc defaultModelBaseFilename = Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameDefaultBase");
669           String JavaDoc defaultModelFilenameExtension = Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreEditorFilenameExtension");
670           String JavaDoc modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension;
671           for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i)
672           {
673             modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension;
674           }
675           newFileCreationPage.setFileName(modelFilename);
676         }
677       }
678     }
679     initialObjectCreationPage = new Ecore2EcoreModelWizardInitialObjectCreationPage("Whatever2");
680     initialObjectCreationPage.setTitle(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Ecore2EcoreModelWizard_label"));
681     initialObjectCreationPage.setDescription(Ecore2EcoreEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description"));
682     addPage(initialObjectCreationPage);
683   }
684
685   /**
686    * Get the file from the page.
687    * <!-- begin-user-doc -->
688    * <!-- end-user-doc -->
689    * @generated
690    */

691   public IFile getModelFile()
692   {
693     return newFileCreationPage.getModelFile();
694   }
695
696 }
Popular Tags