View Javadoc

1   /*
2    * $Id: OWLEditor.java,v 1.37 2005/07/11 15:12:34 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   
9   package com.bbn.swede.editor;
10  
11  import java.util.ArrayList;
12  
13  import org.eclipse.core.resources.IMarker;
14  import org.eclipse.core.runtime.CoreException;
15  import org.eclipse.core.runtime.IAdaptable;
16  import org.eclipse.core.runtime.IConfigurationElement;
17  import org.eclipse.core.runtime.IExtension;
18  import org.eclipse.core.runtime.IExtensionPoint;
19  import org.eclipse.core.runtime.IExtensionRegistry;
20  import org.eclipse.core.runtime.IProgressMonitor;
21  import org.eclipse.core.runtime.Platform;
22  import org.eclipse.ui.IEditorInput;
23  import org.eclipse.ui.IEditorPart;
24  import org.eclipse.ui.IEditorSite;
25  import org.eclipse.ui.PartInitException;
26  import org.eclipse.ui.PlatformUI;
27  import org.eclipse.ui.ide.IDE;
28  import org.eclipse.ui.ide.IGotoMarker;
29  import org.eclipse.ui.part.FileEditorInput;
30  import org.eclipse.ui.part.MultiPageEditorPart;
31  import org.eclipse.ui.texteditor.ITextEditor;
32  import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
33  
34  import com.bbn.swede.core.OWLCore;
35  import com.bbn.swede.core.dom.OASTNode;
36  import com.hp.hpl.jena.rdf.model.Resource;
37  
38  /***
39   * Multi-page editor for OWL documents.  The source tab is present by default
40   * and provides access to the OWL text editor.  Additional tabs are added
41   * automatically for each extension of the 
42   * com.bbn.swede.editor.visualizationPage extension point.
43   * @author jlerner
44   */
45  public class OWLEditor
46     extends MultiPageEditorPart
47     implements IGotoMarker, IAdaptable
48  {
49     /***
50      * Array containing all extenders of com.bbn.swede.editors.visualizationPage.
51      */
52     protected static IConfigurationElement[] _visualizationPages;
53     /***
54      * Array containing all extenders of com.bbn.swede.editors.visualEditor.
55      */
56     protected static IConfigurationElement[] _visualEditors;
57     /***
58      * Finds all extenders of com.bbn.swede.editor.visualizationPage and stores
59      * stores them in <code>_visualizationPages</code>.
60      */
61     protected static void locateVisualizations()
62     {
63        if (_visualizationPages != null)
64        {
65           return;
66        }
67        IExtensionRegistry registry = Platform.getExtensionRegistry();
68        IExtensionPoint point =
69           registry.getExtensionPoint("com.bbn.swede.editor.visualizationPage");
70        if (point != null)
71        {
72           IExtension[] extensions = point.getExtensions();
73           ArrayList al = new ArrayList();
74           for (int i = 0; i < extensions.length; i++)
75           {
76              _visualizationPages = extensions[i].getConfigurationElements();
77              for (int j = 0; j < _visualizationPages.length; j++)
78              {
79                 if (_visualizationPages[j].getName().equals("visualization"))
80                 {
81                    al.add(_visualizationPages[j]);
82                 }      
83              }
84           }
85           _visualizationPages = (IConfigurationElement[])al.toArray(new IConfigurationElement[0]);
86        }
87        else
88        {
89           OWLCore
90              .logWarning(OWLCore.getID(),
91                 "visualizationPage extension point not found",
92                 new Exception());
93        }
94     }
95     
96     /***
97      * Finds all extenders of com.bbn.swede.editor.visualEditor and stores
98      * stores them in <code>_visualEditors</code>.
99      */
100    protected static void locateVisualEditors()
101    {
102       if (_visualEditors != null)
103       {
104          return;
105       }
106       IExtensionRegistry registry = Platform.getExtensionRegistry();
107       IExtensionPoint point =
108          registry.getExtensionPoint("com.bbn.swede.editor.visualEditor");
109       if (point != null)
110       {
111          IExtension[] extensions = point.getExtensions();
112          ArrayList al = new ArrayList();
113          for (int i = 0; i < extensions.length; i++)
114          {
115             _visualEditors = extensions[i].getConfigurationElements();
116             for (int j = 0; j < _visualEditors.length; j++)
117             {
118                if (_visualEditors[j].getName().equals("visualEditor"))
119                {
120                   al.add(_visualEditors[j]);
121                }
122             }
123          }
124          _visualEditors = (IConfigurationElement[])al.toArray(new IConfigurationElement[0]);
125       }
126       else
127       {
128          OWLCore
129          .logWarning(OWLCore.getID(),
130             "visualEditor extension point not found",
131             new Exception());
132       }
133    }
134 
135    /***
136     * Creates the pages for the multiple page editor.  
137     * The first page will be "Source", followed by a page for each entry in 
138     * <code>_visualizationPages</code>.
139     */
140    protected void createPages()
141    {
142       OWLEditorInput input = getOwlEditorInput();
143       try
144       {
145          OWLSourceEditor ed = new OWLSourceEditor(input.getOAST());
146          this.addPage(ed, input);
147          setPageText(0, "Source");
148          setPartName(input.getName());
149          int iNumPages = 1;
150          locateVisualEditors();
151          for (int i = 0; i < _visualEditors.length; i++)
152          {
153             String sName = _visualEditors[i].getAttribute("name");
154             String sClass = _visualEditors[i].getAttribute("class");
155             try
156             {
157                OWLVisualEditor ove = 
158                   (OWLVisualEditor)_visualEditors[i].createExecutableExtension("class");
159                addPage(ove, input);
160                setPageText(iNumPages++, sName);
161             }
162             catch (CoreException e)
163             {
164                OWLCore.logError(
165                   EditorPlugin.getID(),
166                   "Error creating executable extension for " + sClass,
167                   e);
168             }
169          }
170          locateVisualizations();
171          for (int i = 0; i < _visualizationPages.length; i++)
172          {
173             String sName = _visualizationPages[i].getAttribute("name");
174             String sClass = _visualizationPages[i].getAttribute("class");
175             try
176             {
177                IVisualizationPage page =
178                   (IVisualizationPage) _visualizationPages[i].createExecutableExtension(
179                      "class");
180                this.addPage(page, input);
181                setPageText(iNumPages++, sName);
182             }
183             catch (CoreException e)
184             {
185                OWLCore.logError(
186                   EditorPlugin.getID(),
187                   "Error creating executable extension for " + sClass,
188                   e);
189             }
190          }
191       }
192       catch (PartInitException e)
193       {
194          OWLCore.logError(
195             EditorPlugin.getID(),
196             "Error initializing editor part",
197             e);
198       }
199    }
200 
201    /*
202     *  (non-Javadoc)
203     * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
204     */
205    public void doSave(IProgressMonitor monitor)
206    {
207       IEditorPart ed = this.getEditor(0);
208       ed.doSave(monitor);
209    }
210 
211    /*
212     *  (non-Javadoc)
213     * @see org.eclipse.ui.ISaveablePart#doSaveAs()
214     */
215    public void doSaveAs()
216    {
217       IEditorPart ed = this.getEditor(0);
218       ed.doSaveAs();
219    }
220 
221    /*
222     *  (non-Javadoc)
223     * @see org.eclipse.ui.ide.IGotoMarker#gotoMarker(org.eclipse.core.resources.IMarker)
224     */
225    public void gotoMarker(IMarker marker)
226    {
227       setActivePage(0);
228 //      pageChange(0); // commented out to avoid the CME
229       IDE.gotoMarker(getSourceEditor(), marker);
230    }
231 
232    /*
233     *  (non-Javadoc)
234     * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
235     */
236    public boolean isSaveAsAllowed()
237    {
238       return getSourceEditor().isSaveAsAllowed();
239    }
240 
241    /*
242     *  (non-Javadoc)
243     * @see org.eclipse.ui.IEditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
244     */
245    public void init(IEditorSite site, IEditorInput input)
246       throws PartInitException
247    {
248       if (!(input instanceof FileEditorInput))
249       {
250          throw new PartInitException("Invalid Input");
251       }
252       FileEditorInput fei = (FileEditorInput) input;
253       input = new OWLEditorInput(fei.getFile());
254       //        setInput(input);
255            
256       super.init(site, input);
257    }
258 
259    /***
260     * Select and scroll to a specific com.hp.hpl.jena.rdf.model.Resource in
261     * the editor.  The text selected for <code>res</code> is determined by
262     * searching the editor's associated com.bbn.swede.core.dom.OAST for a
263     * node that corresponds to the resource.
264     * @param res The resource to select and display
265     */
266    public void goToResource(Resource res)
267    {
268       OASTNode node = getOwlEditorInput().getOAST().getNode(res);
269       if (node != null)
270       {
271          setActivePage(0);
272          pageChange(0);
273          getSourceEditor().selectNode(node);
274       }
275    }
276 
277    /***
278     * Select and scroll to a specific URI in the editor.  The text selected
279     * for <code>sUri</code> is determined by searching the editor's associated
280     * com.bbn.swede.core.dom.OAST for a node whose corresponding
281     * com.hp.hpl.jena.rdf.model.Resource has the specified URI.
282     * @param sUri The URI to select and display
283     */
284    public void goToURI(String sUri)
285    {
286       OASTNode node = getOwlEditorInput().getOAST().getNode(sUri);
287       if (node != null)
288       {
289          setActivePage(0);
290          pageChange(0);
291          getSourceEditor().selectNode(node);
292       }
293    }
294 
295    /***
296     * Returns the OWLEditorInput being used by this editor.  This is a
297     * convenience method, fully equivalent to (OWLEditorInput)getEditorInput().
298     * @return The OWLEditorInput associated with this editor
299     */
300    public OWLEditorInput getOwlEditorInput()
301    {
302       return (OWLEditorInput) getEditorInput();
303    }
304 
305    /***
306     * Provides direct access to the OWL text editor that appears as the first
307     * tab in the multi-page editor.
308     * @return The OWLSourceEditor associated with the "Source" tab
309     */
310    public OWLSourceEditor getSourceEditor()
311    {
312       return (OWLSourceEditor) getEditor(0);
313    }
314 
315    /***
316     * The content outline page for this instance of the editor.
317     */
318    protected OWLEditorContentOutlinePage _outline;
319    /*
320     *  (non-Javadoc)
321     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
322     */
323    public Object getAdapter(Class adapter)
324    {
325       if (IContentOutlinePage.class.equals(adapter))
326       {
327          _outline = ((OWLSourceEditor) this.getEditor(0)).getContentOutline(this);
328 //         _outline.addSelectionChangedListener(this);
329          return _outline;
330       }
331       if(ITextEditor.class.equals(adapter))
332       {
333          return ((ITextEditor)getEditor(0));
334       }
335       return super.getAdapter(adapter);
336    }
337 
338    /***
339     * Switches the multi-page editor to the "Source" tab.
340     */
341    public void enableSourcePage()
342    {
343       setActivePage(0);
344       pageChange(0);
345    }
346    
347    /* (non-Javadoc)
348     * @see org.eclipse.ui.part.MultiPageEditorPart#pageChange(int)
349     */
350    protected void pageChange(int newPageIndex)
351    {
352       if (newPageIndex > _visualEditors.length)
353       {
354          IVisualizationPage viz = (IVisualizationPage)getEditor(newPageIndex);
355          viz.activate();
356       }
357       super.pageChange(newPageIndex);
358    }
359    
360    /***
361     * Indicates whether a given sub-editor is the currently active page.
362     * @param part The sub-editor
363     * @return <code>true</code> if <code>part</code> is the active page,
364     *         <code>false</code> if not.
365     */
366    /*package*/ boolean isActivePage(final IEditorPart part)/package-summary/html">class="comment">package*/ boolean isActivePage(final IEditorPart part)/package-summary.html">/*package*/ boolean isActivePage(final IEditorPart part)/package-summary.html">class="comment">package*/ boolean isActivePage(final IEditorPart part)
367    {
368       //The active page check will blow up if run from a non-UI thread,
369       //so first check if we can get a handle on the active workbench window.
370       //If not, we're in the wrong kind of thread and we assume that the 
371       //calling editor must not be active.  Initial testing indicates that
372       //this is a safe conclusion.
373       if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() == null)
374       {
375          return false;
376       }
377       
378       IEditorPart active = getActiveEditor();
379       if (active == null || !active.equals(part))
380       {
381          return false;
382       }
383       
384       return true;
385    }
386 }
387