View Javadoc

1   /*
2    * $Id: NewOWLClassWizard.java,v 1.7 2005/07/08 18:21:59 jlerner Exp $
3    * Created on May 19, 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 org.eclipse.jface.viewers.IStructuredSelection;
13  import org.eclipse.jface.wizard.Wizard;
14  import org.eclipse.ui.INewWizard;
15  import org.eclipse.ui.IWorkbench;
16  
17  import com.bbn.swede.core.IOWLDocument;
18  import com.bbn.swede.core.OWLCore;
19  import com.bbn.swede.core.dom.IOASTRunnable;
20  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
21  import com.bbn.swede.core.dom.OASTException;
22  import com.bbn.swede.core.dom.OASTNode;
23  import com.bbn.swede.core.dom.owl.Klass;
24  import com.bbn.swede.core.dom.rdf.Rdf;
25  
26  /***
27   * Wizard for adding a class to an OWL document.
28   * @author tself
29   */
30  public class NewOWLClassWizard extends Wizard implements INewWizard
31  {
32     private NewOWLClassWizardPage _classPage;
33     private IStructuredSelection _selection;
34     private IWorkbench _workbench;
35  
36     /*
37      *  (non-Javadoc)
38      * @see org.eclipse.jface.wizard.IWizard#addPages()
39      */
40     public void addPages()
41     {
42        _classPage = new NewOWLClassWizardPage();
43        _classPage.setDescription(
44           "Insert an OWL Class into the selected document.");
45        //ontPage.setTitle("New OWL Project");
46        addPage(_classPage);
47     }
48     /*
49      *  (non-Javadoc)
50      * @see org.eclipse.jface.wizard.IWizard#performFinish()
51      */
52     public boolean performFinish()
53     {
54        createClass();
55        return true;
56     }
57     private void createClass()
58     {
59        final Klass cl =
60           new Klass(
61              _classPage.getClassName(),
62              _classPage.getComments(),
63              _classPage.getLabel());
64        IOWLDocument doc = (IOWLDocument) _selection.getFirstElement();
65        IOWLAbstractSyntaxTree tree = doc.getDocumentInfo().getOAST();
66        OASTNode[] nodes = tree.getRoot().getNodesOfType(OASTNode.RDF_RDF);
67        if (nodes.length != 1)
68        {
69           //TODO: indicate error & cancel dialog?
70        }
71        else
72        {
73           final Rdf rdf = (Rdf)nodes[0];
74           try
75           {
76              //This doesn't need to be in a runnable since we're only adding
77              //one thing, but this lets us specify a primary node for the event
78              //and ensure that the proper text is selected after the editor
79              //updates and formats the document.
80              tree.run(new IOASTRunnable()
81              {
82              
83                 public OASTNode getPrimaryNode()
84                 {
85                    return cl;
86                 }
87              
88                 public void run() throws OASTException
89                 {
90                    rdf.appendChildTag(cl);
91                 }
92              
93              });
94           }
95           catch (OASTException e)
96           {
97              //This should never happen, since the actions disable automatically
98              //for read-only documents, but make sure it gets reported anyway
99              OWLCore.logWarning(OWLCore.getID(),
100                "Unable to add class tag to " + doc.getElementName(),
101                e);
102          }
103       }
104    }
105    /*
106     *  (non-Javadoc)
107     * @see org.eclipse.ui.IWorkbenchWizard#init(
108     *    org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
109     */
110    public void init(IWorkbench workbench, IStructuredSelection selection)
111    {
112       _selection = selection;
113       _workbench = workbench;
114    }
115 
116 }