How is eclipse line number status line contribution item implemented?

后端 未结 3 1938
执笔经年
执笔经年 2021-01-20 03:55

I need to update status line editor-specific information. I already have my own implementation, but I would like to take a look how is eclipse contribution item, which shows

相关标签:
3条回答
  • 2021-01-20 04:35

    I've been looking into it, it's quite involved, and I'm not sure I got the complete picture, but in case this helps someone...

    The declarative way of binding an Editor with the contributions to the StatusLine (and Menu and Toolbar) is through IEditorActionBarContributor class. This class is declared for a editor type in plugin.xml - and typically a single instance is created for each editor type (several running instances of a same editor type will share a IEditorActionBarContributor instance, calling its doSetActiveEditor() method when activated), and it will be disposed when when the last running editor of that type is closed.

    Lets take as an example how the default text editor in Eclipse updates the "Insert/Override" info in the status line (from Eclipse 3.7)

    The default text editor is declared in org.eclipse.ui.editors's plugin.xml (some lines trimmed) as:

     <extension point="org.eclipse.ui.editors">
          <editor  name="%Editors.DefaultTextEditor"
                class="org.eclipse.ui.editors.text.TextEditor"
                contributorClass="org.eclipse.ui.editors.text.TextEditorActionContributor"
                id="org.eclipse.ui.DefaultTextEditor">
          </editor>
     </extension>
    

    TextEditorActionContributor is the key. What interest us is implemented in the parent class BasicTextEditorActionContributor; it defines (statically) the 4 status fields (STATUS_FIELD_DEFS) and it stores internally a fixed map (fStatusFields) of each statusField (the spec, say) to a StatusLineContributionItem object). When called from the Eclipse UI, it registers the 4 fields in the status line (the titles, basically) in the method contributeToStatusLine(IStatusLineManager statusLineManager) And each time a editor is activated, it passes to it -in doSetActiveEditor(IEditorPart part)- the full set of StatusLineContributionItems , prepared with the corresponding actionHandlers. The editor understands all this because it implements ITextEditorExtension.setStatusField().

    In the case of AbstractTextEditor, it has an private field of (inner class) type ToggleOverwriteModeAction, which calls

    toggleOverwriteMode()->handleInsertModeChanged()->updateStatusField("InputMode")
    

    The editor looks if it has a statusField stored with this category, if so it will call IStatusField.setText("Insert" / "Overwrite") and this will result in the update of the status line message.

    This is an example, but I guess it gives the general idea: an instance of EditorActionContributor, binded to a editor type, mantains a list of the StatusLineContributionItem to be updated, and the editor must write into the objects of this list when the corresponding status changes. In this way, the editor is decoupled from the status line (it doesn't know if/how a status change will be displayed in the UI).

    0 讨论(0)
  • 2021-01-20 04:37

    In order to find out how something is implemented in Eclipse, you can also use the so called Plug-in spy. The Plug-in spy is included in the Plug-in Development Environmend (PDE). It is executed with ALT+SHIFT+F1. For further details look this Plug-in development FAQ.

    0 讨论(0)
  • 2021-01-20 04:41

    I'm not exactly sure what you are asking for, but there is a concrete implementation of IStatusLineManager here: org.eclipse.jface.action.StatusLineManager

    Typically, if you want to access the status line and you have a handle for an editor, you can do something like this (borrowed from org.eclipse.jdt.internal.ui.javaeditor.AddImportOnSelectionAction:

    private IStatusLineManager getStatusLineManager() {
        return fEditor.getEditorSite().getActionBars().getStatusLineManager();
    }
    
    0 讨论(0)
提交回复
热议问题