KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > ui > contribution > base > ModelPackagePage


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  * ModelPackagePage.java,v 1.1 2005/05/12 17:10:24 marcelop Exp
16  */

17 package org.eclipse.emf.importer.ui.contribution.base;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.resources.ResourcesPlugin;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.jface.viewers.AbstractTreeViewer;
32 import org.eclipse.jface.viewers.CheckStateChangedEvent;
33 import org.eclipse.jface.viewers.CheckboxTableViewer;
34 import org.eclipse.jface.viewers.CheckboxTreeViewer;
35 import org.eclipse.jface.viewers.ColumnWeightData;
36 import org.eclipse.jface.viewers.ICheckStateListener;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.jface.viewers.TableLayout;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.jface.viewers.ViewerSorter;
41 import org.eclipse.jface.window.Window;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.custom.SashForm;
44 import org.eclipse.swt.events.FocusAdapter;
45 import org.eclipse.swt.events.FocusEvent;
46 import org.eclipse.swt.events.KeyAdapter;
47 import org.eclipse.swt.events.KeyEvent;
48 import org.eclipse.swt.events.ModifyEvent;
49 import org.eclipse.swt.events.ModifyListener;
50 import org.eclipse.swt.events.SelectionAdapter;
51 import org.eclipse.swt.events.SelectionEvent;
52 import org.eclipse.swt.graphics.Image;
53 import org.eclipse.swt.layout.GridData;
54 import org.eclipse.swt.layout.GridLayout;
55 import org.eclipse.swt.widgets.Button;
56 import org.eclipse.swt.widgets.Composite;
57 import org.eclipse.swt.widgets.Label;
58 import org.eclipse.swt.widgets.Table;
59 import org.eclipse.swt.widgets.TableColumn;
60 import org.eclipse.swt.widgets.TableItem;
61 import org.eclipse.swt.widgets.Text;
62 import org.eclipse.swt.widgets.Tree;
63 import org.eclipse.ui.dialogs.ResourceSelectionDialog;
64
65 import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
66 import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
67 import org.eclipse.emf.codegen.ecore.genmodel.provider.GenBaseItemProvider;
68 import org.eclipse.emf.codegen.ecore.genmodel.provider.GenModelEditPlugin;
69 import org.eclipse.emf.codegen.ecore.genmodel.provider.GenModelItemProviderAdapterFactory;
70 import org.eclipse.emf.codegen.ecore.genmodel.provider.GenPackageItemProvider;
71 import org.eclipse.emf.codegen.util.CodeGenUtil;
72 import org.eclipse.emf.common.notify.Adapter;
73 import org.eclipse.emf.common.notify.AdapterFactory;
74 import org.eclipse.emf.common.notify.impl.AdapterFactoryImpl;
75 import org.eclipse.emf.common.ui.celleditor.ExtendedTableEditor;
76 import org.eclipse.emf.common.util.URI;
77 import org.eclipse.emf.common.util.UniqueEList;
78 import org.eclipse.emf.ecore.EClassifier;
79 import org.eclipse.emf.ecore.EObject;
80 import org.eclipse.emf.ecore.EPackage;
81 import org.eclipse.emf.ecore.resource.Resource;
82 import org.eclipse.emf.ecore.resource.ResourceSet;
83 import org.eclipse.emf.edit.provider.ItemProvider;
84 import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
85 import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
86 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
87 import org.eclipse.emf.importer.ImporterPlugin;
88 import org.eclipse.emf.importer.ModelImporter;
89
90
91 /**
92  * @since 2.1.0
93  */

94 public class ModelPackagePage extends ModelImporterPage
95 {
96   public static final int ECORE_FILE_COLUMN = 0;
97
98   protected CheckboxTableViewer packagesCheckboxTableViewer;
99   protected CheckboxTreeViewer referencedGenModelsCheckboxTreeViewer;
100
101   protected List JavaDoc filteredEPackages = new ModelImporter.EPackageList();
102
103   protected boolean isCellEditing = false;
104   protected boolean showReferencedGenModels = false;
105
106   public ModelPackagePage(ModelImporter modelImporter, String JavaDoc pageName)
107   {
108     super(modelImporter, pageName);
109
110     setTitle(ImporterPlugin.INSTANCE.getString("_UI_PackageSelection_title"));
111     setDescription(ImporterPlugin.INSTANCE.getString("_UI_PackageSelection_description"));
112   }
113
114   public void dispose()
115   {
116     packagesCheckboxTableViewer = null;
117     referencedGenModelsCheckboxTreeViewer = null;
118
119     if (filteredEPackages != null)
120     {
121       filteredEPackages.clear();
122       filteredEPackages = null;
123     }
124
125     super.dispose();
126   }
127
128   public void setShowReferencedGenModels(boolean showReferencedGenModels)
129   {
130     this.showReferencedGenModels = showReferencedGenModels;
131   }
132
133   public boolean showReferencedGenModels()
134   {
135     return showReferencedGenModels;
136   }
137
138   protected void pageActivated(final boolean firstTime, int cause)
139   {
140     getControl().getDisplay().asyncExec(new Runnable JavaDoc()
141       {
142         public void run()
143         {
144           filterPackagesTable(true);
145           validate();
146           getContainer().updateButtons();
147         }
148       });
149   }
150
151   public boolean isPageComplete()
152   {
153     return super.isPageComplete()
154       && !getModelImporter().getEPackages().isEmpty()
155       && packagesCheckboxTableViewer != null
156       && packagesCheckboxTableViewer.getCheckedElements().length > 0
157       && !isCellEditing;
158   }
159
160   public void createControl(Composite parent)
161   {
162     Composite composite = null;
163     if (showReferencedGenModels())
164     {
165       composite = new SashForm(parent, SWT.VERTICAL);
166       composite.setLayoutData(new GridData(GridData.FILL_BOTH));
167     }
168     else
169     {
170       composite = new Composite(parent, SWT.NONE);
171       GridLayout layout = new GridLayout();
172       layout.verticalSpacing = 12;
173       composite.setLayout(layout);
174       composite.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL));
175     }
176     
177     Composite packageComposite = new Composite(composite, SWT.NONE);
178     {
179       GridLayout layout = new GridLayout();
180       layout.verticalSpacing = 12;
181       packageComposite.setLayout(layout);
182       packageComposite.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL));
183     }
184     createPackageControl(packageComposite);
185     
186     if (showReferencedGenModels())
187     {
188       Composite referencedGenModelComposite = new Composite(composite, SWT.NONE);
189       {
190         GridLayout layout = new GridLayout();
191         layout.verticalSpacing = 12;
192         referencedGenModelComposite.setLayout(layout);
193         referencedGenModelComposite.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL));
194       }
195       createReferencedGenModelControl(referencedGenModelComposite);
196       ((SashForm)composite).setWeights(new int[] { 70, 30 });
197     }
198
199     setControl(composite);
200   }
201
202   protected void createPackageControl(Composite parent)
203   {
204     Composite composite = new Composite(parent, SWT.NONE);
205     composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
206     {
207       GridLayout layout = new GridLayout(2, false);
208       layout.marginLeft = -5;
209       layout.marginRight = -5;
210       layout.marginTop = -5;
211       layout.marginBottom = -5;
212       composite.setLayout(layout);
213     }
214     
215     Label packagesLabel = new Label(composite, SWT.LEFT);
216     packagesLabel.setText(getPackagesLabel());
217     packagesLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
218
219     Composite selectionComposite = new Composite(composite, SWT.NONE);
220     selectionComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
221      {
222       GridLayout layout = new GridLayout(2, true);
223       layout.marginLeft = -5;
224       layout.marginRight = -5;
225       layout.marginTop = -5;
226       layout.marginBottom = -5;
227       selectionComposite.setLayout(layout);
228     }
229
230     Button selectAllButton = new Button(selectionComposite, SWT.PUSH);
231     selectAllButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
232     selectAllButton.setText(getSelectAllLabel());
233     selectAllButton.addSelectionListener(new SelectionAdapter()
234       {
235         public void widgetSelected(SelectionEvent event)
236         {
237           packagesCheckboxTableViewer.setCheckedElements(getModelImporter().getEPackages().toArray());
238           ePackageCheckStateChanged();
239         }
240       });
241
242     Button deselectAllButton = new Button(selectionComposite, SWT.PUSH);
243     deselectAllButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
244     deselectAllButton.setText(getDeselectAllLabel());
245     deselectAllButton.addSelectionListener(new SelectionAdapter()
246       {
247         public void widgetSelected(SelectionEvent event)
248         {
249           packagesCheckboxTableViewer.setCheckedElements(new Object JavaDoc [0]);
250           ePackageCheckStateChanged();
251         }
252       });
253
254     Table packagesTable = new Table(parent, SWT.CHECK | SWT.BORDER);
255     packagesCheckboxTableViewer = new CheckboxTableViewer(packagesTable);
256     {
257       GridData data = new GridData();
258       data.verticalAlignment = GridData.FILL;
259       data.grabExcessHorizontalSpace = true;
260       data.grabExcessVerticalSpace = true;
261       data.horizontalAlignment = GridData.FILL;
262       data.horizontalSpan = 2;
263       data.heightHint = 90;
264       packagesTable.setLayoutData(data);
265     }
266
267     packagesTable.setHeaderVisible(true);
268     packagesTable.setLinesVisible(true);
269     {
270       TableLayout layout = new TableLayout();
271
272       TableColumn packageColumn = new TableColumn(packagesTable, SWT.NONE);
273       layout.addColumnData(new ColumnWeightData(1, true));
274       packageColumn.setText(getPackageColumnLabel());
275       packageColumn.setResizable(true);
276
277       TableColumn ecoreFileNameColumn = new TableColumn(packagesTable, SWT.NONE);
278       layout.addColumnData(new ColumnWeightData(1, true));
279       ecoreFileNameColumn.setText(getEcoreNameColumnLabel());
280       ecoreFileNameColumn.setResizable(true);
281
282       packagesTable.setLayout(layout);
283     }
284
285     new ExtendedTableEditor(packagesTable)
286     {
287       protected void editItem(final TableItem tableItem, final int column)
288       {
289         switch (column)
290         {
291           case 1:
292           case 2: {
293             final String JavaDoc string = tableItem.getText(column);
294             horizontalAlignment = SWT.LEFT;
295             minimumWidth = Math.max(50, tableItem.getBounds(column).width);
296
297             final Text text = new Text(table, SWT.NONE);
298             setEditor(text, tableItem, column);
299             text.setFocus();
300             text.setText(string);
301             text.setSelection(0, string.length());
302             validateEcoreModelFileName(string, null);
303
304             text.addFocusListener(new FocusAdapter()
305               {
306                 public void focusLost(FocusEvent event)
307                 {
308                   modify(tableItem, column, text);
309                 }
310               });
311
312             text.addKeyListener(new KeyAdapter()
313               {
314                 public void keyPressed(KeyEvent event)
315                 {
316                   if (event.character == '\r' || event.character == '\n')
317                   {
318                     modify(tableItem, column, text);
319                     setEditor(null);
320                     text.dispose();
321                   }
322                   else if (event.character == '\033')
323                   {
324                     setEditor(null);
325                     text.dispose();
326                   }
327                 }
328               });
329
330             text.addModifyListener(new ModifyListener()
331               {
332                 public void modifyText(ModifyEvent event)
333                 {
334                   if (column == 1)
335                   {
336                     validateEcoreModelFileName(text.getText(), null);
337                   }
338                 }
339               });
340
341             isCellEditing = true;
342             setPageComplete(false);
343             break;
344           }
345         }
346       }
347
348       protected void modify(TableItem tableItem, int column, Text text)
349       {
350         tableItem.setText(column, text.getText());
351         String JavaDoc value = tableItem.getText(column);
352         text.setVisible(false);
353
354         ModelImporter.EPackageInfo ePackageInfo = getModelImporter().getEPackageInfo((EPackage)tableItem.getData());
355         StringBuffer JavaDoc ecoreFileName = new StringBuffer JavaDoc(ePackageInfo.getEcoreFileName());
356         if (ecoreFileName != null)
357         {
358           ecoreFileName.replace(0, ecoreFileName.length(), value);
359           ePackageInfo.setEcoreFileName(ecoreFileName.toString());
360         }
361         isCellEditing = false;
362         validate();
363         setPageComplete(isPageComplete());
364       }
365     };
366
367     AdapterFactory adapterFactory = new AdapterFactoryImpl();
368     packagesCheckboxTableViewer.setColumnProperties(new String JavaDoc []{ "a", "b" });
369     packagesCheckboxTableViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory));
370     packagesCheckboxTableViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)
371       {
372         public Image getColumnImage(Object JavaDoc o, int columnIndex)
373         {
374           switch (columnIndex)
375           {
376             case 0: {
377               return getEPackageImage();
378             }
379             default: {
380               return null;
381             }
382           }
383         }
384
385         public String JavaDoc getColumnText(Object JavaDoc o, int columnIndex)
386         {
387           switch (columnIndex)
388           {
389             case 0: {
390               return ((EPackage)o).getName();
391             }
392             case 1:
393             case 2: {
394               return getModelImporter().getEPackageInfo((EPackage)o).getEcoreFileName();
395             }
396             default: {
397               return "";
398             }
399           }
400         }
401       });
402
403     packagesCheckboxTableViewer.addCheckStateListener(new ICheckStateListener()
404       {
405         public void checkStateChanged(CheckStateChangedEvent event)
406         {
407           ePackageCheckStateChanged();
408         }
409       });
410   }
411
412   protected void ePackageCheckStateChanged()
413   {
414     validate();
415     getContainer().updateButtons();
416
417     Set JavaDoc checkedElements = new HashSet JavaDoc(Arrays.asList(packagesCheckboxTableViewer.getCheckedElements()));
418     for (Iterator JavaDoc i = getModelImporter().getEPackages().iterator(); i.hasNext();)
419     {
420       EPackage ePackage = (EPackage)i.next();
421       getModelImporter().getEPackageInfo(ePackage).setGenerate(checkedElements.contains(ePackage));
422     }
423   }
424
425   public List JavaDoc getCheckedEPackages()
426   {
427     if (packagesCheckboxTableViewer != null)
428     {
429       return Arrays.asList(packagesCheckboxTableViewer.getCheckedElements());
430     }
431     else
432     {
433       return Collections.EMPTY_LIST;
434     }
435   }
436
437   protected String JavaDoc getEcoreNameColumnLabel()
438   {
439     return ImporterPlugin.INSTANCE.getString("_UI_EcoreModelName_label");
440   }
441
442   protected String JavaDoc getPackageColumnLabel()
443   {
444     return ImporterPlugin.INSTANCE.getString("_UI_Package_label");
445   }
446
447   protected String JavaDoc getDeselectAllLabel()
448   {
449     return ImporterPlugin.INSTANCE.getString("_UI_DeselectAll_label");
450   }
451
452   protected String JavaDoc getSelectAllLabel()
453   {
454     return ImporterPlugin.INSTANCE.getString("_UI_SelectAll_label");
455   }
456
457   protected String JavaDoc getPackagesLabel()
458   {
459     return ImporterPlugin.INSTANCE.getString("_UI_RootPackages_label");
460   }
461
462   protected Image getEPackageImage()
463   {
464     return ExtendedImageRegistry.INSTANCE.getImage(new GenBaseItemProvider.UnderlayedImage(
465       GenModelEditPlugin.INSTANCE.getImage("full/obj16/EPackage")));
466   }
467
468   protected void createReferencedGenModelControl(Composite parent)
469   {
470     Composite composite = new Composite(parent, SWT.NONE);
471     composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
472     {
473       GridLayout layout = new GridLayout(2, false);
474       layout.marginLeft = -5;
475       layout.marginRight = -5;
476       layout.marginTop = -5;
477       layout.marginBottom = -5;
478       composite.setLayout(layout);
479     }
480     
481     Label referencedGenModelsLabel = new Label(composite, SWT.LEFT);
482     referencedGenModelsLabel.setText(getReferencedGenModelLabel());
483     referencedGenModelsLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
484
485     Button referencedGenModelsTreeBrowseButton = new Button(composite, SWT.PUSH);
486     referencedGenModelsTreeBrowseButton.setText(getBrowseButtonLabel());
487     referencedGenModelsTreeBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
488
489     referencedGenModelsTreeBrowseButton.addSelectionListener(new SelectionAdapter()
490       {
491         public void widgetSelected(SelectionEvent event)
492         {
493           Collection JavaDoc genModelResources = new ArrayList JavaDoc();
494           ResourceSelectionDialog resourceSelectionDialog = new ResourceSelectionDialog(
495             getShell(),
496             ResourcesPlugin.getWorkspace().getRoot(),
497             getSelectGenModelDialogTitle());
498
499           resourceSelectionDialog.setInitialSelections(genModelResources.toArray());
500           Object JavaDoc[] result = resourceSelectionDialog.open() == Window.OK ?
501             resourceSelectionDialog.getResult() : null;
502           if (result != null)
503           {
504             ResourceSet referencedGenModels = getModelImporter().createResourceSet();
505             List JavaDoc genModels = new UniqueEList.FastCompare(getModelImporter().getExternalGenModels());
506             for (int i = 0; i < result.length; ++i)
507             {
508               IResource resource = (IResource)result[i];
509               if (resource.getType() == IResource.FILE && !CodeGenUtil.isInJavaOutput(resource)
510                 && "genmodel".equals(resource.getFullPath().getFileExtension()))
511               {
512                 URI genModelURI = URI.createPlatformResourceURI(resource.getFullPath().toString(), true);
513                 Resource genModelResource = referencedGenModels.getResource(genModelURI, true);
514                 genModels.add(genModelResource.getContents().get(0));
515               }
516             }
517
518             referencedGenModelsCheckboxTreeViewer.getTree().deselectAll();
519             referencedGenModelsCheckboxTreeViewer.setInput(new ItemProvider(genModels));
520             for (Iterator JavaDoc i = genModels.iterator(); i.hasNext();)
521             {
522               referencedGenModelsCheckboxTreeViewer.expandToLevel(i.next(), AbstractTreeViewer.ALL_LEVELS);
523             }
524             referencedGenModelsCheckboxTreeViewerCheckStateChanged();
525           }
526         }
527       });
528
529     final Tree referencedGenModelsTree = new Tree(parent, SWT.CHECK | SWT.BORDER | SWT.MULTI);
530     referencedGenModelsTree.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL));
531
532     referencedGenModelsCheckboxTreeViewer = new CheckboxTreeViewer(referencedGenModelsTree);
533     GenModelItemProviderAdapterFactory genModelItemProviderAdapterFactory = new GenModelItemProviderAdapterFactory()
534       {
535         public Adapter createGenPackageAdapter()
536         {
537           if (genPackageItemProvider == null)
538           {
539             genPackageItemProvider = new GenPackageItemProvider(this)
540               {
541                 public Collection JavaDoc getChildrenFeatures(Object JavaDoc object)
542                 {
543                   return Collections.EMPTY_LIST;
544                 }
545               };
546           }
547
548           return genPackageItemProvider;
549         }
550       };
551       
552     // Sorting only the genModels
553
referencedGenModelsCheckboxTreeViewer.setSorter(new ViewerSorter()
554       {
555         public void sort(Viewer viewer, Object JavaDoc[] elements)
556         {
557           if (elements.length > 0 && elements[0] instanceof GenModel)
558           {
559             super.sort(viewer, elements);
560           }
561         }
562       });
563     
564     referencedGenModelsCheckboxTreeViewer.setContentProvider(new AdapterFactoryContentProvider(genModelItemProviderAdapterFactory));
565     referencedGenModelsCheckboxTreeViewer.setLabelProvider(new AdapterFactoryLabelProvider(genModelItemProviderAdapterFactory)
566       {
567         public String JavaDoc getText(Object JavaDoc element)
568         {
569           String JavaDoc text = super.getText(element);
570
571           if (element instanceof GenModel)
572           {
573             Resource resource = ((GenModel)element).eResource();
574             URI uri = resource != null ? resource.getURI() : null;
575             if (uri != null)
576             {
577               String JavaDoc location = uri.toString();
578
579               if ("platform".equals(uri.scheme()) && uri.segmentCount() > 1)
580               {
581                 boolean plugin = "plugin".equals(uri.segment(0));
582                 String JavaDoc type = ImporterPlugin.INSTANCE.getString(plugin ? "_UI_PlatformPlugin_label" : "_UI_PlatformResource_label");
583                 location = ImporterPlugin.INSTANCE.getString("_UI_PlatformLocation_label", new Object JavaDoc [] { type, uri.segment(1) });
584               }
585               text = ImporterPlugin.INSTANCE.getString("_UI_ReferencedGenModel_label", new Object JavaDoc [] { text, location });
586             }
587           }
588           return text;
589         }
590       });
591
592     referencedGenModelsCheckboxTreeViewer.addCheckStateListener(new ICheckStateListener()
593       {
594         public void checkStateChanged(CheckStateChangedEvent event)
595         {
596           referencedGenModelsCheckboxTreeViewerCheckStateChanged();
597         }
598       });
599   }
600   
601   protected void referencedGenModelsCheckboxTreeViewerCheckStateChanged()
602   {
603     filterPackagesTable(false);
604     validate();
605     getContainer().updateButtons();
606   }
607
608   protected String JavaDoc getBrowseButtonLabel()
609   {
610     return ImporterPlugin.INSTANCE.getString("_UI_Browse_label");
611   }
612
613   protected String JavaDoc getReferencedGenModelLabel()
614   {
615     return ImporterPlugin.INSTANCE.getString("_UI_ReferencedGeneratorModels_label");
616   }
617
618   protected String JavaDoc getSelectGenModelDialogTitle()
619   {
620     return ImporterPlugin.INSTANCE.getString("_UI_SelectAllGeneratorModels_description");
621   }
622
623   protected void validate()
624   {
625     List JavaDoc tableCheckedEPackages = getCheckedEPackages();
626
627     List JavaDoc referencedEPackages = new ModelImporter.EPackageList();
628     for (Iterator JavaDoc i = getModelImporter().getEPackages().iterator(); i.hasNext();)
629     {
630       EPackage ePackage = (EPackage)i.next();
631       if (tableCheckedEPackages.contains(ePackage) || !filteredEPackages.contains(ePackage))
632       {
633         for (Iterator JavaDoc j = ePackage.eAllContents(); j.hasNext();)
634         {
635           EObject eObject = (EObject)j.next();
636           for (Iterator JavaDoc k = eObject.eCrossReferences().iterator(); k.hasNext();)
637           {
638             Object JavaDoc o = k.next();
639             if (o instanceof EClassifier)
640             {
641               EClassifier eClassifier = (EClassifier)o;
642               referencedEPackages.add(eClassifier.getEPackage());
643             }
644           }
645         }
646       }
647     }
648
649     String JavaDoc errorMessage = null;
650     for (Iterator JavaDoc i = referencedEPackages.iterator(); i.hasNext();)
651     {
652       EPackage ePackage = (EPackage)i.next();
653       if (!tableCheckedEPackages.contains(ePackage) && filteredEPackages.contains(ePackage))
654       {
655         errorMessage = ImporterPlugin.INSTANCE.getString(
656           "_UI_PackageIsUsedBySelectedPackage_message",
657           new Object JavaDoc []{ ePackage.getName() });
658       }
659     }
660
661     if (errorMessage != null)
662     {
663       setErrorMessage(errorMessage);
664     }
665     else
666     {
667       boolean allNamesAreValid = true;
668       for (Iterator JavaDoc i = tableCheckedEPackages.iterator(); i.hasNext();)
669       {
670         EPackage ePackage = (EPackage)i.next();
671         String JavaDoc fileName = getModelImporter().getEPackageInfo(ePackage).getEcoreFileName();
672         allNamesAreValid = validateEcoreModelFileName(fileName, ePackage.getName());
673         if (!allNamesAreValid)
674         {
675           break;
676         }
677       }
678       
679       if (allNamesAreValid)
680       {
681         setErrorMessage(checkEcoreFileNames());
682       }
683     }
684   }
685
686   protected boolean validateEcoreModelFileName(String JavaDoc fileName, String JavaDoc packageName)
687   {
688     IStatus status = getModelImporter().checkEcoreModelFileName(fileName, packageName);
689     handleStatus(status);
690     return status.isOK();
691   }
692
693   protected void filterPackagesTable(boolean reloadReferencedGenPackagesTable)
694   {
695     if (referencedGenModelsCheckboxTreeViewer != null)
696     {
697       if (reloadReferencedGenPackagesTable)
698       {
699         if (!getModelImporter().getReferencedGenPackages().isEmpty() || !getModelImporter().getExternalGenModels().isEmpty())
700         {
701           GenPackage[] referencedGenPackages = (GenPackage[])getModelImporter().getReferencedGenPackages().toArray(new GenPackage [getModelImporter().getReferencedGenPackages().size()]);
702           Set JavaDoc genModels = new HashSet JavaDoc();
703           for (int i = 0; i < referencedGenPackages.length; i++)
704           {
705             genModels.add(referencedGenPackages[i].getGenModel());
706           }
707           genModels.addAll(getModelImporter().getExternalGenModels());
708           referencedGenModelsCheckboxTreeViewer.setInput(new ItemProvider(genModels));
709           referencedGenModelsCheckboxTreeViewer.expandAll();
710           referencedGenModelsCheckboxTreeViewer.setCheckedElements(referencedGenPackages);
711           referencedGenModelsCheckboxTreeViewer.setSelection(new StructuredSelection(referencedGenPackages), true);
712         }
713       }
714       else
715       {
716         getModelImporter().getReferencedGenPackages().clear();
717         Object JavaDoc[] checkedElements = referencedGenModelsCheckboxTreeViewer.getCheckedElements();
718         for (int i = 0; i < checkedElements.length; i++)
719         {
720           if (checkedElements[i] instanceof GenPackage)
721           {
722             getModelImporter().getReferencedGenPackages().add(checkedElements[i]);
723           }
724         }
725       }
726     }
727
728     filteredEPackages = getModelImporter().filterReferencedEPackages(getModelImporter().getEPackages());
729     List JavaDoc checkedEPackages = new ArrayList JavaDoc();
730     for (Iterator JavaDoc i = getModelImporter().getEPackages().iterator(); i.hasNext();)
731     {
732       EPackage ePackage = (EPackage)i.next();
733       ModelImporter.EPackageInfo ePackageInfo = getModelImporter().getEPackageInfo(ePackage);
734       if (filteredEPackages.contains(ePackage) && ePackageInfo.isGenerate())
735       {
736         checkedEPackages.add(ePackage);
737       }
738       else
739       {
740         ePackageInfo.setGenerate(false);
741       }
742     }
743     packagesCheckboxTableViewer.setInput(new ItemProvider(filteredEPackages));
744     packagesCheckboxTableViewer.setCheckedElements(checkedEPackages.toArray());
745   }
746   
747   /**
748    * Checks the ecore file names and returns null if everything is
749    * OK or the error message otherwise.
750    * @return String
751    */

752   protected String JavaDoc checkEcoreFileNames()
753   {
754     Set JavaDoc fileNames = new HashSet JavaDoc();
755     int checkedCount = 0;
756     Table table = packagesCheckboxTableViewer.getTable();
757     TableItem[] tableItems = table.getItems();
758     for (int i = 0; i < tableItems.length; i++)
759     {
760       if (tableItems[i].getChecked())
761       {
762         checkedCount++;
763         fileNames.add(tableItems[i].getText(1));
764       }
765     }
766     return fileNames.size() < checkedCount ? ImporterPlugin.INSTANCE.getString("_UI_DuplicateEcoreNames_message") : null;
767   }
768 }
769
Popular Tags