KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > presentation > DynamicModelWizard


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 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: DynamicModelWizard.java,v 1.5 2005/06/12 13:30:07 emerks Exp $
16  */

17 package org.eclipse.emf.ecore.presentation;
18
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IFolder;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.jface.wizard.Wizard;
36 import org.eclipse.ui.IEditorDescriptor;
37 import org.eclipse.ui.INewWizard;
38 import org.eclipse.ui.IWorkbench;
39 import org.eclipse.ui.IWorkbenchPage;
40 import org.eclipse.ui.IWorkbenchPart;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.PartInitException;
43 import org.eclipse.ui.actions.WorkspaceModifyOperation;
44 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
45 import org.eclipse.ui.part.FileEditorInput;
46 import org.eclipse.ui.part.ISetSelectionTarget;
47
48 import org.eclipse.emf.common.util.URI;
49 import org.eclipse.emf.ecore.EClass;
50 import org.eclipse.emf.ecore.EObject;
51 import org.eclipse.emf.ecore.plugin.EcorePlugin;
52 import org.eclipse.emf.ecore.resource.Resource;
53 import org.eclipse.emf.ecore.resource.ResourceSet;
54 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
55 import org.eclipse.emf.ecore.util.EcoreUtil;
56 import org.eclipse.emf.edit.EMFEditPlugin;
57 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
58
59
60 /**
61  * This is a simple wizard for creating a new dynamic model file.
62  */

63 public class DynamicModelWizard extends Wizard implements INewWizard
64 {
65   /**
66    * This caches the class instance.
67    */

68   protected EClass eClass;
69
70   /**
71    * This is the file creation page.
72    */

73   protected DynamicModelWizardNewFileCreationPage newFileCreationPage;
74
75   /**
76    * Remember the selection during initialization for populating the default container.
77    */

78   protected IStructuredSelection selection;
79
80   /**
81    * Remember the workbench during initialization.
82    */

83   protected IWorkbench workbench;
84
85   /**
86    * Creates an instance.
87    */

88   public DynamicModelWizard(EClass eClass)
89   {
90     this.eClass = eClass;
91   }
92
93   /**
94    * This just records the information.
95    */

96   public void init(IWorkbench workbench, IStructuredSelection selection)
97   {
98     this.workbench = workbench;
99     this.selection = selection;
100     setDefaultPageImageDescriptor
101       (ExtendedImageRegistry.INSTANCE.getImageDescriptor(EMFEditPlugin.INSTANCE.getImage("full/wizban/NewModel")));
102   }
103
104   /**
105    * Create a new model.
106    */

107   EObject createInitialModel()
108   {
109     return EcoreUtil.create(eClass);
110   }
111
112   /**
113    * Do the work after everything is specified.
114    */

115   public boolean performFinish()
116   {
117     try
118     {
119       // Remember the file.
120
//
121
final IFile modelFile = getModelFile();
122
123       // Do the work within an operation.
124
//
125
WorkspaceModifyOperation operation =
126         new WorkspaceModifyOperation()
127         {
128           protected void execute(IProgressMonitor progressMonitor)
129           {
130             try
131             {
132               // Create a resource set
133
//
134
ResourceSet resourceSet = new ResourceSetImpl();
135               resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap());
136
137               // Get the URI of the model file.
138
//
139
URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString());
140
141               // Create a resource for this file.
142
//
143
Resource resource = resourceSet.createResource(fileURI);
144
145               // Add the initial model object to the contents.
146
//
147
EObject rootObject = createInitialModel();
148               if (rootObject != null)
149               {
150                 resource.getContents().add(rootObject);
151               }
152
153               // Save the contents of the resource to the file system.
154
//
155
Map JavaDoc options = new HashMap JavaDoc();
156               options.put("SCHEMA_LOCATION", Boolean.TRUE);
157               options.put("LINE_WIDTH", new Integer JavaDoc(80));
158               resource.save(options);
159             }
160             catch (Exception JavaDoc exception)
161             {
162               exception.printStackTrace();
163             }
164             finally
165             {
166               progressMonitor.done();
167             }
168           }
169         };
170
171       getContainer().run(false, false, operation);
172
173       // Select the new file resource in the current view.
174
//
175
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
176       IWorkbenchPage page = workbenchWindow.getActivePage();
177       final IWorkbenchPart activePart = page.getActivePart();
178       if (activePart instanceof ISetSelectionTarget)
179       {
180         final ISelection targetSelection = new StructuredSelection(modelFile);
181         getShell().getDisplay().asyncExec
182           (new Runnable JavaDoc()
183            {
184              public void run()
185              {
186                ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
187              }
188            });
189       }
190
191       // Open an editor on the new file.
192
//
193
try
194       {
195         IEditorDescriptor editorDescriptor = workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString());
196         if (editorDescriptor != null)
197         {
198           page.openEditor(new FileEditorInput(modelFile), editorDescriptor.getId());
199         }
200       }
201       catch (PartInitException exception)
202       {
203         MessageDialog.openError
204           (workbenchWindow.getShell(), EcoreEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
205         return false;
206       }
207
208       return true;
209     }
210     catch (Exception JavaDoc exception)
211     {
212       exception.printStackTrace();
213       return false;
214     }
215   }
216
217   /**
218    * This is the one page of the wizard.
219    */

220   public class DynamicModelWizardNewFileCreationPage extends WizardNewFileCreationPage
221   {
222     /**
223      * Remember the model file.
224      */

225     protected IFile modelFile;
226
227     /**
228      * Pass in the selection.
229      */

230     public DynamicModelWizardNewFileCreationPage(String JavaDoc pageId, IStructuredSelection selection)
231     {
232       super(pageId, selection);
233     }
234
235     /**
236      * The framework calls this to see if the file is correct.
237      */

238     protected boolean validatePage()
239     {
240       if (super.validatePage())
241       {
242         // Make sure the file ends in ".ecore".
243
//
244
String JavaDoc requiredExt = EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicInstanceFilenameExtension");
245         String JavaDoc enteredExt = new Path(getFileName()).getFileExtension();
246         if (enteredExt == null || !enteredExt.equals(requiredExt))
247         {
248           setErrorMessage(EcoreEditorPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object JavaDoc [] { requiredExt }));
249           return false;
250         }
251         else
252         {
253           return true;
254         }
255       }
256       else
257       {
258         return false;
259       }
260     }
261
262     /**
263      * Store the dialog field settings upon completion.
264      */

265     public boolean performFinish()
266     {
267       modelFile = getModelFile();
268       return true;
269     }
270
271     /**
272      */

273     public IFile getModelFile()
274     {
275       return
276         modelFile == null ?
277           ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())) :
278           modelFile;
279     }
280   }
281
282   /**
283    * The framework calls this to create the contents of the wizard.
284    */

285   public void addPages()
286   {
287     // Create a page, set the title, and the initial model file name.
288
//
289
newFileCreationPage = new DynamicModelWizardNewFileCreationPage("Whatever", selection);
290     newFileCreationPage.setTitle(EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicModelWizard_label"));
291     newFileCreationPage.setDescription
292       (EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicModelWizard_description", new Object JavaDoc [] { eClass.getName() }));
293     newFileCreationPage.setFileName
294       (eClass.getName() + "." + EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicInstanceFilenameExtension"));
295     addPage(newFileCreationPage);
296
297     // Try and get the resource selection to determine a current directory for the file dialog.
298
//
299
if (selection != null && !selection.isEmpty())
300     {
301       // Get the resource...
302
//
303
Object JavaDoc selectedElement = selection.iterator().next();
304       if (selectedElement instanceof IResource)
305       {
306         // Get the resource parent, if its a file.
307
//
308
IResource selectedResource = (IResource)selectedElement;
309         if (selectedResource.getType() == IResource.FILE)
310         {
311           selectedResource = selectedResource.getParent();
312         }
313
314         // This gives us a directory...
315
//
316
if (selectedResource instanceof IFolder || selectedResource instanceof IProject)
317         {
318           // Set this for the container.
319
//
320
newFileCreationPage.setContainerFullPath(selectedResource.getFullPath());
321
322           // Make up a unique new name here.
323
//
324
String JavaDoc defaultModelBaseFilename = eClass.getName();
325           String JavaDoc defaultModelFilenameExtension = EcoreEditorPlugin.INSTANCE.getString("_UI_DynamicInstanceFilenameExtension");
326           String JavaDoc modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension;
327           for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i)
328           {
329             modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension;
330           }
331           newFileCreationPage.setFileName(modelFilename);
332         }
333       }
334     }
335   }
336
337   /**
338    * Get the file from the page.
339    */

340   public IFile getModelFile()
341   {
342     return newFileCreationPage.getModelFile();
343   }
344 }
345
Popular Tags