View Javadoc

1   /*
2    * $Id: ToolsPlugin.java,v 1.2 2005/05/31 20:25:08 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.tools;
10  
11  import org.eclipse.core.runtime.CoreException;
12  import org.eclipse.core.runtime.IConfigurationElement;
13  import org.eclipse.core.runtime.IExtension;
14  import org.eclipse.core.runtime.IExtensionPoint;
15  import org.eclipse.core.runtime.IExtensionRegistry;
16  import org.eclipse.core.runtime.Platform;
17  import org.eclipse.core.runtime.Plugin;
18  import org.eclipse.jface.action.IMenuManager;
19  import org.eclipse.jface.action.MenuManager;
20  
21  import com.bbn.swede.core.OWLCore;
22  import com.bbn.swede.tools.codegenerator.AbstractCodeGenerator;
23  import com.bbn.swede.tools.codegenerator.CodeGenerationAction;
24  
25  /***
26   * Plug-in class for com.bbn.swede.tools.  Contains convenience methods for
27   * working with SWeDE tools.
28   * @author jlerner
29   */
30  public class ToolsPlugin extends Plugin
31  {
32     private static ToolsPlugin _plugin;
33     /***
34      * ID string for the Schemagen code generator.
35      */
36     public static final String S_SCHEMAGEN_ID = "com.bbn.swede.tools.VocabularyGenerator";
37     /***
38      * ID string for the code generator extension point.
39      */
40     public static final String S_CODEGENERATOR_ID = "com.bbn.swede.tools.codeGenerator";
41  
42     /***
43      * Array of extenders of com.bbn.swede.tools.codeGenerator.
44      */
45     protected static IExtension[] _extensions;
46     private IMenuManager _menuGenerators;
47     
48     /***
49      * Creates a new Tools plug-in and sets it as the singleton instance.
50      */
51     public ToolsPlugin()
52     {
53        super();
54        _plugin = this;
55        IExtensionRegistry registry = Platform.getExtensionRegistry();
56        IExtensionPoint point =
57           registry.getExtensionPoint(S_CODEGENERATOR_ID);
58        if (point != null)
59        {
60           _extensions = point.getExtensions();
61        }
62        else
63        {
64           _extensions = new IExtension[0];
65           OWLCore.logWarning(getID(), "codeGenerator extension point not found", new Exception());
66        }
67  
68        _menuGenerators = new MenuManager("Generate Code");
69        if (_extensions.length == 0)
70        {
71           _menuGenerators.setVisible(false);
72        }
73        for (int i = 0; i < _extensions.length; i++)
74        {
75           IConfigurationElement[] configElems =
76              _extensions[i].getConfigurationElements();
77           for (int j = 0; j < configElems.length; j++)
78           {
79              if (!configElems[j].getName().equals("generator"))
80              {
81                 continue;
82              }
83              String sName = configElems[j].getAttribute("name");
84              try
85              {
86                 AbstractCodeGenerator codeGen =
87                    (AbstractCodeGenerator) configElems[j].createExecutableExtension(
88                       "class");
89                 _menuGenerators.add(new CodeGenerationAction(sName, codeGen, configElems[j].getAttribute("id")));
90              }
91              catch (CoreException e)
92              {
93                 OWLCore.logWarning(
94                    getID(),
95                    "Code Generator menu generation failed",
96                    e);
97              }
98           }
99        }
100    }
101    
102    /***
103     * Provides access to the code generation menu, which is populated with
104     * all extenders of the com.bbn.swede.codeGenerator extension point.
105     * @return The code generator menu
106     */
107    public IMenuManager getCodeGeneratorMenu()
108    {
109       return _menuGenerators;
110    }
111 
112    /***
113     * Retrieves the one and only instance of the plugin.
114     * @return The singleton instance
115     */
116    public static Plugin getPlugin()
117    {
118       return _plugin;
119    }
120    /***
121     * Retrieves the one and only instance of ToolsPlugin.
122     * @return The singleton instance, cast as a ToolsPlugin
123     */
124    public static ToolsPlugin getToolsPlugin()
125    {
126       return _plugin;
127    }
128    
129    /***
130     * Retrieves the plugin's ID from the resource bundle.
131     * @return The plugin ID
132     */
133    public static String getID()
134    {
135       return ToolsPlugin.getPlugin().getBundle().getSymbolicName();
136    }
137 
138 }