View Javadoc

1   /*
2    * $Id: OWLEditorContentOutlinePage.java,v 1.18 2005/05/31 19:37:20 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   package com.bbn.swede.editor;
9   
10  import org.eclipse.jface.viewers.ISelection;
11  import org.eclipse.jface.viewers.IStructuredSelection;
12  import org.eclipse.jface.viewers.SelectionChangedEvent;
13  import org.eclipse.jface.viewers.TreeViewer;
14  import org.eclipse.swt.widgets.Composite;
15  import org.eclipse.swt.widgets.Control;
16  import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
17  
18  import com.bbn.swede.core.dom.IOASTChangeListener;
19  import com.bbn.swede.core.dom.IOASTDelta;
20  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
21  import com.bbn.swede.core.dom.OAST;
22  import com.bbn.swede.core.dom.OASTEvent;
23  import com.bbn.swede.core.dom.OASTNode;
24  import com.bbn.swede.core.dom.TextNode;
25  import com.bbn.swede.ui.OASTContentProvider;
26  import com.bbn.swede.ui.OASTNodeLabelProvider;
27  
28  /***
29   * Provides controls that appear in the Outline View when an OWLEditor is active.
30   * It generates a tree control that directly
31   * represents the contents of the com.bbn.swede.core.dom.OAST for the active
32   * editor.
33   * @author jlerner
34   */
35  public class OWLEditorContentOutlinePage
36     extends ContentOutlinePage
37     implements IOASTChangeListener
38  {
39     private IOWLAbstractSyntaxTree _tree;
40     private OWLEditor _owlEditor;
41     /***
42      * Creates a new OWLEditorContentOutlinePage to represent a specified OAST. 
43      * @param owlEditor The editor displaying the document to be outlined
44      * @param tree The OAST to outline
45      */
46     public OWLEditorContentOutlinePage(OWLEditor owlEditor, 
47        IOWLAbstractSyntaxTree tree)
48     {
49        super();
50        _tree = tree;
51        _owlEditor = owlEditor;
52        if (_tree instanceof OAST)
53        {
54           ((OAST)_tree).addOASTChangeListener(this);
55        }
56     }
57  
58     /*
59      *  (non-Javadoc)
60      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
61      */
62     public void createControl(Composite parent)
63     {
64        super.createControl(parent);
65        TreeViewer treeViewer = getTreeViewer();
66        treeViewer.setLabelProvider(new OASTNodeLabelProvider());
67        treeViewer.setContentProvider(new OASTContentProvider());
68        treeViewer.setInput(_tree);
69        treeViewer.expandToLevel(2);
70     }
71  
72     private Object[] _aobjExpanded;
73  
74     /* (non-Javadoc)
75      * @see com.bbn.swede.core.dom.IOASTChangeListener#oastChanged(com.bbn.swede.core.dom.OASTEvent)
76      */
77     public void oastChanged(final OASTEvent event)
78     {
79        Control ctrl = getControl();
80        if (ctrl.isDisposed())
81        {
82           return;
83        }
84        
85        ctrl.getDisplay().asyncExec(new Runnable()
86        {
87           public void run()
88           {
89              _aobjExpanded = getTreeViewer().getExpandedElements();
90              int mask = IOASTDelta.CHANGED | IOASTDelta.REPLACED;
91              for (int i = 0; i < _aobjExpanded.length; i++)
92              {
93                 OASTNode node = (OASTNode)_aobjExpanded[i];
94                 IOASTDelta delta = event.getDelta(node);
95                 if (delta != null && delta.typeMatches(mask))
96                 {
97                    _aobjExpanded[i] = delta.getNodeAfter();
98                 }
99              }
100             getTreeViewer().refresh();
101             getTreeViewer().setExpandedElements(_aobjExpanded);
102          }
103       });
104    }
105 
106    /*
107     *  (non-Javadoc)
108     * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(
109     *    org.eclipse.jface.viewers.SelectionChangedEvent)
110     */
111    public void selectionChanged(SelectionChangedEvent event)
112    {
113       super.selectionChanged(event);
114       ISelection sel = event.getSelection();
115       if (sel.isEmpty())
116       {
117          return;
118       }
119       if (sel instanceof IStructuredSelection)
120       {
121          IStructuredSelection selection = (IStructuredSelection) sel;
122          OASTNode node = (OASTNode) selection.getFirstElement();
123          _owlEditor.enableSourcePage();
124          if (node instanceof TextNode)
125          {
126             _owlEditor.getSourceEditor().selectNode(node);
127          }
128          else
129          {
130             _owlEditor.getSourceEditor().selectQName(node);
131          }
132       }
133       this.setFocus();
134 
135    }
136 }