mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
no link to ZLTextView in ZLTextParagraphCursor
This commit is contained in:
parent
a0fc30a1b4
commit
67217c4264
4 changed files with 36 additions and 46 deletions
|
@ -24,35 +24,26 @@ import java.util.*;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.text.model.ZLTextModel;
|
import org.geometerplus.zlibrary.text.model.ZLTextModel;
|
||||||
|
|
||||||
class ZLTextParagraphCursorCache {
|
final class CursorManager {
|
||||||
private final static class Key {
|
private final ZLTextModel myModel;
|
||||||
private final ZLTextModel myModel;
|
final ExtensionElementManager ExtensionManager;
|
||||||
private final int myIndex;
|
|
||||||
|
|
||||||
public Key(ZLTextModel model, int index) {
|
CursorManager(ZLTextModel model, ExtensionElementManager extManager) {
|
||||||
myModel = model;
|
myModel = model;
|
||||||
myIndex = index;
|
ExtensionManager = extManager;
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
Key k = (Key)o;
|
|
||||||
return (myModel == k.myModel) && (myIndex == k.myIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int hashCode() {
|
|
||||||
return myModel.hashCode() + myIndex;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final HashMap<Key,WeakReference<ZLTextParagraphCursor>> myMap = new HashMap<Key,WeakReference<ZLTextParagraphCursor>>();
|
private final HashMap<Integer,WeakReference<ZLTextParagraphCursor>> myMap =
|
||||||
|
new HashMap<Integer,WeakReference<ZLTextParagraphCursor>>();
|
||||||
|
|
||||||
void put(ZLTextModel model, int index, ZLTextParagraphCursor cursor) {
|
ZLTextParagraphCursor cursor(int index) {
|
||||||
myMap.put(new Key(model, index), new WeakReference<ZLTextParagraphCursor>(cursor));
|
final WeakReference<ZLTextParagraphCursor> ref = myMap.get(index);
|
||||||
}
|
ZLTextParagraphCursor result = ref != null ? ref.get() : null;
|
||||||
|
if (result == null) {
|
||||||
ZLTextParagraphCursor get(ZLTextModel model, int index) {
|
result = new ZLTextParagraphCursor(this, myModel, index);
|
||||||
WeakReference<ZLTextParagraphCursor> ref = myMap.get(new Key(model, index));
|
myMap.put(index, new WeakReference<ZLTextParagraphCursor>(result));
|
||||||
return ref != null ? ref.get() : null;
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
|
@ -29,8 +29,8 @@ import org.geometerplus.zlibrary.text.model.*;
|
||||||
|
|
||||||
public final class ZLTextParagraphCursor {
|
public final class ZLTextParagraphCursor {
|
||||||
private static final class Processor {
|
private static final class Processor {
|
||||||
private final ZLTextView myView;
|
|
||||||
private final ZLTextParagraph myParagraph;
|
private final ZLTextParagraph myParagraph;
|
||||||
|
private final ExtensionElementManager myExtManager;
|
||||||
private final LineBreaker myLineBreaker;
|
private final LineBreaker myLineBreaker;
|
||||||
private final ArrayList<ZLTextElement> myElements;
|
private final ArrayList<ZLTextElement> myElements;
|
||||||
private int myOffset;
|
private int myOffset;
|
||||||
|
@ -38,8 +38,8 @@ public final class ZLTextParagraphCursor {
|
||||||
private int myLastMark;
|
private int myLastMark;
|
||||||
private final List<ZLTextMark> myMarks;
|
private final List<ZLTextMark> myMarks;
|
||||||
|
|
||||||
private Processor(ZLTextView view, ZLTextParagraph paragraph, LineBreaker lineBreaker, List<ZLTextMark> marks, int paragraphIndex, ArrayList<ZLTextElement> elements) {
|
private Processor(ZLTextParagraph paragraph, ExtensionElementManager extManager, LineBreaker lineBreaker, List<ZLTextMark> marks, int paragraphIndex, ArrayList<ZLTextElement> elements) {
|
||||||
myView = view;
|
myExtManager = extManager;
|
||||||
myParagraph = paragraph;
|
myParagraph = paragraph;
|
||||||
myLineBreaker = lineBreaker;
|
myLineBreaker = lineBreaker;
|
||||||
myElements = elements;
|
myElements = elements;
|
||||||
|
@ -109,7 +109,9 @@ public final class ZLTextParagraphCursor {
|
||||||
elements.add(new ZLTextVideoElement(it.getVideoEntry().sources()));
|
elements.add(new ZLTextVideoElement(it.getVideoEntry().sources()));
|
||||||
break;
|
break;
|
||||||
case ZLTextParagraph.Entry.EXTENSION:
|
case ZLTextParagraph.Entry.EXTENSION:
|
||||||
elements.addAll(myView.getExtensionManager().getElements(it.getExtensionEntry()));
|
if (myExtManager != null) {
|
||||||
|
elements.addAll(myExtManager.getElements(it.getExtensionEntry()));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ZLTextParagraph.Entry.STYLE_CSS:
|
case ZLTextParagraph.Entry.STYLE_CSS:
|
||||||
case ZLTextParagraph.Entry.STYLE_OTHER:
|
case ZLTextParagraph.Entry.STYLE_OTHER:
|
||||||
|
@ -204,12 +206,12 @@ public final class ZLTextParagraphCursor {
|
||||||
}
|
}
|
||||||
|
|
||||||
public final int Index;
|
public final int Index;
|
||||||
final ZLTextView View;
|
final CursorManager CursorManager;
|
||||||
public final ZLTextModel Model;
|
public final ZLTextModel Model;
|
||||||
private final ArrayList<ZLTextElement> myElements = new ArrayList<ZLTextElement>();
|
private final ArrayList<ZLTextElement> myElements = new ArrayList<ZLTextElement>();
|
||||||
|
|
||||||
ZLTextParagraphCursor(ZLTextView view, ZLTextModel model, int index) {
|
ZLTextParagraphCursor(CursorManager cManager, ZLTextModel model, int index) {
|
||||||
View = view;
|
CursorManager = cManager;
|
||||||
Model = model;
|
Model = model;
|
||||||
Index = Math.min(index, model.getParagraphsNumber() - 1);
|
Index = Math.min(index, model.getParagraphsNumber() - 1);
|
||||||
fill();
|
fill();
|
||||||
|
@ -220,7 +222,7 @@ public final class ZLTextParagraphCursor {
|
||||||
ZLTextParagraph paragraph = Model.getParagraph(Index);
|
ZLTextParagraph paragraph = Model.getParagraph(Index);
|
||||||
switch (paragraph.getKind()) {
|
switch (paragraph.getKind()) {
|
||||||
case ZLTextParagraph.Kind.TEXT_PARAGRAPH:
|
case ZLTextParagraph.Kind.TEXT_PARAGRAPH:
|
||||||
new Processor(View, paragraph, new LineBreaker(Model.getLanguage()), Model.getMarks(), Index, myElements).fill();
|
new Processor(paragraph, CursorManager.ExtensionManager, new LineBreaker(Model.getLanguage()), Model.getMarks(), Index, myElements).fill();
|
||||||
break;
|
break;
|
||||||
case ZLTextParagraph.Kind.EMPTY_LINE_PARAGRAPH:
|
case ZLTextParagraph.Kind.EMPTY_LINE_PARAGRAPH:
|
||||||
myElements.add(new ZLTextWord(SPACE_ARRAY, 0, 1, 0));
|
myElements.add(new ZLTextWord(SPACE_ARRAY, 0, 1, 0));
|
||||||
|
@ -259,11 +261,11 @@ public final class ZLTextParagraphCursor {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ZLTextParagraphCursor previous() {
|
public ZLTextParagraphCursor previous() {
|
||||||
return isFirst() ? null : View.cursor(Index - 1);
|
return isFirst() ? null : CursorManager.cursor(Index - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ZLTextParagraphCursor next() {
|
public ZLTextParagraphCursor next() {
|
||||||
return isLast() ? null : View.cursor(Index + 1);
|
return isLast() ? null : CursorManager.cursor(Index + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ZLTextElement getElement(int index) {
|
ZLTextElement getElement(int index) {
|
||||||
|
|
|
@ -64,14 +64,14 @@ public abstract class ZLTextView extends ZLTextViewBase {
|
||||||
private final Set<ZLTextHighlighting> myHighlightings =
|
private final Set<ZLTextHighlighting> myHighlightings =
|
||||||
Collections.synchronizedSet(new TreeSet<ZLTextHighlighting>());
|
Collections.synchronizedSet(new TreeSet<ZLTextHighlighting>());
|
||||||
|
|
||||||
private final ZLTextParagraphCursorCache myCursorCache = new ZLTextParagraphCursorCache();
|
private CursorManager myCursorManager;
|
||||||
|
|
||||||
public ZLTextView(ZLApplication application) {
|
public ZLTextView(ZLApplication application) {
|
||||||
super(application);
|
super(application);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void setModel(ZLTextModel model) {
|
public synchronized void setModel(ZLTextModel model) {
|
||||||
myCursorCache.clear();
|
myCursorManager = model != null ? new CursorManager(model, getExtensionManager()) : null;
|
||||||
|
|
||||||
mySelection.clear();
|
mySelection.clear();
|
||||||
myHighlightings.clear();
|
myHighlightings.clear();
|
||||||
|
@ -83,7 +83,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
|
||||||
if (myModel != null) {
|
if (myModel != null) {
|
||||||
final int paragraphsNumber = myModel.getParagraphsNumber();
|
final int paragraphsNumber = myModel.getParagraphsNumber();
|
||||||
if (paragraphsNumber > 0) {
|
if (paragraphsNumber > 0) {
|
||||||
myCurrentPage.moveStartCursor(cursor(0));
|
myCurrentPage.moveStartCursor(myCursorManager.cursor(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Application.getViewWidget().reset();
|
Application.getViewWidget().reset();
|
||||||
|
@ -1587,7 +1587,9 @@ public abstract class ZLTextView extends ZLTextViewBase {
|
||||||
protected synchronized void rebuildPaintInfo() {
|
protected synchronized void rebuildPaintInfo() {
|
||||||
myPreviousPage.reset();
|
myPreviousPage.reset();
|
||||||
myNextPage.reset();
|
myNextPage.reset();
|
||||||
myCursorCache.clear();
|
if (myCursorManager != null) {
|
||||||
|
myCursorManager.clear();
|
||||||
|
}
|
||||||
|
|
||||||
if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) {
|
if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) {
|
||||||
myCurrentPage.LineInfos.clear();
|
myCurrentPage.LineInfos.clear();
|
||||||
|
@ -1850,12 +1852,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
ZLTextParagraphCursor cursor(int index) {
|
ZLTextParagraphCursor cursor(int index) {
|
||||||
ZLTextParagraphCursor result = myCursorCache.get(myModel, index);
|
return myCursorManager.cursor(index);
|
||||||
if (result == null) {
|
|
||||||
result = new ZLTextParagraphCursor(this, myModel, index);
|
|
||||||
myCursorCache.put(myModel, index, result);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract ExtensionElementManager getExtensionManager();
|
protected abstract ExtensionElementManager getExtensionManager();
|
||||||
|
|
|
@ -163,7 +163,7 @@ public final class ZLTextWordCursor extends ZLTextPosition {
|
||||||
if (!isNull() && (paragraphIndex != myParagraphCursor.Index)) {
|
if (!isNull() && (paragraphIndex != myParagraphCursor.Index)) {
|
||||||
final ZLTextModel model = myParagraphCursor.Model;
|
final ZLTextModel model = myParagraphCursor.Model;
|
||||||
paragraphIndex = Math.max(0, Math.min(paragraphIndex, model.getParagraphsNumber() - 1));
|
paragraphIndex = Math.max(0, Math.min(paragraphIndex, model.getParagraphsNumber() - 1));
|
||||||
myParagraphCursor = myParagraphCursor.View.cursor(paragraphIndex);
|
myParagraphCursor = myParagraphCursor.CursorManager.cursor(paragraphIndex);
|
||||||
moveToParagraphStart();
|
moveToParagraphStart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue