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

Network tree classes & Network Adapter have been added

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1060 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Vasiliy Bout 2010-02-24 20:51:27 +00:00
parent 30e9f8dae0
commit 1cddf78fa1
12 changed files with 404 additions and 14 deletions

View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="5dip"
android:orientation="horizontal"
android:gravity="top|left"
>
<ImageView
android:id="@+id/network_tree_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="5dip"
android:orientation="vertical"
>
<TextView
android:id="@+id/network_tree_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_alignParentTop="true"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="@+id/network_tree_item_childrenlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_below="@id/network_tree_item_name"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
</LinearLayout>

View file

@ -29,13 +29,13 @@ import org.geometerplus.zlibrary.core.tree.ZLTree;
import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.zlibrary.ui.android.R;
abstract class ZLTreeAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, View.OnCreateContextMenuListener { public abstract class ZLTreeAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, View.OnCreateContextMenuListener {
private final ListView myParent; private final ListView myParent;
private ZLTree myTree; private ZLTree myTree;
private ZLTree[] myItems; private ZLTree[] myItems;
private final HashSet<ZLTree> myOpenItems = new HashSet<ZLTree>(); private final HashSet<ZLTree> myOpenItems = new HashSet<ZLTree>();
ZLTreeAdapter(ListView parent, ZLTree tree) { protected ZLTreeAdapter(ListView parent, ZLTree tree) {
myParent = parent; myParent = parent;
myTree = tree; myTree = tree;
myItems = new ZLTree[tree.getSize() - 1]; myItems = new ZLTree[tree.getSize() - 1];

View file

@ -32,6 +32,10 @@ import org.geometerplus.zlibrary.core.tree.ZLTree;
import org.geometerplus.zlibrary.core.options.ZLStringOption; import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.android.fbreader.ZLTreeAdapter;
import org.geometerplus.fbreader.network.*;
public class NetworkLibraryActivity extends ListActivity implements MenuItem.OnMenuItemClickListener { public class NetworkLibraryActivity extends ListActivity implements MenuItem.OnMenuItemClickListener {
static NetworkLibraryActivity Instance; static NetworkLibraryActivity Instance;
@ -47,7 +51,7 @@ public class NetworkLibraryActivity extends ListActivity implements MenuItem.OnM
//requestWindowFeature(Window.FEATURE_NO_TITLE); //requestWindowFeature(Window.FEATURE_NO_TITLE);
//setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); //setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
//new NetworkLibraryAdapter((ListView)findViewById(viewId), tree); new LibraryAdapter(getListView(), NetworkLibrary.Instance().getTree());
} }
@Override @Override
@ -72,6 +76,48 @@ public class NetworkLibraryActivity extends ListActivity implements MenuItem.OnM
super.onDestroy(); super.onDestroy();
} }
private final class LibraryAdapter extends ZLTreeAdapter {
LibraryAdapter(ListView view, NetworkTree tree) {
super(view, tree);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
/*final int position = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
final LibraryTree tree = (LibraryTree)getItem(position);
if (tree instanceof BookTree) {
menu.setHeaderTitle(tree.getName());
final ZLResource resource = ZLResource.resource("libraryView");
menu.add(0, OPEN_BOOK_ITEM_ID, 0, resource.getResource("openBook").getValue());
if (Library.Instance().canDeleteBook(((BookTree)tree).Book)) {
menu.add(0, DELETE_BOOK_ITEM_ID, 0, resource.getResource("deleteBook").getValue());
}
}*/
}
public View getView(int position, View convertView, ViewGroup parent) {
final View view = (convertView != null) ? convertView :
LayoutInflater.from(parent.getContext()).inflate(R.layout.network_tree_item, parent, false);
final NetworkTree tree = (NetworkTree)getItem(position);
((TextView)view.findViewById(R.id.network_tree_item_name)).setText(tree.getName());
((TextView)view.findViewById(R.id.network_tree_item_childrenlist)).setText(tree.getSecondString());
return view;
}
/*protected boolean runTreeItem(ZLTree tree) {
if (super.runTreeItem(tree)) {
return true;
}
return true;
}*/
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu); super.onCreateOptionsMenu(menu);

View file

@ -22,7 +22,7 @@ package org.geometerplus.fbreader.network;
import java.util.*; import java.util.*;
abstract public class NetworkCatalogItem extends NetworkLibraryItem { public abstract class NetworkCatalogItem extends NetworkLibraryItem {
public final int Visibility; public final int Visibility;
@ -34,7 +34,7 @@ abstract public class NetworkCatalogItem extends NetworkLibraryItem {
public interface VisibilityType { public interface VisibilityType {
int NEVER = 0; int NEVER = 0;
int ALWAYS = 1; int ALWAYS = 1;
int LOGGED_USERS = 3; int LOGGED_USERS = 2;
}; };
public NetworkCatalogItem(NetworkLink link, String title, String summary, int visibility) { public NetworkCatalogItem(NetworkLink link, String title, String summary, int visibility) {
@ -42,9 +42,9 @@ abstract public class NetworkCatalogItem extends NetworkLibraryItem {
Visibility = visibility; Visibility = visibility;
} }
abstract String loadChildren(List<NetworkLibraryItem> children); public abstract String loadChildren(List<NetworkLibraryItem> children); // returns Error Message
int catalogType() { public int catalogType() {
return CatalogType.OTHER; return CatalogType.OTHER;
} }
} }

View file

@ -21,6 +21,8 @@ package org.geometerplus.fbreader.network;
import java.util.*; import java.util.*;
import org.geometerplus.fbreader.tree.FBTree;
import org.geometerplus.fbreader.network.tree.*;
public class NetworkLibrary { public class NetworkLibrary {
private static NetworkLibrary ourInstance; private static NetworkLibrary ourInstance;
@ -33,17 +35,32 @@ public class NetworkLibrary {
} }
private List<NetworkLink> myLinks; private final ArrayList<NetworkLink> myLinks = new ArrayList<NetworkLink>();
private final RootTree myRootTree = new RootTree();
private boolean myUpdateChildren = true;
private static class DummyCatalogItem extends NetworkCatalogItem {
public DummyCatalogItem(NetworkLink link, String title, String summary) {
super(link, title, summary, VisibilityType.ALWAYS);
}
@Override
public String loadChildren(List<NetworkLibraryItem> children) {
return "";
}
}
public NetworkLibrary() { public NetworkLibrary() {
myLinks.add(new NetworkLink("feedbooks.com") { myLinks.add(new NetworkLink("feedbooks.com") {
public NetworkLibraryItem libraryItem() { public NetworkLibraryItem libraryItem() {
return new NetworkLibraryItem(this, "Feedbooks catalog", "feedbooks online catalog"); return new DummyCatalogItem(this, "Feedbooks catalog", "feedbooks online catalog");
} }
}); });
myLinks.add(new NetworkLink("litres.ru") { myLinks.add(new NetworkLink("litres.ru") {
public NetworkLibraryItem libraryItem() { public NetworkLibraryItem libraryItem() {
return new NetworkLibraryItem(this, "Litres catalog", "litres online catalog"); return new DummyCatalogItem(this, "Litres catalog", "litres online catalog");
} }
}); });
} }
@ -52,4 +69,105 @@ public class NetworkLibrary {
return Collections.unmodifiableList(myLinks); return Collections.unmodifiableList(myLinks);
} }
public void invalidate() {
myUpdateChildren = true;
}
private void makeUpToDate() {
final List<FBTree> toRemove = new LinkedList<FBTree>();
Iterator<FBTree> nodeIterator = myRootTree.iterator();
FBTree currentNode = null;
int nodeCount = 0;
for (int i = 0; i < myLinks.size(); ++i) {
NetworkLink link = myLinks.get(i);
if (!link.OnOption.getValue()) {
continue;
}
boolean processed = false;
while (currentNode != null || nodeIterator.hasNext()) {
if (currentNode == null) {
currentNode = nodeIterator.next();
}
if (!(currentNode instanceof NetworkCatalogTree)) {
currentNode = null;
continue;
}
final NetworkLink nodeLink = ((NetworkCatalogTree)currentNode).Item.Link;
if (nodeLink == link) {
currentNode = null;
++nodeCount;
processed = true;
break;
} else {
boolean found = false;
for (int j = i + 1; j < myLinks.size(); ++j) {
if (nodeLink == myLinks.get(j)) {
found = true;
break;
}
}
if (!found) {
toRemove.add(currentNode);
currentNode = null;
++nodeCount;
} else {
break;
}
}
}
if (!processed) {
NetworkCatalogRootTree ptr = new NetworkCatalogRootTree(myRootTree, link, nodeCount++);
//ptr.item().onDisplayItem();
}
}
/*SearchResultNode srNode = null;
while (nodeIterator.hasNext()) {
FBTree node = nodeIterator.next();
++nodeCount;
if (node instanceof SearchResultNode) {
srNode = (SearchResultNode) node;
} else {
toRemove.add(node);
}
}
final SearchResult searchResult = SearchResult.lastSearchResult();
NetworkBookCollection result = searchResult.collection();
if (result.isNull()) {
if (srNode != 0) {
toRemove.add(srNode);
}
} else if (srNode == null || srNode->searchResult() != result) {
if (srNode != null) {
toRemove.add(srNode);
}
srNode = new SearchResultNode(myRootTree, result, searchResult.summary()); // at nodeCount ??? or not???
NetworkNodesFactory::createSubnodes(srNode, result);
}*/
for (FBTree tree : toRemove) {
tree.removeSelf();
}
/*if (srNode != null) {
srNode->open(false);
srNode->expandOrCollapseSubtree();
}*/
}
public void synchronize() {
if (myUpdateChildren) {
myUpdateChildren = false;
makeUpToDate();
}
}
public NetworkTree getTree() {
synchronize();
return myRootTree;
}
} }

View file

@ -19,13 +19,13 @@
package org.geometerplus.fbreader.network; package org.geometerplus.fbreader.network;
public class NetworkLibraryItem { public abstract class NetworkLibraryItem {
public final NetworkLink Link; public final NetworkLink Link;
public final String Title; public final String Title;
public final String Summary; public final String Summary;
public NetworkLibraryItem(NetworkLink link, String title, String summary) { protected NetworkLibraryItem(NetworkLink link, String title, String summary) {
Link = link; Link = link;
Title = title; Title = title;
Summary = summary; Summary = summary;

View file

@ -0,0 +1,60 @@
/*
* Copyright (C) 2010 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.network;
import java.util.*;
//import org.geometerplus.zlibrary.core.tree.ZLTree;
import org.geometerplus.fbreader.tree.FBTree;
public abstract class NetworkTree extends FBTree {
protected NetworkTree() {
super();
}
protected NetworkTree(NetworkTree parent) {
super(parent);
}
protected NetworkTree(NetworkTree parent, int position) {
super(parent, position);
}
private String myChildrenString;
public String getSecondString() {
if (myChildrenString == null) {
StringBuilder builder = new StringBuilder();
int count = 0;
for (FBTree subtree : subTrees()) {
if (count++ > 0) {
builder.append(", ");
}
builder.append(subtree.getName());
if (count == 5) {
break;
}
}
myChildrenString = builder.toString();
}
return myChildrenString;
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (C) 2010 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.network.tree;
import java.util.*;
import org.geometerplus.fbreader.network.*;
public class NetworkCatalogRootTree extends NetworkCatalogTree {
public NetworkCatalogRootTree(RootTree parent, NetworkLink link, int position) {
super(parent, (NetworkCatalogItem) link.libraryItem(), position);
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2010 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.network.tree;
import java.util.*;
import org.geometerplus.fbreader.network.*;
public class NetworkCatalogTree extends NetworkTree {
public final NetworkCatalogItem Item;
private List<NetworkLibraryItem> myChildrenItems;
NetworkCatalogTree(RootTree parent, NetworkCatalogItem item, int position) {
super(parent, position);
Item = item;
}
NetworkCatalogTree(NetworkCatalogTree parent, NetworkCatalogItem item, int position) {
super(parent, position);
Item = item;
}
@Override
public String getName() {
return Item.Title;
}
public void updateChildren() {
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (C) 2010 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.network.tree;
import org.geometerplus.fbreader.network.NetworkTree;
public final class RootTree extends NetworkTree {
@Override
public String getName() {
return null;
}
}

View file

@ -32,6 +32,10 @@ public abstract class FBTree extends ZLTree<FBTree> implements Comparable<FBTree
super(parent); super(parent);
} }
protected FBTree(FBTree parent, int position) {
super(parent, position);
}
public abstract String getName(); public abstract String getName();
protected String getSortKey() { protected String getSortKey() {

View file

@ -32,10 +32,14 @@ public abstract class ZLTree<T extends ZLTree> implements Iterable<T> {
} }
protected ZLTree(T parent) { protected ZLTree(T parent) {
this(parent, -1);
}
protected ZLTree(T parent, int position) {
Parent = parent; Parent = parent;
if (parent != null) { if (parent != null) {
Level = parent.Level + 1; Level = parent.Level + 1;
parent.addSubTree(this); parent.addSubTree(this, position);
} else { } else {
Level = 0; Level = 0;
} }
@ -75,11 +79,14 @@ public abstract class ZLTree<T extends ZLTree> implements Iterable<T> {
throw new RuntimeException("That's impossible!!!"); throw new RuntimeException("That's impossible!!!");
} }
private void addSubTree(T subtree) { private void addSubTree(T subtree, int position) {
if (mySubTrees == null) { if (mySubTrees == null) {
mySubTrees = new ArrayList<T>(); mySubTrees = new ArrayList<T>();
} }
final int subTreeSize = subtree.getSize(); final int subTreeSize = subtree.getSize();
while (position >= 0 && position < mySubTrees.size()) {
subtree = mySubTrees.set(position++, subtree);
}
mySubTrees.add(subtree); mySubTrees.add(subtree);
for (ZLTree parent = this; parent != null; parent = parent.Parent) { for (ZLTree parent = this; parent != null; parent = parent.Parent) {
parent.mySize += subTreeSize; parent.mySize += subTreeSize;