Skip to content

Commit 264c301

Browse files
committed
Add progress outputs.
1 parent 88fd961 commit 264c301

File tree

9 files changed

+91
-123
lines changed

9 files changed

+91
-123
lines changed

tmc-application/manifest.mf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ OpenIDE-Module: fi.helsinki.cs.tmc.application
44
OpenIDE-Module-Localizing-Bundle: fi/helsinki/cs/tmc/application/Bundle.properties
55
OpenIDE-Module-Specification-Version: 1.0
66
OpenIDE-Module-Install: fi/helsinki/cs/tmc/application/TmcAppModuleInstall.class
7+
OpenIDE-Module-Needs: org.openide.filesystems.FileUtil.toFileObject

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import javax.swing.AbstractAction;
2424
import javax.swing.Icon;
2525
import org.apache.commons.lang3.StringUtils;
26+
import org.netbeans.api.progress.ProgressHandle;
27+
import org.netbeans.api.progress.ProgressHandleFactory;
2628
import org.openide.awt.ActionID;
2729
import org.openide.awt.ActionReference;
2830
import org.openide.awt.ActionReferences;
@@ -50,7 +52,6 @@ public static void startTimer() {
5052
private static final TmcNotificationDisplayer.SingletonToken notifierToken = TmcNotificationDisplayer.createSingletonToken();
5153

5254
private CourseDb courseDb;
53-
private ServerAccess serverAccess;
5455
private TmcNotificationDisplayer notifier;
5556
private ConvenientDialogDisplayer dialogs;
5657
private boolean beQuiet;
@@ -63,7 +64,6 @@ public CheckForNewExercisesOrUpdates() {
6364

6465
public CheckForNewExercisesOrUpdates(boolean beQuiet, boolean backgroundCheck) {
6566
this.courseDb = CourseDb.getInstance();
66-
this.serverAccess = new ServerAccess();
6767
this.notifier = TmcNotificationDisplayer.getDefault();
6868
this.dialogs = ConvenientDialogDisplayer.getDefault();
6969
this.beQuiet = beQuiet;
@@ -76,20 +76,19 @@ public void actionPerformed(ActionEvent e) {
7676
run();
7777
}
7878

79-
//"Checking for new exercises" päivitä tämä näkymään progressHandleen myöhemmin
80-
/**
81-
*
82-
*/
8379
public void run() {
8480
try {
81+
ProgressHandle exerciseRefresh = ProgressHandleFactory.createSystemHandle(
82+
"Checking for new exercises");
83+
exerciseRefresh.start();
8584
final Course currentCourseBeforeUpdate = courseDb.getCurrentCourse();
8685
if (backgroundProcessingOrNoCurrentCourse(currentCourseBeforeUpdate)) {
8786
return;
8887
}
8988
ListenableFuture<Course> currentCourseFuture = this.tmcCore.getCourse(
9089
NBTmcSettings.getDefault(), currentCourseBeforeUpdate.getDetailsUrl()
9190
);
92-
Futures.addCallback(currentCourseFuture, new UpdateCourseForExerciseUpdate());
91+
Futures.addCallback(currentCourseFuture, new UpdateCourseForExerciseUpdate(exerciseRefresh));
9392
} catch (TmcCoreException ex) {
9493
Exceptions.printStackTrace(ex);
9594
}
@@ -113,16 +112,20 @@ private boolean backgroundProcessingOrNoCurrentCourse(final Course currentCourse
113112
}
114113

115114
class UpdateCourseForExerciseUpdate implements FutureCallback<Course> {
115+
116+
private ProgressHandle lastAction;
116117

117118
/**
118119
* This should be attached to listenableFuture. When future is ready,
119120
* receivedCourse will be saved to courseDb and view will be updated.
120121
*/
121-
public UpdateCourseForExerciseUpdate() {
122+
public UpdateCourseForExerciseUpdate(ProgressHandle lastAction) {
123+
this.lastAction = lastAction;
122124
}
123125

124126
@Override
125127
public void onSuccess(Course receivedCourse) {
128+
lastAction.finish();
126129
if (receivedCourse != null) {
127130
courseDb.putDetailedCourse(receivedCourse);
128131
final LocalExerciseStatus status = LocalExerciseStatus.get(receivedCourse.getExercises());
@@ -131,7 +134,9 @@ public void onSuccess(Course receivedCourse) {
131134
}
132135

133136
private void updateGUI(final LocalExerciseStatus status) {
134-
if (status.thereIsSomethingToDownload(false)) {
137+
boolean thereIsSomethingToDownload = status.thereIsSomethingToDownload(false);
138+
System.out.println("on ladattavaa: " + thereIsSomethingToDownload);
139+
if (thereIsSomethingToDownload) {
135140
if (beQuiet) {
136141
displayNotification(status, new ActionListener() {
137142
@Override
@@ -149,6 +154,7 @@ public void actionPerformed(ActionEvent e) {
149154

150155
@Override
151156
public void onFailure(Throwable ex) {
157+
lastAction.finish();
152158
if (!beQuiet || ex instanceof ObsoleteClientException) {
153159
dialogs.displayError("Failed to check for new exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
154160
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.List;
1616
import java.util.logging.Level;
1717
import java.util.logging.Logger;
18+
import org.netbeans.api.progress.ProgressHandle;
19+
import org.netbeans.api.progress.ProgressHandleFactory;
1820

1921
/**
2022
* Downloads and opens the given exercises in the background.
@@ -43,15 +45,24 @@ public void run() throws TmcCoreException {
4345
// final AggregatingBgTaskListener<TmcProjectInfo> aggregator
4446
// = new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished);
4547

48+
ProgressHandle exerciseDownload = ProgressHandleFactory.createSystemHandle(
49+
"Downloading " + exercisesToDownload.size() + " exercises.");
50+
exerciseDownload.start();
4651
ListenableFuture<List<Exercise>> dlFuture = tmcCore.downloadExercises(exercisesToDownload, settings);
4752

48-
Futures.addCallback(dlFuture, new ProjectOpener());
53+
Futures.addCallback(dlFuture, new ProjectOpener(exerciseDownload));
4954
}
5055

5156
private class ProjectOpener implements FutureCallback<List<Exercise>> {
57+
private ProgressHandle lastAction;
5258

59+
public ProjectOpener(ProgressHandle lastAction) {
60+
this.lastAction = lastAction;
61+
}
62+
5363
@Override
5464
public void onSuccess(List<Exercise> downloadedExercises) {
65+
lastAction.finish();
5566
List<TmcProjectInfo> projects = new ArrayList<TmcProjectInfo>();
5667
for (Exercise exercise : downloadedExercises) {
5768
TmcProjectInfo info = projectMediator.tryGetProjectForExercise(exercise);
@@ -65,6 +76,7 @@ public void onSuccess(List<Exercise> downloadedExercises) {
6576

6677
@Override
6778
public void onFailure(Throwable thrwbl) {
79+
lastAction.finish();
6880
logger.log(Level.INFO, "Failed to download exercise file.", thrwbl);
6981
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(thrwbl));
7082

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

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.List;
1818
import java.util.logging.Level;
1919
import java.util.logging.Logger;
20+
import org.netbeans.api.progress.ProgressHandle;
21+
import org.netbeans.api.progress.ProgressHandleFactory;
2022
import org.openide.util.Exceptions;
2123

2224
/**
@@ -37,14 +39,14 @@ public final class RefreshCoursesAction {
3739
public RefreshCoursesAction() {
3840
this(NBTmcSettings.getDefault());
3941
}
40-
42+
4143
/**
4244
* Default constructor.
4345
*/
4446
public RefreshCoursesAction(NBTmcSettings settings) {
4547
this(settings, TmcCoreSingleton.getInstance());
4648
}
47-
49+
4850
/**
4951
* Dependency inject TmcCore for tests.
5052
*/
@@ -69,34 +71,54 @@ public RefreshCoursesAction addListener(FutureCallback<List<Course>> callback) {
6971
}
7072

7173
/**
72-
* Starts downloading course-jsons from TMC-server.
73-
* Url of TMC-server is defined in TmcSettings object.
74-
* TmcCore includes all logic, callbacks here are run after core-futures are ready.
74+
* Starts downloading course-jsons from TMC-server. Url of TMC-server is
75+
* defined in TmcSettings object. TmcCore includes all logic, callbacks here
76+
* are run after core-futures are ready.
7577
*/
7678
public void run() {
7779
try {
80+
ProgressHandle courseRefresh = ProgressHandleFactory.createSystemHandle(
81+
"Refreshing course list");
82+
courseRefresh.start();
7883
ListenableFuture<List<Course>> listCourses = this.tmcCore.listCourses(tmcSettings);
79-
Futures.addCallback(listCourses, new LoadCourses());
84+
Futures.addCallback(listCourses, new LoadCourses(courseRefresh));
8085
} catch (TmcCoreException ex) {
8186
Exceptions.printStackTrace(ex);
8287
callbacks.onFailure(ex);
8388
}
8489
}
8590

86-
/**
87-
* This callBack is run when ListenableFuture (to witch this is attached) is done.
88-
* On success method takes list of Course-objects, searches the current course and starts uploading
89-
* the details of the course.
90-
* If no currentCourse found, no need to update details.
91-
*/
9291
class LoadCourses implements FutureCallback<List<Course>> {
92+
93+
private ProgressHandle lastAction;
94+
95+
/**
96+
* This callBack is run when ListenableFuture (to witch this is
97+
* attached) is done. On success method takes list of Course-objects,
98+
* searches the current course and starts uploading the details of the
99+
* course. If no currentCourse found, no need to update details.
100+
*
101+
* @param ProgressHandle shows to user that action is processing.
102+
*/
103+
public LoadCourses(ProgressHandle lastAction) {
104+
this.lastAction = lastAction;
105+
}
106+
93107
@Override
94108
public void onSuccess(final List<Course> courses) {
95-
Course currentCourse = CourseListUtils.getCourseByName(courses, courseDb.getCurrentCourseName());
109+
lastAction.finish();
110+
Course currentCourse = CourseListUtils.getCourseByName(
111+
courses, courseDb.getCurrentCourseName()
112+
);
96113
if (currentCourse != null) {
97114
try {
98-
ListenableFuture<Course> courseFuture = tmcCore.getCourse(tmcSettings, currentCourse.getDetailsUrl());
99-
Futures.addCallback(courseFuture, new UpdateCourse(courses));
115+
ProgressHandle loadingCourse = ProgressHandleFactory.
116+
createSystemHandle("Loading course");
117+
loadingCourse.start();
118+
ListenableFuture<Course> courseFuture = tmcCore.getCourse(
119+
tmcSettings, currentCourse.getDetailsUrl()
120+
);
121+
Futures.addCallback(courseFuture, new UpdateCourse(courses, loadingCourse));
100122
} catch (TmcCoreException ex) {
101123
Exceptions.printStackTrace(ex);
102124
callbacks.onFailure(ex);
@@ -108,24 +130,30 @@ public void onSuccess(final List<Course> courses) {
108130

109131
@Override
110132
public void onFailure(Throwable ex) {
133+
lastAction.finish();
111134
log.log(Level.INFO, "Failed to download current course info.", ex);
112135
callbacks.onFailure(ex);
113136
}
114137
}
115138

116139
/**
117-
* When detailed current course is present, courses will be given to FutureCallbackList,
118-
* that shares the result to every callback that is attached to that list.
140+
* When detailed current course is present, courses will be given to
141+
* FutureCallbackList, that shares the result to every callback that is
142+
* attached to that list.
119143
*/
120144
class UpdateCourse implements FutureCallback<Course> {
121-
List<Course> courses;
122-
123-
public UpdateCourse(List<Course> courses) {
145+
146+
private List<Course> courses;
147+
private ProgressHandle lastAction;
148+
149+
public UpdateCourse(List<Course> courses, ProgressHandle lastAction) {
124150
this.courses = courses;
151+
this.lastAction = lastAction;
125152
}
126153

127154
@Override
128155
public void onSuccess(Course detailedCourse) {
156+
lastAction.finish();
129157
detailedCourse.setExercisesLoaded(true);
130158
ArrayList<Course> finalCourses = new ArrayList<Course>();
131159
for (Course course : courses) {
@@ -140,6 +168,7 @@ public void onSuccess(Course detailedCourse) {
140168

141169
@Override
142170
public void onFailure(Throwable ex) {
171+
lastAction.finish();
143172
log.log(Level.INFO, "Failed to download current course info.", ex);
144173
callbacks.onFailure(ex);
145174
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void putDetailedCourse(Course course) {
8686
if (availableCourses.get(i).getName().equals(course.getName())) {
8787
availableCourses.set(i, course);
8888
save();
89-
break;
89+
return;
9090
}
9191
}
9292
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ private LocalExerciseStatus(CourseDb courseDb, ProjectMediator projectMediator,
3131

3232
for (Exercise ex : allExercises) {
3333
if (!ex.hasDeadlinePassed()) {
34+
System.out.println("Exercise: " + ex);
3435
TmcProjectInfo proj = projectMediator.tryGetProjectForExercise(ex);
3536
boolean isDownloaded = proj != null;
3637
if (courseDb.isUnlockable(ex)) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public File getProjectDirForExercise(Exercise ex) {
116116
getProjectRootDir() + File.separator +
117117
ex.getCourseName() + File.separator +
118118
ex.getName().replaceAll("-", "/");
119-
return new File(path);
119+
File file = new File(path);
120+
return FileUtil.normalizeFile(file);
120121
}
121122

122123
/**
@@ -160,12 +161,16 @@ public TmcProjectInfo tryGetProjectOwningFile(FileObject fo) {
160161
*/
161162
public TmcProjectInfo tryGetProjectForExercise(Exercise exercise) {
162163
projectManager.clearNonProjectCache(); // Just to be sure.
164+
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
163165

164166
File path = getProjectDirForExercise(exercise);
165167
FileObject fo = FileUtil.toFileObject(path);
168+
System.out.println("PATH: " + path);
169+
System.out.println("FIlEOBJECT: " + fo);
166170
if (fo != null) {
167171
try {
168172
Project project = projectManager.findProject(fo);
173+
System.out.println("PROJECT: " + project);
169174
if (project != null) {
170175
return wrapProject(project);
171176
} else {
@@ -180,6 +185,9 @@ public TmcProjectInfo tryGetProjectForExercise(Exercise exercise) {
180185
return null;
181186
}
182187
} else {
188+
logger.log(Level.WARNING, "ProjectMediator failed to convert "
189+
+ "File to org.openide.filesystems.FileObject. Check "
190+
+ "http://bits.netbeans.org/7.4/javadoc/org-openide-filesystems/org/openide/filesystems/FileUtil.html#toFileObject(java.io.File)");
183191
return null;
184192
}
185193
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public boolean needsOnlyPassword() {
109109
!settings.getServerAddress().isEmpty();
110110
}
111111

112+
@Deprecated
112113
public CancellableCallable<List<Course>> getDownloadingCourseListTask() {
113114
final CancellableCallable<String> download = createHttpTasks().getForText(getCourseListUrl());
114115
return new CancellableCallable<List<Course>>() {
@@ -129,6 +130,7 @@ public boolean cancel() {
129130
};
130131
}
131132

133+
@Deprecated
132134
public CancellableCallable<Course> getFullCourseInfoTask(Course courseStub) {
133135
String url = addApiCallQueryParameters(courseStub.getDetailsUrl());
134136
final CancellableCallable<String> download = createHttpTasks().getForText(url);

0 commit comments

Comments
 (0)