View Javadoc

1   /*
2    * $Id: OWLNature.java,v 1.12 2004/09/02 20:45:52 jlerner Exp $
3    * Created on Apr 26, 2004
4    *
5    * Copyright (c) 1999-2004, BBN Technologies, LLC.
6    * All rights reserved.
7    * http://www.daml.org/legal/opensource/bbn_license.html
8    */
9   
10  package com.bbn.swede.core;
11  
12  import org.eclipse.core.resources.ICommand;
13  import org.eclipse.core.resources.IProject;
14  import org.eclipse.core.resources.IProjectDescription;
15  import org.eclipse.core.resources.IProjectNature;
16  import org.eclipse.core.runtime.CoreException;
17  
18  /***
19   * Implementation of the Semantic Web nature.  OWLNature is responsible for
20   * configuring Eclipse projects with the OWL syntax and Semantic Web project
21   * info builders.
22   * @author tself
23   */
24  public class OWLNature implements IProjectNature
25  {
26     private IProject _project;
27  
28     /***
29      * Default constructor for OWLNature.
30      */
31     public OWLNature()
32     {
33        _project = null;
34     }
35  
36     /*
37      * (non-Javadoc)
38      * @see org.eclipse.core.resources.IProjectNature#configure()
39      */
40     public void configure() throws CoreException
41     {
42        //checkOWLSyntaxBuilder(getProject());
43        checkBuilder(getProject(), OWLCore.SYNTAX_BUILDER);
44        checkBuilder(getProject(), OWLCore.CONFIG_BUILDER);
45     }
46  
47  
48     /***
49      * Checks for the presence of a builder in a project.  If the builder is
50      * missing, it is added.  If it is already there, nothing happens.
51      * @param project The project to check.
52      * @param builder The builder to check for.
53      */
54     private void checkBuilder(IProject project, String builder) throws CoreException
55     {
56        // get project description and then the associated build commands
57        IProjectDescription desc = project.getDescription();
58        ICommand[] commands = desc.getBuildSpec();
59  
60        // determine if builder already associated
61        boolean foundBuilder = false;
62  
63        for (int i = 0; i < commands.length; ++i)
64        {
65           if (commands[i].getBuilderName().equals(builder))
66           {
67              foundBuilder = true;
68              break;
69           }
70        }
71        // add builder if not already in project
72        if (!foundBuilder)
73        {
74           addBuilder(project, builder);
75        }
76  
77     }
78  
79     /***
80      * Checks for the presence of the OWL syntax builder in a project.  If the
81      * builder is missing, it is added.  If it is already there, nothing happens.
82      * @param project The project to check.
83      */
84     private void checkOWLSyntaxBuilder(IProject project) throws CoreException
85     {
86        checkBuilder(project, OWLCore.SYNTAX_BUILDER);
87     }
88  
89     /***
90      * Adds the OWL Syntax Builder to a project.
91      * @param project The project to add the builder to
92      */
93     public static void addOWLSyntaxBuilder(IProject project)
94     {
95        try
96        {
97           addBuilder(project, OWLCore.SYNTAX_BUILDER);
98        }
99        catch (CoreException e)
100       {
101          OWLCore.trace("OWLNature", "CoreException adding syntax builder to"
102                                     + project.getName(), true);
103       }
104    }
105 
106    /***
107     * Adds the Semantic Web Project Info Builder to a project.
108     * @param project The project to add the builder to
109     * @throws CoreException if there is an error accessing the project
110     *                       description to modify its builders.
111     */
112    public static void addSWProjectInfoBuilder(IProject project) throws CoreException
113    {
114       System.out.println("Adding config builder to " + project.getName());
115       addBuilder(project, OWLCore.CONFIG_BUILDER);
116    }
117 
118    private static void addBuilder(IProject project, String builder) throws CoreException
119    {
120       IProjectDescription desc = project.getDescription();
121       ICommand[] commands = desc.getBuildSpec();
122       ICommand command = desc.newCommand();
123       command.setBuilderName(builder);
124       // create map with arguments specific to builder in project here
125       // command.setArguments(Map args);
126       ICommand[] newCommands = new ICommand[commands.length + 1];
127 
128       // Add it after other builders.
129       System.arraycopy(commands, 0, newCommands, 1, commands.length);
130       newCommands[0] = command;
131       desc.setBuildSpec(newCommands);
132       project.setDescription(desc, null);
133    }
134 
135    /*
136     * (non-Javadoc)
137     * @see org.eclipse.core.resources.IProjectNature#deconfigure()
138     */
139    public void deconfigure() throws CoreException
140    {
141    // remove private resources here later if OWL Nature removed
142    // (e.g. remove .nt files, etc)
143    }
144 
145    /*
146     * (non-Javadoc)
147     * @see org.eclipse.core.resources.IProjectNature#getProject()
148     */
149    public IProject getProject()
150    {
151       return _project;
152    }
153 
154    /*
155     * (non-Javadoc)
156     * @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject)
157     */
158    public void setProject(IProject project)
159    {
160       _project = project;
161    }
162 
163 }