View Javadoc

1   /*
2    * $Id: NewOWLOntologyWizard.java,v 1.9 2005/07/11 20:31:07 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.AttributeNode;
20  import com.bbn.swede.core.dom.IOASTRunnable;
21  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
22  import com.bbn.swede.core.dom.OASTException;
23  import com.bbn.swede.core.dom.OASTNode;
24  import com.bbn.swede.core.dom.owl.Ontology;
25  import com.bbn.swede.core.dom.rdf.Rdf;
26  
27  /***
28   * Wizard for adding an Ontology tag with versioning information to an OWL
29   * document.
30   * @author tself
31   */
32  public class NewOWLOntologyWizard extends Wizard implements INewWizard
33  {
34     private NewOWLOntologyWizardPage _ontPage;
35     private IOWLDocument _doc;
36     private IWorkbench _workbench;
37  
38     /***
39      * Default constructor.
40      */
41     public NewOWLOntologyWizard()
42     {
43        super();
44     }
45     
46     /*
47      *  (non-Javadoc)
48      * @see org.eclipse.jface.wizard.IWizard#addPages()
49      */
50     public void addPages()
51     {
52        _ontPage = new NewOWLOntologyWizardPage();
53        _ontPage.setDescription(
54           "Insert an OWL Ontology into the selected document.");
55        //ontPage.setTitle("New OWL Project");
56        addPage(_ontPage);
57     }
58     /* (non-Javadoc)
59      * @see org.eclipse.jface.wizard.IWizard#performFinish()
60      */
61     public boolean performFinish()
62     {
63        createOntology();
64        return true;
65     }
66     
67     /***
68      * Creates the Ontology node for insertion into the document's OAST.
69      */
70     private void createOntology()
71     {
72        IOWLAbstractSyntaxTree tree = _doc.getDocumentInfo().getOAST();
73        OASTNode[] nodes = tree.getRoot().getNodesOfType(OASTNode.RDF_RDF);
74        if (nodes.length != 1)
75        {
76           //TODO: indicate error & cancel dialog?
77        }
78        else
79        {
80           //TODO: check for existing abbreviations for dc & ves and use those?
81           final Rdf rdf = (Rdf)nodes[0];
82           final Ontology ont =
83              new Ontology(
84                 _ontPage.getOntologyTitle(),
85                 _ontPage.getVersion(),
86                 _ontPage.getEmail(),
87                 _ontPage.getCreator(),
88                 null,
89                 _ontPage.getReleaseDate(),
90                 _ontPage.getReleaseLevel(),
91                 _ontPage.getStatus(),
92                 _ontPage.getComments());
93           try
94           {
95              tree.run(new IOASTRunnable()
96              {
97                 /* (non-Javadoc)
98                  * @see com.bbn.swede.core.dom.IOASTRunnable#run()
99                  */
100                public void run() throws OASTException
101                {
102                   AttributeNode att = rdf.getAttribute("xmlns:dc");
103                   if (att == null)
104                   {
105                      rdf.addAttribute("xmlns:dc", Ontology.S_URI_DC);
106                   }
107                   att = rdf.getAttribute("xmlns:ves");
108                   if (att == null)
109                   {
110                      rdf.addAttribute("xmlns:ves", Ontology.S_URI_VES);
111                   }
112                   rdf.prependChildTag(ont);
113                }
114 
115                public OASTNode getPrimaryNode()
116                {
117                   return ont;
118                }
119                
120             });
121          }
122          catch (OASTException e)
123          {
124             //This should never happen, since the actions disable automatically
125             //for read-only documents, but make sure it gets reported anyway
126             OWLCore.logWarning(OWLCore.getID(),
127                "Unable to add ontology tag to " + _doc.getElementName(),
128                e);
129          }
130       }
131    }
132    /* (non-Javadoc)
133     * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, 
134     *       org.eclipse.jface.viewers.IStructuredSelection)
135     */
136    public void init(IWorkbench workbench, IStructuredSelection selection)
137    {
138       _doc = (IOWLDocument) selection.getFirstElement();
139       _workbench = workbench;
140    }
141 
142 }