From d6a2a3d9c29f954ddbe812855ea1146d9c5ae2f9 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Wed, 6 Feb 2013 00:58:46 +0000 Subject: [PATCH] Each LibraryTree instance contains an IBookCollection link --- .../android/fbreader/libraryService/Util.java | 37 +++++++++++++++++++ .../fbreader/library/AuthorListTree.java | 13 +++---- .../fbreader/library/AuthorTree.java | 9 ++--- .../fbreader/library/BookInSeriesTree.java | 5 ++- .../fbreader/library/BookTree.java | 3 +- .../fbreader/library/FavoritesTree.java | 13 +++---- .../fbreader/library/FileFirstLevelTree.java | 7 +--- .../fbreader/library/FileTree.java | 7 +--- .../fbreader/library/Library.java | 12 +++--- .../fbreader/library/LibraryTree.java | 15 +++++--- .../fbreader/library/RecentBooksTree.java | 8 +--- .../fbreader/library/RootTree.java | 5 ++- .../fbreader/library/SeriesTree.java | 8 ++-- .../fbreader/library/TagTree.java | 6 +-- .../fbreader/library/TitleTree.java | 4 +- 15 files changed, 91 insertions(+), 61 deletions(-) create mode 100644 src/org/geometerplus/android/fbreader/libraryService/Util.java diff --git a/src/org/geometerplus/android/fbreader/libraryService/Util.java b/src/org/geometerplus/android/fbreader/libraryService/Util.java new file mode 100644 index 000000000..5c9e828f7 --- /dev/null +++ b/src/org/geometerplus/android/fbreader/libraryService/Util.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2007-2013 Geometer Plus + * + * 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; + } + } +} diff --git a/src/org/geometerplus/fbreader/library/AuthorListTree.java b/src/org/geometerplus/fbreader/library/AuthorListTree.java index ff04df75a..8a375cac4 100644 --- a/src/org/geometerplus/fbreader/library/AuthorListTree.java +++ b/src/org/geometerplus/fbreader/library/AuthorListTree.java @@ -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; } } diff --git a/src/org/geometerplus/fbreader/library/AuthorTree.java b/src/org/geometerplus/fbreader/library/AuthorTree.java index d33e1d3f5..ac14687f8 100644 --- a/src/org/geometerplus/fbreader/library/AuthorTree.java +++ b/src/org/geometerplus/fbreader/library/AuthorTree.java @@ -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); diff --git a/src/org/geometerplus/fbreader/library/BookInSeriesTree.java b/src/org/geometerplus/fbreader/library/BookInSeriesTree.java index 4d15af03e..1b613bc8c 100644 --- a/src/org/geometerplus/fbreader/library/BookInSeriesTree.java +++ b/src/org/geometerplus/fbreader/library/BookInSeriesTree.java @@ -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) { diff --git a/src/org/geometerplus/fbreader/library/BookTree.java b/src/org/geometerplus/fbreader/library/BookTree.java index 6fb2acfa3..e03083771 100644 --- a/src/org/geometerplus/fbreader/library/BookTree.java +++ b/src/org/geometerplus/fbreader/library/BookTree.java @@ -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; } diff --git a/src/org/geometerplus/fbreader/library/FavoritesTree.java b/src/org/geometerplus/fbreader/library/FavoritesTree.java index 7aa8503f8..445ca71d4 100644 --- a/src/org/geometerplus/fbreader/library/FavoritesTree.java +++ b/src/org/geometerplus/fbreader/library/FavoritesTree.java @@ -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); diff --git a/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java b/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java index 36235e560..78e758279 100644 --- a/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java +++ b/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java @@ -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() diff --git a/src/org/geometerplus/fbreader/library/FileTree.java b/src/org/geometerplus/fbreader/library/FileTree.java index db709324e..3dac39e28 100644 --- a/src/org/geometerplus/fbreader/library/FileTree.java +++ b/src/org/geometerplus/fbreader/library/FileTree.java @@ -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; } diff --git a/src/org/geometerplus/fbreader/library/Library.java b/src/org/geometerplus/fbreader/library/Library.java index d21e542f3..bc3fdd0d5 100644 --- a/src/org/geometerplus/fbreader/library/Library.java +++ b/src/org/geometerplus/fbreader/library/Library.java @@ -79,7 +79,7 @@ public final class Library { public final IBookCollection Collection; private final Map myBooks = Collections.synchronizedMap(new HashMap()); - 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() { diff --git a/src/org/geometerplus/fbreader/library/LibraryTree.java b/src/org/geometerplus/fbreader/library/LibraryTree.java index 95f2f06b3..d358606d6 100644 --- a/src/org/geometerplus/fbreader/library/LibraryTree.java +++ b/src/org/geometerplus/fbreader/library/LibraryTree.java @@ -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); diff --git a/src/org/geometerplus/fbreader/library/RecentBooksTree.java b/src/org/geometerplus/fbreader/library/RecentBooksTree.java index e339fdc14..65c507eac 100644 --- a/src/org/geometerplus/fbreader/library/RecentBooksTree.java +++ b/src/org/geometerplus/fbreader/library/RecentBooksTree.java @@ -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); } } diff --git a/src/org/geometerplus/fbreader/library/RootTree.java b/src/org/geometerplus/fbreader/library/RootTree.java index e4b0164e6..a5387035d 100644 --- a/src/org/geometerplus/fbreader/library/RootTree.java +++ b/src/org/geometerplus/fbreader/library/RootTree.java @@ -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 diff --git a/src/org/geometerplus/fbreader/library/SeriesTree.java b/src/org/geometerplus/fbreader/library/SeriesTree.java index 1d8c578ea..b2cae1a49 100644 --- a/src/org/geometerplus/fbreader/library/SeriesTree.java +++ b/src/org/geometerplus/fbreader/library/SeriesTree.java @@ -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); diff --git a/src/org/geometerplus/fbreader/library/TagTree.java b/src/org/geometerplus/fbreader/library/TagTree.java index 1b1748551..792ec39e4 100644 --- a/src/org/geometerplus/fbreader/library/TagTree.java +++ b/src/org/geometerplus/fbreader/library/TagTree.java @@ -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; } diff --git a/src/org/geometerplus/fbreader/library/TitleTree.java b/src/org/geometerplus/fbreader/library/TitleTree.java index e82fd2805..be9519b11 100644 --- a/src/org/geometerplus/fbreader/library/TitleTree.java +++ b/src/org/geometerplus/fbreader/library/TitleTree.java @@ -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; }