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

library code uniformization

This commit is contained in:
Nikolay Pultsin 2011-07-14 13:02:15 +01:00
parent 3433318dc8
commit 8ed1bcaf30
8 changed files with 131 additions and 68 deletions

View file

@ -175,7 +175,7 @@ abstract class BaseActivity extends ListActivity implements View.OnCreateContext
if (info != null && info.Action != null) {
info.Action.run();
} else {
new OpenTreeRunnable(tree).run();
new OpenTreeRunnable(tree, LibraryTreeActivity.class).run();
}
}
}
@ -333,21 +333,21 @@ abstract class BaseActivity extends ListActivity implements View.OnCreateContext
}
protected class OpenTreeRunnable implements Runnable {
private final FBTree myTree;
private final LibraryTree myTree;
private final Class<?> myActivityClass;
public OpenTreeRunnable(FBTree tree) {
myTree = tree;
public OpenTreeRunnable(FBTree tree, Class<?> activityClass) {
myTree = (LibraryTree)tree;
myActivityClass = activityClass;
}
public void run() {
if (LibraryInstance.hasState(Library.STATE_FULLY_INITIALIZED)) {
openTree();
} else {
if (myTree.getOpeningStatus() == LibraryTree.Status.WAIT_FOR_OPEN) {
UIUtil.runWithMessage(
BaseActivity.this, "loadingBookList",
BaseActivity.this, myTree.getOpeningStatusMessage(),
new Runnable() {
public void run() {
LibraryInstance.waitForState(Library.STATE_FULLY_INITIALIZED);
myTree.waitForOpening();
}
},
new Runnable() {
@ -356,16 +356,25 @@ abstract class BaseActivity extends ListActivity implements View.OnCreateContext
}
}
);
} else {
openTree();
}
}
protected void openTree() {
switch (myTree.getOpeningStatus()) {
case READY_TO_OPEN:
startActivityForResult(
new Intent(BaseActivity.this, LibraryTreeActivity.class)
new Intent(BaseActivity.this, myActivityClass)
.putExtra(SELECTED_BOOK_PATH_KEY, mySelectedBookPath)
.putExtra(TREE_KEY_KEY, myTree.getUniqueKey()),
CHILD_LIST_REQUEST
);
break;
case CANNOT_OPEN:
UIUtil.showErrorMessage(BaseActivity.this, myTree.getOpeningStatusMessage());
break;
}
}
}
}

View file

@ -42,46 +42,19 @@ public class LibraryTopLevelActivity extends LibraryBaseActivity {
final ListAdapter adapter = new ListAdapter(this, new LinkedList<FBTree>());
final RootTree rootFavorites = LibraryInstance.getRootTree(Library.ROOT_FAVORITES);
addFBTreeWithInfo(
rootFavorites,
R.drawable.ic_list_library_favorites,
new OpenTreeRunnable(rootFavorites) {
@Override
protected void openTree() {
if (LibraryInstance.favorites().hasChildren()) {
super.openTree();
} else {
UIUtil.showErrorMessage(LibraryTopLevelActivity.this, "noFavorites");
}
}
}
);
addTopLevelTree(Library.ROOT_RECENT, R.drawable.ic_list_library_recent);
addTopLevelTree(Library.ROOT_BY_AUTHOR, R.drawable.ic_list_library_authors);
addTopLevelTree(Library.ROOT_BY_TITLE, R.drawable.ic_list_library_books);
addTopLevelTree(Library.ROOT_BY_TAG, R.drawable.ic_list_library_tags);
final RootTree fileTreeRoot = LibraryInstance.getRootTree(Library.ROOT_FILE_TREE);
addFBTreeWithInfo(
fileTreeRoot,
R.drawable.ic_list_library_folder,
new Runnable() {
public void run() {
startActivity(
new Intent(LibraryTopLevelActivity.this, FileManager.class)
.putExtra(TREE_KEY_KEY, fileTreeRoot.getUniqueKey())
.putExtra(SELECTED_BOOK_PATH_KEY, mySelectedBookPath)
);
}
}
);
addTopLevelTree(Library.ROOT_FAVORITES, R.drawable.ic_list_library_favorites, LibraryTreeActivity.class);
addTopLevelTree(Library.ROOT_RECENT, R.drawable.ic_list_library_recent, LibraryTreeActivity.class);
addTopLevelTree(Library.ROOT_BY_AUTHOR, R.drawable.ic_list_library_authors, LibraryTreeActivity.class);
addTopLevelTree(Library.ROOT_BY_TITLE, R.drawable.ic_list_library_books, LibraryTreeActivity.class);
addTopLevelTree(Library.ROOT_BY_TAG, R.drawable.ic_list_library_tags, LibraryTreeActivity.class);
addTopLevelTree(Library.ROOT_FILE_TREE, R.drawable.ic_list_library_folder, FileManager.class);
onNewIntent(getIntent());
}
private void addTopLevelTree(String key, int imageId) {
private void addTopLevelTree(String key, int imageId, Class<?> activityClass) {
final RootTree root = LibraryInstance.getRootTree(key);
addFBTreeWithInfo(root, imageId, new OpenTreeRunnable(root));
addFBTreeWithInfo(root, imageId, new OpenTreeRunnable(root, activityClass));
}
@Override
@ -97,7 +70,7 @@ public class LibraryTopLevelActivity extends LibraryBaseActivity {
adapter.add(0, mySearchResultsItem);
getListView().invalidateViews();
adapter.notifyDataSetChanged();
new OpenTreeRunnable(mySearchResultsItem).run();
new OpenTreeRunnable(mySearchResultsItem, LibraryTreeActivity.class).run();
}
public void onNewIntent(Intent intent) {

View file

@ -0,0 +1,43 @@
/*
* Copyright (C) 2009-2011 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.library;
import org.geometerplus.zlibrary.core.resources.ZLResource;
public class FavoritesTree extends RootTree {
FavoritesTree(Library library, String id) {
super(library, id);
}
@Override
public Status getOpeningStatus() {
final Status status = super.getOpeningStatus();
if (status == Status.READY_TO_OPEN && !hasChildren()) {
return Status.CANNOT_OPEN;
}
return status;
}
@Override
public String getOpeningStatusMessage() {
return getOpeningStatus() == Status.CANNOT_OPEN
? "noFavorites" : super.getOpeningStatusMessage();
}
}

View file

@ -25,8 +25,8 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.Paths;
public class FileRootTree extends RootTree {
FileRootTree(String id) {
super(id);
FileRootTree(Library library, String id) {
super(library, id);
addChild(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary");
addChild("/", "fileTreeRoot");
addChild(Paths.cardDirectory(), "fileTreeCard");
@ -46,4 +46,9 @@ public class FileRootTree extends RootTree {
public String getTreeTitle() {
return getName();
}
@Override
public Status getOpeningStatus() {
return Status.READY_TO_OPEN;
}
}

View file

@ -33,8 +33,8 @@ import org.geometerplus.fbreader.formats.PluginCollection;
import org.geometerplus.fbreader.Paths;
public final class Library {
public static final int STATE_NOT_INITIALIZED = 0;
public static final int STATE_FULLY_INITIALIZED = 1;
static final int STATE_NOT_INITIALIZED = 0;
static final int STATE_FULLY_INITIALIZED = 1;
public static final String ROOT_FAVORITES = "favorites";
public static final String ROOT_SEARCH_RESULTS = "searchResults";
@ -56,13 +56,14 @@ public final class Library {
private volatile boolean myInterrupted = false;
public Library() {
myRootTrees.put(ROOT_FILE_TREE, new FileRootTree(ROOT_FILE_TREE));
myRootTrees.put(ROOT_FAVORITES, new FavoritesTree(this, ROOT_FAVORITES));
myRootTrees.put(ROOT_FILE_TREE, new FileRootTree(this, ROOT_FILE_TREE));
}
public RootTree getRootTree(String id) {
RootTree root = myRootTrees.get(id);
if (root == null) {
root = new RootTree(id);
root = new RootTree(this, id);
myRootTrees.put(id, root);
}
return root;
@ -79,11 +80,11 @@ public final class Library {
return parentTree != null ? (LibraryTree)parentTree.getSubTree(key.Id) : null;
}
public boolean hasState(int state) {
boolean hasState(int state) {
return myState >= state || myInterrupted;
}
public void waitForState(int state) {
void waitForState(int state) {
while (myState < state && !myInterrupted) {
synchronized(this) {
if (myState < state && !myInterrupted) {
@ -400,18 +401,13 @@ public final class Library {
return (recentIds.size() > 1) ? Book.getById(recentIds.get(1)) : null;
}
public LibraryTree favorites() {
waitForState(STATE_FULLY_INITIALIZED);
return getRootTree(ROOT_FAVORITES);
}
public LibraryTree searchResults() {
return getRootTree(ROOT_SEARCH_RESULTS);
}
public LibraryTree searchBooks(String pattern) {
waitForState(STATE_FULLY_INITIALIZED);
final RootTree newSearchResults = new SearchResultsTree(ROOT_SEARCH_RESULTS, pattern);
final RootTree newSearchResults = new SearchResultsTree(this, ROOT_SEARCH_RESULTS, pattern);
if (pattern != null) {
pattern = pattern.toLowerCase();
for (Book book : myBooks) {

View file

@ -24,6 +24,12 @@ import java.util.*;
import org.geometerplus.fbreader.tree.FBTree;
public abstract class LibraryTree extends FBTree {
public static enum Status {
READY_TO_OPEN,
WAIT_FOR_OPEN,
CANNOT_OPEN
};
protected LibraryTree() {
super();
}
@ -36,6 +42,17 @@ public abstract class LibraryTree extends FBTree {
return getName();
}
public Status getOpeningStatus() {
return Status.READY_TO_OPEN;
}
public String getOpeningStatusMessage() {
return null;
}
public void waitForOpening() {
}
public Book getBook() {
return null;
}

View file

@ -22,10 +22,12 @@ package org.geometerplus.fbreader.library;
import org.geometerplus.zlibrary.core.resources.ZLResource;
public class RootTree extends LibraryTree {
private final Library myLibrary;
private final String myId;
private final ZLResource myResource;
RootTree(String id) {
RootTree(Library library, String id) {
myLibrary = library;
myId = id;
myResource = Library.resource().getResource(myId);
}
@ -49,4 +51,22 @@ public class RootTree extends LibraryTree {
protected String getStringId() {
return myId;
}
@Override
public Status getOpeningStatus() {
return
myLibrary.hasState(Library.STATE_FULLY_INITIALIZED)
? Status.READY_TO_OPEN
: Status.WAIT_FOR_OPEN;
}
@Override
public String getOpeningStatusMessage() {
return "loadingBookList";
}
@Override
public void waitForOpening() {
myLibrary.waitForState(Library.STATE_FULLY_INITIALIZED);
}
}

View file

@ -22,8 +22,8 @@ package org.geometerplus.fbreader.library;
class SearchResultsTree extends RootTree {
private final String myPattern;
SearchResultsTree(String id, String pattern) {
super(id);
SearchResultsTree(Library library, String id, String pattern) {
super(library, id);
myPattern = pattern != null ? pattern : "";
}