View Javadoc

1   /*
2    * $Id: NewOWLDocumentWizard.java,v 1.2 2005/05/06 19:30:26 jlerner Exp $
3    * Created on Apr 28, 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.io.IOException;
13  import java.io.InputStream;
14  
15  import org.eclipse.core.resources.IFile;
16  import org.eclipse.core.resources.IProject;
17  import org.eclipse.core.resources.IWorkspace;
18  import org.eclipse.core.resources.ResourcesPlugin;
19  import org.eclipse.jface.dialogs.MessageDialog;
20  import org.eclipse.jface.viewers.IStructuredSelection;
21  import org.eclipse.jface.wizard.Wizard;
22  import org.eclipse.swt.widgets.Display;
23  import org.eclipse.swt.widgets.Shell;
24  import org.eclipse.ui.INewWizard;
25  import org.eclipse.ui.IWorkbench;
26  import org.eclipse.ui.IWorkbenchPage;
27  import org.eclipse.ui.IWorkbenchWindow;
28  import org.eclipse.ui.PartInitException;
29  import org.eclipse.ui.PlatformUI;
30  import org.eclipse.ui.actions.NewProjectAction;
31  import org.eclipse.ui.ide.IDE;
32  import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
33  
34  import com.bbn.swede.core.IOWLDocument;
35  import com.bbn.swede.core.OWLCore;
36  import com.bbn.swede.core.resources.SWResourceManager;
37  import com.bbn.swede.ui.UIPlugin;
38  
39  /***
40   * Wizard for adding a new OWL document to a project.
41   * @author tself
42   */
43  public class NewOWLDocumentWizard extends Wizard implements INewWizard
44  {
45     private NewOWLDocumentWizardPage _pageMain;
46  
47     private IStructuredSelection _selection;
48  
49     // switch to control write of trace data
50     private boolean _bTraceEnabled = true;
51  
52     private boolean _allowEmptyWorkspace;
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        /*
64         * This will at least provide the user the ability to create a project if
65         * necessary. It will not cancel the Document wizard because I don't know
66         * how to do that. dispose() and performCancel() do nothing.
67         */
68        workspaceNotEmpty();
69  
70        _pageMain = new NewOWLDocumentWizardPage("New OWL Document", _selection);
71        addPage(_pageMain);
72        _pageMain.setTitle("New OWL Document");
73        _pageMain
74           .setDescription("Creates a new OWL document and adds it to the selected project.");
75     }
76  
77     /***
78      * Initializes this creation wizard using the passed workbench and object
79      * selection.
80      * <p>
81      * This method is called after the no argument constructor and before other
82      * methods are called.
83      * </p>
84      * 
85      * @param workbench the current workbench
86      * @param selection the current object selection
87      * 
88      * @see org.eclipse.ui.IWorkbenchWizard#init(IWorkbench,
89      *      IStructuredSelection)
90      */
91     public void init(IWorkbench workbench, IStructuredSelection selection)
92     {
93        setWindowTitle("New OWL Document");
94        _selection = selection;
95     }
96  
97     /***
98      * Creates a project with a <code>CustomNature</code> association.
99      * 
100     * @return <code>true</code> to indicate the finish request was accepted,
101     *         and <code>false</code> to indicate that the finish request was
102     *         refused
103     * 
104     * @see org.eclipse.jface.wizard.IWizard#performFinish()
105     */
106    public boolean performFinish()
107    {
108       try
109       {
110          InputStream input = _pageMain.openStream();
111       }
112       catch (IOException e)
113       {
114          _pageMain.setErrorMessage("The specified URL could not be resolved.");
115          return false;
116       }
117 
118       final IOWLDocument newDoc = createNewDocument(null);
119       if (newDoc == null)
120       {
121          return false;
122       }
123       IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
124       BasicNewResourceWizard.selectAndReveal(
125          newDoc.getCorrespondingResource(),
126          window);
127       final IWorkbenchPage page = window.getActivePage();
128       if (page != null)
129       {
130          Display display = getShell().getDisplay();
131          if (display != null)
132          {
133             display.asyncExec(new Runnable()
134             {
135                public void run()
136                {
137                   try
138                   {
139                      IDE.openEditor(page, newDoc.getFile(), true);
140                   }
141                   catch (PartInitException e)
142                   {
143                      OWLCore.logWarning(UIPlugin.getID(), "Unable to launch editor for new OWL document", e);
144                   }
145                }
146             });
147          }
148       }
149       
150       return true;
151    }
152 
153    /***
154     * Creates a new project resource with the name selected in the wizard page.
155     * Project creation is wrapped in a <code>WorkspaceModifyOperation</code>.
156     * <p>
157     * 
158     * @see org.eclipse.ui.actions.WorkspaceModifyOperation
159     * 
160     * @deprecated As of SWeDE 1.0.2, replaced by 
161     *             {@link #createNewDocument(InputStream)}.  Creating the
162     *             input stream externally allows the wizard to recover 
163     *             gracefully and notify the user if the URL is unreachable.
164     *             See also {@link NewOWLDocumentWizardPage#openStream()}.
165     * 
166     * @return the created project resource, or <code>null</code> if the
167     *         project was not created
168     */
169    public IOWLDocument createNewDocument()
170    {
171       IFile newFile = _pageMain.createNewFile(null);
172       if(newFile == null)
173       {
174          return null;
175       }
176       return SWResourceManager.create(newFile);
177    }
178    
179    /***
180     * Creates a new project resource with the name selected in the wizard page.
181     * Project creation is wrapped in a <code>WorkspaceModifyOperation</code>.
182     * <p>
183     * 
184     * @param input An input stream for the external document being imported.
185     *              If no document is being imported, this parameter is ignored
186     *              and may be <code>null</code>.
187     * 
188     * @see NewOWLDocumentWizardPage#openStream()
189     * @see org.eclipse.ui.actions.WorkspaceModifyOperation
190     * 
191     * @return the created project resource, or <code>null</code> if the
192     *         project was not created
193     */
194    public IOWLDocument createNewDocument(InputStream input)
195    {
196       IFile newFile = _pageMain.createNewFile(input);
197       if(newFile == null)
198       {
199          return null;
200       }
201       return SWResourceManager.create(newFile);
202    }
203 
204    /***
205     * Checks that there is at least one active/open project in the workspace.
206     * Provides user with opporunity to create one if not.
207     * 
208     * @return boolean true if projects exist
209     */
210    protected boolean workspaceNotEmpty()
211    {
212       if(_allowEmptyWorkspace)
213       {
214          return true;
215       }
216       boolean openProjectExists = false;
217       IWorkspace workspace = ResourcesPlugin.getWorkspace();
218       IProject[] allProjects = workspace.getRoot().getProjects();
219       for(int i = 0; i < allProjects.length; i++)
220       {
221          // caution: assignment within conditional
222          if(openProjectExists = allProjects[i].isOpen())
223          {
224             break;
225          }
226       }
227       if(!openProjectExists)
228       {
229          Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
230             .getShell();
231          String title = "Wizard Creation Error";
232          String message = "This wizard requires an active project. Select 'Yes' to create one, 'No' to abort.";
233          if(MessageDialog.openQuestion(shell, title, message))
234          {
235             IWorkbenchWindow window = PlatformUI.getWorkbench()
236                .getActiveWorkbenchWindow();
237             (new NewProjectAction(window)).run();
238             return workspace.getRoot().getProjects().length != 0;
239          }
240          return false;
241       }
242       return true;
243    }
244 
245    /***
246     * Used to show action results.
247     * 
248     * @see org.eclipse.jface.dialogs.MessageDialog
249     */
250    protected void resultInformation(String title, String msg)
251    {
252       // Confirm Result
253 
254       if(_bTraceEnabled)
255       {
256          // trace only to console
257          System.out.println(title + msg);
258       }
259       else
260       {
261          // user interaction response
262          MessageDialog.openInformation(getShell(), title, msg);
263       }
264 
265    }
266 
267    /***
268     * Used to show action results.
269     * 
270     * @see org.eclipse.jface.dialogs.MessageDialog
271     */
272    protected void resultError(String title, String msg)
273    {
274       // Indicate Error
275       if(_bTraceEnabled)
276       {
277          // trace only to console
278          System.out.println(title + msg);
279       }
280       else
281       {
282          // user interaction response
283          MessageDialog.openError(getShell(), title, msg);
284       }
285    }
286 }