Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I create an Outline view for my own language editor?"

(See Also:)
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
the user changes  editors, your editor will be asked to provide
 
the user changes  editors, your editor will be asked to provide
 
an <tt>IContentOutlinePage</tt> adapter for an Outline view.  
 
an <tt>IContentOutlinePage</tt> adapter for an Outline view.  
This is how you could implement your outline viewer:
+
This is how you should implement your outline viewer:
<pre>
+
 
   public Object getAdapter(Class required) {
+
In <tt>plugin.xml</tt>, define an adapter for your editor to content outline page:
 +
<source lang="xml">
 +
<extension point="org.eclipse.core.runtime.adapters">
 +
  <factory
 +
    adaptableType="org.acme.editor.CoolLanguageEditor"
 +
    class="org.acme.editor.CoolLanguageEditorToOutlineAdapterFactory">
 +
    <adapter type="org.eclipse.ui.views.contentoutline.IContentOutlinePage"/>
 +
  </factory>
 +
</extension>
 +
</source>
 +
 
 +
 
 +
and the Java code
 +
<source lang="java">
 +
public class CoolLanguageEditorToOutlineAdapterFactory implements IAdapterFactory {
 +
  @Override
 +
   public Object getAdapter(Object adaptableObject, Class<?> required) {
 
       if (IContentOutlinePage.class.equals(required)) {
 
       if (IContentOutlinePage.class.equals(required)) {
        if (myOutlinePage == null) {
+
            CoolLanguageEditor editor = (CoolLanguageEditor)adaptableObject;
             myOutlinePage = new CoolLanguageContentOutlinePage(
+
             return new CoolLanguageContentOutlinePage(...);
                          getDocumentProvider(), this);
+
            myOutlinePage.setInput(getEditorInput());
+
 
         }
 
         }
        return myOutlinePage;
 
 
       }
 
       }
      return super.getAdapter(required);
 
 
   }
 
   }
</pre>
+
 
 +
  @Override
 +
  public Class<?>[] getAdapterList() {
 +
      return new Class<?>[] { IContentOutlinePage.class };
 +
  }
 +
}
 +
</source>
 +
 
  
 
Most programming languages are inherently hierarchical. Therefore,
 
Most programming languages are inherently hierarchical. Therefore,
Line 26: Line 45:
 
This class already sets you up with a <tt>TreeViewer</tt>, and all you need to  
 
This class already sets you up with a <tt>TreeViewer</tt>, and all you need to  
 
provide are a content provider, a label provider, and the input:
 
provide are a content provider, a label provider, and the input:
<pre>
+
<source lang="java">
 
   public void createControl(Composite parent) {
 
   public void createControl(Composite parent) {
 
       super.createControl(parent);
 
       super.createControl(parent);
Line 35: Line 54:
 
       viewer.setInput(myInput);
 
       viewer.setInput(myInput);
 
   }
 
   }
</pre>
+
</source>
 
You will want to update the selection in your Outline view when the cursor is
 
You will want to update the selection in your Outline view when the cursor is
 
moved in the editor. Similarly, if the structure of the program changed&#151;code
 
moved in the editor. Similarly, if the structure of the program changed&#151;code
Line 43: Line 62:
 
When the user selects a node in the Outline view, the editor should change selection
 
When the user selects a node in the Outline view, the editor should change selection
 
to the selected element and make it visible.
 
to the selected element and make it visible.
 +
 +
 +
When https://bugs.eclipse.org/bugs/show_bug.cgi?id=507205 is fixed, a recommended way would be to rely on the Common Navigator Framework in order to implement Tree-based navigation outline pages. For the moment, you can already write your own outline view consuming the <tt>CommonViewer</tt> class.
 +
<source lang="java">
 +
  public void createControl(Composite parent) {
 +
      CommonViewer viewer = new CommonViewer(ID, parent, SWT.V_SCROLL | SWT.H_SCROLL);
 +
      viewer.setInput(myInput);
 +
  }
 +
</source>
 +
With <tt>ID</tt> being the name of the common navigator defined in <tt>org.eclipse.ui.navigator.viewer</tt> extension. Then the outline should receive most features as expected by the common navigator framework. This approach allows a better refactoring as multiple viewers can very easily share some common parts.
  
 
== See Also: ==
 
== See Also: ==

Latest revision as of 10:59, 14 March 2017

The Outline view is not generated by the editor framework. In fact, this view is offered by the org.eclipse.ui.views plug-in. When the user changes editors, your editor will be asked to provide an IContentOutlinePage adapter for an Outline view. This is how you should implement your outline viewer:

In plugin.xml, define an adapter for your editor to content outline page:

<extension point="org.eclipse.core.runtime.adapters">
  <factory
    adaptableType="org.acme.editor.CoolLanguageEditor"
    class="org.acme.editor.CoolLanguageEditorToOutlineAdapterFactory">
    <adapter type="org.eclipse.ui.views.contentoutline.IContentOutlinePage"/>
  </factory>
</extension>


and the Java code

public class CoolLanguageEditorToOutlineAdapterFactory implements IAdapterFactory {
   @Override
   public Object getAdapter(Object adaptableObject, Class<?> required) {
      if (IContentOutlinePage.class.equals(required)) {
            CoolLanguageEditor editor = (CoolLanguageEditor)adaptableObject;
            return new CoolLanguageContentOutlinePage(...);
         }
      }
   }
 
   @Override
   public Class<?>[] getAdapterList() {
      return new Class<?>[] { IContentOutlinePage.class };
   }
}


Most programming languages are inherently hierarchical. Therefore, to show the content outline of a certain program file, most editors deploy a tree. If you think that a tree is the most appropriate way to show the outline of your programs, you should consider subclassing from class ContentOutlinePage in the org.eclipse.ui.views.contentoutline package. This class already sets you up with a TreeViewer, and all you need to provide are a content provider, a label provider, and the input:

   public void createControl(Composite parent) {
      super.createControl(parent);
      TreeViewer viewer= getTreeViewer();
      viewer.setContentProvider(new MyContentProvider());
      viewer.setLabelProvider(new MyLabelProvider());
      viewer.addSelectionChangedListener(this);
      viewer.setInput(myInput);
   }

You will want to update the selection in your Outline view when the cursor is moved in the editor. Similarly, if the structure of the program changed&#151;code added or removed&#151;the outline has to be updated. This is typically performed with a JFace text model reconciler.

When the user selects a node in the Outline view, the editor should change selection to the selected element and make it visible.


When https://bugs.eclipse.org/bugs/show_bug.cgi?id=507205 is fixed, a recommended way would be to rely on the Common Navigator Framework in order to implement Tree-based navigation outline pages. For the moment, you can already write your own outline view consuming the CommonViewer class.

   public void createControl(Composite parent) {
      CommonViewer viewer = new CommonViewer(ID, parent, SWT.V_SCROLL | SWT.H_SCROLL);
      viewer.setInput(myInput);
   }

With ID being the name of the common navigator defined in org.eclipse.ui.navigator.viewer extension. Then the outline should receive most features as expected by the common navigator framework. This approach allows a better refactoring as multiple viewers can very easily share some common parts.

See Also:

FAQ How do I use a model reconciler?

FAQ Language integration phase 4: What are the finishing touches?


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.