4
4
import com .google .common .util .concurrent .Futures ;
5
5
import com .google .common .util .concurrent .ListenableFuture ;
6
6
import hy .tmc .core .domain .Exercise ;
7
- import fi .helsinki .cs .tmc .model .CourseDb ;
8
7
import fi .helsinki .cs .tmc .model .NBTmcSettings ;
9
8
import fi .helsinki .cs .tmc .model .ProjectMediator ;
10
- import fi .helsinki .cs .tmc .model .ServerAccess ;
11
9
import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
12
10
import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
13
11
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 ;
18
12
import hy .tmc .core .TmcCore ;
19
13
import hy .tmc .core .exceptions .TmcCoreException ;
20
14
import java .util .ArrayList ;
21
- import java .util .Collection ;
22
15
import java .util .List ;
23
- import java .util .concurrent .Callable ;
24
16
import java .util .logging .Level ;
25
17
import java .util .logging .Logger ;
26
- import javax .swing .SwingUtilities ;
27
18
28
19
/**
29
20
* Downloads and opens the given exercises in the background.
@@ -32,8 +23,6 @@ public class DownloadExercisesAction {
32
23
33
24
private static final Logger logger = Logger .getLogger (DownloadExercisesAction .class .getName ());
34
25
35
- private ServerAccess serverAccess ;
36
- private CourseDb courseDb ;
37
26
private ProjectMediator projectMediator ;
38
27
private ConvenientDialogDisplayer dialogs ;
39
28
@@ -42,8 +31,6 @@ public class DownloadExercisesAction {
42
31
private NBTmcSettings settings ;
43
32
44
33
public DownloadExercisesAction (List <Exercise > exercisesToOpen ) {
45
- this .serverAccess = new ServerAccess ();
46
- this .courseDb = CourseDb .getInstance ();
47
34
this .projectMediator = ProjectMediator .getInstance ();
48
35
this .dialogs = ConvenientDialogDisplayer .getDefault ();
49
36
@@ -58,89 +45,31 @@ public void run() throws TmcCoreException {
58
45
59
46
ListenableFuture <List <Exercise >> dlFuture = tmcCore .donwloadExercises (exercisesToDownload , settings );
60
47
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 ());
85
49
}
86
50
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 >> {
98
52
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>>() {
129
53
@ 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
+ }
131
65
projectMediator .openProjects (projects );
132
66
}
133
67
134
68
@ 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 ));
137
72
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));
142
73
}
143
- };
144
- */
145
-
74
+ }
146
75
}
0 commit comments