Class BaseEditor

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class BaseEditor extends JPanel
An extended editor component with an additional tool bar and a popup menu.
See Also:
  • Field Details

    • PROPERTY_AUTO_SCROLL

      public static final String PROPERTY_AUTO_SCROLL
      Client property, Value: Boolean, if set to Boolean.FALSE, setText will not reset the scroll position
      See Also:
  • Constructor Details

    • BaseEditor

      public BaseEditor(boolean hasScrollpane)
      Complete construction of an editor, which is editable and has a toolbar.
      NOTE: This may take a while (up to 50ms!).
      Parameters:
      hasScrollpane - true, if the editor is supposed to have a scroll pane
    • BaseEditor

      public BaseEditor(boolean hasScrollpane, boolean editable, boolean hasToolbar, boolean isHtml)
      Complete construction of an editor.
      Parameters:
      hasScrollpane - true, if the editor is supposed to have a scroll pane
      editable - if true, the editor is editable by default
      hasToolbar - if true, the editor will have a toolbar
      isHtml - set to true, if the content will be HTML by default
  • Method Details

    • requestFocus

      public void requestFocus()
      Overrides:
      requestFocus in class JComponent
    • isAutoSpellCheck

      protected boolean isAutoSpellCheck()
      Is the automatic spell checking enabled. The default is true. This can be overridden form a custom instance.
      Returns:
      true if the spell checker is enabled, false if it's disabled or not present
    • setAutoSpellCkeck

      protected void setAutoSpellCkeck(boolean enabled)
      Enable the automatic spell checking.
      Parameters:
      enabled - the new value
    • createPopup

      protected void createPopup(JPopupMenu popupMenu)
      Create the context menu. The default implementation add the spell checker. You can override this method to add your own menu items.
      Parameters:
      popupMenu - an empty JPopupMenu
    • addButtons

      protected void addButtons(List<Component> defaultComponents, List<Component> editComponents, List<Component> formatComponents)
      Add buttons and commponent to the toolbar. You can override if you want add additional buttons.
      NOTE: The buttons defined here will be added to the BEGINNING of the groups in the toolbar
      Parameters:
      defaultComponents - a list of components, which will always be visible, if there is a toolbar
      editComponents - a list of component that will hide if isEditable() is false.
      formatComponents - a list of components that will hide if the format is plain text
    • hasFormatButton

      protected boolean hasFormatButton()
      Has the current Editor a format button in the toolbar. With the format button can the user switch between HTML and Plan text.
      Returns:
      ever true
    • showDocument

      public void showDocument(URL url, String string)
      Opens the specified location. The default implementation opens the page in the current editor window.
      NOTE: It is recommended to override this method to open the page by the default browser!
      Parameters:
      url - The page to load
      string - The target for the page, e.g. _blank
    • getBase

      protected URL getBase()
      Get the location to resolve relative URLs against. By default this is null. If a base tag is found and can be parsed, it will be used as the base location. It can be overridden. This also sets the base of the StyleSheet to be u as well as the base of the document.
      Returns:
      the currently set base URL
    • setBase

      public void setBase(URL base)
      Set a default value for getBase()
      Parameters:
      base - the new base value.
    • setText

      public void setText(String str, boolean isHtml)
      Sets the text to display. This action is performed in a separate Thread, so this method will return before the document has finished loading. To check the load state of the document, query the isLoading function or register an BaseEditor.TextLoadListener.
      Parameters:
      str - The content to display
      isHtml - has to be true, if the content is in HTML format
    • setTextWithLoadID

      public int setTextWithLoadID(String str, boolean isHtml)
      Sets the text to display. This action is performed in a separate Thread, so this method will return before the document has finished loading. To check the load state of the document, query the isLoading function or register an BaseEditor.TextLoadListener.
      The value returned by this method is the load-ID of this method call. When and if this content gets displayed, getCurrentLoadID will have the same value. Note, that this may never happen, if this method is called several times in a row.
      Parameters:
      str - The content to display
      isHtml - has to be true, if the content is in HTML format
      Returns:
      The load-ID of this call.
    • getCurrentLoadID

      public int getCurrentLoadID()
      Returns the load-ID of the currently displayed document. The load-ID is updated every time, a document gets displayed while no other load-action is pending.
      Returns:
      The current load-ID.
    • textLoaded

      protected void textLoaded()
      This method is called when the text was loaded. The default sets the editors inner margins to align HTML and text/plain equally
    • createEditorKit

      protected StyledEditorKit createEditorKit(boolean isHtml)
      Create a new EditorKit. The EditorKit is used for a new Document.
      Parameters:
      isHtml - true, HTML
      Returns:
      a EditorKit, never null
    • getText

      public String getText()
      Returns the content of the internal editor instance. The content can be HTML or plain text.
      Returns:
      the content of the internal editor instance
    • isHtml

      public boolean isHtml()
      Returns whether the content type of the internal editor is text/html
      Returns:
      true, if the content type is text/html
    • isChanged

      public boolean isChanged()
      If the text was changed since the last setText()
      Returns:
      true if it was changed
      See Also:
    • isLoading

      public boolean isLoading()
      If the text of setText is completly loaded. Does not means that it is completly layouted. Only the model (Document) is completly.
      Returns:
      true, if the text is parsing.
    • setUnChanged

      public void setUnChanged()
      Reset the status of changed.
    • isEditable

      public boolean isEditable()
      Returns whether the internal editor is currently editable
      Returns:
      true, if editable, false if not
    • setTextShowMode

      public void setTextShowMode()
      Show only the text and hide the tool bar
    • setEditable

      public void setEditable(boolean editable)
      Sets the editable state of the internal editor and updates the tool bar accordingly
      Parameters:
      editable - set true for editable
    • setEnabled

      public void setEnabled(boolean enabled)
      Overrides:
      setEnabled in class JComponent
    • getEditor

      public JTextPane getEditor()
      Returns the internal editor component
      Returns:
      the internal editor component, never null
    • getToolbar

      public JToolBar getToolbar()
      Returns the tool bar component
      Returns:
      the tool bar component, may be null if the editor has no tool bar
    • getToolBarController

      public BaseEditorToolBar getToolBarController()
      Returns the toolbar controller used by this editor
      Returns:
      the toolbar controller used by this editor, never null
    • insertHtml

      public void insertHtml(String htmlText)
      Inserts a HTML content into the current document. The content will be inserted at the current caret location. If there is a selection, this selection will be replaced.
      This is a convenience method to programmatically insert content without the requirement for any further settings.
      Parameters:
      htmlText - the HTML content to insert
    • insertFrom

      public void insertFrom(BaseEditor editor)
      Inserts the text of another editor into the current one. If the source editor has a selection, only the selected text will be inserted. The text will be inserted at the current caret location.
      Parameters:
      editor - the editor to get the content from, must not be null
    • addActionListener

      public void addActionListener(ActionListener listener)
      Adds an action listener to this editor
      Parameters:
      listener - the listener to add, must not be null
    • getEmbeddedImages

      public EmbeddedImage[] getEmbeddedImages()
      Returns a list with the reference to all images used in this document or null, if the document isn't an InetHtmlDocument.
      Returns:
      A list of all images used in this document or null
    • allowImagePaste

      public void allowImagePaste(boolean allowImagePaste)
      Enables or disables the image-paste function of the editor.
      Parameters:
      allowImagePaste - true, to enable
    • getUndoManager

      public UndoManager getUndoManager()
      Returns the undo manager registered to the internal JEditorPane. Use this function to reset the undo points in case you've done changes directly to the internal editor
      Returns:
      the undo manager of this editor
    • getPreferredSize

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent
    • getMinimumSize

      public Dimension getMinimumSize()
      Overrides:
      getMinimumSize in class JComponent
    • setConfiguration

      public void setConfiguration(InetHtmlConfiguration htmlConfiguration)
      Set the default configuration for HTML documents, opened by this editor.
      Parameters:
      htmlConfiguration - the configuration to set
    • setDefaultFont

      public void setDefaultFont(Font defaultFont)
      Sets the default font for this editor. The size of this font will be interpreted as pt!
      NOTE: This font and it's size will be preselected whe opening a document. This means: if the selection changes for any reasons, these settings will be overwritten by the font and size at the caret's location.
      Parameters:
      defaultFont - the default font and font-size
    • setDefaultFontPX

      public void setDefaultFontPX(Font defaultFont)
      Sets the default font for this editor. The size of this font will internally be converted into pt.
      NOTE: This font and it's size will be preselected whe opening a document. This means: if the selection changes for any reasons, these settings will be overwritten by the font and size at the caret's location.
      Parameters:
      defaultFont - the default font and font-size
    • setFontScale

      public void setFontScale(float scale)
      Sets the scale factor
      Parameters:
      scale - a value between 0.1 and 10
    • getDefaultFont

      public Font getDefaultFont()
      Returns the default font, if any.
      Note: The size of this font has to be interpreted as px!
      Returns:
      default font or null if none is set.
    • setPage

      public void setPage(URL page)
      Loads the page references by the page URL and thes the base URL. If the page could not be loaded, an error page is displayed.
      NOTE: This method will ensure that the current editor kit is an BaseEditor.SetPageEditorKit. If this is not the case, the editor kit will be replaced by a BaseEditor.SetPageEditorKit.
      Parameters:
      page - The URL to load.
    • getDocumentProperties

      public Dictionary<Object,Object> getDocumentProperties()
      Returns the persistant document properties table. If the current document is an inetHtmlDocument the properties of this document will be merged into the returned properties
      Returns:
      the current properties table
    • setDocumentProperties

      public void setDocumentProperties(Dictionary<Object,Object> properties)
      Sets a persistant document property table. This property table will be applied to every document handeled by this editor.
      Parameters:
      properties - the properties table to use
    • setDocumentProperty

      public void setDocumentProperty(Object key, Object value)
      Adds or sets a persistant document property. This property will be applied to every InetHtmlDocument handeled by this editor.
      Parameters:
      key - the key of the property, must not be null
      value - the value to set
    • setShowLinkURL

      public void setShowLinkURL(boolean showLinkURL)
      A switch to descide whether a link-tooltip always displayes the target URL. By default set to false, which means, that the link tooltips will only show the target if there is no title-attribute defined.
      Parameters:
      showLinkURL - true, to force the link to be displayed
    • isShowLinkURL

      public boolean isShowLinkURL()
      A flag which descides whether a link-tooltip always displayes the target URL. By default set to false, which means, that the link tooltips will only show the target if there is no title-attribute defined.
      Returns:
      true, if the target will always be displayed
    • setCustomHyperlinkHandler

      public void setCustomHyperlinkHandler(HyperlinkHandler handler)
      Sets the custom handler for the HyperlinkListener of this editor. The handler will be the fist code to be executed when this listener receives an event. It may interrupt the default behavior of this listener.
      Parameters:
      handler - the instance custom handler, may be null to reset
    • setCustomPasteHandler

      public void setCustomPasteHandler(PasteFileHandler handler)
      Sets the custom PasteFileHandler of the current TransferHandler.
      Note: This only affects the TransferHandler of Html-Editors!
      Parameters:
      handler - the custom handler to use
    • setTextAntiAlias

      public void setTextAntiAlias(boolean enabled)
      Enables or disables the text anti-alias render hint
      Parameters:
      enabled - true to enable text anti-alias
    • setSearchVisible

      public void setSearchVisible(boolean visible)
      Displays or hides the search-button, if available
      Parameters:
      visible - true for visible, false for hidden
    • addTextLoadListener

      public void addTextLoadListener(BaseEditor.TextLoadListener listener)
      Adds a listener to the editor to receive notification whenever a text or page has been loaded
      Parameters:
      listener - the listener to be added
    • removeTextLoadListener

      public void removeTextLoadListener(BaseEditor.TextLoadListener listener)
      Removes a listener from the editor
      Parameters:
      listener - the listener to be removed
    • setTextSelectionEnabled

      public void setTextSelectionEnabled(boolean enabled)
      Enables or disables the selectability of the content of the editor. If the selection is disabled, all transfer actions are blocked as well (copy, cut, and so on)
      Parameters:
      enabled - true to enable, false to disable