KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > action > CreateDynamicInstanceAction


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-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: CreateDynamicInstanceAction.java,v 1.3 2005/06/08 06:17:55 nickb Exp $
16  */

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 /**
38  * Create a dynamic instance of an {@link EClass}.
39  */

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 JavaDoc 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