Skip to content

Commit 7616413

Browse files
committed
Docks for RefreshCoursesAction.
1 parent 4bb9ce5 commit 7616413

File tree

2 files changed

+59
-30
lines changed

2 files changed

+59
-30
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/actions/RefreshCoursesAction.java

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public final class RefreshCoursesAction {
3030
private CourseDb courseDb;
3131
private ConvenientDialogDisplayer dialogs;
3232

33-
//private BgTaskListenerList<List<Course>> listeners;
3433
private FutureCallbackList<List<Course>> callbacks;
3534
private final TmcCore tmcCore;
3635
private final NBTmcSettings tmcSettings;
@@ -61,47 +60,62 @@ public RefreshCoursesAction addListener(FutureCallback<List<Course>> callbacks)
6160
return this;
6261
}
6362

64-
//HUOM metodissa oldRun on vanha toteutus. Se hakee palvelmelta nykyisen kurssin "fullcourse info".
63+
/**
64+
* Starts downloading course-jsons from TMC-server.
65+
* Url of TMC-server is defined in TmcSettings object.
66+
* TmcCore includes all logic, callbacks here are run after core-futures are ready.
67+
*/
6568
public void run() {
6669
try {
6770
ListenableFuture<List<Course>> listCourses = this.tmcCore.listCourses(tmcSettings);
68-
Futures.addCallback(listCourses, new FutureCallback<List<Course>>() {
69-
@Override
70-
public void onSuccess(final List<Course> courses) {
71-
Course currentCourse = CourseListUtils.getCourseByName(courses, courseDb.getCurrentCourseName());
72-
if (currentCourse != null) {
73-
try {
74-
ListenableFuture<Course> courseFuture = tmcCore.getCourse(tmcSettings, currentCourse.getDetailsUrl());
75-
Futures.addCallback(courseFuture, new UpdateCourse(courses));
76-
} catch (TmcCoreException ex) {
77-
Exceptions.printStackTrace(ex);
78-
callbacks.onFailure(ex);
79-
}
80-
} else {
81-
callbacks.onSuccess(courses);
82-
}
83-
}
71+
Futures.addCallback(listCourses, new LoadCourses());
72+
} catch (TmcCoreException ex) {
73+
Exceptions.printStackTrace(ex);
74+
callbacks.onFailure(ex);
75+
}
76+
}
8477

85-
@Override
86-
public void onFailure(Throwable ex) {
87-
log.log(Level.INFO, "Failed to download current course info.", ex);
78+
/**
79+
* This callBack is run when ListenableFuture (to witch this is attached) is done.
80+
* On success method takes list of Course-objects, searches the current course and starts uploading
81+
* the details of the course.
82+
* If no currentCourse found, no need to update details.
83+
*/
84+
class LoadCourses implements FutureCallback<List<Course>> {
85+
@Override
86+
public void onSuccess(final List<Course> courses) {
87+
Course currentCourse = CourseListUtils.getCourseByName(courses, courseDb.getCurrentCourseName());
88+
if (currentCourse != null) {
89+
try {
90+
ListenableFuture<Course> courseFuture = tmcCore.getCourse(tmcSettings, currentCourse.getDetailsUrl());
91+
Futures.addCallback(courseFuture, new UpdateCourse(courses));
92+
} catch (TmcCoreException ex) {
93+
Exceptions.printStackTrace(ex);
8894
callbacks.onFailure(ex);
8995
}
96+
} else {
97+
callbacks.onSuccess(courses);
9098
}
91-
);
92-
} catch (TmcCoreException ex) {
93-
Exceptions.printStackTrace(ex);
99+
}
100+
101+
@Override
102+
public void onFailure(Throwable ex) {
103+
log.log(Level.INFO, "Failed to download current course info.", ex);
94104
callbacks.onFailure(ex);
95105
}
96106
}
97107

108+
/**
109+
* When detailed current course is present, courses will be given to FutureCallbackList,
110+
* that shares the result to every callback that is attached to that list.
111+
*/
98112
class UpdateCourse implements FutureCallback<Course> {
99-
100113
List<Course> courses;
101-
public UpdateCourse(List<Course> courses){
114+
115+
public UpdateCourse(List<Course> courses) {
102116
this.courses = courses;
103117
}
104-
118+
105119
@Override
106120
public void onSuccess(Course detailedCourse) {
107121
detailedCourse.setExercisesLoaded(true);
@@ -123,6 +137,9 @@ public void onFailure(Throwable ex) {
123137
}
124138
}
125139

140+
/**
141+
* Updates the courseDb after all course-jsons are downloaded.
142+
*/
126143
private class DefaultListener implements FutureCallback<List<Course>> {
127144

128145
private final boolean showDialogOnError;
@@ -132,7 +149,7 @@ public DefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
132149
this.showDialogOnError = showDialogOnError;
133150
this.updateCourseDb = updateCourseDb;
134151
}
135-
152+
136153
@Override
137154
public void onSuccess(List<Course> result) {
138155
if (updateCourseDb) {

tmc-plugin/src/fi/helsinki/cs/tmc/utilities/FutureCallbackList.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
package fi.helsinki.cs.tmc.utilities;
33

44
import com.google.common.util.concurrent.FutureCallback;
5-
import com.google.common.util.concurrent.ListenableFuture;
65
import java.util.ArrayList;
76
import java.util.List;
87

9-
8+
/**
9+
* When multiple listeners/callback are needed for one action, they should be added to
10+
* FutureCallbackList.
11+
* Then the first callback can execute the rest by onSuccess method.
12+
*
13+
* @param <T> can be any Object defined by the return value of the ListenableFuture.
14+
*/
1015
public class FutureCallbackList<T> {
1116

1217
private List<FutureCallback<T>> callbacks;
@@ -19,12 +24,19 @@ public void addListener(FutureCallback<T> callback) {
1924
this.callbacks.add(callback);
2025
}
2126

27+
/**
28+
* Calls the onSuccess of all callbacks.
29+
* @param result that should be shared to all callbacks.
30+
*/
2231
public void onSuccess(T result) {
2332
for (FutureCallback<T> callback : callbacks) {
2433
callback.onSuccess(result);
2534
}
2635
}
2736

37+
/**
38+
* Calls the onFailure of all callbacks.
39+
*/
2840
public void onFailure(Throwable ex) {
2941
for (FutureCallback<T> callback : callbacks) {
3042
callback.onFailure(ex);

0 commit comments

Comments
 (0)