KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > ui > EMFModelWizard


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: EMFModelWizard.java,v 1.9 2005/06/09 21:40:34 marcelop Exp $
16  */

17 package org.eclipse.emf.importer.ui;
18
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.jface.dialogs.IMessageProvider;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.viewers.ArrayContentProvider;
31 import org.eclipse.jface.viewers.DoubleClickEvent;
32 import org.eclipse.jface.viewers.IDoubleClickListener;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.ISelectionChangedListener;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.SelectionChangedEvent;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.jface.viewers.TableViewer;
39 import org.eclipse.jface.viewers.ViewerSorter;
40 import org.eclipse.jface.wizard.IWizardNode;
41 import org.eclipse.jface.wizard.IWizardPage;
42 import org.eclipse.jface.wizard.Wizard;
43 import org.eclipse.jface.wizard.WizardSelectionPage;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Display;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Table;
51 import org.eclipse.ui.INewWizard;
52 import org.eclipse.ui.IWorkbench;
53 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
54
55 import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
56 import org.eclipse.emf.common.util.URI;
57 import org.eclipse.emf.ecore.resource.Resource;
58 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
59 import org.eclipse.emf.importer.ImporterPlugin;
60 import org.eclipse.emf.importer.ui.contribution.IModelImporterWizard;
61 import org.eclipse.emf.importer.ui.contribution.ModelImporterDescriptor;
62 import org.eclipse.emf.importer.ui.contribution.ModelImporterUtil;
63 import org.eclipse.emf.importer.util.ImporterUtil;
64
65
66 /**
67  * @since 2.1.0
68  */

69 public class EMFModelWizard extends Wizard implements INewWizard
70 {
71   public class NewGenModelFileCreationPage extends WizardNewFileCreationPage
72   {
73     protected boolean firstTime = true;
74
75     /**
76      * Pass in the selection.
77      */

78     public NewGenModelFileCreationPage(String JavaDoc pageId)
79     {
80       super(pageId, selection);
81     }
82
83     /**
84      * The framework calls this to see if the file is correct.
85      */

86     protected boolean validatePage()
87     {
88       if (super.validatePage())
89       {
90         String JavaDoc extension = new Path(getFileName()).getFileExtension();
91         if (extension == null || !extension.equals("genmodel"))
92         {
93           setErrorMessage(ImporterPlugin.INSTANCE.getString("_UI_GeneratorModelFileNameMustEndWithGenModel_message"));
94           return false;
95         }
96         else
97         {
98           genModelContainerPath = getContainerFullPath();
99           genModelFileName = getFileName();
100           return true;
101         }
102       }
103       else
104       {
105         return false;
106       }
107     }
108
109     public void setVisible(boolean visible)
110     {
111       super.setVisible(visible);
112       if (visible && firstTime)
113       {
114         firstTime = false;
115         if (modelFile != null)
116         {
117           String JavaDoc fileName = modelFile.getFullPath().removeFileExtension().lastSegment();
118           setFileName(fileName + ".genmodel");
119         }
120         else
121         {
122           setFileName(getDefaultGenModelFileName());
123         }
124       }
125     }
126
127     public void setFileName(String JavaDoc value)
128     {
129       super.setFileName(value);
130       genModelFileName = value;
131     }
132   }
133
134   public class SelectionPage extends WizardSelectionPage implements ISelectionChangedListener
135   {
136     protected ModelImporterDescriptor modelImporterDescriptor;
137     protected TableViewer modelImpoterDescriptorTableViewer;
138
139     protected boolean firstTime = true;
140     protected Set JavaDoc initializedWizards = new HashSet JavaDoc();
141
142     public SelectionPage(String JavaDoc pageId)
143     {
144       super(pageId);
145     }
146
147     public void dispose()
148     {
149       if (modelImpoterDescriptorTableViewer != null)
150       {
151         modelImpoterDescriptorTableViewer.removeSelectionChangedListener(this);
152         modelImpoterDescriptorTableViewer = null;
153       }
154       modelImporterDescriptor = null;
155
156       if (initializedWizards != null)
157       {
158         initializedWizards.clear();
159         initializedWizards = null;
160       }
161       
162       super.dispose();
163     }
164
165     public void setModelImporterDescriptor(ModelImporterDescriptor modelImporterDescriptor)
166     {
167       this.modelImporterDescriptor = modelImporterDescriptor;
168       if (modelImpoterDescriptorTableViewer != null)
169       {
170         if (modelImporterDescriptor != null)
171         {
172           modelImpoterDescriptorTableViewer.setSelection(new StructuredSelection(modelImporterDescriptor), true);
173         }
174         else
175         {
176           modelImpoterDescriptorTableViewer.setSelection(StructuredSelection.EMPTY);
177         }
178       }
179     }
180
181     public ModelImporterDescriptor getModelImporterDescriptor()
182     {
183       return modelImporterDescriptor;
184     }
185
186     public void setVisible(boolean visible)
187     {
188       super.setVisible(visible);
189       if (visible && firstTime)
190       {
191         firstTime = false;
192         Table table = modelImpoterDescriptorTableViewer.getTable();
193
194         ModelImporterDescriptor descriptor = suggestedDescriptor;
195         if (descriptor != null)
196         {
197           setModelImporterDescriptor(descriptor);
198         }
199         else if (table.getItemCount() > 0)
200         {
201           setModelImporterDescriptor((ModelImporterDescriptor)table.getItem(0).getData());
202         }
203         table.setFocus();
204       }
205     }
206
207     public void createControl(Composite parent)
208     {
209       Composite composite = new Composite(parent, SWT.NONE);
210       {
211         GridLayout layout = new GridLayout();
212         layout.numColumns = 1;
213         layout.verticalSpacing = 12;
214         composite.setLayout(layout);
215
216         GridData data = new GridData();
217         data.verticalAlignment = GridData.FILL;
218         data.grabExcessVerticalSpace = true;
219         data.horizontalAlignment = GridData.FILL;
220         composite.setLayoutData(data);
221       }
222
223       Label label = new Label(composite, SWT.NONE);
224       label.setText(ImporterPlugin.INSTANCE.getString("_UI_SelectModelImporters_label"));
225       {
226         GridData data = new GridData();
227         data.verticalAlignment = SWT.FILL;
228         data.horizontalAlignment = SWT.FILL;
229         label.setLayoutData(data);
230       }
231
232       Table modelImpoterDescriptorTable = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
233       {
234         GridData data = new GridData();
235         data.widthHint = Display.getCurrent().getBounds().width / 5;
236         data.heightHint = Display.getCurrent().getBounds().height / 3;
237         data.verticalAlignment = SWT.FILL;
238         data.horizontalAlignment = SWT.FILL;
239         data.grabExcessHorizontalSpace = true;
240         data.grabExcessVerticalSpace = true;
241         modelImpoterDescriptorTable.setLayoutData(data);
242       }
243
244       modelImpoterDescriptorTableViewer = new TableViewer(modelImpoterDescriptorTable);
245       modelImpoterDescriptorTableViewer.setContentProvider(new ArrayContentProvider());
246       modelImpoterDescriptorTableViewer.setLabelProvider(new ModelImporterUtil.ModelImporterDescriptorLabelProvider());
247       modelImpoterDescriptorTableViewer.setSorter(new ViewerSorter());
248
249       modelImpoterDescriptorTableViewer.addDoubleClickListener(new IDoubleClickListener()
250         {
251           public void doubleClick(DoubleClickEvent event)
252           {
253             if (canFlipToNextPage())
254             {
255               getContainer().showPage(getNextPage());
256             }
257           }
258         });
259
260       modelImpoterDescriptorTableViewer.setInput(getModelImporterDescriptors().toArray());
261       if (modelImporterDescriptor != null)
262       {
263         modelImpoterDescriptorTableViewer.setSelection(new StructuredSelection(modelImporterDescriptor), true);
264       }
265       modelImpoterDescriptorTableViewer.addSelectionChangedListener(this);
266       setControl(composite);
267     }
268
269     public void selectionChanged(SelectionChangedEvent event)
270     {
271       ISelection selection = event.getSelection();
272       if (!selection.isEmpty() && selection instanceof IStructuredSelection)
273       {
274         Object JavaDoc selectedObject = ((IStructuredSelection)selection).getFirstElement();
275         if (selectedObject instanceof ModelImporterDescriptor)
276         {
277           modelImporterDescriptor = (ModelImporterDescriptor)selectedObject;
278           if (modelImporterWizardNodeMap == null)
279           {
280             modelImporterWizardNodeMap = ModelImporterUtil.createModelImporterDescriptorWizardNodesMap();
281           }
282           setMessage(modelImporterDescriptor.getDescription(), IMessageProvider.NONE);
283           setSelectedNode((IWizardNode)modelImporterWizardNodeMap.get(modelImporterDescriptor));
284           return;
285         }
286       }
287
288       setPageComplete(false);
289     }
290
291     public boolean isPageComplete()
292     {
293       return modelImporterDescriptor != null;
294     }
295
296     public IWizardPage getNextPage()
297     {
298       IModelImporterWizard modelImporterWizard = (IModelImporterWizard)getSelectedNode().getWizard();
299       if (!initializedWizards.contains(modelImporterWizard))
300       {
301         initializedWizards.add(modelImporterWizard);
302         if (modelImporterWizard instanceof Wizard)
303         {
304           Wizard wizard = (Wizard)modelImporterWizard;
305           wizard.setDefaultPageImageDescriptor(getDefaultImageDescriptor());
306           if (wizard.getWindowTitle() == null)
307           {
308             wizard.setWindowTitle(getWindowTitle());
309           }
310         }
311         modelImporterWizard.init(workbench, selection);
312       }
313
314       adjustModelImporterWizard(modelImporterWizard, getModelImporterDescriptor());
315       IWizardPage wizardPage = super.getNextPage();
316       
317       IWizardNode wizardNode = getSelectedNode();
318       if (wizardNode instanceof ModelImporterUtil.ModelImporterDescriptorWizardNode)
319       {
320         ((ModelImporterUtil.ModelImporterDescriptorWizardNode)wizardNode).setContentCreated(true);
321       }
322       return wizardPage;
323     }
324   }
325
326   protected IStructuredSelection selection;
327   protected IWorkbench workbench;
328   protected IPath genModelContainerPath;
329   protected String JavaDoc genModelFileName;
330   protected IFile reloadFile;
331
332   protected IFile modelFile;
333   protected ModelImporterDescriptor suggestedDescriptor;
334   protected Map JavaDoc modelImporterWizardNodeMap;
335
336   protected boolean canFinish = false;
337
338   public EMFModelWizard()
339   {
340     super();
341     setWindowTitle(ImporterPlugin.INSTANCE.getString("_UI_EMFWizardModel_title"));
342   }
343
344   public EMFModelWizard(IFile reloadFile)
345   {
346     this();
347     setWindowTitle(ImporterPlugin.INSTANCE.getString("_UI_ReloadWizard_title"));
348     this.reloadFile = reloadFile;
349   }
350
351   public void dispose()
352   {
353     selection = null;
354     workbench = null;
355     genModelContainerPath = null;
356     reloadFile = null;
357     
358     if (modelImporterWizardNodeMap != null)
359     {
360       for (Iterator JavaDoc i = modelImporterWizardNodeMap.values().iterator(); i.hasNext();)
361       {
362         IWizardNode wizardNode = (IWizardNode)i.next();
363         wizardNode.dispose();
364       }
365       modelImporterWizardNodeMap.clear();
366       modelImporterWizardNodeMap = null;
367     }
368
369     super.dispose();
370   }
371   
372   protected ImageDescriptor getDefaultImageDescriptor()
373   {
374     return ExtendedImageRegistry.INSTANCE.getImageDescriptor(ImporterPlugin.INSTANCE.getImage("full/wizban/NewGenModel"));
375   }
376
377   public void addPages()
378   {
379     if (reloadFile == null)
380     {
381       NewGenModelFileCreationPage page = new NewGenModelFileCreationPage("NewModelFileCreationPageID");
382       page.setTitle(ImporterPlugin.INSTANCE.getString("_UI_EMFModelWizard_name"));
383       page.setDescription(ImporterPlugin.INSTANCE.getString("_UI_CreateGeneratorModel_label"));
384       addPage(page);
385     }
386     else
387     {
388       setForcePreviousAndNextButtons(true);
389     }
390
391     SelectionPage selectionPage = new SelectionPage("ModelImporterDescriptorSelectionPage");
392     selectionPage.setTitle(ImporterPlugin.INSTANCE.getString("_UI_SelectModelImporters_title"));
393     addPage(selectionPage);
394   }
395
396   public void init(IWorkbench workbench, IStructuredSelection selection)
397   {
398     this.workbench = workbench;
399     this.selection = selection;
400     computeSuggestedDescriptor();
401     init();
402   }
403   
404   protected void init()
405   {
406     setDefaultPageImageDescriptor(getDefaultImageDescriptor());
407   }
408   
409   protected String JavaDoc getDefaultGenModelFileName()
410   {
411     return "My.genmodel";
412   }
413
414   protected List JavaDoc getModelImporterDescriptors()
415   {
416     return ModelImporterUtil.filterModelImporterDescriptors(ModelImporterDescriptor.TYPE_FILE);
417   }
418   
419   protected void computeSuggestedDescriptor()
420   {
421     if (suggestedDescriptor == null)
422     {
423       if (reloadFile != null)
424       {
425         URI reloadURI = URI.createPlatformResourceURI(reloadFile.getFullPath().toString());
426         Resource reloadResource = null;
427         try
428         {
429           reloadResource = ImporterUtil.createResourceSet().getResource(reloadURI, true);
430         }
431         catch (Exception JavaDoc e)
432         {
433         }
434
435         if (reloadResource != null && !reloadResource.getContents().isEmpty())
436         {
437           Object JavaDoc content = reloadResource.getContents().get(0);
438           if (content instanceof GenModel)
439           {
440             GenModel genModel = (GenModel)content;
441             if (genModel.getImporterID() != null)
442             {
443               suggestedDescriptor = ModelImporterUtil.getModelImporterDescriptor(genModel.getImporterID());
444             }
445             else if (!genModel.getForeignModel().isEmpty())
446             {
447               String JavaDoc foreignModel = (String JavaDoc)genModel.getForeignModel().get(0);
448               if (foreignModel.endsWith(".mdl"))
449               {
450                 suggestedDescriptor = ModelImporterUtil.getModelImporterDescriptor("org.eclipse.emf.importer.rose");
451               }
452               else if (foreignModel.endsWith(".xsd") || foreignModel.endsWith(".wsdl"))
453               {
454                 suggestedDescriptor = ModelImporterUtil.getModelImporterDescriptor("org.eclipse.xsd.ecore.importer");
455               }
456               else if (foreignModel.endsWith("@model"))
457               {
458                 suggestedDescriptor = ModelImporterUtil.getModelImporterDescriptor("org.eclipse.emf.importer.java");
459               }
460               else if (foreignModel.endsWith(".ecore") || foreignModel.endsWith(".emof"))
461               {
462                 suggestedDescriptor = ModelImporterUtil.getModelImporterDescriptor("org.eclipse.emf.importer.ecore");
463               }
464             }
465           }
466         }
467       }
468
469       if (suggestedDescriptor == null)
470       {
471         if (selection != null && !selection.isEmpty())
472         {
473           Object JavaDoc element = selection.getFirstElement();
474           if (element instanceof IFile)
475           {
476             List JavaDoc descriptors = ModelImporterUtil.filterModelImporterDescriptors(((IFile)element).getFileExtension());
477             if (!descriptors.isEmpty())
478             {
479               modelFile = ((IFile)element);
480               suggestedDescriptor = (ModelImporterDescriptor)descriptors.get(0);
481             }
482           }
483         }
484       }
485
486     }
487   }
488   
489   protected boolean isValidNewValue(Object JavaDoc newValue, Object JavaDoc oldValue)
490   {
491     return newValue == null ? oldValue != null : !newValue.equals(oldValue);
492   }
493
494   protected void adjustModelImporterWizard(IModelImporterWizard modelImporterWizard, ModelImporterDescriptor modelImporterDescriptor)
495   {
496     if (isValidNewValue(reloadFile, modelImporterWizard.getOriginalGenModelFile()))
497     {
498       modelImporterWizard.setOriginalGenModelFile(reloadFile);
499     }
500     if (isValidNewValue(genModelContainerPath, modelImporterWizard.getGenModelContainerPath()))
501     {
502       modelImporterWizard.setGenModelContainerPath(genModelContainerPath);
503     }
504     if (isValidNewValue(genModelFileName, modelImporterWizard.getGenModelFileName()))
505     {
506       modelImporterWizard.setGenModelFileName(genModelFileName);
507     }
508     if (isValidNewValue(modelFile, modelImporterWizard.getModelFile()))
509     {
510       if (modelFile == null || modelImporterDescriptor.getExtensions().contains(modelFile.getFileExtension()))
511       {
512         modelImporterWizard.setModelFile(modelFile);
513       }
514     }
515     modelImporterWizard.getFileExtensions().clear();
516     modelImporterWizard.getFileExtensions().addAll(modelImporterDescriptor.getExtensions());
517   }
518
519   public boolean canFinish()
520   {
521     return false;
522   }
523
524   public boolean performFinish()
525   {
526     return true;
527   }
528 }
529
Popular Tags