diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursorCache.java b/src/org/geometerplus/zlibrary/text/view/CursorManager.java similarity index 53% rename from src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursorCache.java rename to src/org/geometerplus/zlibrary/text/view/CursorManager.java index c3f153d44..a399a1f29 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursorCache.java +++ b/src/org/geometerplus/zlibrary/text/view/CursorManager.java @@ -24,35 +24,26 @@ import java.util.*; import org.geometerplus.zlibrary.text.model.ZLTextModel; -class ZLTextParagraphCursorCache { - private final static class Key { - private final ZLTextModel myModel; - private final int myIndex; +final class CursorManager { + private final ZLTextModel myModel; + final ExtensionElementManager ExtensionManager; - public Key(ZLTextModel model, int index) { - myModel = model; - myIndex = index; - } - - public boolean equals(Object o) { - Key k = (Key)o; - return (myModel == k.myModel) && (myIndex == k.myIndex); - } - - public int hashCode() { - return myModel.hashCode() + myIndex; - } + CursorManager(ZLTextModel model, ExtensionElementManager extManager) { + myModel = model; + ExtensionManager = extManager; } - private final HashMap> myMap = new HashMap>(); + private final HashMap> myMap = + new HashMap>(); - void put(ZLTextModel model, int index, ZLTextParagraphCursor cursor) { - myMap.put(new Key(model, index), new WeakReference(cursor)); - } - - ZLTextParagraphCursor get(ZLTextModel model, int index) { - WeakReference ref = myMap.get(new Key(model, index)); - return ref != null ? ref.get() : null; + ZLTextParagraphCursor cursor(int index) { + final WeakReference ref = myMap.get(index); + ZLTextParagraphCursor result = ref != null ? ref.get() : null; + if (result == null) { + result = new ZLTextParagraphCursor(this, myModel, index); + myMap.put(index, new WeakReference(result)); + } + return result; } void clear() { diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java index edf8c1148..c7cb74400 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextParagraphCursor.java @@ -29,8 +29,8 @@ import org.geometerplus.zlibrary.text.model.*; public final class ZLTextParagraphCursor { private static final class Processor { - private final ZLTextView myView; private final ZLTextParagraph myParagraph; + private final ExtensionElementManager myExtManager; private final LineBreaker myLineBreaker; private final ArrayList myElements; private int myOffset; @@ -38,8 +38,8 @@ public final class ZLTextParagraphCursor { private int myLastMark; private final List myMarks; - private Processor(ZLTextView view, ZLTextParagraph paragraph, LineBreaker lineBreaker, List marks, int paragraphIndex, ArrayList elements) { - myView = view; + private Processor(ZLTextParagraph paragraph, ExtensionElementManager extManager, LineBreaker lineBreaker, List marks, int paragraphIndex, ArrayList elements) { + myExtManager = extManager; myParagraph = paragraph; myLineBreaker = lineBreaker; myElements = elements; @@ -109,7 +109,9 @@ public final class ZLTextParagraphCursor { elements.add(new ZLTextVideoElement(it.getVideoEntry().sources())); break; case ZLTextParagraph.Entry.EXTENSION: - elements.addAll(myView.getExtensionManager().getElements(it.getExtensionEntry())); + if (myExtManager != null) { + elements.addAll(myExtManager.getElements(it.getExtensionEntry())); + } break; case ZLTextParagraph.Entry.STYLE_CSS: case ZLTextParagraph.Entry.STYLE_OTHER: @@ -204,12 +206,12 @@ public final class ZLTextParagraphCursor { } public final int Index; - final ZLTextView View; + final CursorManager CursorManager; public final ZLTextModel Model; private final ArrayList myElements = new ArrayList(); - ZLTextParagraphCursor(ZLTextView view, ZLTextModel model, int index) { - View = view; + ZLTextParagraphCursor(CursorManager cManager, ZLTextModel model, int index) { + CursorManager = cManager; Model = model; Index = Math.min(index, model.getParagraphsNumber() - 1); fill(); @@ -220,7 +222,7 @@ public final class ZLTextParagraphCursor { ZLTextParagraph paragraph = Model.getParagraph(Index); switch (paragraph.getKind()) { 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; case ZLTextParagraph.Kind.EMPTY_LINE_PARAGRAPH: myElements.add(new ZLTextWord(SPACE_ARRAY, 0, 1, 0)); @@ -259,11 +261,11 @@ public final class ZLTextParagraphCursor { } public ZLTextParagraphCursor previous() { - return isFirst() ? null : View.cursor(Index - 1); + return isFirst() ? null : CursorManager.cursor(Index - 1); } public ZLTextParagraphCursor next() { - return isLast() ? null : View.cursor(Index + 1); + return isLast() ? null : CursorManager.cursor(Index + 1); } ZLTextElement getElement(int index) { diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java index 1c5c116dc..84cfe77d4 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java @@ -64,14 +64,14 @@ public abstract class ZLTextView extends ZLTextViewBase { private final Set myHighlightings = Collections.synchronizedSet(new TreeSet()); - private final ZLTextParagraphCursorCache myCursorCache = new ZLTextParagraphCursorCache(); + private CursorManager myCursorManager; public ZLTextView(ZLApplication application) { super(application); } public synchronized void setModel(ZLTextModel model) { - myCursorCache.clear(); + myCursorManager = model != null ? new CursorManager(model, getExtensionManager()) : null; mySelection.clear(); myHighlightings.clear(); @@ -83,7 +83,7 @@ public abstract class ZLTextView extends ZLTextViewBase { if (myModel != null) { final int paragraphsNumber = myModel.getParagraphsNumber(); if (paragraphsNumber > 0) { - myCurrentPage.moveStartCursor(cursor(0)); + myCurrentPage.moveStartCursor(myCursorManager.cursor(0)); } } Application.getViewWidget().reset(); @@ -1587,7 +1587,9 @@ public abstract class ZLTextView extends ZLTextViewBase { protected synchronized void rebuildPaintInfo() { myPreviousPage.reset(); myNextPage.reset(); - myCursorCache.clear(); + if (myCursorManager != null) { + myCursorManager.clear(); + } if (myCurrentPage.PaintState != PaintStateEnum.NOTHING_TO_PAINT) { myCurrentPage.LineInfos.clear(); @@ -1850,12 +1852,7 @@ public abstract class ZLTextView extends ZLTextViewBase { } ZLTextParagraphCursor cursor(int index) { - ZLTextParagraphCursor result = myCursorCache.get(myModel, index); - if (result == null) { - result = new ZLTextParagraphCursor(this, myModel, index); - myCursorCache.put(myModel, index, result); - } - return result; + return myCursorManager.cursor(index); } protected abstract ExtensionElementManager getExtensionManager(); diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java b/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java index f71afb45f..0b64987ac 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextWordCursor.java @@ -163,7 +163,7 @@ public final class ZLTextWordCursor extends ZLTextPosition { if (!isNull() && (paragraphIndex != myParagraphCursor.Index)) { final ZLTextModel model = myParagraphCursor.Model; paragraphIndex = Math.max(0, Math.min(paragraphIndex, model.getParagraphsNumber() - 1)); - myParagraphCursor = myParagraphCursor.View.cursor(paragraphIndex); + myParagraphCursor = myParagraphCursor.CursorManager.cursor(paragraphIndex); moveToParagraphStart(); } }