Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

bits of optimization

  • Loading branch information...
commit 8ba7fd3a930f6e5a9e8e5516723a2d4eda23b3e7 1 parent 0be3919
authored October 31, 2012

Showing 1 changed file with 29 additions and 8 deletions. Show diff stats Hide diff stats

  1. 37  src/dloader/gui/GUI.java
37  src/dloader/gui/GUI.java
@@ -6,6 +6,7 @@
6 6
 import java.util.Deque;
7 7
 import java.util.Enumeration;
8 8
 import java.util.LinkedList;
  9
+import java.util.List;
9 10
 import java.util.NoSuchElementException;
10 11
 import java.util.concurrent.ExecutionException;
11 12
 import java.util.logging.Level;
@@ -344,9 +345,14 @@ else if (!p.getParent().childPages.contains(p))
344 345
 			if (childNode == null) {
345 346
 				// currentPage's node was not found in parent node 
346 347
 				
  348
+				List<TreeNodePageWrapper> oldChildren = null;
347 349
 				if (parentNode.getChildCount() > 0) {
348 350
 					// some children are present, but not all of them.
349  
-					// remove all old children then add all new ones
  351
+					// remove all old children then add re-add with the new ones
  352
+					oldChildren = new LinkedList<>();
  353
+					for (@SuppressWarnings("unchecked")
  354
+					Enumeration<TreeNodePageWrapper> children = parentNode.children(); children.hasMoreElements();) 
  355
+						oldChildren.add(children.nextElement());
350 356
 					parentNode.removeAllChildren();
351 357
 					// we don't need to worry about removed pages' jobs because 
352 358
 					//  PageJob algorithms operates on updated data, it is GUI tree that lags behind 
@@ -355,13 +361,13 @@ else if (!p.getParent().childPages.contains(p))
355 361
 				// add new items under current parent
356 362
 				// we are assuming that if one (this) child is present, then all of them are already present,
357 363
 				//   as children list is initiated / updated as a whole at the same time.
358  
-				childNode = addChildrenNodes(parentNode, childPage);
  364
+				childNode = addChildrenNodes(parentNode, childPage, oldChildren);
359 365
 				
360 366
 				assert (childNode != null); // can happen only if ghost sneaked in - other thread compromised AbstractPage tree
361 367
 				
362 368
 				// new item's children if any 
363 369
 				if (childPage.childPages.size() > 0)
364  
-					addChildrenNodes(childNode, childPage.childPages.get(0)); // bit awkward code reuse
  370
+					addChildrenNodes(childNode, childPage.childPages.get(0), null); // bit awkward code reuse
365 371
 				
366 372
 				model.nodeStructureChanged(parentNode); // notification to repaint
367 373
 			}
@@ -374,7 +380,7 @@ else if (!p.getParent().childPages.contains(p))
374 380
 		// case of new page downloads it is visually more pleasing to see what is going on asap 
375 381
 		if (parentNode.page.equals(pathToPage.getFirst())) {
376 382
 			if (parentNode.page.childPages.size() > 0 && parentNode.getChildCount() == 0)
377  
-				addChildrenNodes(parentNode, parentNode.page.childPages.get(0)); // bit awkward code reuse
  383
+				addChildrenNodes(parentNode, parentNode.page.childPages.get(0), null); // bit awkward code reuse
378 384
 			unfoldFirst(); 
379 385
 		}
380 386
 		
@@ -585,19 +591,19 @@ private TreeNodePageWrapper getNodeOfPage (TreeNodePageWrapper parentNode, Abstr
585 591
 	 * Adds to a given tree node children nodes, which are all wrappers of siblings of given childPage (including the very childPage)
586 592
 	 * @param parentNode - parent tree node where children will be added
587 593
 	 * @param childPage - a page whose siblings will be add.
  594
+	 * @param oldChildren - old children to be reused if possible
588 595
 	 * @return a new node created for a given child page.
589 596
 	 */
590  
-	private TreeNodePageWrapper addChildrenNodes (TreeNodePageWrapper parentNode, AbstractPage childPage) {
  597
+	private TreeNodePageWrapper addChildrenNodes (TreeNodePageWrapper parentNode, AbstractPage childPage, List<TreeNodePageWrapper> oldChildren) {
591 598
 		TreeNodePageWrapper returnedChildNode = null;
592  
-		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
593 599
 		
594 600
 		if (childPage.getParent() == null) {
595  
-			returnedChildNode = new TreeNodePageWrapper(childPage, model);
  601
+			returnedChildNode = searchListForPageWrapperOrCreateNew(oldChildren, childPage);
596 602
 			parentNode.add(returnedChildNode);
597 603
 			return returnedChildNode;
598 604
 		} else {
599 605
 			for (AbstractPage childPageSibling: childPage.getParent().childPages) {
600  
-				TreeNodePageWrapper childNode = new TreeNodePageWrapper(childPageSibling, model);
  606
+				TreeNodePageWrapper childNode = searchListForPageWrapperOrCreateNew(oldChildren, childPageSibling);
601 607
 				if (childPageSibling.equals(childPage))
602 608
 					returnedChildNode = childNode;
603 609
 				parentNode.add(childNode);
@@ -605,4 +611,19 @@ private TreeNodePageWrapper addChildrenNodes (TreeNodePageWrapper parentNode, Ab
605 611
 			return returnedChildNode;
606 612
 		}
607 613
 	}
  614
+	
  615
+	/**
  616
+	 * Search the list for appropriate preexisting wrapper for given page. If not exists create a new wrapper. 
  617
+	 * @param list - list of existing wrappers. Might be null.
  618
+	 * @param page
  619
+	 * @return a TreeNodePageWrapper object for a page object provided.
  620
+	 */
  621
+	private
  622
+	TreeNodePageWrapper searchListForPageWrapperOrCreateNew(List<TreeNodePageWrapper> list, AbstractPage page) {
  623
+		if (list == null) return new TreeNodePageWrapper(page, (DefaultTreeModel) tree.getModel());
  624
+		for (TreeNodePageWrapper element: list)
  625
+			if (element.page.url.equals(page.url))
  626
+				return element;
  627
+		return new TreeNodePageWrapper(page, (DefaultTreeModel) tree.getModel());
  628
+	}
608 629
 }

0 notes on commit 8ba7fd3

Please sign in to comment.
Something went wrong with that request. Please try again.