Class CustomXtextEditorCallback

  • All Implemented Interfaces:
    org.eclipse.xtext.ui.editor.IXtextEditorCallback

    public class CustomXtextEditorCallback
    extends org.eclipse.xtext.ui.editor.CompoundXtextEditorCallback
    Implements a custom CompoundXtextEditorCallback which can be registered to a XtextEditor in oder to react on key events. In particular, this class is designed to enable the "Jump-to-definition" feature if the key F3 is pressed. However, other custom key bindings can be defined here as wells.
    Author:
    kroeher
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.eclipse.xtext.ui.editor.IXtextEditorCallback

        org.eclipse.xtext.ui.editor.IXtextEditorCallback.NullImpl
    • Constructor Summary

      Constructors 
      Constructor Description
      CustomXtextEditorCallback​(com.google.inject.Injector injector)
      The constructor of a custom CompoundXtextEditorCallback.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addKeyListener​(org.eclipse.xtext.ui.editor.XtextEditor editor)
      Sets up this CompoundXtextEditorCallback by creating a new KeyListener which will be add to the text widget of the given XtextEditor.
      void afterCreatePartControl​(org.eclipse.xtext.ui.editor.XtextEditor editor)  
      private java.lang.String getSelectedElement()
      Returns the name of the selected element in the editor as String by calling getSelectedElementBySelection.
      private java.lang.String getSelectedElementByOffset()
      Returns the name of the element around the current position of the cursor in the document (defined by the offset of the current text-selection) of the XtextEditor.
      private java.lang.String getSelectedElementBySelection()
      Returns the name of the selected element in the editor as String.
      private void gotoElementDefinition()
      This method is called if F3-key is released.
      private boolean isValidElementCharacter​(char elementNameChar)
      Checks whether a given character is a valid character in the sense of its usage as part of a name of an IVML/VIL/VTL element.
      private boolean selectionAvailable()
      Checks whether an ISelection-instance is available.
      private boolean setCursorToDefinition​(java.lang.String selectedElement, org.eclipse.xtext.ui.editor.model.IXtextDocument xtextDocument, org.eclipse.xtext.ui.editor.XtextEditor documentEditor)
      Sets the focus of the cursor in the given editor to the definition of the selected element in the given document.
      • Methods inherited from class org.eclipse.xtext.ui.editor.CompoundXtextEditorCallback

        afterSave, afterSetInput, beforeDispose, beforeSetInput, handle, onValidateEditorInputState
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • editorDocument

        private org.eclipse.xtext.ui.editor.model.IXtextDocument editorDocument
    • Constructor Detail

      • CustomXtextEditorCallback

        public CustomXtextEditorCallback​(com.google.inject.Injector injector)
        The constructor of a custom CompoundXtextEditorCallback.
        Parameters:
        injector - the Injector-instance of the language the editor support, e.g. from the IvmlActivator-instance
    • Method Detail

      • afterCreatePartControl

        public void afterCreatePartControl​(org.eclipse.xtext.ui.editor.XtextEditor editor)
        Specified by:
        afterCreatePartControl in interface org.eclipse.xtext.ui.editor.IXtextEditorCallback
        Overrides:
        afterCreatePartControl in class org.eclipse.xtext.ui.editor.CompoundXtextEditorCallback
      • addKeyListener

        private void addKeyListener​(org.eclipse.xtext.ui.editor.XtextEditor editor)
        Sets up this CompoundXtextEditorCallback by creating a new KeyListener which will be add to the text widget of the given XtextEditor.
        Parameters:
        editor - the XtextEditor to which the KeyListener should be added
      • gotoElementDefinition

        private void gotoElementDefinition()
        This method is called if F3-key is released. It identifies the selected language element, tries to find the corresponding definition of this element and, if found, sets the focus of the cursor to that definition.
      • setCursorToDefinition

        private boolean setCursorToDefinition​(java.lang.String selectedElement,
                                              org.eclipse.xtext.ui.editor.model.IXtextDocument xtextDocument,
                                              org.eclipse.xtext.ui.editor.XtextEditor documentEditor)
        Sets the focus of the cursor in the given editor to the definition of the selected element in the given document.
        Parameters:
        selectedElement - the selected element for which the definition has to be found in order to set the focus of the cursor to that definition
        xtextDocument - the document in which the definition has to be found
        documentEditor - the editor in which the focus of the editor has to be set
        Returns:
        true if setting the focus of the editor was successful. False otherwise.
      • getSelectedElement

        private java.lang.String getSelectedElement()
        Returns the name of the selected element in the editor as String by calling getSelectedElementBySelection. If no element is selected, the name of the element around the current position of the cursor will be returned by calling getSelectedElementByOffset.
        Returns:
        the name of the selected element in the editor or the element around the current position of the cursor. May be null.
      • getSelectedElementBySelection

        private java.lang.String getSelectedElementBySelection()
        Returns the name of the selected element in the editor as String.
        Returns:
        the name of the selected element in the editor. May be null.
      • getSelectedElementByOffset

        private java.lang.String getSelectedElementByOffset()
        Returns the name of the element around the current position of the cursor in the document (defined by the offset of the current text-selection) of the XtextEditor.
        Returns:
        the name of the element around the current position of the cursor or null if no element could be found.
      • isValidElementCharacter

        private boolean isValidElementCharacter​(char elementNameChar)
        Checks whether a given character is a valid character in the sense of its usage as part of a name of an IVML/VIL/VTL element.
        Parameters:
        elementNameChar - the character that has the be checked for validity
        Returns:
        true if the character is alphabetic, a digit, or an underscore. False otherwise.
      • selectionAvailable

        private boolean selectionAvailable()
        Checks whether an ISelection-instance is available. However, the availability of such an instance does not mean that a specific part of the text in the editor is selected. It just ensures that an instance for checking such a selection is available and, thus, should prevent from producing NPEs.
        Returns:
        true if an ISelection-instance is available. False otherwise.