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

watch bookmark changes

This commit is contained in:
Nikolay Pultsin 2013-04-30 23:31:11 +02:00
parent c222d3ded1
commit 4fe48e6455
3 changed files with 78 additions and 27 deletions

View file

@ -0,0 +1,46 @@
/*
* Copyright (C) 2007-2013 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.fbreader;
import org.geometerplus.zlibrary.core.util.ZLColor;
import org.geometerplus.zlibrary.text.view.*;
import org.geometerplus.fbreader.book.Bookmark;
public final class BookmarkHighlighting extends ZLTextSimpleHighlighting {
private static ZLTextPosition endPosition(Bookmark bookmark) {
final ZLTextPosition end = bookmark.getEnd();
if (end != null) {
return end;
}
// TODO: compute end and save bookmark
return bookmark;
}
BookmarkHighlighting(ZLTextView view, Bookmark bookmark) {
super(view, bookmark, endPosition(bookmark));
}
@Override
public ZLColor getBackgroundColor() {
return new ZLColor(127, 127, 127);
}
}

View file

@ -128,6 +128,18 @@ public final class FBReaderApp extends ZLApplication {
public FBReaderApp(IBookCollection collection) {
Collection = collection;
collection.addListener(new IBookCollection.Listener() {
public void onBookEvent(BookEvent event, Book book) {
if (event == BookEvent.BookmarksUpdated &&
Model != null && book.equals(Model.Book)) {
setBookmarkHighlightings();
}
}
public void onBuildEvent(IBookCollection.Status status) {
}
});
addAction(ActionCode.INCREASE_FONT, new ChangeFontSizeAction(this, +2));
addAction(ActionCode.DECREASE_FONT, new ChangeFontSizeAction(this, -2));
@ -238,6 +250,21 @@ public final class FBReaderApp extends ZLApplication {
FootnoteView.clearCaches();
}
private void setBookmarkHighlightings() {
BookTextView.removeHighlightings(BookmarkHighlighting.class);
for (BookmarkQuery query = new BookmarkQuery(Model.Book, 20); ; query = query.next()) {
final List<Bookmark> bookmarks = Collection.bookmarks(query);
if (bookmarks.isEmpty()) {
break;
}
for (Bookmark b : bookmarks) {
if (b.ModelId == null) {
BookTextView.addHighlighting(new BookmarkHighlighting(BookTextView, b));
}
}
}
}
synchronized void openBookInternal(Book book, Bookmark bookmark, boolean force) {
if (book == null) {
book = Collection.getRecentBook(0);
@ -272,29 +299,7 @@ public final class FBReaderApp extends ZLApplication {
Collection.saveBook(book, false);
ZLTextHyphenator.Instance().load(book.getLanguage());
BookTextView.setModel(Model.getTextModel());
for (BookmarkQuery query = new BookmarkQuery(book, 20); ; query = query.next()) {
final List<Bookmark> bookmarks = Collection.bookmarks(query);
if (bookmarks.isEmpty()) {
break;
}
for (Bookmark b : bookmarks) {
if (b.ModelId == null) {
ZLTextPosition end = b.getEnd();
if (end == null) {
// TODO: compute end and save bookmark
continue;
}
BookTextView.addHighlighting(
new ZLTextSimpleHighlighting(BookTextView, b, end) {
@Override
public ZLColor getBackgroundColor() {
return new ZLColor(127, 127, 127);
}
}
);
}
}
}
setBookmarkHighlightings();
BookTextView.gotoPosition(Collection.getStoredPosition(book.getId()));
if (bookmark == null) {
setView(BookTextView);

View file

@ -258,11 +258,11 @@ public abstract class ZLTextView extends ZLTextViewBase {
}
}
private boolean removeManualHighlightings() {
public boolean removeHighlightings(Class<? extends ZLTextHighlighting> type) {
boolean result = false;
for (Iterator<ZLTextHighlighting> it = myHighlightingsByStart.iterator(); it.hasNext(); ) {
final ZLTextHighlighting h = it.next();
if (h instanceof ZLTextManualHighlighting) {
if (type.isInstance(h)) {
it.remove();
myHighlightingsByEnd.remove(h);
result = true;
@ -272,7 +272,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
}
public void highlight(ZLTextPosition start, ZLTextPosition end) {
removeManualHighlightings();
removeHighlightings(ZLTextManualHighlighting.class);
addHighlighting(new ZLTextManualHighlighting(this, start, end));
}
@ -291,7 +291,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
}
public void clearHighlighting() {
if (removeManualHighlightings()) {
if (removeHighlightings(ZLTextManualHighlighting.class)) {
Application.getViewWidget().reset();
Application.getViewWidget().repaint();
}