View Javadoc

1   /*
2    * $Id: NewOWLProjectWizard.java,v 1.3 2005/05/31 21:57:12 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.ui.wizards;
11  
12  import java.lang.reflect.InvocationTargetException;
13  
14  import org.eclipse.core.resources.IProject;
15  import org.eclipse.core.resources.IProjectDescription;
16  import org.eclipse.core.resources.IWorkspace;
17  import org.eclipse.core.resources.ResourcesPlugin;
18  import org.eclipse.core.runtime.CoreException;
19  import org.eclipse.core.runtime.IConfigurationElement;
20  import org.eclipse.core.runtime.IExecutableExtension;
21  import org.eclipse.core.runtime.IPath;
22  import org.eclipse.core.runtime.IProgressMonitor;
23  import org.eclipse.core.runtime.OperationCanceledException;
24  import org.eclipse.core.runtime.Platform;
25  import org.eclipse.core.runtime.SubProgressMonitor;
26  import org.eclipse.jface.dialogs.MessageDialog;
27  import org.eclipse.jface.viewers.IStructuredSelection;
28  import org.eclipse.jface.wizard.Wizard;
29  import org.eclipse.ui.INewWizard;
30  import org.eclipse.ui.IWorkbench;
31  import org.eclipse.ui.PlatformUI;
32  import org.eclipse.ui.actions.WorkspaceModifyOperation;
33  import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
34  import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
35  
36  import com.bbn.swede.core.OWLCore;
37  
38  
39  /***
40   * Wizard for creating a new semantic web project.
41   * @author tself
42   */
43  public class NewOWLProjectWizard extends Wizard implements INewWizard, IExecutableExtension
44  {
45     // Reuse of the new project page provided by the platform UI dialogs
46     private WizardNewProjectCreationPage _mainPage;
47  
48     // cache of newly-created project
49     private IProject _newProject;
50  
51     // switch to control write of trace data
52     private boolean _bTraceEnabled = true;
53  
54     /***
55      * Add the one page to the wizard, the reused page
56      * <code>WizardNewProjectCreationPage</code>. This page provides basic
57      * project name validation and allows for
58      * 
59      * @see org.eclipse.jface.wizard.Wizard#addPages()
60      */
61     public void addPages()
62     {
63        _mainPage = new WizardNewProjectCreationPage("NewOWLProject");
64        _mainPage
65           .setDescription("Create a new project for Semantic Web development.");
66        _mainPage.setTitle("New SWeDE Project");
67        addPage(_mainPage);
68     }
69  
70     /***
71      * Returns the newly created project.
72      * 
73      * @return the created project, or <code>null</code> if project not created
74      */
75     public IProject getNewProject()
76     {
77        return _newProject;
78     }
79  
80     /***
81      * Initializes this creation wizard using the passed workbench and object
82      * selection.
83      * <p>
84      * This method is called after the no argument constructor and before other
85      * methods are called.
86      * </p>
87      * 
88      * @param workbench the current workbench
89      * @param selection the current object selection
90      * 
91      * @see org.eclipse.ui.IWorkbenchWizard#init(IWorkbench,
92      *      IStructuredSelection)
93      */
94     public void init(IWorkbench workbench, IStructuredSelection selection)
95     {
96  
97     }
98  
99     /***
100     * Creates a project with a <code>CustomNature</code> association.
101     * 
102     * @return <code>true</code> to indicate the finish request was accepted,
103     *         and <code>false</code> to indicate that the finish request was
104     *         refused
105     * 
106     * @see org.eclipse.jface.wizard.IWizard#performFinish()
107     */
108    public boolean performFinish()
109    {
110       createNewProject();
111       if (_config != null)
112       {
113          BasicNewProjectResourceWizard.updatePerspective(_config);
114          BasicNewProjectResourceWizard.selectAndReveal(this._newProject,
115             PlatformUI.getWorkbench().getActiveWorkbenchWindow());
116       }
117       return true;
118    }
119 
120    /***
121     * Creates a new project resource with the name selected in the wizard page.
122     * Project creation is wrapped in a <code>WorkspaceModifyOperation</code>.
123     * <p>
124     * 
125     * @see org.eclipse.ui.actions.WorkspaceModifyOperation
126     * 
127     * @return the created project resource, or <code>null</code> if the
128     *         project was not created
129     */
130    public IProject createNewProject()
131    {
132       if(_newProject != null)
133       {
134          return _newProject;
135       }
136 
137       // get a project handle
138       final IProject newProjectHandle = _mainPage.getProjectHandle();
139 
140       // get a project descriptor
141       IPath defaultPath = Platform.getLocation();
142       IPath newPath = _mainPage.getLocationPath();
143       if(defaultPath.equals(newPath))
144       {
145          newPath = null;
146       }
147       IWorkspace workspace = ResourcesPlugin.getWorkspace();
148       final IProjectDescription description = workspace
149          .newProjectDescription(newProjectHandle.getName());
150       description.setLocation(newPath);
151 
152       // create the new project operation
153       WorkspaceModifyOperation op = new WorkspaceModifyOperation()
154       {
155          protected void execute(IProgressMonitor monitor) throws CoreException
156          {
157             createProject(description, newProjectHandle, monitor);
158             //            addOWLNature(newProjectHandle);
159             OWLCore.addOWLNature(newProjectHandle);
160          }
161       };
162 
163       // run the new project creation operation
164       try
165       {
166          getContainer().run(false, true, op);
167       }
168       catch (InterruptedException e)
169       {
170          return null;
171       }
172       catch (InvocationTargetException e)
173       {
174          // ie.- one of the steps resulted in a core exception
175          //         resultError(
176          //            "NewOWLProjectWizard:createNewProject:",
177          //            "Project creation failed");
178          //         e.printStackTrace();
179          OWLCore.logError(WizardsPlugin.getID(), "Project creation failed", e);
180 
181          return null;
182       }
183 
184       _newProject = newProjectHandle;
185 
186       return _newProject;
187    }
188 
189    /***
190     * Creates a project resource given the project handle and description.
191     * 
192     * @param description the project description to create a project resource
193     *        for
194     * @param projectHandle the project handle to create a project resource for
195     * @param monitor the progress monitor to show visual progress with
196     * 
197     * @exception CoreException if the operation fails
198     */
199    public void createProject(IProjectDescription description,
200       IProject projectHandle, IProgressMonitor monitor) throws CoreException
201    {
202       try
203       {
204          monitor.beginTask("", 2000);
205 
206          projectHandle.create(description,
207             new SubProgressMonitor(monitor, 1000));
208 
209          if(monitor.isCanceled())
210          {
211             throw new OperationCanceledException();
212          }
213 
214          projectHandle.open(new SubProgressMonitor(monitor, 1000));
215 
216       }
217       finally
218       {
219          monitor.done();
220       }
221    }
222 
223    /***
224     * Used to show action results.
225     * 
226     * @see org.eclipse.jface.dialogs.MessageDialog
227     */
228    protected void resultInformation(String title, String msg)
229    {
230       // Confirm Result
231 
232       if(_bTraceEnabled)
233       {
234          // trace only to console
235          System.out.println(title + msg);
236       }
237       else
238       {
239          // user interaction response
240          MessageDialog.openInformation(getShell(), title, msg);
241       }
242 
243    }
244 
245    /***
246     * Used to show action results.
247     * 
248     * @see org.eclipse.jface.dialogs.MessageDialog
249     */
250    protected void resultError(String title, String msg)
251    {
252       // Indicate Error
253       if(_bTraceEnabled)
254       {
255          // trace only to console
256          System.out.println(title + msg);
257       }
258       else
259       {
260          // user interaction response
261          MessageDialog.openError(getShell(), title, msg);
262       }
263 
264    }
265 
266    private IConfigurationElement _config;
267    /*
268     * (non-Javadoc)
269     * 
270     * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(
271     *    org.eclipse.core.runtime.IConfigurationElement,
272     *    java.lang.String, java.lang.Object)
273     */
274    public void setInitializationData(IConfigurationElement config,
275       String propertyName, Object data) throws CoreException
276    {
277       _config = config;
278    }
279 }