1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

2 ordered sets for highlightings: by start & by end

This commit is contained in:
Nikolay Pultsin 2013-04-30 18:50:55 +02:00
parent 3265f97512
commit 9c63c508cb
3 changed files with 47 additions and 25 deletions

View file

@ -19,9 +19,11 @@
package org.geometerplus.zlibrary.text.view;
import java.util.Comparator;
import org.geometerplus.zlibrary.core.util.ZLColor;
public abstract class ZLTextHighlighting implements Comparable<ZLTextHighlighting> {
public abstract class ZLTextHighlighting {
public abstract boolean isEmpty();
public abstract ZLTextPosition getStartPosition();
@ -31,8 +33,19 @@ public abstract class ZLTextHighlighting implements Comparable<ZLTextHighlightin
public abstract ZLColor getBackgroundColor();
public int compareTo(ZLTextHighlighting highlighting) {
final int cmp = getStartPosition().compareTo(highlighting.getStartPosition());
return cmp != 0 ? cmp : getEndPosition().compareTo(highlighting.getEndPosition());
}
public static final Comparator<ZLTextHighlighting> ByStartComparator =
new Comparator<ZLTextHighlighting>() {
public int compare(ZLTextHighlighting h0, ZLTextHighlighting h1) {
final int cmp = h0.getStartPosition().compareTo(h1.getStartPosition());
return cmp != 0 ? cmp : h0.getEndPosition().compareTo(h1.getEndPosition());
}
};
public static final Comparator<ZLTextHighlighting> ByEndComparator =
new Comparator<ZLTextHighlighting>() {
public int compare(ZLTextHighlighting h0, ZLTextHighlighting h1) {
final int cmp = h0.getEndPosition().compareTo(h1.getEndPosition());
return cmp != 0 ? cmp : h0.getStartPosition().compareTo(h1.getStartPosition());
}
};
}

View file

@ -23,30 +23,18 @@ import org.geometerplus.zlibrary.core.util.ZLColor;
class ZLTextManualHighlighting extends ZLTextHighlighting {
private final ZLTextView myView;
private ZLTextPosition myStartPosition;
private ZLTextPosition myEndPosition;
private final ZLTextPosition myStartPosition;
private final ZLTextPosition myEndPosition;
ZLTextManualHighlighting(ZLTextView view) {
ZLTextManualHighlighting(ZLTextView view, ZLTextPosition start, ZLTextPosition end) {
myView = view;
}
void setup(ZLTextPosition start, ZLTextPosition end) {
myStartPosition = new ZLTextFixedPosition(start);
myEndPosition = new ZLTextFixedPosition(end);
}
public boolean clear() {
if (isEmpty()) {
return false;
}
myStartPosition = null;
myEndPosition = null;
return true;
}
@Override
public boolean isEmpty() {
return myStartPosition == null;
return false;
}
@Override

View file

@ -61,7 +61,10 @@ public abstract class ZLTextView extends ZLTextViewBase {
private boolean myHighlightSelectedRegion = true;
private final ZLTextSelection mySelection = new ZLTextSelection(this);
private final ZLTextManualHighlighting myHighlighting = new ZLTextManualHighlighting(this);
private final SortedSet<ZLTextHighlighting> myHighlightingsByStart =
new TreeSet<ZLTextHighlighting>(ZLTextHighlighting.ByStartComparator);
private final SortedSet<ZLTextHighlighting> myHighlightingsByEnd =
new TreeSet<ZLTextHighlighting>(ZLTextHighlighting.ByEndComparator);
public ZLTextView(ZLApplication application) {
super(application);
@ -251,14 +254,30 @@ public abstract class ZLTextView extends ZLTextViewBase {
}
}
private boolean removeManualHighlightings() {
boolean result = false;
for (Iterator<ZLTextHighlighting> it = myHighlightingsByStart.iterator(); it.hasNext(); ) {
final ZLTextHighlighting h = it.next();
if (h instanceof ZLTextManualHighlighting) {
it.remove();
myHighlightingsByEnd.remove(h);
result = true;
}
}
return result;
}
public void highlight(ZLTextPosition start, ZLTextPosition end) {
myHighlighting.setup(start, end);
removeManualHighlightings();
final ZLTextHighlighting h = new ZLTextManualHighlighting(this, start, end);
myHighlightingsByStart.add(h);
myHighlightingsByEnd.add(h);
Application.getViewWidget().reset();
Application.getViewWidget().repaint();
}
public void clearHighlighting() {
if (myHighlighting.clear()) {
if (removeManualHighlightings()) {
Application.getViewWidget().reset();
Application.getViewWidget().repaint();
}
@ -775,7 +794,9 @@ public abstract class ZLTextView extends ZLTextViewBase {
private static final char[] SPACE = new char[] { ' ' };
private void drawTextLine(ZLTextPage page, ZLTextLineInfo info, int from, int to, int x, int y) {
drawBackgroung(mySelection, page, info, from, to, x, y);
drawBackgroung(myHighlighting, page, info, from, to, x, y);
for (ZLTextHighlighting h : myHighlightingsByStart) {
drawBackgroung(h, page, info, from, to, x, y);
}
final ZLPaintContext context = getContext();
final ZLTextParagraphCursor paragraph = info.ParagraphCursor;