KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > sdo > presentation > SDOModelWizard


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2003-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: SDOModelWizard.java,v 1.13 2005/06/12 13:30:37 emerks Exp $
16  */

17 package org.eclipse.emf.ecore.sdo.presentation;
18
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.MissingResourceException JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import org.eclipse.core.resources.IContainer;
33 import org.eclipse.core.resources.IFile;
34 import org.eclipse.core.resources.IFolder;
35 import org.eclipse.core.resources.IProject;
36 import org.eclipse.core.resources.IResource;
37 import org.eclipse.core.resources.ResourcesPlugin;
38 import org.eclipse.core.runtime.IProgressMonitor;
39 import org.eclipse.core.runtime.Path;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.viewers.StructuredSelection;
44 import org.eclipse.jface.wizard.Wizard;
45 import org.eclipse.jface.wizard.WizardPage;
46 import org.eclipse.swt.SWT;
47 import org.eclipse.swt.events.ModifyListener;
48 import org.eclipse.swt.events.ModifyEvent;
49
50 import org.eclipse.swt.custom.CCombo;
51 import org.eclipse.swt.events.SelectionAdapter;
52 import org.eclipse.swt.events.SelectionEvent;
53 import org.eclipse.swt.layout.GridData;
54 import org.eclipse.swt.layout.GridLayout;
55 import org.eclipse.swt.layout.RowLayout;
56 import org.eclipse.swt.widgets.Button;
57 import org.eclipse.swt.widgets.Composite;
58 import org.eclipse.swt.widgets.Combo;
59 import org.eclipse.swt.widgets.FileDialog;
60 import org.eclipse.swt.widgets.Label;
61 import org.eclipse.ui.INewWizard;
62 import org.eclipse.ui.IWorkbench;
63 import org.eclipse.ui.IWorkbenchPage;
64 import org.eclipse.ui.IWorkbenchPart;
65 import org.eclipse.ui.IWorkbenchWindow;
66 import org.eclipse.ui.PartInitException;
67 import org.eclipse.ui.actions.WorkspaceModifyOperation;
68 import org.eclipse.ui.dialogs.ResourceSelectionDialog;
69 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
70 import org.eclipse.ui.part.FileEditorInput;
71 import org.eclipse.ui.part.ISetSelectionTarget;
72
73 import org.eclipse.emf.common.util.URI;
74 import org.eclipse.emf.ecore.EAttribute;
75 import org.eclipse.emf.ecore.EClass;
76 import org.eclipse.emf.ecore.EClassifier;
77 import org.eclipse.emf.ecore.EObject;
78 import org.eclipse.emf.ecore.EPackage;
79 import org.eclipse.emf.ecore.EStructuralFeature;
80 import org.eclipse.emf.ecore.resource.Resource;
81 import org.eclipse.emf.ecore.resource.ResourceSet;
82
83 import org.eclipse.emf.ecore.sdo.EDataGraph;
84 import org.eclipse.emf.ecore.sdo.SDOFactory;
85 import org.eclipse.emf.ecore.sdo.SDOPackage;
86 import org.eclipse.emf.ecore.sdo.provider.SDOEditPlugin;
87
88
89 import org.eclipse.emf.ecore.sdo.impl.DynamicEDataObjectImpl;
90 import org.eclipse.emf.ecore.util.ExtendedMetaData;
91 import org.eclipse.emf.ecore.xmi.XMLResource;
92 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
93
94 //import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
95

96
97 /**
98  * This is a simple wizard for creating a new model file.
99  */

100 public class SDOModelWizard extends Wizard implements INewWizard
101 {
102   /**
103    * This caches an instance of the model package.
104    * <!-- begin-user-doc -->
105    * <!-- end-user-doc -->
106    * @generated
107    */

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

116   protected SDOFactory sdoFactory = sdoPackage.getSDOFactory();
117
118   /**
119    * This is the file creation page.
120    */

121   protected SDOModelWizardNewFileCreationPage newFileCreationPage;
122
123   /**
124    * This is the file creation page.
125    */

126   protected SDOModelWizardInitialObjectCreationPage initialObjectCreationPage;
127
128   /**
129    * Remember the selection during initialization for populating the default container.
130    */

131   protected IStructuredSelection selection;
132
133   /**
134    * Remember the workbench during initialization.
135    */

136   protected IWorkbench workbench;
137
138   /**
139    * Caches the names of the types that can be created as the root object.
140    * <!-- begin-user-doc -->
141    * <!-- end-user-doc -->
142    * @generated
143    */

144   protected List JavaDoc initialObjectNames;
145
146   /**
147    * This just records the information.
148    */

149   public void init(IWorkbench workbench, IStructuredSelection selection)
150   {
151     this.workbench = workbench;
152     this.selection = selection;
153     setWindowTitle(SDOEditorPlugin.INSTANCE.getString("_UI_Wizard_label"));
154     setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(SDOEditorPlugin.INSTANCE.getImage("full/wizban/NewSDO")));
155   }
156
157   /**
158    * Returns 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 Collection JavaDoc getInitialObjectNames()
164   {
165     if (initialObjectNames == null)
166     {
167       initialObjectNames = new ArrayList JavaDoc();
168       for (Iterator JavaDoc classifiers = sdoPackage.getEClassifiers().iterator(); classifiers.hasNext(); )
169       {
170         EClassifier eClassifier = (EClassifier)classifiers.next();
171         if (eClassifier instanceof EClass)
172         {
173           EClass eClass = (EClass)eClassifier;
174           if (!eClass.isAbstract())
175           {
176             initialObjectNames.add(eClass.getName());
177           }
178         }
179       }
180       Collections.sort(initialObjectNames, java.text.Collator.getInstance());
181     }
182     return initialObjectNames;
183   }
184
185   /**
186    * Create a new model.
187    * <!-- begin-user-doc -->
188    * <!-- end-user-doc -->
189    * @generated
190    */

191   protected EObject createInitialModel()
192   {
193     EClass eClass = (EClass)sdoPackage.getEClassifier(initialObjectCreationPage.getInitialObjectName());
194     EObject rootObject = sdoFactory.create(eClass);
195     return rootObject;
196   }
197
198   /**
199    * Do the work after everything is specified.
200    */

201   public boolean performFinish()
202   {
203     try
204     {
205       // Remember the file.
206
//
207
final IFile modelFile = getModelFile();
208
209       // Do the work within an operation.
210
//
211
WorkspaceModifyOperation operation =
212         new WorkspaceModifyOperation()
213         {
214           protected void execute(IProgressMonitor progressMonitor)
215           {
216             try
217             {
218               // Create the initial model object.
219
//
220
EDataGraph eDataGraph = initialObjectCreationPage.createRootObject();
221
222               // Get the URI of the model file.
223
//
224
URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString());
225               eDataGraph.getDataGraphResource().setURI(fileURI);
226
227               // Save the contents of the resource to the file system.
228
//
229
Map JavaDoc options = new HashMap JavaDoc();
230               options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding());
231               eDataGraph.getDataGraphResource().save(options);
232             }
233             catch (Exception JavaDoc exception)
234             {
235               SDOEditorPlugin.INSTANCE.log(exception);
236             }
237             finally
238             {
239               progressMonitor.done();
240             }
241           }
242         };
243
244       getContainer().run(false, false, operation);
245
246       // Select the new file resource in the current view.
247
//
248
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
249       IWorkbenchPage page = workbenchWindow.getActivePage();
250       final IWorkbenchPart activePart = page.getActivePart();
251       if (activePart instanceof ISetSelectionTarget)
252       {
253         final ISelection targetSelection = new StructuredSelection(modelFile);
254         getShell().getDisplay().asyncExec
255           (new Runnable JavaDoc()
256            {
257              public void run()
258              {
259                ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
260              }
261            });
262       }
263
264       // Open an editor on the new file.
265
//
266
try
267       {
268         page.openEditor
269           (new FileEditorInput(modelFile),
270            workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
271       }
272       catch (PartInitException exception)
273       {
274         MessageDialog.openError(workbenchWindow.getShell(), SDOEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
275         return false;
276       }
277
278       return true;
279     }
280     catch (Exception JavaDoc exception)
281     {
282       SDOEditorPlugin.INSTANCE.log(exception);
283       return false;
284     }
285   }
286
287   /**
288    * This is the one page of the wizard.
289    */

290   public class SDOModelWizardNewFileCreationPage extends WizardNewFileCreationPage
291   {
292     /**
293      * Remember the model file.
294      */

295     protected IFile modelFile;
296
297     /**
298      * Pass in the selection.
299      */

300     public SDOModelWizardNewFileCreationPage(String JavaDoc pageId, IStructuredSelection selection)
301     {
302       super(pageId, selection);
303     }
304
305     /**
306      * The framework calls this to see if the file is correct.
307      */

308     protected boolean validatePage()
309     {
310       if (super.validatePage())
311       {
312         // Make sure the file ends in ".sdo".
313
//
314
String JavaDoc requiredExt = SDOEditorPlugin.INSTANCE.getString("_UI_SDOEditorFilenameExtension");
315         String JavaDoc enteredExt = new Path(getFileName()).getFileExtension();
316         if (enteredExt == null || !enteredExt.equals(requiredExt))
317         {
318           setErrorMessage(SDOEditorPlugin.INSTANCE.getString("_WARN_FilenameExtension", new Object JavaDoc [] { requiredExt }));
319           return false;
320         }
321         else
322         {
323           return true;
324         }
325       }
326       else
327       {
328         return false;
329       }
330     }
331
332     /**
333      * Store the dialog field settings upon completion.
334      */

335     public boolean performFinish()
336     {
337       modelFile = getModelFile();
338       return true;
339     }
340
341     public IFile getModelFile()
342     {
343       return
344         modelFile == null ?
345           ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName())) :
346           modelFile;
347     }
348   }
349
350   /**
351    * This is the page where the type of object to create is selected.
352    */

353   public class SDOModelWizardInitialObjectCreationPage extends WizardPage
354   {
355     /**
356      * <!-- begin-user-doc -->
357      * <!-- end-user-doc -->
358      * @generated
359      */

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

367     protected List JavaDoc encodings;
368
369     /**
370      * The URI of the model to fetch
371      */

372     protected String JavaDoc modelURI;
373
374     /**
375      */

376     protected CCombo modelURIField;
377
378     /**
379      * The name of the model to fetch
380      */

381     protected String JavaDoc modelObject;
382
383     /**
384      */

385     protected EDataGraph eDataGraph;
386
387     protected boolean isType;
388
389     /**
390      */

391     protected String JavaDoc encoding;
392
393     /**
394      */

395     protected CCombo encodingField;
396
397     /**
398      * Pass in the selection.
399      */

400     public SDOModelWizardInitialObjectCreationPage(String JavaDoc pageId)
401     {
402       super(pageId);
403     }
404
405     public EDataGraph createRootObject()
406     {
407       if (isType)
408       {
409         eDataGraph.createRootObject(eDataGraph.getType(getModelURI(), getModelObject()));
410       }
411       else
412       {
413         EClass documentRoot = (EClass)eDataGraph.getEClassifier(getModelURI(), "");
414         EObject rootObject = eDataGraph.createEObject(documentRoot);
415         eDataGraph.setERootObject(rootObject);
416
417         EStructuralFeature eStructuralFeature = documentRoot.getEStructuralFeature(getModelObject());
418         if (eStructuralFeature instanceof EAttribute)
419         {
420         }
421         else
422         {
423           rootObject.eSet(eStructuralFeature, eDataGraph.createEObject((EClass)eStructuralFeature.getEType()));
424         }
425       }
426       return eDataGraph;
427     }
428
429     /**
430      */

431     public void createControl(Composite parent)
432     {
433       Composite composite = new Composite(parent, SWT.NONE);
434       {
435         GridLayout layout = new GridLayout();
436         layout.numColumns = 2;
437         layout.verticalSpacing = 12;
438         composite.setLayout(layout);
439
440         GridData data = new GridData();
441         data.verticalAlignment = GridData.FILL;
442         data.grabExcessVerticalSpace = true;
443         data.horizontalAlignment = GridData.FILL;
444         composite.setLayoutData(data);
445       }
446
447       Label modelURILabel = new Label(composite, SWT.LEFT);
448       {
449         modelURILabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_ModelURI"));
450
451         GridData data = new GridData();
452         data.horizontalAlignment = GridData.FILL;
453         modelURILabel.setLayoutData(data);
454       }
455
456       Composite buttonComposite = new Composite(composite, SWT.NONE);
457       {
458         GridData data = new GridData();
459         data.horizontalAlignment = GridData.END;
460         buttonComposite.setLayoutData(data);
461       
462         RowLayout layout = new RowLayout();
463         layout.justify = true;
464         layout.pack = true;
465         layout.spacing = 15;
466         buttonComposite.setLayout(layout);
467       }
468       Button modelURIBrowseFileSystemButton = new Button(buttonComposite, SWT.PUSH);
469       modelURIBrowseFileSystemButton.setText(SDOEditorPlugin.INSTANCE.getString("_UI_BrowseFileSystem_label"));
470
471       final String JavaDoc [] filters = { "*.xsd;*.wsdl;*.ecore;*.emof", "*.xsd;*.wsdl", "*.xsd", "*.wsdl", "*.ecore;*.emof", "*.ecore", "*.emof" };
472       final List JavaDoc suffixList = Arrays.asList(new String JavaDoc [] { "xsd", "wsdl", "ecore", "emof" });
473       modelURIBrowseFileSystemButton.addSelectionListener
474         (new SelectionAdapter()
475          {
476            public void widgetSelected(SelectionEvent event)
477            {
478              FileDialog fileDialog = new FileDialog(SDOModelWizardInitialObjectCreationPage.this.getShell());
479              fileDialog.setFilterExtensions(filters);
480              fileDialog.open();
481              if (fileDialog.getFileName() != null && fileDialog.getFileName().length() > 0)
482              {
483                String JavaDoc filePath = fileDialog.getFilterPath() + File.separator + fileDialog.getFileName();
484                modelURIField.setText((modelURIField.getText() + " " + URI.createFileURI(filePath).toString()).trim());
485                loadModelURI();
486              }
487            }
488          });
489       
490       Button modelURIBrowseWorkspaceButton = new Button(buttonComposite, SWT.PUSH);
491       modelURIBrowseWorkspaceButton.setText(SDOEditorPlugin.INSTANCE.getString("_UI_BrowseWorkspace_label"));
492       modelURIBrowseWorkspaceButton.addSelectionListener
493         (new SelectionAdapter()
494          {
495            public void widgetSelected(SelectionEvent event)
496            {
497              ResourceSelectionDialog resourceSelectionDialog =
498                new ResourceSelectionDialog
499                  (SDOModelWizardInitialObjectCreationPage.this.getShell(),
500                   ResourcesPlugin.getWorkspace().getRoot(),
501                   SDOEditorPlugin.INSTANCE.getString("_UI_SelectTheModel_label"));
502
503              resourceSelectionDialog.open();
504              Object JavaDoc [] result = resourceSelectionDialog.getResult();
505              if (result != null)
506              {
507                StringBuffer JavaDoc text = new StringBuffer JavaDoc();
508                for (int i = 0; i < result.length; ++i)
509                {
510                  IResource resource = (IResource)result[i];
511                  if (resource.getType() == IResource.FILE && suffixList.contains(resource.getFullPath().getFileExtension()))
512                  {
513                    text.append(URI.createPlatformResourceURI(resource.getFullPath().toString()));
514                    text.append(" ");
515                  }
516                }
517                modelURIField.setText((modelURIField.getText() + " " + text.toString()).trim());
518                loadModelURI();
519              }
520            }
521          });
522
523       modelURIField = new CCombo(composite, SWT.BORDER);
524       {
525         GridData data = new GridData();
526         data.horizontalAlignment = GridData.FILL;
527         data.grabExcessHorizontalSpace = true;
528         data.horizontalSpan = 2;
529         modelURIField.setLayoutData(data);
530       }
531
532       List JavaDoc packageURIs = new ArrayList JavaDoc(EPackage.Registry.INSTANCE.keySet());
533       Collections.sort(packageURIs, java.text.Collator.getInstance());
534       for (Iterator JavaDoc i = packageURIs.iterator(); i.hasNext(); )
535       {
536         String JavaDoc objectName = (String JavaDoc)i.next();
537         modelURIField.add(objectName);
538       }
539
540       modelURIField.addSelectionListener
541         (new SelectionAdapter()
542          {
543            public void widgetSelected(SelectionEvent e)
544            {
545              loadModelURI();
546            }
547          });
548
549
550       Label initialObjectLabel = new Label(composite, SWT.LEFT);
551       {
552         initialObjectLabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_ModelObject"));
553
554         GridData data = new GridData();
555         data.horizontalAlignment = GridData.FILL;
556         initialObjectLabel.setLayoutData(data);
557       }
558
559       Button loadModelButton = new Button(composite, SWT.PUSH);
560       loadModelButton.setText(SDOEditorPlugin.INSTANCE.getString("_UI_Load_label"));
561       {
562         GridData data = new GridData();
563         data.horizontalAlignment = GridData.END;
564         loadModelButton.setLayoutData(data);
565       }
566       
567       loadModelButton.addSelectionListener
568         (new SelectionAdapter()
569          {
570            public void widgetSelected(SelectionEvent event)
571            {
572              loadModelURI();
573            }
574          });
575
576       initialObjectField = new Combo(composite, SWT.BORDER);
577       {
578         GridData data = new GridData();
579         data.horizontalAlignment = GridData.FILL;
580         data.grabExcessHorizontalSpace = true;
581         data.horizontalSpan = 2;
582         initialObjectField.setLayoutData(data);
583       }
584       initialObjectField.addSelectionListener
585         (new SelectionAdapter()
586          {
587            public void widgetSelected(SelectionEvent e)
588            {
589              setPageComplete(isPageComplete());
590            }
591          });
592
593       Label encodingLabel = new Label(composite, SWT.LEFT);
594       {
595         encodingLabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_XMLEncoding"));
596
597         GridData data = new GridData();
598         data.horizontalAlignment = GridData.FILL;
599         data.horizontalSpan = 2;
600         encodingLabel.setLayoutData(data);
601       }
602       encodingField = new CCombo(composite, SWT.BORDER);
603       {
604         GridData data = new GridData();
605         data.horizontalAlignment = GridData.FILL;
606         data.grabExcessHorizontalSpace = true;
607         data.horizontalSpan = 2;
608         encodingField.setLayoutData(data);
609       }
610
611       for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(SDOEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); )
612       {
613         encodingField.add(stringTokenizer.nextToken());
614       }
615       encodingField.select(0);
616
617       setControl(composite);
618     }
619
620     /**
621      * <!-- begin-user-doc -->
622      * <!-- end-user-doc -->
623      * @generated
624      */

625     protected ModifyListener validator =
626       new ModifyListener()
627       {
628         public void modifyText(ModifyEvent e)
629         {
630           setPageComplete(validatePage());
631         }
632       };
633
634     /**
635      * <!-- begin-user-doc -->
636      * <!-- end-user-doc -->
637      * @generated
638      */

639     protected boolean validatePage()
640     {
641       return getInitialObjectName() != null && getEncodings().contains(encodingField.getText());
642     }
643
644     /**
645      * <!-- begin-user-doc -->
646      * <!-- end-user-doc -->
647      * @generated
648      */

649     public void setVisible(boolean visible)
650     {
651       super.setVisible(visible);
652       if (visible)
653       {
654         if (initialObjectField.getItemCount() == 1)
655         {
656           initialObjectField.clearSelection();
657           encodingField.setFocus();
658         }
659         else
660         {
661           encodingField.clearSelection();
662           initialObjectField.setFocus();
663         }
664       }
665     }
666
667     protected void loadModelURI()
668     {
669       eDataGraph = SDOFactory.eINSTANCE.createEDataGraph();
670       ResourceSet resourceSet = eDataGraph.getResourceSet();
671       Resource resource = resourceSet.getResource(URI.createURI(getModelURI()), true);
672       List JavaDoc objectNames = new ArrayList JavaDoc();
673       for (Iterator JavaDoc i = resource.getContents().iterator(); i.hasNext(); )
674       {
675         Object JavaDoc object = i.next();
676         if (object instanceof EPackage)
677         {
678           EPackage ePackage = (EPackage)object;
679
680           if (resource.getResourceSet() == resourceSet)
681           {
682             ePackage.setEFactoryInstance(new DynamicEDataObjectImpl.FactoryImpl());
683             resourceSet.getPackageRegistry().put(resource.getURI().toString(), ePackage);
684             resourceSet.getPackageRegistry().put(ePackage.getNsURI(), ePackage);
685           }
686
687           EClass documentRoot = ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
688           if (documentRoot != null)
689           {
690             isType = false;
691             for (Iterator JavaDoc j = ExtendedMetaData.INSTANCE.getElements(documentRoot).iterator(); j.hasNext(); )
692             {
693               EStructuralFeature eStructuralFeature = (EStructuralFeature)j.next();
694               objectNames.add(eStructuralFeature.getName());
695             }
696           }
697           else
698           {
699             isType = true;
700             for (Iterator JavaDoc j = ePackage.getEClassifiers().iterator(); j.hasNext(); )
701             {
702               EClassifier eClassifier = (EClassifier)j.next();
703               if (eClassifier instanceof EClass)
704               {
705                 objectNames.add(eClassifier.getName());
706               }
707             }
708           }
709
710           break;
711         }
712       }
713
714       initialObjectField.removeAll();
715       Collections.sort(objectNames, java.text.Collator.getInstance());
716       for (Iterator JavaDoc i = objectNames.iterator(); i.hasNext(); )
717       {
718         String JavaDoc objectName = (String JavaDoc)i.next();
719         initialObjectField.add(objectName);
720       }
721
722       setPageComplete(isPageComplete());
723     }
724
725     public boolean isPageComplete()
726     {
727       return initialObjectField.getSelectionIndex() != -1;
728     }
729
730     /**
731      * Store the dialog field settings upon completion.
732      */

733     public boolean performFinish()
734     {
735       modelURI = getModelURI();
736       modelObject = getModelObject();
737       encoding = getEncoding();
738       return true;
739     }
740
741     /**
742      * <!-- begin-user-doc -->
743      * <!-- end-user-doc -->
744      * @generated
745      */

746     public String JavaDoc getInitialObjectName()
747     {
748       String JavaDoc label = initialObjectField.getText();
749
750       for (Iterator JavaDoc i = getInitialObjectNames().iterator(); i.hasNext(); )
751       {
752         String JavaDoc name = (String JavaDoc)i.next();
753         if (getLabel(name).equals(label))
754         {
755           return name;
756         }
757       }
758       return null;
759     }
760
761     /**
762      */

763     public String JavaDoc getModelURI()
764     {
765       return
766         modelURI == null ?
767           modelURIField.getText() :
768           modelURI;
769     }
770
771     /**
772      */

773     public String JavaDoc getModelObject()
774     {
775       return
776         modelObject == null ?
777           initialObjectField.getText() :
778           modelObject;
779     }
780
781     /**
782      */

783     public String JavaDoc getEncoding()
784     {
785       return
786         encoding == null ?
787           encodingField.getText() :
788           encoding;
789     }
790
791     /**
792      * Returns the label for the specified type name.
793      * <!-- begin-user-doc -->
794      * <!-- end-user-doc -->
795      * @generated
796      */

797     protected String JavaDoc getLabel(String JavaDoc typeName)
798     {
799       try
800       {
801         return SDOEditPlugin.INSTANCE.getString("_UI_" + typeName + "_type");
802       }
803       catch(MissingResourceException JavaDoc mre)
804       {
805         SDOEditorPlugin.INSTANCE.log(mre);
806       }
807       return typeName;
808     }
809
810     /**
811      * <!-- begin-user-doc -->
812      * <!-- end-user-doc -->
813      * @generated
814      */

815     protected Collection JavaDoc getEncodings()
816     {
817       if (encodings == null)
818       {
819         encodings = new ArrayList JavaDoc();
820         for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(SDOEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); )
821         {
822           encodings.add(stringTokenizer.nextToken());
823         }
824       }
825       return encodings;
826     }
827   }
828
829   /**
830    * The framework calls this to create the contents of the wizard.
831    */

832   public void addPages()
833   {
834     // Create a page, set the title, and the initial model file name.
835
//
836
newFileCreationPage = new SDOModelWizardNewFileCreationPage("Whatever", selection);
837     newFileCreationPage.setTitle(SDOEditorPlugin.INSTANCE.getString("_UI_SDOModelWizard_label"));
838     newFileCreationPage.setDescription(SDOEditorPlugin.INSTANCE.getString("_UI_SDOModelWizard_description"));
839     newFileCreationPage.setFileName(SDOEditorPlugin.INSTANCE.getString("_UI_SDOEditorFilenameDefaultBase") + "." + SDOEditorPlugin.INSTANCE.getString("_UI_SDOEditorFilenameExtension"));
840     addPage(newFileCreationPage);
841
842     // Try and get the resource selection to determine a current directory for the file dialog.
843
//
844
if (selection != null && !selection.isEmpty())
845     {
846       // Get the resource...
847
//
848
Object JavaDoc selectedElement = selection.iterator().next();
849       if (selectedElement instanceof IResource)
850       {
851         // Get the resource parent, if its a file.
852
//
853
IResource selectedResource = (IResource)selectedElement;
854         if (selectedResource.getType() == IResource.FILE)
855         {
856           selectedResource = selectedResource.getParent();
857         }
858
859         // This gives us a directory...
860
//
861
if (selectedResource instanceof IFolder || selectedResource instanceof IProject)
862         {
863           // Set this for the container.
864
//
865
newFileCreationPage.setContainerFullPath(selectedResource.getFullPath());
866
867           // Make up a unique new name here.
868
//
869
String JavaDoc defaultModelBaseFilename = SDOEditorPlugin.INSTANCE.getString("_UI_SDOEditorFilenameDefaultBase");
870           String JavaDoc defaultModelFilenameExtension = SDOEditorPlugin.INSTANCE.getString("_UI_SDOEditorFilenameExtension");
871           String JavaDoc modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension;
872           for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i)
873           {
874             modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension;
875           }
876           newFileCreationPage.setFileName(modelFilename);
877         }
878       }
879     }
880     initialObjectCreationPage = new SDOModelWizardInitialObjectCreationPage("Whatever2");
881     initialObjectCreationPage.setTitle(SDOEditorPlugin.INSTANCE.getString("_UI_SDOModelWizard_label"));
882     initialObjectCreationPage.setDescription(SDOEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description"));
883     addPage(initialObjectCreationPage);
884   }
885
886   /**
887    * Get the file from the page.
888    */

889   public IFile getModelFile()
890   {
891     return newFileCreationPage.getModelFile();
892   }
893 }
894
Popular Tags