How is eclipse line number status line contribution item implemented?

后端 未结 3 1948
执笔经年
执笔经年 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:

     
          
          
     
    

    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).

提交回复
热议问题