Skip to content

Commit c4e85e5

Browse files
committed
merge
2 parents f66423b + 7616413 commit c4e85e5

File tree

10 files changed

+198
-129
lines changed

10 files changed

+198
-129
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: 84 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
import fi.helsinki.cs.tmc.model.ServerAccess;
1010
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
1111
import fi.helsinki.cs.tmc.model.NBTmcSettings;
12-
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
1312
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
14-
import fi.helsinki.cs.tmc.utilities.BgTask;
15-
import fi.helsinki.cs.tmc.utilities.BgTaskListenerList;
16-
import fi.helsinki.cs.tmc.utilities.CancellableCallable;
13+
import fi.helsinki.cs.tmc.utilities.FutureCallbackList;
1714
import hy.tmc.core.TmcCore;
18-
import hy.tmc.core.domain.Credentials;
1915
import hy.tmc.core.exceptions.TmcCoreException;
2016
import java.util.ArrayList;
2117
import java.util.List;
@@ -27,122 +23,125 @@
2723
* Refreshes the course list in the background.
2824
*/
2925
public final class RefreshCoursesAction {
26+
3027
private final static Logger log = Logger.getLogger(RefreshCoursesAction.class.getName());
3128

3229
private ServerAccess serverAccess;
3330
private CourseDb courseDb;
3431
private ConvenientDialogDisplayer dialogs;
35-
36-
private BgTaskListenerList<List<Course>> listeners;
32+
33+
private FutureCallbackList<List<Course>> callbacks;
3734
private final TmcCore tmcCore;
3835
private final NBTmcSettings tmcSettings;
3936

4037
public RefreshCoursesAction() {
4138
this(NBTmcSettings.getDefault());
4239
}
43-
40+
4441
public RefreshCoursesAction(NBTmcSettings settings) {
4542
this.tmcSettings = settings;
4643
this.serverAccess = new ServerAccess(settings);
4744
this.serverAccess.setSettings(settings);
4845
this.courseDb = CourseDb.getInstance();
4946
this.dialogs = ConvenientDialogDisplayer.getDefault();
5047

51-
this.listeners = new BgTaskListenerList<List<Course>>();
48+
//this.listeners = new BgTaskListenerList<List<Course>>();
49+
this.callbacks = new FutureCallbackList<List<Course>>();
5250
this.tmcCore = TmcCoreSingleton.getInstance();
5351
}
5452

5553
public RefreshCoursesAction addDefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
56-
this.listeners.addListener(new DefaultListener(showDialogOnError, updateCourseDb));
54+
this.callbacks.addListener(new DefaultListener(showDialogOnError, updateCourseDb));
5755
return this;
5856
}
5957

60-
public RefreshCoursesAction addListener(BgTaskListener<List<Course>> listener) {
61-
this.listeners.addListener(listener);
58+
public RefreshCoursesAction addListener(FutureCallback<List<Course>> callbacks) {
59+
this.callbacks.addListener(callbacks);
6260
return this;
6361
}
64-
65-
/*public void run() {
62+
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+
*/
68+
public void run() {
6669
try {
67-
Credentials credentials = new Credentials(this.tmcSettings.getUsername(),
68-
this.tmcSettings.getPassword()) {};
69-
System.out.println(credentials);
70-
ListenableFuture<Boolean> login = this.tmcCore.login(credentials, tmcSettings.getServerBaseUrl() + "/user");
71-
Futures.addCallback(login, new FutureCallback<Boolean>() {
72-
@Override
73-
public void onSuccess(Boolean v) {
74-
System.out.println("TOIMIIIII: " + v);
75-
}
76-
77-
@Override
78-
public void onFailure(Throwable thrwbl) {
79-
System.out.println("LOGIN LATAUS FEILASI1: " );
80-
Exceptions.printStackTrace(thrwbl);
81-
}
82-
});
70+
ListenableFuture<List<Course>> listCourses = this.tmcCore.listCourses(tmcSettings);
71+
Futures.addCallback(listCourses, new LoadCourses());
8372
} catch (TmcCoreException ex) {
8473
Exceptions.printStackTrace(ex);
74+
callbacks.onFailure(ex);
75+
}
76+
}
77+
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);
94+
callbacks.onFailure(ex);
95+
}
96+
} else {
97+
callbacks.onSuccess(courses);
98+
}
8599
}
86-
}*/
87100

88-
public void run() {
101+
@Override
102+
public void onFailure(Throwable ex) {
103+
log.log(Level.INFO, "Failed to download current course info.", ex);
104+
callbacks.onFailure(ex);
105+
}
106+
}
107+
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+
*/
112+
class UpdateCourse implements FutureCallback<Course> {
113+
List<Course> courses;
89114

90-
CancellableCallable<List<Course>> courseListTask = serverAccess.getDownloadingCourseListTask();
91-
BgTask.start("Refreshing course list", courseListTask, new BgTaskListener<List<Course>>() {
92-
93-
@Override
94-
public void bgTaskReady(final List<Course> courses) {
95-
Course currentCourseStub = CourseListUtils.getCourseByName(courses, courseDb.getCurrentCourseName());
96-
if (currentCourseStub != null) {
97-
CancellableCallable<Course> currentCourseTask = serverAccess.getFullCourseInfoTask(currentCourseStub);
98-
99-
BgTask.start("Loading course", currentCourseTask, new BgTaskListener<Course>() {
100-
@Override
101-
public void bgTaskReady(Course currentCourse) {
102-
currentCourse.setExercisesLoaded(true);
103-
104-
ArrayList<Course> finalCourses = new ArrayList<Course>();
105-
for (Course course : courses) {
106-
if (course.getName().equals(currentCourse.getName())) {
107-
finalCourses.add(currentCourse);
108-
} else {
109-
finalCourses.add(course);
110-
}
111-
}
112-
listeners.bgTaskReady(finalCourses);
113-
}
114-
115-
@Override
116-
public void bgTaskCancelled() {
117-
listeners.bgTaskCancelled();
118-
}
119-
120-
@Override
121-
public void bgTaskFailed(Throwable ex) {
122-
log.log(Level.INFO, "Failed to download current course info.", ex);
123-
listeners.bgTaskFailed(ex);
124-
}
125-
});
126-
115+
public UpdateCourse(List<Course> courses) {
116+
this.courses = courses;
117+
}
118+
119+
@Override
120+
public void onSuccess(Course detailedCourse) {
121+
detailedCourse.setExercisesLoaded(true);
122+
ArrayList<Course> finalCourses = new ArrayList<Course>();
123+
for (Course course : courses) {
124+
if (course.getName().equals(detailedCourse.getName())) {
125+
finalCourses.add(detailedCourse);
127126
} else {
128-
listeners.bgTaskReady(courses);
127+
finalCourses.add(course);
129128
}
130129
}
131-
132-
@Override
133-
public void bgTaskCancelled() {
134-
listeners.bgTaskCancelled();
135-
}
136-
137-
@Override
138-
public void bgTaskFailed(Throwable ex) {
139-
log.log(Level.INFO, "Failed to download course list.", ex);
140-
listeners.bgTaskFailed(ex);
141-
}
142-
});
130+
callbacks.onSuccess(finalCourses);
131+
}
132+
133+
@Override
134+
public void onFailure(Throwable ex) {
135+
log.log(Level.INFO, "Failed to download current course info.", ex);
136+
callbacks.onFailure(ex);
137+
}
143138
}
144139

145-
private class DefaultListener implements BgTaskListener<List<Course>> {
140+
/**
141+
* Updates the courseDb after all course-jsons are downloaded.
142+
*/
143+
private class DefaultListener implements FutureCallback<List<Course>> {
144+
146145
private final boolean showDialogOnError;
147146
private final boolean updateCourseDb;
148147

@@ -152,18 +151,14 @@ public DefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
152151
}
153152

154153
@Override
155-
public void bgTaskReady(List<Course> result) {
154+
public void onSuccess(List<Course> result) {
156155
if (updateCourseDb) {
157156
courseDb.setAvailableCourses(result);
158157
}
159158
}
160159

161160
@Override
162-
public void bgTaskCancelled() {
163-
}
164-
165-
@Override
166-
public void bgTaskFailed(Throwable ex) {
161+
public void onFailure(Throwable ex) {
167162
if (showDialogOnError) {
168163
dialogs.displayError("Course refresh failed.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
169164
}

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
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;
67
import fi.helsinki.cs.tmc.model.NBTmcSettings;
78
import fi.helsinki.cs.tmc.ui.PreferencesUI;
89
import fi.helsinki.cs.tmc.ui.DownloadOrUpdateExercisesDialog;
9-
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
10-
import hy.tmc.core.exceptions.TmcCoreException;
1110
import java.awt.event.ActionEvent;
1211
import java.util.List;
1312
import javax.swing.AbstractAction;
14-
import org.openide.util.Exceptions;
13+
import javax.swing.SwingUtilities;
1514

1615
public class SaveSettingsAction extends AbstractAction {
1716

@@ -49,26 +48,26 @@ public void actionPerformed(ActionEvent e) {
4948

5049
RefreshCoursesAction refresh = new RefreshCoursesAction();
5150
refresh.addDefaultListener(true, true);
52-
refresh.addListener(new BgTaskListener<List<Course>>() {
51+
refresh.addListener(new FutureCallback<List<Course>>() {
5352
@Override
54-
public void bgTaskReady(List<Course> result) {
55-
LocalExerciseStatus status = LocalExerciseStatus.get(courseDb.getCurrentCourseExercises());
56-
if (status.thereIsSomethingToDownload(false)) {
57-
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
58-
}
53+
public void onSuccess(List<Course> v) {
54+
SwingUtilities.invokeLater(new Runnable() {
55+
@Override
56+
public void run() {
57+
LocalExerciseStatus status = LocalExerciseStatus.get(courseDb.getCurrentCourseExercises());
58+
if (status.thereIsSomethingToDownload(false)) {
59+
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
60+
}
61+
}
62+
});
5963
}
6064

6165
@Override
62-
public void bgTaskCancelled() {
63-
}
64-
65-
@Override
66-
public void bgTaskFailed(Throwable ex) {
66+
public void onFailure(Throwable thrwbl) {
6767
}
6868
});
6969
refresh.run();
7070
}
71-
7271
settings.save();
7372
}
7473
}

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)