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

Each LibraryTree instance contains an IBookCollection link

This commit is contained in:
Nikolay Pultsin 2013-02-06 00:58:46 +00:00
parent 6c3b9028a1
commit d6a2a3d9c2
15 changed files with 91 additions and 61 deletions

View file

@ -0,0 +1,37 @@
/*
* 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.android.fbreader.libraryService;
import org.geometerplus.fbreader.book.Author;
abstract class Util {
static String authorToString(Author author) {
return new StringBuilder(author.DisplayName).append('\000').append(author.SortKey).toString();
}
static Author stringToAuthor(String string) {
final String[] splitted = string.split("\000");
if (splitted.length == 2) {
return new Author(splitted[0], splitted[1]);
} else {
return Author.NULL;
}
}
}

View file

@ -25,11 +25,8 @@ import java.util.List;
import org.geometerplus.fbreader.book.*;
public class AuthorListTree extends FirstLevelTree {
private final IBookCollection myCollection;
AuthorListTree(IBookCollection collection, RootTree root) {
AuthorListTree(RootTree root) {
super(root, Library.ROOT_BY_AUTHOR);
myCollection = collection;
}
@Override
@ -40,7 +37,7 @@ public class AuthorListTree extends FirstLevelTree {
@Override
public void waitForOpening() {
clear();
for (Author a : myCollection.authors()) {
for (Author a : Collection.authors()) {
createAuthorSubTree(a);
}
}
@ -55,7 +52,7 @@ public class AuthorListTree extends FirstLevelTree {
boolean changed = false;
if (bookAuthors.isEmpty()) {
changed &= createAuthorSubTree(Author.NULL);
} else for (Author a : myCollection.authors()) {
} else for (Author a : Collection.authors()) {
changed &= createAuthorSubTree(a);
}
return changed;
@ -70,12 +67,12 @@ public class AuthorListTree extends FirstLevelTree {
}
private boolean createAuthorSubTree(Author author) {
final AuthorTree temp = new AuthorTree(myCollection, author);
final AuthorTree temp = new AuthorTree(Collection, author);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return false;
} else {
new AuthorTree(myCollection, this, author, - position - 1);
new AuthorTree(this, author, - position - 1);
return true;
}
}

View file

@ -22,18 +22,15 @@ package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.*;
public class AuthorTree extends LibraryTree {
private final IBookCollection myCollection;
public final Author Author;
AuthorTree(IBookCollection collection, Author author) {
myCollection = collection;
super(collection);
Author = author;
}
AuthorTree(IBookCollection collection, AuthorListTree parent, Author author, int position) {
AuthorTree(AuthorListTree parent, Author author, int position) {
super(parent, position);
myCollection = collection;
Author = author;
}
@ -76,7 +73,7 @@ public class AuthorTree extends LibraryTree {
@Override
public void waitForOpening() {
clear();
for (Book book : myCollection.books(Author)) {
for (Book book : Collection.books(Author)) {
final SeriesInfo seriesInfo = book.getSeriesInfo();
if (seriesInfo == null) {
getBookSubTree(book, false);

View file

@ -22,11 +22,12 @@ package org.geometerplus.fbreader.library;
import java.math.BigDecimal;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.book.IBookCollection;
import org.geometerplus.fbreader.tree.FBTree;
public final class BookInSeriesTree extends BookTree {
BookInSeriesTree(Book book) {
super(book, false);
BookInSeriesTree(IBookCollection collection, Book book) {
super(collection, book, false);
}
BookInSeriesTree(LibraryTree parent, Book book, int position) {

View file

@ -28,7 +28,8 @@ public class BookTree extends LibraryTree {
public final Book Book;
private final boolean myShowAuthors;
BookTree(Book book, boolean showAuthors) {
BookTree(IBookCollection collection, Book book, boolean showAuthors) {
super(collection);
Book = book;
myShowAuthors = showAuthors;
}

View file

@ -22,16 +22,13 @@ package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.*;
public class FavoritesTree extends FirstLevelTree {
private final IBookCollection myCollection;
FavoritesTree(IBookCollection collection, RootTree root) {
FavoritesTree(RootTree root) {
super(root, Library.ROOT_FAVORITES);
myCollection = collection;
}
@Override
public Status getOpeningStatus() {
if (!myCollection.hasFavorites()) {
if (!Collection.hasFavorites()) {
return Status.CANNOT_OPEN;
}
return Status.ALWAYS_RELOAD_BEFORE_OPENING;
@ -46,16 +43,16 @@ public class FavoritesTree extends FirstLevelTree {
@Override
public void waitForOpening() {
clear();
for (Book book : myCollection.favorites()) {
for (Book book : Collection.favorites()) {
new BookTree(this, book, true);
}
}
public boolean onBookEvent(BookEvent event, Book book) {
if (event == BookEvent.Added && myCollection.isFavorite(book)) {
if (event == BookEvent.Added && Collection.isFavorite(book)) {
new BookTree(this, book, true);
return true;
} if (event == BookEvent.Updated && !myCollection.isFavorite(book)) {
} if (event == BookEvent.Updated && !Collection.isFavorite(book)) {
return removeBook(book, false);
} else {
return super.onBookEvent(event, book);

View file

@ -23,14 +23,10 @@ import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.Paths;
import org.geometerplus.fbreader.book.IBookCollection;
public class FileFirstLevelTree extends FirstLevelTree {
private final IBookCollection myCollection;
FileFirstLevelTree(IBookCollection collection, RootTree root) {
FileFirstLevelTree(RootTree root) {
super(root, Library.ROOT_FILE_TREE);
myCollection = collection;
addChild(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary");
addChild("/", "fileTreeRoot");
addChild(Paths.cardDirectory(), "fileTreeCard");
@ -42,7 +38,6 @@ public class FileFirstLevelTree extends FirstLevelTree {
final ZLResource resource = Library.resource().getResource(resourceKey);
new FileTree(
this,
myCollection,
file,
resource.getValue(),
resource.getResource("summary").getValue()

View file

@ -29,15 +29,13 @@ import org.geometerplus.fbreader.formats.PluginCollection;
import org.geometerplus.fbreader.tree.FBTree;
public class FileTree extends LibraryTree {
private final IBookCollection myCollection;
private final ZLFile myFile;
private final String myName;
private final String mySummary;
private final boolean myIsSelectable;
FileTree(LibraryTree parent, IBookCollection collection, ZLFile file, String name, String summary) {
FileTree(LibraryTree parent, ZLFile file, String name, String summary) {
super(parent);
myCollection = collection;
myFile = file;
myName = name;
mySummary = summary;
@ -46,7 +44,6 @@ public class FileTree extends LibraryTree {
public FileTree(FileTree parent, ZLFile file) {
super(parent);
myCollection = parent.myCollection;
myFile = file;
myName = null;
mySummary = null;
@ -102,7 +99,7 @@ public class FileTree extends LibraryTree {
@Override
public Book getBook() {
if (myBook == null) {
myBook = myCollection.getBookByFile(myFile);
myBook = Collection.getBookByFile(myFile);
if (myBook == null) {
myBook = NULL_BOOK;
}

View file

@ -79,7 +79,7 @@ public final class Library {
public final IBookCollection Collection;
private final Map<Long,Book> myBooks = Collections.synchronizedMap(new HashMap<Long,Book>());
private final RootTree myRootTree = new RootTree();
private final RootTree myRootTree;
private boolean myDoGroupTitlesByFirstLetter;
private final static int STATUS_LOADING = 1;
@ -99,12 +99,14 @@ public final class Library {
public Library(IBookCollection collection) {
Collection = collection;
new FavoritesTree(collection, myRootTree);
new RecentBooksTree(collection, myRootTree);
new AuthorListTree(collection, myRootTree);
myRootTree = new RootTree(collection);
new FavoritesTree(myRootTree);
new RecentBooksTree(myRootTree);
new AuthorListTree(myRootTree);
new FirstLevelTree(myRootTree, ROOT_BY_TITLE);
new FirstLevelTree(myRootTree, ROOT_BY_TAG);
new FileFirstLevelTree(collection, myRootTree);
new FileFirstLevelTree(myRootTree);
}
public void init() {

View file

@ -25,16 +25,21 @@ import org.geometerplus.fbreader.book.*;
import org.geometerplus.fbreader.tree.FBTree;
public abstract class LibraryTree extends FBTree {
protected LibraryTree() {
public final IBookCollection Collection;
protected LibraryTree(IBookCollection collection) {
super();
Collection = collection;
}
protected LibraryTree(LibraryTree parent) {
super(parent);
Collection = parent.Collection;
}
protected LibraryTree(LibraryTree parent, int position) {
super(parent, position);
Collection = parent.Collection;
}
public Book getBook() {
@ -50,7 +55,7 @@ public abstract class LibraryTree extends FBTree {
}
TagTree getTagSubTree(Tag tag) {
final TagTree temp = new TagTree(tag);
final TagTree temp = new TagTree(Collection, tag);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (TagTree)subTrees().get(position);
@ -60,7 +65,7 @@ public abstract class LibraryTree extends FBTree {
}
TitleTree getTitleSubTree(String title) {
final TitleTree temp = new TitleTree(title);
final TitleTree temp = new TitleTree(Collection, title);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (TitleTree)subTrees().get(position);
@ -70,7 +75,7 @@ public abstract class LibraryTree extends FBTree {
}
BookTree getBookSubTree(Book book, boolean showAuthors) {
final BookTree temp = new BookTree(book, showAuthors);
final BookTree temp = new BookTree(Collection, book, showAuthors);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (BookTree)subTrees().get(position);
@ -80,7 +85,7 @@ public abstract class LibraryTree extends FBTree {
}
SeriesTree getSeriesSubTree(String series) {
final SeriesTree temp = new SeriesTree(series);
final SeriesTree temp = new SeriesTree(Collection, series);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (SeriesTree)subTrees().get(position);

View file

@ -20,14 +20,10 @@
package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.book.IBookCollection;
public class RecentBooksTree extends FirstLevelTree {
private final IBookCollection myCollection;
RecentBooksTree(IBookCollection collection, RootTree root) {
RecentBooksTree(RootTree root) {
super(root, Library.ROOT_RECENT);
myCollection = collection;
}
@Override
@ -38,7 +34,7 @@ public class RecentBooksTree extends FirstLevelTree {
@Override
public void waitForOpening() {
clear();
for (Book book : myCollection.recentBooks()) {
for (Book book : Collection.recentBooks()) {
new BookTree(this, book, true);
}
}

View file

@ -19,8 +19,11 @@
package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.IBookCollection;
class RootTree extends LibraryTree {
RootTree() {
RootTree(IBookCollection collection) {
super(collection);
}
@Override

View file

@ -21,13 +21,13 @@ package org.geometerplus.fbreader.library;
import java.util.Collections;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.book.SeriesInfo;
import org.geometerplus.fbreader.book.*;
public final class SeriesTree extends LibraryTree {
public final String Series;
SeriesTree(String series) {
SeriesTree(IBookCollection collection, String series) {
super(collection);
Series = series;
}
@ -47,7 +47,7 @@ public final class SeriesTree extends LibraryTree {
}
BookTree getBookInSeriesSubTree(Book book) {
final BookInSeriesTree temp = new BookInSeriesTree(book);
final BookInSeriesTree temp = new BookInSeriesTree(Collection, book);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (BookInSeriesTree)subTrees().get(position);

View file

@ -19,13 +19,13 @@
package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.book.Tag;
import org.geometerplus.fbreader.book.*;
public final class TagTree extends LibraryTree {
public final Tag Tag;
TagTree(Tag tag) {
TagTree(IBookCollection collection, Tag tag) {
super(collection);
Tag = tag;
}

View file

@ -19,6 +19,7 @@
package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.IBookCollection;
import org.geometerplus.fbreader.book.Book;
public final class TitleTree extends LibraryTree {
@ -45,7 +46,8 @@ public final class TitleTree extends LibraryTree {
public final String Title;
TitleTree(String title) {
TitleTree(IBookCollection collection, String title) {
super(collection);
Title = title;
}