javax.swing.text

Class DefaultStyledDocument

  • All Implemented Interfaces:
    Serializable, Document, StyledDocument
    Direct Known Subclasses:
    HTMLDocument

    public class DefaultStyledDocument
    extends AbstractDocument
    implements StyledDocument
    A document that can be marked up with character and paragraph styles in a manner similar to the Rich Text Format. The element structure for this document represents style crossings for style runs. These style runs are mapped into a paragraph element structure (which may reside in some other structure). The style runs break at paragraph boundaries since logical styles are assigned to paragraph boundaries.

    Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

    See Also:
    Document, AbstractDocument
    • Constructor Detail

      • DefaultStyledDocument

        public DefaultStyledDocument(AbstractDocument.Content c,
                             StyleContext styles)
        Constructs a styled document.
        Parameters:
        c - the container for the content
        styles - resources and style definitions which may be shared across documents
      • DefaultStyledDocument

        public DefaultStyledDocument(StyleContext styles)
        Constructs a styled document with the default content storage implementation and a shared set of styles.
        Parameters:
        styles - the styles
      • DefaultStyledDocument

        public DefaultStyledDocument()
        Constructs a default styled document. This buffers input content by a size of BUFFER_SIZE_DEFAULT and has a style context that is scoped by the lifetime of the document and is not shared with other documents.
    • Method Detail

      • create

        protected void create(DefaultStyledDocument.ElementSpec[] data)
        Initialize the document to reflect the given element structure (i.e. the structure reported by the getDefaultRootElement method. If the document contained any data it will first be removed.
      • insert

        protected void insert(int offset,
                  DefaultStyledDocument.ElementSpec[] data)
                       throws BadLocationException
        Inserts new elements in bulk. This is useful to allow parsing with the document in an unlocked state and prepare an element structure modification. This method takes an array of tokens that describe how to update an element structure so the time within a write lock can be greatly reduced in an asynchronous update situation.

        This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

        Parameters:
        offset - the starting offset >= 0
        data - the element data
        Throws:
        BadLocationException - for an invalid starting offset
      • removeElement

        public void removeElement(Element elem)
        Removes an element from this document.

        The element is removed from its parent element, as well as the text in the range identified by the element. If the element isn't associated with the document, IllegalArgumentException is thrown.

        As empty branch elements are not allowed in the document, if the element is the sole child, its parent element is removed as well, recursively. This means that when replacing all the children of a particular element, new children should be added before removing old children.

        Element removal results in two events being fired, the DocumentEvent for changes in element structure and UndoableEditEvent for changes in document content.

        If the element contains end-of-content mark (the last "\n" character in document), this character is not removed; instead, preceding leaf element is extended to cover the character. If the last leaf already ends with "\n", it is included in content removal.

        If the element is null, NullPointerException is thrown. If the element structure would become invalid after the removal, for example if the element is the document root element, IllegalArgumentException is thrown. If the current element structure is invalid, IllegalStateException is thrown.

        Parameters:
        elem - the element to remove
        Throws:
        NullPointerException - if the element is null
        IllegalArgumentException - if the element could not be removed
        IllegalStateException - if the element structure is invalid
        Since:
        1.7
      • addStyle

        public Style addStyle(String nm,
                     Style parent)
        Adds a new style into the logical style hierarchy. Style attributes resolve from bottom up so an attribute specified in a child will override an attribute specified in the parent.
        Specified by:
        addStyle in interface StyledDocument
        Parameters:
        nm - the name of the style (must be unique within the collection of named styles). The name may be null if the style is unnamed, but the caller is responsible for managing the reference returned as an unnamed style can't be fetched by name. An unnamed style may be useful for things like character attribute overrides such as found in a style run.
        parent - the parent style. This may be null if unspecified attributes need not be resolved in some other style.
        Returns:
        the style
      • removeStyle

        public void removeStyle(String nm)
        Removes a named style previously added to the document.
        Specified by:
        removeStyle in interface StyledDocument
        Parameters:
        nm - the name of the style to remove
      • getStyle

        public Style getStyle(String nm)
        Fetches a named style previously added.
        Specified by:
        getStyle in interface StyledDocument
        Parameters:
        nm - the name of the style
        Returns:
        the style
      • getStyleNames

        public Enumeration<?> getStyleNames()
        Fetches the list of of style names.
        Returns:
        all the style names
      • setLogicalStyle

        public void setLogicalStyle(int pos,
                           Style s)
        Sets the logical style to use for the paragraph at the given position. If attributes aren't explicitly set for character and paragraph attributes they will resolve through the logical style assigned to the paragraph, which in turn may resolve through some hierarchy completely independent of the element hierarchy in the document.

        This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

        Specified by:
        setLogicalStyle in interface StyledDocument
        Parameters:
        pos - the offset from the start of the document >= 0
        s - the logical style to assign to the paragraph, null if none
      • getLogicalStyle

        public Style getLogicalStyle(int p)
        Fetches the logical style assigned to the paragraph represented by the given position.
        Specified by:
        getLogicalStyle in interface StyledDocument
        Parameters:
        p - the location to translate to a paragraph and determine the logical style assigned >= 0. This is an offset from the start of the document.
        Returns:
        the style, null if none
      • setCharacterAttributes

        public void setCharacterAttributes(int offset,
                                  int length,
                                  AttributeSet s,
                                  boolean replace)
        Sets attributes for some part of the document. A write lock is held by this operation while changes are being made, and a DocumentEvent is sent to the listeners after the change has been successfully completed.

        This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

        Specified by:
        setCharacterAttributes in interface StyledDocument
        Parameters:
        offset - the offset in the document >= 0
        length - the length >= 0
        s - the attributes
        replace - true if the previous attributes should be replaced before setting the new attributes
      • setParagraphAttributes

        public void setParagraphAttributes(int offset,
                                  int length,
                                  AttributeSet s,
                                  boolean replace)
        Sets attributes for a paragraph.

        This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

        Specified by:
        setParagraphAttributes in interface StyledDocument
        Parameters:
        offset - the offset into the paragraph >= 0
        length - the number of characters affected >= 0
        s - the attributes
        replace - whether to replace existing attributes, or merge them
      • getParagraphElement

        public Element getParagraphElement(int pos)
        Gets the paragraph element at the offset pos. A paragraph consists of at least one child Element, which is usually a leaf.
        Specified by:
        getParagraphElement in interface StyledDocument
        Specified by:
        getParagraphElement in class AbstractDocument
        Parameters:
        pos - the starting offset >= 0
        Returns:
        the element
      • getCharacterElement

        public Element getCharacterElement(int pos)
        Gets a character element based on a position.
        Specified by:
        getCharacterElement in interface StyledDocument
        Parameters:
        pos - the position in the document >= 0
        Returns:
        the element
      • insertUpdate

        protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng,
                        AttributeSet attr)
        Updates document structure as a result of text insertion. This will happen within a write lock. This implementation simply parses the inserted content for line breaks and builds up a set of instructions for the element buffer.
        Overrides:
        insertUpdate in class AbstractDocument
        Parameters:
        chng - a description of the document change
        attr - the attributes
      • createDefaultRoot

        protected AbstractDocument.AbstractElement createDefaultRoot()
        Creates the root element to be used to represent the default document structure.
        Returns:
        the element base
      • getForeground

        public Color getForeground(AttributeSet attr)
        Gets the foreground color from an attribute set.
        Specified by:
        getForeground in interface StyledDocument
        Parameters:
        attr - the attribute set
        Returns:
        the color
      • getBackground

        public Color getBackground(AttributeSet attr)
        Gets the background color from an attribute set.
        Specified by:
        getBackground in interface StyledDocument
        Parameters:
        attr - the attribute set
        Returns:
        the color
      • getFont

        public Font getFont(AttributeSet attr)
        Gets the font from an attribute set.
        Specified by:
        getFont in interface StyledDocument
        Parameters:
        attr - the attribute set
        Returns:
        the font
      • styleChanged

        protected void styleChanged(Style style)
        Called when any of this document's styles have changed. Subclasses may wish to be intelligent about what gets damaged.
        Parameters:
        style - The Style that has changed.

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/swing/text/defaultstyleddocument.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : https://docs.oracle.com

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut