Permalink
Browse files

Improves operation of CheckSaving job bunch results. It is 5x faster

now, but UI lag is still possible if JTree contains too many entries to
be updated at once.
  • Loading branch information...
1 parent 8ba7fd3 commit 133266afb3b4f536cffc20f6fa8f5fdb15e12b09 Acerbic committed Nov 1, 2012
Showing with 49 additions and 41 deletions.
  1. +2 −26 src/dloader/gui/GUI.java
  2. +22 −7 src/dloader/gui/MyWorker.java
  3. +25 −8 src/dloader/gui/TreeNodePageWrapper.java
View
@@ -57,31 +57,6 @@
private JCheckBox chckbxForceTag;
private JButton btnStop;
-
-/* @SuppressWarnings("serial")
- class MyRenderer extends DefaultTreeCellRenderer {
-
- public Component getTreeCellRendererComponent(
- JTree tree,
- Object value,
- boolean sel,
- boolean expanded,
- boolean leaf,
- int row,
- boolean hasFocus) {
-
- super.getTreeCellRendererComponent(
- tree, value, sel,
- expanded, leaf, row,
- hasFocus);
- if (value instanceof DefaultMutableTreeNode)
- return this;
- return this;
- }
-
- } */
-
-
public Thread getEventDispatchThread() {
return eventDispatchThread;
}
@@ -295,7 +270,7 @@ public void myWorkerDone () {
if (theWorker.bulkResults != null)
// if in bulk mode...
try {
- getRootNode().updateSavingReqBunch(theWorker.get());
+ getRootNode().updateSavingReqBunch(theWorker.get(), tree);
} catch (InterruptedException | ExecutionException e) {
// won't happen as get() is being called from inside of SwingWorker.done() - after job execution is finished
}
@@ -312,6 +287,7 @@ public void myWorkerDone () {
// error state reached
Main.log(Level.WARNING, "myWorkerDone () -- Error state in GUI.state: " + state.toString());
}
+
}
/**
@@ -1,5 +1,6 @@
package dloader.gui;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -17,7 +18,7 @@
* @author Acerbic
*
*/
-public class MyWorker extends SwingWorker<Map<AbstractPage, ProgressReportStruct>, MyWorker.ProgressReportStruct> {
+public class MyWorker extends SwingWorker<Map<AbstractPage, List<ProgressReportStruct>>, MyWorker.ProgressReportStruct> {
/**
* Simple structure to toss progress report upstream.
@@ -38,9 +39,17 @@
}
}
+ /**
+ * associated JobMaster
+ */
public final JobMaster jm;
- public Map<AbstractPage, ProgressReportStruct> bulkResults;
- private Thread workerThread;
+
+ /**
+ * Works both as storage of results and as a flag (when != null) that results must be stored, not reported
+ */
+ public Map<AbstractPage, List<ProgressReportStruct>> bulkResults;
+
+ private Thread workerThread; // saved for later interruption
// called from ED thread
public MyWorker(AbstractPage rootPage, JobType whatToDo) {
@@ -50,7 +59,7 @@ public MyWorker(AbstractPage rootPage, JobType whatToDo) {
// called from ED thread
public MyWorker(AbstractPage rootPage, JobType whatToDo, boolean bulk) {
if (bulk)
- bulkResults = new ConcurrentHashMap<AbstractPage, ProgressReportStruct>(200);
+ bulkResults = new ConcurrentHashMap<>(200);
jm = new JobMaster(whatToDo, rootPage, 0) {
// bridge to SwingWorker progress reporting
@@ -60,16 +69,22 @@ public void report(AbstractPage page, String type, long i) {
ProgressReportStruct res = new ProgressReportStruct(page, type, i);
if (bulkResults == null)
publish(res);
- else
- bulkResults.put(page, res);
+ else {
+ List<ProgressReportStruct> resultsForThisPage = bulkResults.get(page);
+ if (resultsForThisPage == null) {
+ resultsForThisPage = new LinkedList<ProgressReportStruct>();
+ bulkResults.put(page, resultsForThisPage);
+ }
+ resultsForThisPage.add(res);
+ }
}
};
}
// called in worker thread
@Override
- protected Map<AbstractPage, ProgressReportStruct> doInBackground() throws Exception {
+ protected Map<AbstractPage, List<ProgressReportStruct>> doInBackground() throws Exception {
workerThread = Thread.currentThread();
if (jm != null)
jm.goGoGo(); // -> several calls to PageJob.report() -> jm.report() -> SwingWorker.publish() -> this.process() -> gui.updateTree()
@@ -1,11 +1,14 @@
package dloader.gui;
import java.util.Enumeration;
+import java.util.List;
import java.util.Map;
+import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
+import javax.swing.tree.TreePath;
import dloader.gui.MyWorker.ProgressReportStruct;
import dloader.page.AbstractPage;
@@ -228,8 +231,8 @@ else if (downloadPageFailed) {
else
return header +
title + " " + strChildrenCount + " " + saveDecorator +
- "<br>" + "<span id='url'>" + page.url + "</span>" +
- "</u>"+ bottom;
+ "<br>" + "<span id='url'>" + page.url + "</span>" +
+ bottom;
}
public void kidChanged(TreeNodePageWrapper kidWrapper, String message, long value) {
@@ -250,28 +253,42 @@ public void kidChanged(TreeNodePageWrapper kidWrapper, String message, long valu
/**
* Updates tree nodes visuals according to the check results all at once
- * @param bulkResults
+ * @param map
+ * @param tree
*/
public void updateSavingReqBunch(
- Map<AbstractPage, ProgressReportStruct> bulkResults) {
+ Map<AbstractPage, List<ProgressReportStruct>> map, JTree tree) {
+
+ boolean needsRefresh = false; // flag to repaint this node (relatively long operation)
- Long value = bulkResults.get(page).value;
- if (value != null)
+ Long value = null;
+ try {
+ value = map.get(page).get(0).value; // for every page CheckSavingJob reports exactly one time
+ } catch (NullPointerException|IndexOutOfBoundsException e) {}
+
+ if (value != null) {
+ if (mustSavePage != (value == 0)) needsRefresh = true;
mustSavePage = value == 0;
+ }
+ needsRefresh |= downloadPageQ || downloading;
downloadPageQ = false;
downloading = false;
+ int oldKidsToSave = kidsToSave;
kidsToSave = 0;
for (@SuppressWarnings("unchecked")
Enumeration<DefaultMutableTreeNode> children = children(); children.hasMoreElements();) {
TreeNodePageWrapper kid = (TreeNodePageWrapper) children.nextElement();
- kid.updateSavingReqBunch(bulkResults);
+ kid.updateSavingReqBunch(map, tree);
if (kid.mustSavePage || kid.saving)
kidsToSave++;
}
+ needsRefresh |= oldKidsToSave != kidsToSave;
+
- model.nodeChanged(this);
+ if (needsRefresh && tree.isVisible(new TreePath(getPath())))
+ model.nodeChanged(this);
}
}

0 comments on commit 133266a

Please sign in to comment.