Skip to content

Commit 8f29d68

Browse files
committed
using tmcCore should work now, unless AWT thread cause trouble
1 parent 38a30f6 commit 8f29d68

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
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;
47
import fi.helsinki.cs.tmc.model.CourseDb;
8+
import fi.helsinki.cs.tmc.model.NBTmcSettings;
59
import fi.helsinki.cs.tmc.model.ProjectMediator;
610
import fi.helsinki.cs.tmc.model.ServerAccess;
11+
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
712
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
813
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
914
import fi.helsinki.cs.tmc.utilities.AggregatingBgTaskListener;
1015
import fi.helsinki.cs.tmc.utilities.BgTask;
1116
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
1217
import fi.helsinki.cs.tmc.utilities.zip.NbProjectUnzipper;
18+
import hy.tmc.core.TmcCore;
19+
import hy.tmc.core.exceptions.TmcCoreException;
20+
import java.util.ArrayList;
1321
import java.util.Collection;
1422
import java.util.List;
1523
import java.util.concurrent.Callable;
@@ -21,6 +29,7 @@
2129
* Downloads and opens the given exercises in the background.
2230
*/
2331
public class DownloadExercisesAction {
32+
2433
private static final Logger logger = Logger.getLogger(DownloadExercisesAction.class.getName());
2534

2635
private ServerAccess serverAccess;
@@ -29,6 +38,8 @@ public class DownloadExercisesAction {
2938
private ConvenientDialogDisplayer dialogs;
3039

3140
private List<Exercise> exercisesToDownload;
41+
private TmcCore tmcCore;
42+
private NBTmcSettings settings;
3243

3344
public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
3445
this.serverAccess = new ServerAccess();
@@ -37,17 +48,43 @@ public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
3748
this.dialogs = ConvenientDialogDisplayer.getDefault();
3849

3950
this.exercisesToDownload = exercisesToOpen;
51+
this.tmcCore = TmcCoreSingleton.getInstance();
52+
this.settings = NBTmcSettings.getDefault();
4053
}
4154

42-
public void run() {
43-
final AggregatingBgTaskListener<TmcProjectInfo> aggregator =
44-
new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished);
55+
public void run() throws TmcCoreException {
56+
// final AggregatingBgTaskListener<TmcProjectInfo> aggregator
57+
// = new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished);
4558

46-
for (final Exercise exercise : exercisesToDownload) {
47-
startDownloading(exercise, aggregator);
48-
}
59+
ListenableFuture<List<Exercise>> dlFuture = tmcCore.donwloadExercises(exercisesToDownload, settings);
60+
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+
//}
4985
}
5086

87+
/*
5188
private void startDownloading(final Exercise exercise, final BgTaskListener<TmcProjectInfo> listener) {
5289
BgTask.start("Downloading " + exercise.getName(), serverAccess.getDownloadingExerciseZipTask(exercise), new BgTaskListener<byte[]>() {
5390
@Override
@@ -75,7 +112,7 @@ public void run() {
75112
}
76113
}, listener);
77114
}
78-
115+
79116
@Override
80117
public void bgTaskCancelled() {
81118
listener.bgTaskCancelled();
@@ -87,7 +124,7 @@ public void bgTaskFailed(Throwable ex) {
87124
}
88125
});
89126
}
90-
127+
/*
91128
private BgTaskListener<Collection<TmcProjectInfo>> whenAllDownloadsFinished = new BgTaskListener<Collection<TmcProjectInfo>>() {
92129
@Override
93130
public void bgTaskReady(Collection<TmcProjectInfo> projects) {
@@ -104,4 +141,6 @@ public void bgTaskFailed(Throwable ex) {
104141
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
105142
}
106143
};
144+
*/
145+
107146
}

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

Lines changed: 1 addition & 1 deletion
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 TmcSettings.getDefault().getProjectRootDir();
66+
return NBTmcSettings.getDefault().getProjectRootDir();
6767
}
6868

6969
public static String getDefaultProjectRootDir() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</Properties>
1010
<SyntheticProperties>
1111
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
12+
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
1213
</SyntheticProperties>
1314
<AuxValues>
1415
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="1"/>

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;
@@ -13,6 +15,7 @@
1315
import java.util.List;
1416
import javax.swing.JCheckBox;
1517
import javax.swing.JDialog;
18+
import org.openide.util.Exceptions;
1619
import org.openide.windows.WindowManager;
1720

1821
public class DownloadOrUpdateExercisesDialog extends JDialog {
@@ -117,8 +120,14 @@ private void updateSelectAllButtonState() {
117120
}
118121

119122
private void doDownloadAndUpdate(List<Exercise> toDownload, List<Exercise> toUpdate) {
120-
new DownloadExercisesAction(toDownload).run();
121-
new UpdateExercisesAction(toUpdate).run();
123+
try {
124+
new DownloadExercisesAction(toDownload).run();
125+
new UpdateExercisesAction(toUpdate).run();
126+
} catch (TmcCoreException ex) {
127+
Exceptions.printStackTrace(ex);
128+
ConvenientDialogDisplayer.getDefault()
129+
.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
130+
}
122131
}
123132

124133
/** This method is called from within the constructor to

0 commit comments

Comments
 (0)