1 17 package org.eclipse.emf.ecore.action; 18 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.jface.wizard.WizardDialog; 28 import org.eclipse.ui.IActionDelegate; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.actions.ActionDelegate; 31 32 import org.eclipse.emf.common.util.URI; 33 import org.eclipse.emf.ecore.EClass; 34 import org.eclipse.emf.ecore.presentation.DynamicModelWizard; 35 36 37 40 public class CreateDynamicInstanceAction 41 extends ActionDelegate 42 implements IActionDelegate 43 { 44 protected static final URI PLATFORM_RESOURCE = URI.createPlatformResourceURI("/"); 45 46 protected EClass eClass; 47 48 public CreateDynamicInstanceAction() 49 { 50 } 51 52 public void run(IAction action) 53 { 54 URI uri = eClass.eResource().getURI(); 55 IStructuredSelection selection = StructuredSelection.EMPTY; 56 if (uri != null && uri.isHierarchical()) 57 { 58 if (uri.isRelative() || (uri = uri.deresolve(PLATFORM_RESOURCE)).isRelative()) 59 { 60 IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toString())); 61 if (file.exists()) 62 { 63 selection = new StructuredSelection(file); 64 } 65 } 66 } 67 68 DynamicModelWizard dynamicModelWizard = new DynamicModelWizard(eClass); 69 dynamicModelWizard.init(PlatformUI.getWorkbench(), selection); 70 WizardDialog wizardDialog = 71 new WizardDialog 72 (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 73 dynamicModelWizard); 74 75 wizardDialog.open(); 76 } 77 78 public void selectionChanged(IAction action, ISelection selection) 79 { 80 if (selection instanceof IStructuredSelection) 81 { 82 Object object = ((IStructuredSelection)selection).getFirstElement(); 83 if (object instanceof EClass) 84 { 85 eClass = (EClass)object; 86 87 action.setEnabled(true); 88 return; 89 } 90 } 91 eClass = null; 92 action.setEnabled(false); 93 } 94 } 95 | Popular Tags |