public final class OffsetsBag extends AbstractHighlightsContainer
The highlighted areas (highlights) are determined by their starting and ending offsets in a document and the set of attributes that should be used for rendering that area. The highlights can be arbitrarily added to and remove from this bag.
The OffsetsBag
is designed to work over a single
document, which is passed in to the constructor. All offsets
accepted by various methods in this class must refer to positions within
this document. Therefore any offsets passed in to the methods in this class
have to be equal to or greater than zero and less than or equal to the document
size.
The OffsetsBag
can operate in two modes depending on a
value passed in its construtor. The mode determines how the bag will treat
newly added highlights that overlap with existing highlights in the bag.
Trimming mode: In the trimming mode the bag will trim any existing highlights that would overlap with a newly added highlight. In this mode the newly added highlights always replace the existing highlights if they overlap.
Merging mode: In the merging mode the bag will merge attributes of the overlapping highlights. The area where the new highlight overlaps with some existing highlights will then constitute a new highlight, which attributes will be a composition of the attributes of both the new and existing highlight. Should there be attributes with the same name the attribute values from the newly added highlight will take precedence.
ATTR_EXTENDS_EMPTY_LINE, ATTR_EXTENDS_EOL
Constructor and Description |
---|
OffsetsBag(Document document)
Creates a new instance of
OffsetsBag , which trims highlights
as they are added. |
OffsetsBag(Document document,
boolean mergeHighlights)
Creates a new instance of
OffsetsBag . |
Modifier and Type | Method and Description |
---|---|
void |
addAllHighlights(HighlightsSequence bag)
Adds all highlights from the bag passed in.
|
void |
addHighlight(int startOffset,
int endOffset,
AttributeSet attributes)
Adds a highlight to this bag.
|
void |
clear()
Removes all highlights previously added to this bag.
|
void |
discard()
Discards this
OffsetsBag . |
HighlightsSequence |
getHighlights(int startOffset,
int endOffset)
Gets highlights from an area of a document.
|
void |
removeHighlights(int startOffset,
int endOffset,
boolean clip)
Removes highlights in a specific area of the document.
|
void |
setHighlights(HighlightsSequence seq)
Resets this bag to use the new set of highlights.
|
void |
setHighlights(OffsetsBag bag)
Resets this bag to use the new set of highlights.
|
addHighlightsChangeListener, fireHighlightsChange, removeHighlightsChangeListener
public OffsetsBag(Document document)
OffsetsBag
, which trims highlights
as they are added. It calls the OffsetsBag.OffsetsBag(Document, boolean)
constructor
passing false
as a parameter.document
- The document that should be highlighted.public OffsetsBag(Document document, boolean mergeHighlights)
OffsetsBag
.document
- The document that should be highlighted.mergeHighlights
- Determines whether highlights should be merged
or trimmed.public void discard()
OffsetsBag
. This method should be called when
a client stops using the bag. After calling this method no other methods
should be called. The bag is effectively empty and it is not possible to
modify it.public void addHighlight(int startOffset, int endOffset, AttributeSet attributes)
mergingHighlights
parameter used for
constructing this bag.startOffset
- The beginning of the highlighted area.endOffset
- The end of the highlighted area.attributes
- The attributes to use for highlighting.public void addAllHighlights(HighlightsSequence bag)
addHighlight
for all the highlights in the
bag
except that the changes are done atomically.bag
- The bag of highlights that will be atomically
added to this bag.public void setHighlights(HighlightsSequence seq)
Document
as this bag.seq
- New highlights to add.public void setHighlights(OffsetsBag bag)
Document
.bag
- New highlights to add.public void removeHighlights(int startOffset, int endOffset, boolean clip)
startOffset
and
endOffset
parameters are removed from this bag. The highlights
that only partialy overlap with the area are treated according to the value
of the clip
parameter.
clip == true
: the overlapping highlights will remain
in this sequence but will be clipped so that they do not overlap anymore
clip == false
: the overlapping highlights will be
removed from this sequence
startOffset
- The beginning of the area to clear.endOffset
- The end of the area to clear.clip
- Whether to clip the partially overlapping highlights.public HighlightsSequence getHighlights(int startOffset, int endOffset)
HighlightsSequence
is
computed using all the highlights present in this bag between the
startOffset
and endOffset
.getHighlights
in interface HighlightsContainer
getHighlights
in class AbstractHighlightsContainer
startOffset
- The beginning of the area.endOffset
- The end of the area.HighlightsSequence
which iterates through the
highlights in the given area of this bag.public void clear()