View Javadoc

1   /*
2    * $Id: NewOWLPropertyWizard.java,v 1.6 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.TagNode;
24  import com.bbn.swede.core.dom.owl.DatatypeProperty;
25  import com.bbn.swede.core.dom.owl.ObjectProperty;
26  import com.bbn.swede.core.dom.rdf.Rdf;
27  
28  /***
29   * Wizard for adding a property to an OWL document.
30   * @author tself
31   */
32  public class NewOWLPropertyWizard extends Wizard implements INewWizard
33  {
34     private NewOWLPropertyWizardPage _propPage;
35     private IStructuredSelection _selection;
36     private IWorkbench _workbench;
37  
38     /***
39      * Default constructor.
40      */
41     public NewOWLPropertyWizard()
42     {
43        super();
44     }
45     
46     /*
47      *  (non-Javadoc)
48      * @see org.eclipse.jface.wizard.IWizard#addPages()
49      */
50     public void addPages()
51     {
52        _propPage = new NewOWLPropertyWizardPage();
53        _propPage.setDescription(
54           "Define a Property within the selected document.");
55        addPage(_propPage);
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.jface.wizard.IWizard#performFinish()
60      */
61     public boolean performFinish()
62     {
63        createProperty();
64        return true;
65     }
66     
67     /***
68      * Creates the ObjectProperty or DatatypeProperty node for insertion in the
69      * selected document's OAST.
70      */
71     private void createProperty()
72     {
73        final TagNode tag;
74        if (_propPage.isObjectProperty())
75        {
76           tag =
77              new ObjectProperty(
78                 _propPage.getPropertyName(),
79                 _propPage.getComments(),
80                 _propPage.getLabel());
81        }
82        else
83        {
84           tag =
85              new DatatypeProperty(
86                 _propPage.getPropertyName(),
87                 _propPage.getComments(),
88                 _propPage.getLabel());
89        }
90        IOWLDocument doc = (IOWLDocument) _selection.getFirstElement();
91        IOWLAbstractSyntaxTree tree = doc.getDocumentInfo().getOAST();
92        OASTNode[] nodes = tree.getRoot().getNodesOfType(OASTNode.RDF_RDF);
93        if (nodes.length != 1)
94        {
95           //TODO: indicate error & cancel dialog?
96        }
97        else
98        {
99           final Rdf rdf = (Rdf)nodes[0];
100          try
101          {
102             //This doesn't need to be in a runnable since we're only adding
103             //one thing, but this lets us specify a primary node for the event
104             //and ensure that the proper text is selected after the editor
105             //updates and formats the document.
106             tree.run(new IOASTRunnable()
107             {
108             
109                public OASTNode getPrimaryNode()
110                {
111                   return tag;
112                }
113             
114                public void run() throws OASTException
115                {
116                   rdf.appendChildTag(tag);
117                }
118             
119             });
120          }
121          catch (OASTException e)
122          {
123             //This should never happen, since the actions disable automatically
124             //for read-only documents, but make sure it gets reported anyway
125             OWLCore.logWarning(OWLCore.getID(),
126                "Unable to add property tag to " + doc.getElementName(),
127                e);
128          }
129       }
130    }
131    /* (non-Javadoc)
132     * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, 
133     *       org.eclipse.jface.viewers.IStructuredSelection)
134     */
135    public void init(IWorkbench workbench, IStructuredSelection selection)
136    {
137       _selection = selection;
138       _workbench = workbench;
139    }
140 
141 }