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?"

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

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.

Back to the top