KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > ecore2xml > presentation > Ecore2XMLModelWizard


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 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: Ecore2XMLModelWizard.java,v 1.4 2005/06/21 16:16:58 khussey Exp $
16  */

17 package org.eclipse.emf.mapping.ecore2xml.presentation;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 //import java.util.Collections;
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
34 //import org.eclipse.emf.ecore.EClassifier;
35

36 import org.eclipse.emf.ecore.resource.Resource;
37 import org.eclipse.emf.ecore.resource.ResourceSet;
38
39 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
40
41 import org.eclipse.emf.ecore.EObject;
42
43 import org.eclipse.emf.ecore.xmi.XMLResource;
44
45 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
46
47 import org.eclipse.core.resources.IContainer;
48 import org.eclipse.core.resources.IFile;
49 import org.eclipse.core.resources.IFolder;
50 import org.eclipse.core.resources.IProject;
51 import org.eclipse.core.resources.IResource;
52 import org.eclipse.core.resources.ResourcesPlugin;
53
54 import org.eclipse.core.runtime.IProgressMonitor;
55
56 import org.eclipse.jface.dialogs.MessageDialog;
57
58 import org.eclipse.jface.viewers.IStructuredSelection;
59
60 import org.eclipse.jface.wizard.Wizard;
61 import org.eclipse.jface.wizard.WizardPage;
62
63 import org.eclipse.swt.SWT;
64
65 import org.eclipse.swt.events.ModifyListener;
66 import org.eclipse.swt.events.ModifyEvent;
67
68 import org.eclipse.swt.layout.GridData;
69 import org.eclipse.swt.layout.GridLayout;
70
71 import org.eclipse.swt.widgets.Combo;
72 import org.eclipse.swt.widgets.Composite;
73 import org.eclipse.swt.widgets.Label;
74
75 import org.eclipse.ui.INewWizard;
76 import org.eclipse.ui.IWorkbench;
77
78 import org.eclipse.ui.actions.WorkspaceModifyOperation;
79
80 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
81
82 import org.eclipse.ui.part.FileEditorInput;
83 import org.eclipse.ui.part.ISetSelectionTarget;
84
85 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLFactory;
86 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLPackage;
87 import org.eclipse.emf.mapping.ecore2xml.ui.Ecore2XMLUIPlugin;
88
89
90 import org.eclipse.core.runtime.Path;
91
92 import org.eclipse.jface.viewers.ISelection;
93 import org.eclipse.jface.viewers.StructuredSelection;
94
95 import org.eclipse.ui.IWorkbenchPage;
96 import org.eclipse.ui.IWorkbenchPart;
97 import org.eclipse.ui.IWorkbenchWindow;
98 import org.eclipse.ui.PartInitException;
99
100
101 /**
102  * This is a simple wizard for creating a new model file.
103  * <!-- begin-user-doc -->
104  * <!-- end-user-doc -->
105  * @generated
106  */

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

115   protected Ecore2XMLPackage ecore2XMLPackage = Ecore2XMLPackage.eINSTANCE;
116
117   /**
118    * This caches an instance of the model factory.
119    * <!-- begin-user-doc -->
120    * <!-- end-user-doc -->
121    * @generated
122    */

123   protected Ecore2XMLFactory ecore2XMLFactory = ecore2XMLPackage.getEcore2XMLFactory();
124
125   /**
126    * This is the file creation page.
127    * <!-- begin-user-doc -->
128    * <!-- end-user-doc -->
129    * @generated
130    */

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

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

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

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

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

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

185   protected Collection JavaDoc getInitialObjectNames()
186   {
187     if (initialObjectNames == null)
188     {
189       initialObjectNames = new ArrayList JavaDoc();
190       initialObjectNames.add(ecore2XMLPackage.getXMLMap().getName());
191     }
192     return initialObjectNames;
193   }
194
195   /**
196    * Create a new model.
197    * <!-- begin-user-doc -->
198    * <!-- end-user-doc -->
199    * @generated
200    */

201   protected EObject createInitialModel()
202   {
203     EClass eClass = (EClass)ecore2XMLPackage.getEClassifier(initialObjectCreationPage.getInitialObjectName());
204     EObject rootObject = ecore2XMLFactory.create(eClass);
205     return rootObject;
206   }
207
208   /**
209    * Do the work after everything is specified.
210    * <!-- begin-user-doc -->
211    * <!-- end-user-doc -->
212    * @generated
213    */

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

317   public class Ecore2XMLModelWizardNewFileCreationPage extends WizardNewFileCreationPage
318   {
319     /**
320      * Pass in the selection.
321      * <!-- begin-user-doc -->
322      * <!-- end-user-doc -->
323      * @generated
324      */

325     public Ecore2XMLModelWizardNewFileCreationPage(String JavaDoc pageId, IStructuredSelection selection)
326     {
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     protected boolean validatePage()
337     {
338       if (super.validatePage())
339       {
340         // Make sure the file ends in ".ecore2xml".
341
//
342
String JavaDoc requiredExt = Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameExtension"); //$NON-NLS-1$
343
String JavaDoc enteredExt = new Path(getFileName()).getFileExtension();
344         if (enteredExt == null || !enteredExt.equals(requiredExt))
345         {
346           setErrorMessage(Ecore2XMLUIPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object JavaDoc [] { requiredExt })); //$NON-NLS-1$
347
return false;
348         }
349         else
350         {
351           return true;
352         }
353       }
354       else
355       {
356         return false;
357       }
358     }
359
360     /**
361      * <!-- begin-user-doc -->
362      * <!-- end-user-doc -->
363      * @generated
364      */

365     public IFile getModelFile()
366     {
367       return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName()));
368     }
369   }
370
371   /**
372    * This is the page where the type of object to create is selected.
373    * <!-- begin-user-doc -->
374    * <!-- end-user-doc -->
375    * @generated
376    */

377   public class Ecore2XMLModelWizardInitialObjectCreationPage extends WizardPage
378   {
379     /**
380      * <!-- begin-user-doc -->
381      * <!-- end-user-doc -->
382      * @generated
383      */

384     protected Combo initialObjectField;
385
386     /**
387      * @generated
388      * <!-- begin-user-doc -->
389      * <!-- end-user-doc -->
390      */

391     protected List JavaDoc encodings;
392
393     /**
394      * <!-- begin-user-doc -->
395      * <!-- end-user-doc -->
396      * @generated
397      */

398     protected Combo encodingField;
399
400     /**
401      * Pass in the selection.
402      * <!-- begin-user-doc -->
403      * <!-- end-user-doc -->
404      * @generated
405      */

406     public Ecore2XMLModelWizardInitialObjectCreationPage(String JavaDoc pageId)
407     {
408       super(pageId);
409     }
410
411     /**
412      * <!-- begin-user-doc -->
413      * <!-- end-user-doc -->
414      * @generated
415      */

416     public void createControl(Composite parent)
417     {
418       Composite composite = new Composite(parent, SWT.NONE);
419       {
420         GridLayout layout = new GridLayout();
421         layout.numColumns = 1;
422         layout.verticalSpacing = 12;
423         composite.setLayout(layout);
424
425         GridData data = new GridData();
426         data.verticalAlignment = GridData.FILL;
427         data.grabExcessVerticalSpace = true;
428         data.horizontalAlignment = GridData.FILL;
429         composite.setLayoutData(data);
430       }
431
432       Label containerLabel = new Label(composite, SWT.LEFT);
433       {
434         containerLabel.setText(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_ModelObject")); //$NON-NLS-1$
435

436         GridData data = new GridData();
437         data.horizontalAlignment = GridData.FILL;
438         containerLabel.setLayoutData(data);
439       }
440
441       initialObjectField = new Combo(composite, SWT.BORDER);
442       {
443         GridData data = new GridData();
444         data.horizontalAlignment = GridData.FILL;
445         data.grabExcessHorizontalSpace = true;
446         initialObjectField.setLayoutData(data);
447       }
448
449       for (Iterator JavaDoc i = getInitialObjectNames().iterator(); i.hasNext(); )
450       {
451         initialObjectField.add(getLabel((String JavaDoc)i.next()));
452       }
453
454       if (initialObjectField.getItemCount() == 1)
455       {
456         initialObjectField.select(0);
457       }
458       initialObjectField.addModifyListener(validator);
459
460       Label encodingLabel = new Label(composite, SWT.LEFT);
461       {
462         encodingLabel.setText(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_XMLEncoding")); //$NON-NLS-1$
463

464         GridData data = new GridData();
465         data.horizontalAlignment = GridData.FILL;
466         encodingLabel.setLayoutData(data);
467       }
468       encodingField = new Combo(composite, SWT.BORDER);
469       {
470         GridData data = new GridData();
471         data.horizontalAlignment = GridData.FILL;
472         data.grabExcessHorizontalSpace = true;
473         encodingField.setLayoutData(data);
474       }
475
476       for (Iterator JavaDoc i = getEncodings().iterator(); i.hasNext(); )
477       {
478         encodingField.add((String JavaDoc)i.next());
479       }
480
481       encodingField.select(0);
482       encodingField.addModifyListener(validator);
483
484       setPageComplete(validatePage());
485       setControl(composite);
486     }
487
488     /**
489      * <!-- begin-user-doc -->
490      * <!-- end-user-doc -->
491      * @generated
492      */

493     protected ModifyListener validator =
494       new ModifyListener()
495       {
496         public void modifyText(ModifyEvent e)
497         {
498           setPageComplete(validatePage());
499         }
500       };
501
502     /**
503      * <!-- begin-user-doc -->
504      * <!-- end-user-doc -->
505      * @generated
506      */

507     protected boolean validatePage()
508     {
509       return getInitialObjectName() != null && getEncodings().contains(encodingField.getText());
510     }
511
512     /**
513      * <!-- begin-user-doc -->
514      * <!-- end-user-doc -->
515      * @generated
516      */

517     public void setVisible(boolean visible)
518     {
519       super.setVisible(visible);
520       if (visible)
521       {
522         if (initialObjectField.getItemCount() == 1)
523         {
524           initialObjectField.clearSelection();
525           encodingField.setFocus();
526         }
527         else
528         {
529           encodingField.clearSelection();
530           initialObjectField.setFocus();
531         }
532       }
533     }
534
535     /**
536      * <!-- begin-user-doc -->
537      * <!-- end-user-doc -->
538      * @generated
539      */

540     public String JavaDoc getInitialObjectName()
541     {
542       String JavaDoc label = initialObjectField.getText();
543
544       for (Iterator JavaDoc i = getInitialObjectNames().iterator(); i.hasNext(); )
545       {
546         String JavaDoc name = (String JavaDoc)i.next();
547         if (getLabel(name).equals(label))
548         {
549           return name;
550         }
551       }
552       return null;
553     }
554
555     /**
556      * <!-- begin-user-doc -->
557      * <!-- end-user-doc -->
558      * @generated
559      */

560     public String JavaDoc getEncoding()
561     {
562       return encodingField.getText();
563     }
564
565     /**
566      * Returns the label for the specified type name.
567      * <!-- begin-user-doc -->
568      * <!-- end-user-doc -->
569      * @generated
570      */

571     protected String JavaDoc getLabel(String JavaDoc typeName)
572     {
573       try
574       {
575         return Ecore2XMLUIPlugin.INSTANCE.getString("_UI_" + typeName + "_type"); //$NON-NLS-1$
576
}
577       catch(MissingResourceException JavaDoc mre)
578       {
579         Ecore2XMLUIPlugin.INSTANCE.log(mre);
580       }
581       return typeName;
582     }
583
584     /**
585      * <!-- begin-user-doc -->
586      * <!-- end-user-doc -->
587      * @generated
588      */

589     protected Collection JavaDoc getEncodings()
590     {
591       if (encodings == null)
592       {
593         encodings = new ArrayList JavaDoc();
594         for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) //$NON-NLS-1$
595
{
596           encodings.add(stringTokenizer.nextToken());
597         }
598       }
599       return encodings;
600     }
601   }
602
603   /**
604    * The framework calls this to create the contents of the wizard.
605    * <!-- begin-user-doc -->
606    * <!-- end-user-doc -->
607    * @generated
608    */

609   public void addPages()
610   {
611     // Create a page, set the title, and the initial model file name.
612
//
613
newFileCreationPage = new Ecore2XMLModelWizardNewFileCreationPage("Whatever", selection); //$NON-NLS-1$
614
newFileCreationPage.setTitle(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLModelWizard_label")); //$NON-NLS-1$
615
newFileCreationPage.setDescription(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLModelWizard_description")); //$NON-NLS-1$
616
newFileCreationPage.setFileName(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameDefaultBase") + "." + Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameExtension")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
617
addPage(newFileCreationPage);
618
619     // Try and get the resource selection to determine a current directory for the file dialog.
620
//
621
if (selection != null && !selection.isEmpty())
622     {
623       // Get the resource...
624
//
625
Object JavaDoc selectedElement = selection.iterator().next();
626       if (selectedElement instanceof IResource)
627       {
628         // Get the resource parent, if its a file.
629
//
630
IResource selectedResource = (IResource)selectedElement;
631         if (selectedResource.getType() == IResource.FILE)
632         {
633           selectedResource = selectedResource.getParent();
634         }
635
636         // This gives us a directory...
637
//
638
if (selectedResource instanceof IFolder || selectedResource instanceof IProject)
639         {
640           // Set this for the container.
641
//
642
newFileCreationPage.setContainerFullPath(selectedResource.getFullPath());
643
644           // Make up a unique new name here.
645
//
646
String JavaDoc defaultModelBaseFilename = Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameDefaultBase"); //$NON-NLS-1$
647
String JavaDoc defaultModelFilenameExtension = Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLEditorFilenameExtension"); //$NON-NLS-1$
648
String JavaDoc modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension; //$NON-NLS-1$
649
for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i)
650           {
651             modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension; //$NON-NLS-1$
652
}
653           newFileCreationPage.setFileName(modelFilename);
654         }
655       }
656     }
657     initialObjectCreationPage = new Ecore2XMLModelWizardInitialObjectCreationPage("Whatever2"); //$NON-NLS-1$
658
initialObjectCreationPage.setTitle(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Ecore2XMLModelWizard_label")); //$NON-NLS-1$
659
initialObjectCreationPage.setDescription(Ecore2XMLUIPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description")); //$NON-NLS-1$
660
addPage(initialObjectCreationPage);
661   }
662
663   /**
664    * Get the file from the page.
665    * <!-- begin-user-doc -->
666    * <!-- end-user-doc -->
667    * @generated
668    */

669   public IFile getModelFile()
670   {
671     return newFileCreationPage.getModelFile();
672   }
673
674 }
675
Popular Tags