public final class PositionsBag extends AbstractHighlightsContainer
Position
s.
The highlighted areas (highlights) are determined by their starting and ending positions 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 the bag.
The PositionsBag
is designed to work over a single
document, which is passed in to the constructor. All the Position
s
accepted by various methods in this class must refer to positions within
this document. Since there is no way how this could be checked it is up to
the users of this class to make sure that Position
s they pass in
to the bag are from the same Document
, which they used for creating
the bag.
The PositionsBag
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 and its 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 |
---|
PositionsBag(Document document)
Creates a new instance of
PositionsBag , which trims highlights
as they are added. |
PositionsBag(Document document,
boolean mergeHighlights)
Creates a new instance of
PositionsBag . |
Modifier and Type | Method and Description |
---|---|
void |
addAllHighlights(PositionsBag bag)
Adds all highlights from the bag passed in.
|
void |
addHighlight(Position startPosition,
Position endPosition,
AttributeSet attributes)
Adds a highlight to this bag.
|
void |
clear()
Removes all highlights previously added to this bag.
|
HighlightsSequence |
getHighlights(int startOffset,
int endOffset)
Gets highlights from an area of a document.
|
void |
removeHighlights(int startOffset,
int endOffset)
Removes highlights in the specific area.
|
void |
removeHighlights(Position startPosition,
Position endPosition,
boolean clip)
Removes highlights in the specific area.
|
void |
setHighlights(PositionsBag bag)
Resets this sequence to use the new set of highlights.
|
addHighlightsChangeListener, fireHighlightsChange, removeHighlightsChangeListener
public PositionsBag(Document document)
PositionsBag
, which trims highlights
as they are added. It calls the PositionsBag.PositionsBag(Document, boolean)
constructore
passing false
as a parameter.document
- The document that should be highlighted.public PositionsBag(Document document, boolean mergeHighlights)
PositionsBag
.document
- The document that should be highlighted.mergeHighlights
- Determines whether highlights should be merged
or trimmed.public void addHighlight(Position startPosition, Position endPosition, AttributeSet attributes)
Position
and by its attributes. Adding a highlight that overlaps
with one or more existing highlights can have a different result depending
on the value of the mergingHighlights
parameter used for
constructing this bag.startPosition
- The beginning of the highlighted area.endPosition
- The end of the highlighted area.attributes
- The attributes to use for highlighting.public void addAllHighlights(PositionsBag bag)
addHighlight
for all the highlights in the
bag
except that the changes are done atomically.bag
- The highlights that will be atomically added to this bag.public void setHighlights(PositionsBag bag)
bag
passed in as a parameter. The changes are done atomically.bag
- The new highlights.public void removeHighlights(Position startPosition, Position endPosition, 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
startPosition
- The beginning of the area to clear.endPosition
- The end of the area to clear.clip
- Whether to clip the partially overlapping highlights.public void removeHighlights(int startOffset, int endOffset)
startOffset
and
endOffset
parameters are removed from this sequence. The highlights
that only partialy overlap with the area will be removed as well.startOffset
- The beginning of the area to clear.endOffset
- The end of the area to clear.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()