Skip to content

Commit f66423b

Browse files
committed
integrated downloading of exercises.
1 parent 8f29d68 commit f66423b

File tree

4 files changed

+22
-92
lines changed

4 files changed

+22
-92
lines changed

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

Lines changed: 17 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,17 @@
44
import com.google.common.util.concurrent.Futures;
55
import com.google.common.util.concurrent.ListenableFuture;
66
import hy.tmc.core.domain.Exercise;
7-
import fi.helsinki.cs.tmc.model.CourseDb;
87
import fi.helsinki.cs.tmc.model.NBTmcSettings;
98
import fi.helsinki.cs.tmc.model.ProjectMediator;
10-
import fi.helsinki.cs.tmc.model.ServerAccess;
119
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
1210
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
1311
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
14-
import fi.helsinki.cs.tmc.utilities.AggregatingBgTaskListener;
15-
import fi.helsinki.cs.tmc.utilities.BgTask;
16-
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
17-
import fi.helsinki.cs.tmc.utilities.zip.NbProjectUnzipper;
1812
import hy.tmc.core.TmcCore;
1913
import hy.tmc.core.exceptions.TmcCoreException;
2014
import java.util.ArrayList;
21-
import java.util.Collection;
2215
import java.util.List;
23-
import java.util.concurrent.Callable;
2416
import java.util.logging.Level;
2517
import java.util.logging.Logger;
26-
import javax.swing.SwingUtilities;
2718

2819
/**
2920
* Downloads and opens the given exercises in the background.
@@ -32,8 +23,6 @@ public class DownloadExercisesAction {
3223

3324
private static final Logger logger = Logger.getLogger(DownloadExercisesAction.class.getName());
3425

35-
private ServerAccess serverAccess;
36-
private CourseDb courseDb;
3726
private ProjectMediator projectMediator;
3827
private ConvenientDialogDisplayer dialogs;
3928

@@ -42,8 +31,6 @@ public class DownloadExercisesAction {
4231
private NBTmcSettings settings;
4332

4433
public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
45-
this.serverAccess = new ServerAccess();
46-
this.courseDb = CourseDb.getInstance();
4734
this.projectMediator = ProjectMediator.getInstance();
4835
this.dialogs = ConvenientDialogDisplayer.getDefault();
4936

@@ -58,89 +45,31 @@ public void run() throws TmcCoreException {
5845

5946
ListenableFuture<List<Exercise>> dlFuture = tmcCore.donwloadExercises(exercisesToDownload, settings);
6047

61-
Futures.addCallback(dlFuture,
62-
new FutureCallback<List<Exercise>>() {
63-
64-
@Override
65-
public void onSuccess(List<Exercise> downloadedExercises) {
66-
List<TmcProjectInfo> projects = new ArrayList<TmcProjectInfo>();
67-
for (Exercise exercise : downloadedExercises) {
68-
projects.add(projectMediator.tryGetProjectForExercise(exercise));
69-
}
70-
projectMediator.openProjects(projects);
71-
}
72-
73-
@Override
74-
public void onFailure(Throwable thrwbl) {
75-
logger.log(Level.INFO, "Failed to download exercise file.", thrwbl);
76-
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(thrwbl));
77-
78-
}
79-
}
80-
);
81-
82-
//for (final Exercise exercise : exercisesToDownload) {
83-
// startDownloading(exercise, aggregator);
84-
//}
48+
Futures.addCallback(dlFuture, new ProjectOpener());
8549
}
8650

87-
/*
88-
private void startDownloading(final Exercise exercise, final BgTaskListener<TmcProjectInfo> listener) {
89-
BgTask.start("Downloading " + exercise.getName(), serverAccess.getDownloadingExerciseZipTask(exercise), new BgTaskListener<byte[]>() {
90-
@Override
91-
public void bgTaskReady(final byte[] zipData) {
92-
BgTask.start("Extracting project", new Callable<TmcProjectInfo>() {
93-
@Override
94-
public TmcProjectInfo call() throws Exception {
95-
NbProjectUnzipper unzipper = new NbProjectUnzipper();
96-
unzipper.unzipProject(zipData, projectMediator.getProjectDirForExercise(exercise));
97-
TmcProjectInfo proj = projectMediator.tryGetProjectForExercise(exercise);
51+
private class ProjectOpener implements FutureCallback<List<Exercise>> {
9852

99-
if (proj == null) {
100-
throw new RuntimeException("Failed to open project for exercise " + exercise.getName());
101-
}
102-
103-
// Need to invoke courseDb in swing thread to avoid races
104-
SwingUtilities.invokeAndWait(new Runnable() {
105-
@Override
106-
public void run() {
107-
courseDb.exerciseDownloaded(exercise);
108-
}
109-
});
110-
111-
return proj;
112-
}
113-
}, listener);
114-
}
115-
116-
@Override
117-
public void bgTaskCancelled() {
118-
listener.bgTaskCancelled();
119-
}
120-
121-
@Override
122-
public void bgTaskFailed(Throwable ex) {
123-
listener.bgTaskFailed(ex);
124-
}
125-
});
126-
}
127-
/*
128-
private BgTaskListener<Collection<TmcProjectInfo>> whenAllDownloadsFinished = new BgTaskListener<Collection<TmcProjectInfo>>() {
12953
@Override
130-
public void bgTaskReady(Collection<TmcProjectInfo> projects) {
54+
public void onSuccess(List<Exercise> downloadedExercises) {
55+
List<TmcProjectInfo> projects = new ArrayList<TmcProjectInfo>();
56+
for (Exercise exercise : downloadedExercises) {
57+
TmcProjectInfo info = projectMediator.tryGetProjectForExercise(exercise);
58+
if (info == null) {
59+
dialogs.displayError(
60+
"Could not find exercise " + exercise.getName() + " from the filesystem");
61+
continue;
62+
}
63+
projects.add(info);
64+
}
13165
projectMediator.openProjects(projects);
13266
}
13367

13468
@Override
135-
public void bgTaskCancelled() {
136-
}
69+
public void onFailure(Throwable thrwbl) {
70+
logger.log(Level.INFO, "Failed to download exercise file.", thrwbl);
71+
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(thrwbl));
13772

138-
@Override
139-
public void bgTaskFailed(Throwable ex) {
140-
logger.log(Level.INFO, "Failed to download exercise file.", ex);
141-
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
14273
}
143-
};
144-
*/
145-
74+
}
14675
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void run() {
5555
prefUI.setPassword(settings.getPassword());
5656
prefUI.setShouldSavePassword(settings.isSavingPassword());
5757
prefUI.setServerBaseUrl(settings.getServerAddress());
58-
prefUI.setProjectDir(settings.getProjectRootDir());
58+
prefUI.setProjectDir(settings.getTmcMainDirectory());
5959
prefUI.setCheckForUpdatesInTheBackground(settings.isCheckingForUpdatesInTheBackground());
6060
prefUI.setCheckForUnopenedExercisesAtStartup(settings.isCheckingForUnopenedAtStartup());
6161
prefUI.setAvailableCourses(courseDb.getAvailableCourses());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public boolean isSavingPassword() {
142142
return settings.get(PREF_PASSWORD, null) != null;
143143
}
144144

145-
public String getProjectRootDir() {
145+
@Override
146+
public String getTmcMainDirectory() {
146147
String path = settings.get(PREF_PROJECT_ROOT_DIR, null);
147148
if (path != null) {
148149
return path;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public List<TmcProjectInfo> wrapProjects(List<Project> projects) {
6363
}
6464

6565
public String getProjectRootDir() {
66-
return NBTmcSettings.getDefault().getProjectRootDir();
66+
return NBTmcSettings.getDefault().getTmcMainDirectory();
6767
}
6868

6969
public static String getDefaultProjectRootDir() {
@@ -115,7 +115,7 @@ public File getProjectDirForExercise(Exercise ex) {
115115
String path =
116116
getProjectRootDir() + File.separator +
117117
ex.getCourseName() + File.separator +
118-
ex.getName().replaceAll("/", "-");
118+
ex.getName().replaceAll("-", "/");
119119
return new File(path);
120120
}
121121

0 commit comments

Comments
 (0)