Skip to content

Commit 1bd2088

Browse files
committed
Merge branch 'core_integration' of github.com:rage/tmc-netbeans into core_integration
Conflicts: tmc-plugin/src/fi/helsinki/cs/tmc/ui/DownloadOrUpdateExercisesDialog.java
2 parents a8d7614 + 6751d9e commit 1bd2088

File tree

3 files changed

+47
-70
lines changed

3 files changed

+47
-70
lines changed
Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,75 @@
11
package fi.helsinki.cs.tmc.actions;
22

3+
import com.google.common.util.concurrent.FutureCallback;
4+
import com.google.common.util.concurrent.Futures;
5+
import com.google.common.util.concurrent.ListenableFuture;
36
import hy.tmc.core.domain.Exercise;
4-
import fi.helsinki.cs.tmc.model.CourseDb;
7+
import fi.helsinki.cs.tmc.model.NBTmcSettings;
58
import fi.helsinki.cs.tmc.model.ProjectMediator;
6-
import fi.helsinki.cs.tmc.model.ServerAccess;
9+
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
710
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
811
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
9-
import fi.helsinki.cs.tmc.utilities.AggregatingBgTaskListener;
10-
import fi.helsinki.cs.tmc.utilities.BgTask;
11-
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
12-
import fi.helsinki.cs.tmc.utilities.zip.NbProjectUnzipper;
13-
import java.util.Collection;
12+
import hy.tmc.core.TmcCore;
13+
import hy.tmc.core.exceptions.TmcCoreException;
14+
import java.util.ArrayList;
1415
import java.util.List;
15-
import java.util.concurrent.Callable;
1616
import java.util.logging.Level;
1717
import java.util.logging.Logger;
18-
import javax.swing.SwingUtilities;
1918

2019
/**
2120
* Downloads and opens the given exercises in the background.
2221
*/
2322
public class DownloadExercisesAction {
23+
2424
private static final Logger logger = Logger.getLogger(DownloadExercisesAction.class.getName());
2525

26-
private ServerAccess serverAccess;
27-
private CourseDb courseDb;
2826
private ProjectMediator projectMediator;
2927
private ConvenientDialogDisplayer dialogs;
3028

3129
private List<Exercise> exercisesToDownload;
30+
private TmcCore tmcCore;
31+
private NBTmcSettings settings;
3232

3333
public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
34-
this.serverAccess = new ServerAccess();
35-
this.courseDb = CourseDb.getInstance();
3634
this.projectMediator = ProjectMediator.getInstance();
3735
this.dialogs = ConvenientDialogDisplayer.getDefault();
3836

3937
this.exercisesToDownload = exercisesToOpen;
38+
this.tmcCore = TmcCoreSingleton.getInstance();
39+
this.settings = NBTmcSettings.getDefault();
4040
}
4141

42-
public void run() {
43-
final AggregatingBgTaskListener<TmcProjectInfo> aggregator =
44-
new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished);
45-
46-
for (final Exercise exercise : exercisesToDownload) {
47-
startDownloading(exercise, aggregator);
48-
}
49-
}
50-
51-
private void startDownloading(final Exercise exercise, final BgTaskListener<TmcProjectInfo> listener) {
52-
BgTask.start("Downloading " + exercise.getName(), serverAccess.getDownloadingExerciseZipTask(exercise), new BgTaskListener<byte[]>() {
53-
@Override
54-
public void bgTaskReady(final byte[] zipData) {
55-
BgTask.start("Extracting project", new Callable<TmcProjectInfo>() {
56-
@Override
57-
public TmcProjectInfo call() throws Exception {
58-
NbProjectUnzipper unzipper = new NbProjectUnzipper();
59-
unzipper.unzipProject(zipData, projectMediator.getProjectDirForExercise(exercise));
60-
TmcProjectInfo proj = projectMediator.tryGetProjectForExercise(exercise);
61-
62-
if (proj == null) {
63-
throw new RuntimeException("Failed to open project for exercise " + exercise.getName());
64-
}
42+
public void run() throws TmcCoreException {
43+
// final AggregatingBgTaskListener<TmcProjectInfo> aggregator
44+
// = new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished);
6545

66-
// Need to invoke courseDb in swing thread to avoid races
67-
SwingUtilities.invokeAndWait(new Runnable() {
68-
@Override
69-
public void run() {
70-
courseDb.exerciseDownloaded(exercise);
71-
}
72-
});
46+
ListenableFuture<List<Exercise>> dlFuture = tmcCore.downloadExercises(exercisesToDownload, settings);
7347

74-
return proj;
75-
}
76-
}, listener);
77-
}
78-
79-
@Override
80-
public void bgTaskCancelled() {
81-
listener.bgTaskCancelled();
82-
}
83-
84-
@Override
85-
public void bgTaskFailed(Throwable ex) {
86-
listener.bgTaskFailed(ex);
87-
}
88-
});
48+
Futures.addCallback(dlFuture, new ProjectOpener());
8949
}
9050

91-
private BgTaskListener<Collection<TmcProjectInfo>> whenAllDownloadsFinished = new BgTaskListener<Collection<TmcProjectInfo>>() {
51+
private class ProjectOpener implements FutureCallback<List<Exercise>> {
52+
9253
@Override
93-
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+
}
9465
projectMediator.openProjects(projects);
9566
}
9667

9768
@Override
98-
public void bgTaskCancelled() {
99-
}
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));
10072

101-
@Override
102-
public void bgTaskFailed(Throwable ex) {
103-
logger.log(Level.INFO, "Failed to download exercise file.", ex);
104-
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
10573
}
106-
};
74+
}
10775
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

tmc-plugin/src/fi/helsinki/cs/tmc/ui/DownloadOrUpdateExercisesDialog.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package fi.helsinki.cs.tmc.ui;
22

33
import fi.helsinki.cs.tmc.actions.DownloadExercisesAction;
4+
import fi.helsinki.cs.tmc.actions.ServerErrorHelper;
45
import fi.helsinki.cs.tmc.actions.UnlockExercisesAction;
56
import fi.helsinki.cs.tmc.actions.UpdateExercisesAction;
67
import hy.tmc.core.domain.Exercise;
8+
import hy.tmc.core.exceptions.TmcCoreException;
79
import java.awt.event.ActionEvent;
810
import java.awt.event.ActionListener;
911
import java.awt.event.ItemEvent;
@@ -14,6 +16,7 @@
1416
import javax.swing.JCheckBox;
1517
import javax.swing.JDialog;
1618
import javax.swing.SwingUtilities;
19+
import org.openide.util.Exceptions;
1720
import org.openide.windows.WindowManager;
1821

1922
public class DownloadOrUpdateExercisesDialog extends JDialog {
@@ -123,8 +126,14 @@ private void updateSelectAllButtonState() {
123126
}
124127

125128
private void doDownloadAndUpdate(List<Exercise> toDownload, List<Exercise> toUpdate) {
126-
new DownloadExercisesAction(toDownload).run();
127-
new UpdateExercisesAction(toUpdate).run();
129+
try {
130+
new DownloadExercisesAction(toDownload).run();
131+
new UpdateExercisesAction(toUpdate).run();
132+
} catch (TmcCoreException ex) {
133+
Exceptions.printStackTrace(ex);
134+
ConvenientDialogDisplayer.getDefault()
135+
.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
136+
}
128137
}
129138

130139
/**

0 commit comments

Comments
 (0)