Class RichTextEditor

  • All Implemented Interfaces:
    org.eclipse.swt.graphics.Drawable

    public class RichTextEditor
    extends org.eclipse.swt.widgets.Composite
    Rich Text Editor control that wraps a Browser with enabled Javascript that shows a simple HTML template containing a ckeditor as rich text editor.

    The following style bits are supported:

    • SWT.RESIZE - specify if the resize function of ckeditor is enabled (mostly used for embedded usage)
    • SWT.MIN - specify if the configured minimum dimensions should be applied to the resize function of ckeditor
    • SWT.EMBEDDED - specify if the rich text editor is used in embedded mode (e.g. as a cell editor of a JFace viewer)
    Additionally the SWT Browser style bits SWT.MOZILLA or SWT.WEBKIT can be set to specify the native browser that should be used for rendering

    See Also:
    http://ckeditor.com/
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFocusListener​(org.eclipse.swt.events.FocusListener listener)  
      void addJavaCallbackListener​(JavaCallbackListener listener)
      Add a JavaCallbackListener that is triggered on executing a Java callback via custom ToolbarButton.
      void addKeyListener​(org.eclipse.swt.events.KeyListener listener)  
      void addModifyListener​(org.eclipse.swt.events.ModifyListener listener)
      Adds the listener to the collection of listeners who will be notified when the receiver's text is modified, by sending it one of the messages defined in the ModifyListener interface.
      void addToolbarButton​(ToolbarButton button)
      Adds the given ToolbarButton to the toolbar of the editor.
      void addToolbarButton​(ToolbarButton button, org.eclipse.swt.browser.BrowserFunction function)
      Adds the given ToolbarButton to the toolbar of the editor.
      private org.eclipse.swt.events.FocusEvent createFocusEvent()  
      private org.eclipse.swt.events.KeyEvent createKeyEvent​(java.lang.Double keyCode, java.lang.Double modifier)
      Creates a SWT KeyEvent out of the given informations.
      private org.eclipse.swt.events.ModifyEvent createModifyEvent()  
      void dispose()  
      private void doNotifyFocusGained​(org.eclipse.swt.events.FocusEvent event)  
      private void doNotifyFocusLost​(org.eclipse.swt.events.FocusEvent event)  
      private void doNotifyJavaExecutionFinished()  
      private void doNotifyJavaExecutionStarted()  
      private void doNotifyKeyPressed​(org.eclipse.swt.events.KeyEvent event)  
      private void doNotifyKeyReleased​(org.eclipse.swt.events.KeyEvent event)  
      private void doNotifyModifyText​(org.eclipse.swt.events.ModifyEvent event)  
      java.lang.Object evaluateJavascript​(java.lang.String script)
      Evaluates the specified script in the internal Browser and returns the result.
      boolean executeJavascript​(java.lang.String script)
      Executes the specified script in the internal Browser.
      boolean forceFocus()  
      RichTextEditorConfiguration getEditorConfiguration()
      This method returns the RichTextEditorConfiguration that is used to configure this RichTextEditor.
      protected int getMinimumHeight()
      Returns the minimum height that should be used for initially open the editor in embedded mode.
      protected int getMinimumWidth()
      Returns the minimum width that should be used for initially open the editor in embedded mode.
      java.lang.String getSelectedHTML()
      Returns the current selected text containing the markup tags for styling.
      java.lang.String getSelectedText()
      Returns the current selected text without any markup tags.
      java.lang.String getText()  
      void insertHTML​(java.lang.String html)
      Inserts HTML code into the currently selected position in the editor in WYSIWYG mode.
      void insertText​(java.lang.String text)
      Insert text content into the currently selected position in the editor in WYSIWYG mode.
      boolean isEditable()
      Returns the editable state.
      boolean isFocusControl()  
      boolean isHandleFocusChanges()  
      private static void locateTemplateURL()  
      void notifyFocusGained​(org.eclipse.swt.events.FocusEvent event)
      Notify the registered FocusListener that the editor gained focus.
      void notifyFocusLost​(org.eclipse.swt.events.FocusEvent event)
      Notify the registered FocusListener that the editor lost focus.
      void notifyKeyPressed​(org.eclipse.swt.events.KeyEvent event)
      Notify the registered KeyListener that a key was pressed.
      void notifyKeyReleased​(org.eclipse.swt.events.KeyEvent event)
      Notify the registered KeyListener that a key was released.
      void notifyModifyListeners​(org.eclipse.swt.events.ModifyEvent event)
      Notifies all of the receiver's listeners when the receiver's text is modified.
      void removeFocusListener​(org.eclipse.swt.events.FocusListener listener)  
      void removeJavaCallbackListener​(JavaCallbackListener listener)
      Remove a JavaCallbackListener that is triggered on executing a Java callback via custom ToolbarButton.
      void removeKeyListener​(org.eclipse.swt.events.KeyListener listener)  
      void removeModifyListener​(org.eclipse.swt.events.ModifyListener listener)
      Removes the listener from the collection of listeners who will be notified when the receiver's text is modified.
      void removeToolbarButton​(ToolbarButton button)
      Removes the given ToolbarButton from the toolbar of the editor.
      void setBounds​(int x, int y, int width, int height)  
      void setBounds​(org.eclipse.swt.graphics.Rectangle rect)  
      void setEditable​(boolean editable)
      Sets the editable state.
      boolean setFocus()  
      void setHandleFocusChanges​(boolean handleFocusChanges)
      Configure whether focus changes should be handled or not.
      (package private) void setInlineContainerBounds​(int x, int y, int width, int height)
      Used in embedded mode to support manual resizing of the editor.
      void setLanguage​(java.lang.String language)
      Sets the user interface language localization to use.
      void setLanguage​(java.lang.String language, boolean update)  
      void setLanguage​(java.util.Locale locale)
      Sets the user interface language localization to use.
      void setLanguage​(java.util.Locale locale, boolean update)  
      void setText​(java.lang.String text)
      Set text to the editing area.
      void setVisible​(boolean visible)  
      void updateEditor()
      Update the editor.
      void updateToolbar()
      Update the toolbar.
      • Methods inherited from class org.eclipse.swt.widgets.Composite

        changed, checkSubclass, drawBackground, getBackgroundMode, getChildren, getLayout, getLayoutDeferred, getTabList, isLayoutDeferred, layout, layout, layout, layout, layout, setBackgroundMode, setLayout, setLayoutDeferred, setTabList
      • Methods inherited from class org.eclipse.swt.widgets.Scrollable

        computeTrim, getClientArea, getHorizontalBar, getScrollbarsMode, getVerticalBar
      • Methods inherited from class org.eclipse.swt.widgets.Control

        addControlListener, addDragDetectListener, addGestureListener, addHelpListener, addMenuDetectListener, addMouseListener, addMouseMoveListener, addMouseTrackListener, addMouseWheelListener, addPaintListener, addTouchListener, addTraverseListener, computeSize, computeSize, dragDetect, dragDetect, getAccessible, getBackground, getBackgroundImage, getBorderWidth, getBounds, getCursor, getDragDetect, getEnabled, getFont, getForeground, getLayoutData, getLocation, getMenu, getMonitor, getOrientation, getParent, getRegion, getShell, getSize, getTextDirection, getToolTipText, getTouchEnabled, getVisible, internal_dispose_GC, internal_new_GC, isAutoScalable, isEnabled, isReparentable, isVisible, moveAbove, moveBelow, pack, pack, print, redraw, redraw, removeControlListener, removeDragDetectListener, removeGestureListener, removeHelpListener, removeMenuDetectListener, removeMouseListener, removeMouseMoveListener, removeMouseTrackListener, removeMouseWheelListener, removePaintListener, removeTouchListener, removeTraverseListener, requestLayout, setBackground, setBackgroundImage, setCapture, setCursor, setDragDetect, setEnabled, setFont, setForeground, setLayoutData, setLocation, setLocation, setMenu, setOrientation, setParent, setRedraw, setRegion, setSize, setSize, setTextDirection, setToolTipText, setTouchEnabled, toControl, toControl, toDisplay, toDisplay, traverse, traverse, traverse, update
      • Methods inherited from class org.eclipse.swt.widgets.Widget

        addDisposeListener, addListener, checkWidget, getData, getData, getDisplay, getListeners, getStyle, isAutoDirection, isDisposed, isListening, notifyListeners, removeDisposeListener, removeListener, removeListener, reskin, setData, setData, toString
      • Methods inherited from class java.lang.Object

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

      • CKEDITOR_ALT

        private java.lang.Double CKEDITOR_ALT
      • CKEDITOR_CTRL

        private java.lang.Double CKEDITOR_CTRL
      • CKEDITOR_SHIFT

        private java.lang.Double CKEDITOR_SHIFT
      • editorLoaded

        private boolean editorLoaded
      • initialValue

        private java.lang.String initialValue
      • initialSetFocus

        private boolean initialSetFocus
      • resizedBounds

        protected org.eclipse.swt.graphics.Rectangle resizedBounds
      • browser

        private final org.eclipse.swt.browser.Browser browser
      • browserFunctions

        private final java.util.List<org.eclipse.swt.browser.BrowserFunction> browserFunctions
      • modifyListener

        private final org.eclipse.core.runtime.ListenerList modifyListener
      • keyListener

        private final org.eclipse.core.runtime.ListenerList keyListener
      • focusListener

        private final org.eclipse.core.runtime.ListenerList focusListener
      • javaCallbackListener

        private final org.eclipse.core.runtime.ListenerList javaCallbackListener
      • embeddedShell

        private org.eclipse.swt.widgets.Shell embeddedShell
      • mouseDragPosition

        private org.eclipse.swt.graphics.Point mouseDragPosition
      • handleFocusChanges

        private boolean handleFocusChanges
      • JAR_UNPACK_LOCATION_PROPERTY

        public static final java.lang.String JAR_UNPACK_LOCATION_PROPERTY
        Key of the system property to specify a fixed directory to unpack the ckeditor resources to. If a system property for that key is registered and the rich text control is deployed within a JAR, the resources will be unpacked into the specified directory. If no value is registered for that key and the rich text control is deployed in a JAR, the resources will be unpacked into a temporary directory, that gets deleted when the runtime is shutdown. If the rich text control is not deployed within a JAR but as part of an Eclipse application, the bundle will be unpacked automatically. In this case this system property won't get interpreted.
        See Also:
        Constant Field Values
      • templateURL

        private static java.net.URL templateURL
    • Method Detail

      • dispose

        public void dispose()
        Overrides:
        dispose in class org.eclipse.swt.widgets.Widget
      • setVisible

        public void setVisible​(boolean visible)
        Overrides:
        setVisible in class org.eclipse.swt.widgets.Control
      • getText

        public java.lang.String getText()
        Returns:
        The text that is currently set in the editing area. Contains HTML tags for formatting.
      • setText

        public void setText​(java.lang.String text)
        Set text to the editing area. Can contain HTML tags for styling.
        Parameters:
        text - The text to set to the editing area.
      • insertText

        public void insertText​(java.lang.String text)
        Insert text content into the currently selected position in the editor in WYSIWYG mode. The styles of the selected element will be applied to the inserted text. Spaces around the text will be left untouched.
        Parameters:
        text - Text to be inserted into the editor.
      • insertHTML

        public void insertHTML​(java.lang.String html)
        Inserts HTML code into the currently selected position in the editor in WYSIWYG mode.
        Parameters:
        html - HTML code to be inserted into the editor.
      • getSelectedText

        public java.lang.String getSelectedText()
        Returns the current selected text without any markup tags.
        Returns:
        The current selected text without any markup tags.
      • getSelectedHTML

        public java.lang.String getSelectedHTML()
        Returns the current selected text containing the markup tags for styling.

        Note: It will not contain the parent tag.

        Returns:
        The current selected text containing any markup styling tags.
      • isEditable

        public boolean isEditable()
        Returns the editable state.
        Returns:
        whether or not the receiver is editable
      • updateToolbar

        public void updateToolbar()
        Update the toolbar. Typically used if buttons where added or removed at runtime.
      • updateEditor

        public void updateEditor()
        Update the editor. Basically it will destroy and recreate the CKEditor. Needed to be used if a basic configuration is changed, e.g. the language.
        Since:
        1.1
      • setEditable

        public void setEditable​(boolean editable)
        Sets the editable state.
        Parameters:
        editable - the new editable state
      • setLanguage

        public void setLanguage​(java.util.Locale locale)
        Sets the user interface language localization to use. Only the language part of the Locale will be used. This method triggers an immediate update of the editor instance.
        Parameters:
        locale - The user interface language localization to use.
        Since:
        1.1
      • setLanguage

        public void setLanguage​(java.util.Locale locale,
                                boolean update)
        Parameters:
        locale - The user interface language localization to use.
        update - true if the editor should be updated immediately, false if the update should not be executed. In that case updateEditor() needs to be executed explicitly.
        Since:
        1.1
      • setLanguage

        public void setLanguage​(java.lang.String language)
        Sets the user interface language localization to use. This method triggers an immediate update of the editor instance.
        Parameters:
        language - The user interface language localization to use.
        Since:
        1.1
      • setLanguage

        public void setLanguage​(java.lang.String language,
                                boolean update)
        Parameters:
        language - The user interface language localization to use.
        update - true if the editor should be updated immediately, false if the update should not be executed. In that case updateEditor() needs to be executed explicitly.
        Since:
        1.1
      • setFocus

        public boolean setFocus()
        Overrides:
        setFocus in class org.eclipse.swt.widgets.Composite
      • forceFocus

        public boolean forceFocus()
        Overrides:
        forceFocus in class org.eclipse.swt.widgets.Control
      • isFocusControl

        public boolean isFocusControl()
        Overrides:
        isFocusControl in class org.eclipse.swt.widgets.Control
      • addFocusListener

        public void addFocusListener​(org.eclipse.swt.events.FocusListener listener)
        Overrides:
        addFocusListener in class org.eclipse.swt.widgets.Control
      • removeFocusListener

        public void removeFocusListener​(org.eclipse.swt.events.FocusListener listener)
        Overrides:
        removeFocusListener in class org.eclipse.swt.widgets.Control
      • notifyFocusGained

        public void notifyFocusGained​(org.eclipse.swt.events.FocusEvent event)
        Notify the registered FocusListener that the editor gained focus.
        Parameters:
        event - The event to fire.
      • doNotifyFocusGained

        private void doNotifyFocusGained​(org.eclipse.swt.events.FocusEvent event)
      • notifyFocusLost

        public void notifyFocusLost​(org.eclipse.swt.events.FocusEvent event)
        Notify the registered FocusListener that the editor lost focus.
        Parameters:
        event - The event to fire.
      • doNotifyFocusLost

        private void doNotifyFocusLost​(org.eclipse.swt.events.FocusEvent event)
      • setBounds

        public void setBounds​(org.eclipse.swt.graphics.Rectangle rect)
        Overrides:
        setBounds in class org.eclipse.swt.widgets.Control
      • setBounds

        public void setBounds​(int x,
                              int y,
                              int width,
                              int height)
        Overrides:
        setBounds in class org.eclipse.swt.widgets.Control
      • setInlineContainerBounds

        void setInlineContainerBounds​(int x,
                                      int y,
                                      int width,
                                      int height)
        Used in embedded mode to support manual resizing of the editor. Executed via callback on ckeditor resize.
        Parameters:
        x - the new x coordinate for the receiver
        y - the new y coordinate for the receiver
        width - the new width for the receiver
        height - the new height for the receiver
      • getMinimumHeight

        protected int getMinimumHeight()
        Returns the minimum height that should be used for initially open the editor in embedded mode. It is also used to specify the resize minimum height if the editor was created using the style bit SWT.MIN. Using the default RichTextEditorConfiguration this is 150 for the toolbar and 50 for showing one row in the editor area.
        Returns:
        The minimum height to use for initially open the editor in embedded mode and for editor resize minimum in case the editor was created with SWT.MIN
      • getMinimumWidth

        protected int getMinimumWidth()
        Returns the minimum width that should be used for initially open the editor in embedded mode. It is also used to specify the resize minimum width if the editor was created using the style bit SWT.MIN. Using the default RichTextEditorConfiguration this is 370 for showing the default options in three lines of the toolbar.
        Returns:
        The minimum width to use for initially open the editor in embedded mode and for editor resize minimum in case the editor was created with SWT.MIN
      • executeJavascript

        public boolean executeJavascript​(java.lang.String script)
        Executes the specified script in the internal Browser. Can be used to execute Javascript directly in the browser from a listener if necessary.
        Parameters:
        script - the script with javascript commands
        Returns:
        true if the operation was successful and false otherwise
        See Also:
        Browser.execute(String)
      • evaluateJavascript

        public java.lang.Object evaluateJavascript​(java.lang.String script)
        Evaluates the specified script in the internal Browser and returns the result. Can be used to evaluate Javascript directly in the browser from a listener if necessary.
        Parameters:
        script - the script with javascript commands
        Returns:
        the return value, if any, of executing the script
        See Also:
        Browser.evaluate(String)
      • isHandleFocusChanges

        public boolean isHandleFocusChanges()
        Returns:
        true if focus changes are handled, false if not
      • setHandleFocusChanges

        public void setHandleFocusChanges​(boolean handleFocusChanges)
        Configure whether focus changes should be handled or not. A typical use case for disabling the focus handling is for example to open a dialog from a Java callback via custom toolbar button.
        Parameters:
        handleFocusChanges - true if focus changes should be handled, false if not
      • addKeyListener

        public void addKeyListener​(org.eclipse.swt.events.KeyListener listener)
        Overrides:
        addKeyListener in class org.eclipse.swt.widgets.Control
      • removeKeyListener

        public void removeKeyListener​(org.eclipse.swt.events.KeyListener listener)
        Overrides:
        removeKeyListener in class org.eclipse.swt.widgets.Control
      • notifyKeyPressed

        public void notifyKeyPressed​(org.eclipse.swt.events.KeyEvent event)
        Notify the registered KeyListener that a key was pressed.
        Parameters:
        event - The event to fire.
      • doNotifyKeyPressed

        private void doNotifyKeyPressed​(org.eclipse.swt.events.KeyEvent event)
      • notifyKeyReleased

        public void notifyKeyReleased​(org.eclipse.swt.events.KeyEvent event)
        Notify the registered KeyListener that a key was released.
        Parameters:
        event - The event to fire.
      • doNotifyKeyReleased

        private void doNotifyKeyReleased​(org.eclipse.swt.events.KeyEvent event)
      • addModifyListener

        public void addModifyListener​(org.eclipse.swt.events.ModifyListener listener)
        Adds the listener to the collection of listeners who will be notified when the receiver's text is modified, by sending it one of the messages defined in the ModifyListener interface.
        Parameters:
        listener - the listener which should be notified
        Throws:
        java.lang.IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the listener is null
        org.eclipse.swt.SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        ModifyListener, removeModifyListener(org.eclipse.swt.events.ModifyListener)
      • removeModifyListener

        public void removeModifyListener​(org.eclipse.swt.events.ModifyListener listener)
        Removes the listener from the collection of listeners who will be notified when the receiver's text is modified.
        Parameters:
        listener - the listener which should no longer be notified
        Throws:
        java.lang.IllegalArgumentException -
        • ERROR_NULL_ARGUMENT - if the listener is null
        org.eclipse.swt.SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        ModifyListener, addModifyListener(org.eclipse.swt.events.ModifyListener)
      • notifyModifyListeners

        public void notifyModifyListeners​(org.eclipse.swt.events.ModifyEvent event)
        Notifies all of the receiver's listeners when the receiver's text is modified.
        Parameters:
        eventType - the type of event which has occurred
        event - the event data
        Throws:
        org.eclipse.swt.SWTException -
        • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
        • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
        See Also:
        addModifyListener(ModifyListener), removeModifyListener(ModifyListener)
      • doNotifyModifyText

        private void doNotifyModifyText​(org.eclipse.swt.events.ModifyEvent event)
      • createKeyEvent

        private org.eclipse.swt.events.KeyEvent createKeyEvent​(java.lang.Double keyCode,
                                                               java.lang.Double modifier)
        Creates a SWT KeyEvent out of the given informations.
        Parameters:
        keyCode - The keyCode sent by ckeditor.
        modifier - The modifier value sent by ckeditor.
        Returns:
        The KeyEvent containing the tranformed key event information.
      • doNotifyJavaExecutionStarted

        private void doNotifyJavaExecutionStarted()
      • doNotifyJavaExecutionFinished

        private void doNotifyJavaExecutionFinished()
      • createFocusEvent

        private org.eclipse.swt.events.FocusEvent createFocusEvent()
      • createModifyEvent

        private org.eclipse.swt.events.ModifyEvent createModifyEvent()
      • locateTemplateURL

        private static void locateTemplateURL()