KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > EMFEditPlugin


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: EMFEditPlugin.java,v 1.5 2005/06/08 06:17:06 nickb Exp $
16  */

17 package org.eclipse.emf.edit;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.eclipse.core.runtime.IConfigurationElement;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.emf.common.EMFPlugin;
29 import org.eclipse.emf.common.notify.AdapterFactory;
30 import org.eclipse.emf.common.util.ResourceLocator;
31 import org.eclipse.emf.ecore.EPackage;
32 import org.eclipse.emf.ecore.plugin.RegistryReader;
33 import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
34
35
36 /**
37  * The <b>Plugin</b> for the model EMF.Edit library.
38  * EMF must run
39  * within an Eclipse workbench,
40  * within a headless Eclipse workspace,
41  * or just stand-alone as part of some other application.
42  * To support this, all resource access should be directed to the resource locator,
43  * which can redirect the service as appopriate to the runtime.
44  * During stand-alone invocation no plugin initialization takes place.
45  * In this case, emf.edit.resources.jar must be on the CLASSPATH.
46  * @see #INSTANCE
47  */

48 public final class EMFEditPlugin extends EMFPlugin
49 {
50   /**
51    * The singleton instance of the plugin.
52    */

53   public static final EMFEditPlugin INSTANCE = new EMFEditPlugin();
54
55   /**
56    * The one instance of this class.
57    */

58   private static Implementation plugin;
59
60   /**
61    * Creates the singleton instance.
62    */

63   private EMFEditPlugin()
64   {
65     super(new ResourceLocator[] {});
66   }
67
68   /*
69    * Javadoc copied from base class.
70    */

71   public ResourceLocator getPluginResourceLocator()
72   {
73     return plugin;
74   }
75   
76   /**
77    * Returns a populated instance of an {@link ComposedAdapterFactory.Descriptor.Registry item provider adapter factory registry}.
78    * @return a populated instance of an item provider adapter factory registry.
79    */

80   public static ComposedAdapterFactory.Descriptor.Registry getComposedAdapterFactoryDescriptorRegistry()
81   {
82     final ComposedAdapterFactory.Descriptor.Registry.Impl result =
83       new ComposedAdapterFactory.Descriptor.Registry.Impl(null)
84       {
85         public ComposedAdapterFactory.Descriptor delegatedGetDescriptor(Collection JavaDoc types)
86         {
87           List JavaDoc stringTypes = new ArrayList JavaDoc(types.size());
88           for (Iterator JavaDoc i = types.iterator(); i.hasNext(); )
89           {
90             Object JavaDoc key = i.next();
91             if (key instanceof EPackage)
92             {
93               stringTypes.add(((EPackage)key).getNsURI());
94             }
95             else if (key instanceof Package JavaDoc)
96             {
97               stringTypes.add(((Package JavaDoc)key).getName());
98             }
99             else if (key instanceof Class JavaDoc)
100             {
101               stringTypes.add(((Class JavaDoc)key).getName());
102             }
103             else
104             {
105               return null;
106             }
107           }
108           ComposedAdapterFactory.Descriptor descriptor = (ComposedAdapterFactory.Descriptor)get(stringTypes);
109           if (descriptor != null)
110           {
111             put(types, descriptor);
112             return descriptor;
113           }
114           
115           return super.delegatedGetDescriptor(types);
116         }
117       };
118     if (INSTANCE.getPluginResourceLocator() instanceof EclipsePlugin)
119     {
120       RegistryReader registryReader =
121          new RegistryReader(Platform.getExtensionRegistry(), INSTANCE.getSymbolicName(), "itemProviderAdapterFactories")
122          {
123            protected boolean readElement(IConfigurationElement element)
124            {
125              if (element.getName().equals("factory"))
126              {
127                String JavaDoc packageURI = element.getAttribute("uri");
128                String JavaDoc className = element.getAttribute("class");
129                String JavaDoc supportedTypes = element.getAttribute("supportedTypes");
130                if (packageURI == null)
131                {
132                  logMissingAttribute(element, "uri");
133                }
134                else if (className == null)
135                {
136                  logMissingAttribute(element, "class");
137                }
138                else if (supportedTypes == null)
139                {
140                  logMissingAttribute(element, "supportedTypes");
141                }
142                
143                class PluginAdapterFactoryDescriptor extends PluginClassDescriptor implements ComposedAdapterFactory.Descriptor
144                {
145                  public PluginAdapterFactoryDescriptor(IConfigurationElement element, String JavaDoc attributeName)
146                  {
147                    super(element, attributeName);
148                  }
149                  
150                  public AdapterFactory createAdapterFactory()
151                  {
152                    return (AdapterFactory)createInstance();
153                  }
154                }
155                
156                for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(supportedTypes); stringTokenizer.hasMoreTokens(); )
157                {
158                  String JavaDoc supportedType = stringTokenizer.nextToken();
159                  List JavaDoc key = new ArrayList JavaDoc();
160                  key.add(packageURI);
161                  key.add(supportedType);
162                  result.put(key, new PluginAdapterFactoryDescriptor(element, "class"));
163                }
164                
165                return true;
166              }
167              return false;
168            }
169          };
170       registryReader.readRegistry();
171     }
172     return result;
173   }
174   
175   /**
176    * Returns the singleton instance of the Eclipse plugin.
177    * @return the singleton instance.
178    */

179   public static Implementation getPlugin()
180   {
181     return plugin;
182   }
183
184   /**
185    * The actual implementation of the Eclipse <b>Plugin</b>.
186    */

187   public static class Implementation extends EclipsePlugin
188   {
189     /**
190      * Creates an instance.
191      */

192     public Implementation()
193     {
194       super();
195
196       // Remember the static instance.
197
//
198
plugin = this;
199     }
200   }
201 }
202
Popular Tags