@@ -35,10 +35,11 @@ public class DownloadExercisesAction {
35
35
private List <Exercise > exercisesToDownload ;
36
36
private TmcCore tmcCore ;
37
37
private NBTmcSettings settings ;
38
+ private int batchSize = 3 ;
38
39
39
40
/**
40
- * Downloads all exercises of the list from TmcServer, unzips and opens them and
41
- * saves the checksums of each Exercise to courseDb.
41
+ * Downloads all exercises of the list from TmcServer, unzips and opens them
42
+ * and saves the checksums of each Exercise to courseDb.
42
43
*/
43
44
public DownloadExercisesAction (List <Exercise > exercisesToOpen ) {
44
45
this .projectMediator = ProjectMediator .getInstance ();
@@ -50,30 +51,43 @@ public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
50
51
}
51
52
52
53
public void run () throws TmcCoreException {
54
+
55
+ int start = 0 ;
56
+ int end = Math .min (exercisesToDownload .size (), batchSize );
57
+
58
+ List <Exercise > batch = exercisesToDownload .subList (start , end );
59
+
53
60
ProgressHandle exerciseDownload = ProgressHandleFactory .createSystemHandle (
54
- "Downloading " + exercisesToDownload .size () + " exercises." );
61
+ "Downloading " + batch .size () + " exercises." );
55
62
exerciseDownload .start ();
56
- ListenableFuture <List <Exercise >> dlFuture = tmcCore .downloadExercises (exercisesToDownload , settings );
57
63
58
- Futures .addCallback (dlFuture , new ProjectOpener (exerciseDownload ));
64
+ ListenableFuture <List <Exercise >> dlFuture = tmcCore .downloadExercises (batch , settings );
65
+
66
+ Futures .addCallback (dlFuture , new ProjectOpener (exerciseDownload , end ));
59
67
}
60
68
61
69
private class ProjectOpener implements FutureCallback <List <Exercise >> {
62
70
63
71
private ProgressHandle lastAction ;
64
-
72
+ private int start ;
73
+
65
74
/**
66
- * Converts Exercise objects to TmcProjectInfo objects.
67
- * Saves them to CourseDb and opens them.
68
- * @param lastAction
75
+ * Converts Exercise objects to TmcProjectInfo objects. Saves them to
76
+ * CourseDb and opens them.
77
+ *
78
+ * @param lastAction
69
79
*/
70
- public ProjectOpener (ProgressHandle lastAction ) {
80
+ public ProjectOpener (ProgressHandle lastAction , int start ) {
71
81
this .lastAction = lastAction ;
82
+ this .start = start ;
72
83
}
73
84
74
85
@ Override
75
86
public void onSuccess (List <Exercise > downloadedExercises ) {
76
87
lastAction .finish ();
88
+
89
+ downloadNextBatch ();
90
+
77
91
List <TmcProjectInfo > projects = new ArrayList <TmcProjectInfo >();
78
92
for (Exercise exercise : downloadedExercises ) {
79
93
TmcProjectInfo info = projectMediator .tryGetProjectForExercise (exercise );
@@ -86,6 +100,30 @@ public void onSuccess(List<Exercise> downloadedExercises) {
86
100
projectMediator .openProjects (projects );
87
101
}
88
102
103
+ private void downloadNextBatch () {
104
+ int end = Math .min (exercisesToDownload .size (), start + batchSize );
105
+ List <Exercise > batch = exercisesToDownload .subList (start , end );
106
+
107
+ if (batch .isEmpty ()) {
108
+ return ;
109
+ }
110
+
111
+ ProgressHandle exerciseDownload = ProgressHandleFactory .createSystemHandle (
112
+ "Downloading " + batch .size () + " exercises." );
113
+ exerciseDownload .start ();
114
+
115
+ ListenableFuture <List <Exercise >> dlFuture ;
116
+ try {
117
+ dlFuture = tmcCore .downloadExercises (batch , settings );
118
+ } catch (TmcCoreException ex ) {
119
+ List <Exercise > empty = new ArrayList <Exercise >();
120
+ dlFuture = Futures .immediateFuture (empty );
121
+ dialogs .displayError ("Could not download exercises: " + batch , ex );
122
+ }
123
+
124
+ Futures .addCallback (dlFuture , new ProjectOpener (exerciseDownload , end ));
125
+ }
126
+
89
127
private void saveDownloadedExercisesToCourseDb (final List <Exercise > downloadedExercises ) {
90
128
try {
91
129
SwingUtilities .invokeAndWait (new Runnable () {
0 commit comments