1
0
Fork 0
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:
Nikolay Pultsin 2015-04-21 19:43:13 +01:00
parent a0fc30a1b4
commit 67217c4264
4 changed files with 36 additions and 46 deletions

View file

@ -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<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) {
myMap.put(new Key(model, index), new WeakReference<ZLTextParagraphCursor>(cursor));
}
ZLTextParagraphCursor get(ZLTextModel model, int index) {
WeakReference<ZLTextParagraphCursor> ref = myMap.get(new Key(model, index));
return ref != null ? ref.get() : null;
ZLTextParagraphCursor cursor(int index) {
final WeakReference<ZLTextParagraphCursor> 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<ZLTextParagraphCursor>(result));
}
return result;
}
void clear() {

View file

@ -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<ZLTextElement> myElements;
private int myOffset;
@ -38,8 +38,8 @@ public final class ZLTextParagraphCursor {
private int myLastMark;
private final List<ZLTextMark> myMarks;
private Processor(ZLTextView view, ZLTextParagraph paragraph, LineBreaker lineBreaker, List<ZLTextMark> marks, int paragraphIndex, ArrayList<ZLTextElement> elements) {
myView = view;
private Processor(ZLTextParagraph paragraph, ExtensionElementManager extManager, LineBreaker lineBreaker, List<ZLTextMark> marks, int paragraphIndex, ArrayList<ZLTextElement> 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<ZLTextElement> myElements = new ArrayList<ZLTextElement>();
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) {

View file

@ -64,14 +64,14 @@ public abstract class ZLTextView extends ZLTextViewBase {
private final Set<ZLTextHighlighting> myHighlightings =
Collections.synchronizedSet(new TreeSet<ZLTextHighlighting>());
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();

View file

@ -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();
}
}