Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactoring and optimisation.

  • Loading branch information...
commit c08e8d608fb9d6cc42c52f1fcd32b61573a70fe4 1 parent 362c8d4
Acerbic authored
Showing with 53 additions and 61 deletions.
  1. +40 −55 src/dloader/GUI.java
  2. +13 −6 src/dloader/gui/TreeNodePageWrapper.java
95 src/dloader/GUI.java
View
@@ -10,6 +10,7 @@
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
+import java.util.NoSuchElementException;
import javax.swing.border.BevelBorder;
@@ -74,7 +75,6 @@ public Thread getEventDispatchThread() {
return eventDispatchThread;
}
- @SuppressWarnings("serial")
public GUI() throws HeadlessException {
assert (SwingUtilities.isEventDispatchThread());
eventDispatchThread = Thread.currentThread();
@@ -104,11 +104,7 @@ public GUI() throws HeadlessException {
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.setEditable(true);
- tree.setModel(new DefaultTreeModel(
- new DefaultMutableTreeNode("JTree") {
- {}
- }
- ));
+ tree.setModel(new DefaultTreeModel(new TreeNodePageWrapper(null)));
// tree.setCellRenderer(new MyRenderer());
tree.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
@@ -237,7 +233,6 @@ public void init() {
chckbxUseCache.setSelected(Main.allowFromCache);
initPrefetch();
-// updateTree (rootPage, "", 1); //empty message to put root page on display;
}
private void initPrefetch() {
@@ -254,6 +249,14 @@ private void finishPrefetch() {
lblStatus.setText("");
btnPrefetch.setEnabled(true);
newWorker = null;
+
+ TreeNodePageWrapper target = (TreeNodePageWrapper)tree.getModel().getRoot();
+ try {
+ target = (TreeNodePageWrapper) target.getFirstChild();
+ tree.expandPath(new TreePath(target.getPath()));
+ } catch (NoSuchElementException e) {
+ }
+
}
private void initScan() {
@@ -295,18 +298,16 @@ else if (!p.getParent().childPages.contains(p))
}
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
- DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
+ TreeNodePageWrapper parentNode = (TreeNodePageWrapper) model.getRoot();
for (AbstractPage childPage: pathToPage) {
// check if this page exists in the parent
- DefaultMutableTreeNode childNode = getNodeOfPage (parentNode, childPage);
+ TreeNodePageWrapper childNode = getNodeOfPage (parentNode, childPage);
if (childNode == null) {
// currentPage's node was not found in parent node
// add new item under this parent
- childNode = new DefaultMutableTreeNode();
- TreeNodePageWrapper childsUserObject = new TreeNodePageWrapper(childPage);
- childNode.setUserObject(childsUserObject);
+ childNode = new TreeNodePageWrapper(childPage);
if (childPage.getParent() == null)
parentNode.add(childNode); // to the end
@@ -321,22 +322,18 @@ else if (!p.getParent().childPages.contains(p))
// new item's children if any (that way they maintain their order)
for (AbstractPage subPage: childPage.childPages) {
- DefaultMutableTreeNode subChild = new DefaultMutableTreeNode();
- subChild.setUserObject(new TreeNodePageWrapper(subPage));
+ TreeNodePageWrapper subChild = new TreeNodePageWrapper(subPage);
+ childNode.add(subChild);
}
int[] indices = new int[1];
indices[0] = parentNode.getIndex(childNode);
model.nodesWereInserted(parentNode, indices); // notification to repaint
- // expand only if not a Track
- if (!(childsUserObject.page instanceof Track))
- tree.expandPath(new TreePath(parentNode.getPath()));
}
parentNode = childNode; // advance to search next element in our pathToPage
}
- // after search is complete, parent points to a DefaultMutableTreeNode
- // containing TreeNodePageWrapper containing original p (but now p is different)
+ // after search is complete, parent points to a TreeNodePageWrapper containing original p (but now p is different)
// Reading cache/ downloading a page forces reset of page data (children refs),
// the node branch must be trimmed accordingly
@@ -348,16 +345,15 @@ else if (!p.getParent().childPages.contains(p))
trimBranch(parentNode, model);
// pass message to the user object and refresh its visual if needed
- TreeNodePageWrapper parentWrapper = (TreeNodePageWrapper)parentNode.getUserObject();
+// TreeNodePageWrapper parentWrapper = (TreeNodePageWrapper)parentNode.getUserObject();
// TODO: consider using EventListener mechanic
- if (parentWrapper.update(message, value)) {
+ if (parentNode.update(message, value)) {
model.nodeChanged(parentNode);
- if (getPageOfNode(parentNode) instanceof Track) {
- DefaultMutableTreeNode gParentNode = (DefaultMutableTreeNode) parentNode.getParent();
+ if (parentNode.page instanceof Track) {
+ TreeNodePageWrapper gParentNode = (TreeNodePageWrapper) parentNode.getParent();
if (gParentNode != null) {
- TreeNodePageWrapper grandParentWrapper = (TreeNodePageWrapper) gParentNode.getUserObject();
- grandParentWrapper.kidChanged(parentWrapper, gParentNode, message, value);
+ gParentNode.kidChanged(parentNode, gParentNode, message, value);
model.nodeChanged(gParentNode);
}
}
@@ -374,16 +370,16 @@ else if (!p.getParent().childPages.contains(p))
* @param childPage - a page to be inserted. childPage.getParent() must not be null.
* @return -1 if new node should be appended to the end, or a proper insertion index
*/
- private int findInsertionPoint(DefaultMutableTreeNode parentNode,
+ private int findInsertionPoint(TreeNodePageWrapper parentNode,
AbstractPage childPage) {
Iterator<AbstractPage> pageLooker = childPage.getParent().childPages.iterator();
@SuppressWarnings("unchecked")
- Enumeration<DefaultMutableTreeNode> nodeLooker = parentNode.children();
+ Enumeration<TreeNodePageWrapper> nodeLooker = parentNode.children();
// *S*
// *A1*
while (nodeLooker.hasMoreElements()) { // *G* exit
- DefaultMutableTreeNode currentNode = nodeLooker.nextElement();
+ TreeNodePageWrapper currentNode = nodeLooker.nextElement();
// *A2*
if (!pageLooker.hasNext())
@@ -392,17 +388,17 @@ private int findInsertionPoint(DefaultMutableTreeNode parentNode,
// these are B-C-D123-B and B-C-E-B loops
b_loop:
- while (! getPageOfNode(currentNode).equals(currentPage)) { // *A2*->*B* check AND *E*->*B* check
+ while (! currentNode.page.equals(currentPage)) { // *A2*->*B* check AND *E*->*B* check
// *B*
if (childPage.equals(currentPage)) {
return parentNode.getIndex(currentNode); // *F*
}
// *C*
- if (getPageOfNode(parentNode).childPages.contains(getPageOfNode(currentNode))) {
+ if (parentNode.page.childPages.contains(currentNode.page)) {
// *D2*
while (pageLooker.hasNext())
currentPage = pageLooker.next();
- if (getPageOfNode(currentNode).equals(currentPage))
+ if (currentNode.page.equals(currentPage))
break b_loop; // -> *A*
// *D3*
if (childPage.equals(currentPage)) {
@@ -429,25 +425,25 @@ private int findInsertionPoint(DefaultMutableTreeNode parentNode,
* @param branch - node branch to clean up
* @param model - reference model
*/
- private void trimBranch(DefaultMutableTreeNode branch, DefaultTreeModel model) {
- AbstractPage branchPage = getPageOfNode(branch);
- Collection<DefaultMutableTreeNode> removeList = new LinkedList<>();
+ private void trimBranch(TreeNodePageWrapper branch, DefaultTreeModel model) {
+ AbstractPage branchPage = branch.page;
+ Collection<TreeNodePageWrapper> removeList = new LinkedList<>();
for (@SuppressWarnings("unchecked")
- Enumeration<DefaultMutableTreeNode> children = branch.children(); children.hasMoreElements();) {
- DefaultMutableTreeNode childNode = children.nextElement();
- AbstractPage childPage = getPageOfNode(childNode);
+ Enumeration<TreeNodePageWrapper> children = branch.children(); children.hasMoreElements();) {
+ TreeNodePageWrapper childNode = children.nextElement();
+ AbstractPage childPage = childNode.page;
if (! branchPage.childPages.contains(childPage)) {
// child's page is no more contained within its parent's page children collection
removeList.add(childNode); // can't remove on spot, it will fuck up the iteration
}
}
- for (DefaultMutableTreeNode element: removeList) {
+ for (TreeNodePageWrapper element: removeList) {
// ordinary, this should never happen. if this is executing, it means that cache data was in conflict
// with updated net data.
model.removeNodeFromParent(element);
//since this page is no more of our concern, all jobs executing and pending are irrelevant CPU consumers
- newWorker.stopJobsForPage(getPageOfNode(element));
+ newWorker.stopJobsForPage(element.page);
}
}
@@ -464,28 +460,17 @@ public void myWorkerDone (AbstractPage root, JobMaster.JobType jobType) {
}
/**
- * Shorthand to extract the page this node is displaying through user object
- * @param node
- * @return null if user object is not a AbstractPage wrapper
- */
- private AbstractPage getPageOfNode(DefaultMutableTreeNode node) {
- if (node.getUserObject() instanceof TreeNodePageWrapper)
- return ((TreeNodePageWrapper)node.getUserObject()).page;
- else return null;
- }
-
- /**
- * Search children nodes of a parentNode for a DefaultMutableTreeNode containing given AbstractPage.
+ * Search children nodes of a parentNode for a TreeNodePageWrapper containing given AbstractPage.
* @param parentNode - parent of nodes to search among
* @param page - page to search
* @return found node or null if not found
*/
- private DefaultMutableTreeNode getNodeOfPage (DefaultMutableTreeNode parentNode, AbstractPage page) {
- DefaultMutableTreeNode childNode = null;
+ private TreeNodePageWrapper getNodeOfPage (TreeNodePageWrapper parentNode, AbstractPage page) {
+ TreeNodePageWrapper childNode = null;
for (@SuppressWarnings("unchecked")
- Enumeration<DefaultMutableTreeNode> children = parentNode.children(); children.hasMoreElements();) {
+ Enumeration<TreeNodePageWrapper> children = parentNode.children(); children.hasMoreElements();) {
childNode = children.nextElement();
- if (page.equals(getPageOfNode(childNode)))
+ if (page.equals(childNode.page))
return childNode;
}
return null;
19 src/dloader/gui/TreeNodePageWrapper.java
View
@@ -16,8 +16,13 @@
* @author Acerbic
*
*/
-public class TreeNodePageWrapper {
+public class TreeNodePageWrapper extends DefaultMutableTreeNode {
+ /**
+ *
+ */
+ private static final long serialVersionUID = -265090747493368344L;
+
public final AbstractPage page; //wrapped object
// TODO job progress flags and logs
@@ -32,11 +37,12 @@
int kidsInProcessing = 0;
public TreeNodePageWrapper(AbstractPage page) {
+ super(null);
this.page = page;
}
/**
- * update flags and states of this node visual representation
+ * Update flags and states of this node visual representation
* @param type
* @param report
* @return true if node must be repainted
@@ -94,6 +100,7 @@ public boolean update(String type, long report) {
@Override
public String toString() {
+ if (page == null) return null;
// TODO
String header = "<html>";
@@ -114,8 +121,8 @@ public String toString() {
// title formatters;
String childrenCount = (page.childPages.size() <= 0)? "":
- "<span id='children'>["+
- (kidsInProcessing <= 0 ? "": (page.childPages.size()-kidsInProcessing)+"/")+
+ "<span id='children'>[" +
+ (kidsInProcessing <= 0 ? "": (page.childPages.size()-kidsInProcessing) +"/") +
page.childPages.size()+"]</span>";
if (downloading) {
@@ -129,7 +136,7 @@ else if (downloadPageFailed) {
}
// finalize title
- title = "<span id='title'>"+title+"</span>";
+ title = "<span id='title'>" + title + "</span>";
// finalize style
header += "<style type='text/css'> " + styleCompilation + "</style>";
@@ -152,7 +159,7 @@ public void kidChanged(TreeNodePageWrapper kidWrapper,
kidsInProcessing = 0;
for (@SuppressWarnings("unchecked")
Enumeration<DefaultMutableTreeNode> children = thisNode.children(); children.hasMoreElements();) {
- TreeNodePageWrapper kid = (TreeNodePageWrapper) children.nextElement().getUserObject();
+ TreeNodePageWrapper kid = (TreeNodePageWrapper) children.nextElement();
if (kid.downloading || kid.downloadPageQ)
kidsInProcessing++;
Please sign in to comment.
Something went wrong with that request. Please try again.