Skip to content

Commit 09eee93

Browse files
committed
merge
2 parents a6e4609 + ba842ab commit 09eee93

File tree

8 files changed

+175
-127
lines changed

8 files changed

+175
-127
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fi.helsinki.cs.tmc.actions;
22

3+
import com.google.common.util.concurrent.FutureCallback;
34
import hy.tmc.core.domain.Course;
45
import fi.helsinki.cs.tmc.data.CourseListUtils;
56
import hy.tmc.core.domain.Exercise;
@@ -47,9 +48,10 @@ public void actionPerformed(ActionEvent e) {
4748

4849
RefreshCoursesAction action = new RefreshCoursesAction();
4950
action.addDefaultListener(true, true);
50-
action.addListener(new BgTaskListener<List<Course>>() {
51+
action.addListener(new FutureCallback<List<Course>>() {
52+
5153
@Override
52-
public void bgTaskReady(List<Course> receivedCourseList) {
54+
public void onSuccess(List<Course> receivedCourseList) {
5355
LocalExerciseStatus status = LocalExerciseStatus.get(courseDb.getCurrentCourseExercises());
5456
if (!status.downloadableCompleted.isEmpty()) {
5557
List<Exercise> emptyList = Collections.emptyList();
@@ -60,11 +62,7 @@ public void bgTaskReady(List<Course> receivedCourseList) {
6062
}
6163

6264
@Override
63-
public void bgTaskCancelled() {
64-
}
65-
66-
@Override
67-
public void bgTaskFailed(Throwable ex) {
65+
public void onFailure(Throwable ex) {
6866
dialogs.displayError("Failed to check for new exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
6967
}
7068
});

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

Lines changed: 120 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
1313
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
1414
import fi.helsinki.cs.tmc.utilities.BgTask;
15-
import fi.helsinki.cs.tmc.utilities.BgTaskListenerList;
1615
import fi.helsinki.cs.tmc.utilities.CancellableCallable;
16+
import fi.helsinki.cs.tmc.utilities.FutureCallbackList;
1717
import hy.tmc.core.TmcCore;
1818
import hy.tmc.core.exceptions.TmcCoreException;
1919
import java.util.ArrayList;
@@ -33,7 +33,8 @@ public final class RefreshCoursesAction {
3333
private CourseDb courseDb;
3434
private ConvenientDialogDisplayer dialogs;
3535

36-
private BgTaskListenerList<List<Course>> listeners;
36+
//private BgTaskListenerList<List<Course>> listeners;
37+
private FutureCallbackList<List<Course>> callbacks;
3738
private final TmcCore tmcCore;
3839
private final NBTmcSettings tmcSettings;
3940

@@ -48,124 +49,134 @@ public RefreshCoursesAction(NBTmcSettings settings) {
4849
this.courseDb = CourseDb.getInstance();
4950
this.dialogs = ConvenientDialogDisplayer.getDefault();
5051

51-
this.listeners = new BgTaskListenerList<List<Course>>();
52+
//this.listeners = new BgTaskListenerList<List<Course>>();
53+
this.callbacks = new FutureCallbackList<List<Course>>();
5254
this.tmcCore = TmcCoreSingleton.getInstance();
5355
}
5456

5557
public RefreshCoursesAction addDefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
56-
this.listeners.addListener(new DefaultListener(showDialogOnError, updateCourseDb));
58+
this.callbacks.addListener(new DefaultListener(showDialogOnError, updateCourseDb));
5759
return this;
5860
}
5961

60-
public RefreshCoursesAction addListener(BgTaskListener<List<Course>> listener) {
61-
this.listeners.addListener(listener);
62+
public RefreshCoursesAction addListener(FutureCallback<List<Course>> callbacks) {
63+
this.callbacks.addListener(callbacks);
6264
return this;
6365
}
6466

6567
//HUOM metodissa oldRun on vanha toteutus. Se hakee palvelmelta nykyisen kurssin "fullcourse info".
66-
public void newrun() {
68+
public void run() {
6769
try {
6870
ListenableFuture<List<Course>> listCourses = this.tmcCore.listCourses(tmcSettings);
6971
Futures.addCallback(listCourses, new FutureCallback<List<Course>>() {
7072
@Override
71-
public void onSuccess(List<Course> courses) {
73+
public void onSuccess(final List<Course> courses) {
7274
Course currentCourse = CourseListUtils.getCourseByName(courses, courseDb.getCurrentCourseName());
7375
if (currentCourse != null) {
74-
ListenableFuture<Course> courseFuture = tmcCore.getCourse(tmcSettings, currentCourse.getDetailsUrl());
75-
Futures.addCallback(courseFuture, new FutureCallback<Course>()
76-
@Override
77-
public void onSuccess(Course detailedCourse) {
78-
detailedCourse.setExercisesLoaded(true);
79-
ArrayList<Course> finalCourses = new ArrayList<Course>();
80-
for(Course course: courses){
81-
if(course.getName().equals(detailedCourse.getName())){
82-
finalCourses.add(detailedCourses);
83-
} else {
84-
finalCourses.add(course);
85-
}
86-
}
87-
listeners.bgTaskReady(finalCourses);
88-
}
89-
@Override
90-
public void onFailure() {
91-
92-
}
93-
);
76+
try {
77+
System.out.println("DETAILS URL: " + currentCourse.getDetailsUrl());
78+
ListenableFuture<Course> courseFuture = tmcCore.getCourse(tmcSettings, currentCourse.getDetailsUrl());
79+
Futures.addCallback(courseFuture, new FutureCallback<Course>() {
80+
@Override
81+
public void onSuccess(Course detailedCourse) {
82+
detailedCourse.setExercisesLoaded(true);
83+
ArrayList<Course> finalCourses = new ArrayList<Course>();
84+
for(Course course: courses){
85+
if(course.getName().equals(detailedCourse.getName())) {
86+
finalCourses.add(detailedCourse);
87+
} else {
88+
finalCourses.add(course);
89+
}
90+
}
91+
callbacks.onSuccess(finalCourses);
92+
}
93+
94+
@Override
95+
public void onFailure(Throwable ex) {
96+
log.log(Level.INFO, "Failed to download current course info.", ex);
97+
callbacks.onFailure(ex);
98+
}
99+
});
100+
} catch (TmcCoreException ex) {
101+
Exceptions.printStackTrace(ex);
102+
callbacks.onFailure(ex);
103+
}
94104
} else {
95-
listeners.bgTaskReady(courses);
105+
callbacks.onSuccess(courses);
96106
}
97107
}
98108

99109
@Override
100110
public void onFailure(Throwable ex) {
101111
log.log(Level.INFO, "Failed to download current course info.", ex);
102-
listeners.bgTaskFailed(ex);
112+
callbacks.onFailure(ex);
103113
}
104114
}
105115
);
106116
} catch (TmcCoreException ex) {
107117
Exceptions.printStackTrace(ex);
118+
callbacks.onFailure(ex);
108119
}
109120
}
110121

111-
/*public void run() {
112-
CancellableCallable<List<Course>> courseListTask = serverAccess.getDownloadingCourseListTask();
113-
BgTask.start("Refreshing course list", courseListTask, new BgTaskListener<List<Course>>() {
114-
115-
@Override
116-
public void bgTaskReady(final List<Course> courses) {
117-
Course currentCourseStub = CourseListUtils.getCourseByName(courses, courseDb.getCurrentCourseName());
118-
// CurrentCourseStub on null jos on vanhentunut/ ei enää olemassa servulla
119-
if (currentCourseStub != null) {
120-
// Jos on olemassa servulla, päivitetään lokaali kurssi servun kurssia vastaavaksi
121-
CancellableCallable<Course> currentCourseTask = serverAccess.getFullCourseInfoTask(currentCourseStub);
122-
123-
BgTask.start("Loading course", currentCourseTask, new BgTaskListener<Course>() {
124-
@Override
125-
public void bgTaskReady(Course currentCourse) {
126-
currentCourse.setExercisesLoaded(true);
127-
128-
ArrayList<Course> finalCourses = new ArrayList<Course>();
129-
for (Course course : courses) {
130-
if (course.getName().equals(currentCourse.getName())) {
131-
finalCourses.add(currentCourse);
132-
} else {
133-
finalCourses.add(course);
134-
}
135-
}
136-
listeners.bgTaskReady(finalCourses);
137-
}
138-
139-
@Override
140-
public void bgTaskCancelled() {
141-
listeners.bgTaskCancelled();
142-
}
143-
144-
@Override
145-
public void bgTaskFailed(Throwable ex) {
146-
log.log(Level.INFO, "Failed to download current course info.", ex);
147-
listeners.bgTaskFailed(ex);
148-
}
149-
});
150-
} else {
151-
listeners.bgTaskReady(courses);
152-
}
153-
}
154-
155-
@Override
156-
public void bgTaskCancelled() {
157-
listeners.bgTaskCancelled();
158-
}
159-
160-
@Override
161-
public void bgTaskFailed(Throwable ex) {
162-
log.log(Level.INFO, "Failed to download course list.", ex);
163-
listeners.bgTaskFailed(ex);
164-
}
165-
});
166-
}*/
167-
168-
private class DefaultListener implements BgTaskListener<List<Course>> {
122+
// public void run() {
123+
// CancellableCallable<List<Course>> courseListTask = serverAccess.getDownloadingCourseListTask();
124+
// BgTask.start("Refreshing course list", courseListTask, new BgTaskListener<List<Course>>() {
125+
//
126+
// @Override
127+
// public void bgTaskReady(final List<Course> courses) {
128+
// Course currentCourseStub = CourseListUtils.getCourseByName(courses, courseDb.getCurrentCourseName());
129+
// // CurrentCourseStub on null jos on vanhentunut/ ei enää olemassa servulla
130+
// if (currentCourseStub != null) {
131+
// // Jos on olemassa servulla, päivitetään lokaali kurssi servun kurssia vastaavaksi
132+
// CancellableCallable<Course> currentCourseTask = serverAccess.getFullCourseInfoTask(currentCourseStub);
133+
//
134+
// BgTask.start("Loading course", currentCourseTask, new BgTaskListener<Course>() {
135+
// @Override
136+
// public void bgTaskReady(Course currentCourse) {
137+
// currentCourse.setExercisesLoaded(true);
138+
//
139+
// ArrayList<Course> finalCourses = new ArrayList<Course>();
140+
// for (Course course : courses) {
141+
// if (course.getName().equals(currentCourse.getName())) {
142+
// finalCourses.add(currentCourse);
143+
// } else {
144+
// finalCourses.add(course);
145+
// }
146+
// }
147+
// listeners.bgTaskReady(finalCourses);
148+
// }
149+
//
150+
// @Override
151+
// public void bgTaskCancelled() {
152+
// listeners.bgTaskCancelled();
153+
// }
154+
//
155+
// @Override
156+
// public void bgTaskFailed(Throwable ex) {
157+
// log.log(Level.INFO, "Failed to download current course info.", ex);
158+
// listeners.bgTaskFailed(ex);
159+
// }
160+
// });
161+
// } else {
162+
// listeners.bgTaskReady(courses);
163+
// }
164+
// }
165+
//
166+
// @Override
167+
// public void bgTaskCancelled() {
168+
// listeners.bgTaskCancelled();
169+
// }
170+
//
171+
// @Override
172+
// public void bgTaskFailed(Throwable ex) {
173+
// log.log(Level.INFO, "Failed to download course list.", ex);
174+
// listeners.bgTaskFailed(ex);
175+
// }
176+
// });
177+
// }
178+
179+
private class DefaultListener implements FutureCallback<List<Course>> {
169180

170181
private final boolean showDialogOnError;
171182
private final boolean updateCourseDb;
@@ -175,22 +186,36 @@ public DefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
175186
this.updateCourseDb = updateCourseDb;
176187
}
177188

189+
// @Override
190+
// public void bgTaskReady(List<Course> result) {
191+
// if (updateCourseDb) {
192+
// courseDb.setAvailableCourses(result);
193+
// }
194+
// }
195+
//
196+
// @Override
197+
// public void bgTaskCancelled() {
198+
// }
199+
//
200+
// @Override
201+
// public void bgTaskFailed(Throwable ex) {
202+
// if (showDialogOnError) {
203+
// dialogs.displayError("Course refresh failed.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
204+
// }
205+
// }
206+
178207
@Override
179-
public void bgTaskReady(List<Course> result) {
208+
public void onSuccess(List<Course> result) {
180209
if (updateCourseDb) {
181210
courseDb.setAvailableCourses(result);
182211
}
183212
}
184213

185214
@Override
186-
public void bgTaskCancelled() {
187-
}
188-
189-
@Override
190-
public void bgTaskFailed(Throwable ex) {
215+
public void onFailure(Throwable ex) {
191216
if (showDialogOnError) {
192217
dialogs.displayError("Course refresh failed.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
193218
}
194219
}
195220
}
196-
}
221+
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fi.helsinki.cs.tmc.actions;
22

3+
import com.google.common.util.concurrent.FutureCallback;
34
import hy.tmc.core.domain.Course;
45
import fi.helsinki.cs.tmc.model.CourseDb;
56
import fi.helsinki.cs.tmc.model.LocalExerciseStatus;
@@ -49,21 +50,17 @@ public void actionPerformed(ActionEvent e) {
4950

5051
RefreshCoursesAction refresh = new RefreshCoursesAction();
5152
refresh.addDefaultListener(true, true);
52-
refresh.addListener(new BgTaskListener<List<Course>>() {
53+
refresh.addListener(new FutureCallback<List<Course>>() {
5354
@Override
54-
public void bgTaskReady(List<Course> result) {
55+
public void onSuccess(List<Course> v) {
5556
LocalExerciseStatus status = LocalExerciseStatus.get(courseDb.getCurrentCourseExercises());
5657
if (status.thereIsSomethingToDownload(false)) {
5758
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
5859
}
5960
}
6061

6162
@Override
62-
public void bgTaskCancelled() {
63-
}
64-
65-
@Override
66-
public void bgTaskFailed(Throwable ex) {
63+
public void onFailure(Throwable thrwbl) {
6764
}
6865
});
6966
refresh.run();

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fi.helsinki.cs.tmc.actions;
22

3+
import com.google.common.util.concurrent.FutureCallback;
34
import hy.tmc.core.domain.Course;
45
import fi.helsinki.cs.tmc.model.CourseDb;
56
import fi.helsinki.cs.tmc.model.PushEventListener;
@@ -62,21 +63,17 @@ public void run() {
6263
LoginDialog.display(new CheckForNewExercisesOrUpdates(false, false));
6364
} else {
6465
// Do full refresh.
65-
new RefreshCoursesAction().addDefaultListener(false, true).addListener(new BgTaskListener<List<Course>>() {
66+
new RefreshCoursesAction().addDefaultListener(false, true).addListener(new FutureCallback<List<Course>>() {
6667
@Override
67-
public void bgTaskReady(List<Course> result) {
68+
public void onSuccess(List<Course> result) {
6869
new CheckForNewExercisesOrUpdates(true, false).run();
6970
if (CheckForUnopenedExercises.shouldRunOnStartup()) {
7071
new CheckForUnopenedExercises().run();
7172
}
7273
}
7374

7475
@Override
75-
public void bgTaskCancelled() {
76-
}
77-
78-
@Override
79-
public void bgTaskFailed(Throwable ex) {
76+
public void onFailure(Throwable thrwbl) {
8077
}
8178
}).run();
8279
}

tmc-plugin/src/fi/helsinki/cs/tmc/model/ServerAccess.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public CancellableCallable<Course> getFullCourseInfoTask(Course courseStub) {
137137
public Course call() throws Exception {
138138
try {
139139
String text = download.call();
140-
System.out.println(text);
141140
return courseInfoParser.parseFromJson(text);
142141
} catch (FailedHttpResponseException ex) {
143142
return checkForObsoleteClient(ex);

0 commit comments

Comments
 (0)