3
3
import fi .helsinki .cs .tmc .core .domain .Exercise ;
4
4
import fi .helsinki .cs .tmc .model .CourseDb ;
5
5
import fi .helsinki .cs .tmc .model .ProjectMediator ;
6
- import fi .helsinki .cs .tmc .model .ServerAccess ;
7
6
import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
8
- import fi .helsinki .cs .tmc .model .NbTmcSettings ;
9
7
import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
10
8
import fi .helsinki .cs .tmc .utilities .BgTask ;
11
9
import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
12
10
import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
13
- import fi .helsinki .cs .tmc .utilities .zip .NbProjectUnzipper ;
14
- import fi .helsinki .cs .tmc .utilities .zip .NbProjectUnzipper .OverwritingDecider ;
11
+ import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
15
12
16
13
import com .google .common .base .Function ;
14
+ import com .google .common .util .concurrent .ListenableFuture ;
17
15
18
16
import org .netbeans .api .project .Project ;
19
17
import org .openide .awt .ActionID ;
25
23
import org .openide .util .HelpCtx ;
26
24
import org .openide .util .NbBundle .Messages ;
27
25
28
- import java .util .concurrent .Callable ;
29
- import java .util .logging .Level ;
30
26
import java .util .logging .Logger ;
31
27
import javax .swing .JComponent ;
32
28
import javax .swing .JMenuItem ;
33
29
34
30
@ ActionID (category = "TMC" , id = "fi.helsinki.cs.tmc.actions.DownloadSolutionAction" )
35
31
@ ActionRegistration (displayName = "#CTL_DownloadSolutionAction" , lazy = false )
36
- @ ActionReferences ({@ ActionReference (path = "Menu/TM&C" , position = -35 , separatorAfter = -30 )})
32
+ @ ActionReferences ({
33
+ @ ActionReference (path = "Menu/TM&C" , position = -35 , separatorAfter = -30 )})
37
34
@ Messages ("CTL_DownloadSolutionAction=Download suggested &solution" )
38
35
public class DownloadSolutionAction extends AbstractExerciseSensitiveAction {
36
+
39
37
private static final Logger logger = Logger .getLogger (DownloadSolutionAction .class .getName ());
40
38
private ProjectMediator projectMediator ;
41
39
private CourseDb courseDb ;
@@ -104,10 +102,10 @@ protected void performAction(Node[] nodes) {
104
102
return ;
105
103
}
106
104
107
- String question =
108
- "Are you sure you want to OVERWRITE your copy of\n "
109
- + ex .getName ()
110
- + " with the suggested solution?" ;
105
+ String question
106
+ = "Are you sure you want to OVERWRITE your copy of\n "
107
+ + ex .getName ()
108
+ + " with the suggested solution?" ;
111
109
String title = "Replace with solution?" ;
112
110
dialogs .askYesNo (
113
111
question ,
@@ -125,88 +123,48 @@ public Void apply(Boolean yes) {
125
123
}
126
124
127
125
private void downloadSolution (final Exercise ex , final TmcProjectInfo proj ) {
128
- ServerAccess serverAccess = new ServerAccess (NbTmcSettings .getDefault ());
129
-
130
- // TODO: Use tmc-core.
131
- CancellableCallable <byte []> downloadTask =
132
- serverAccess .getDownloadingExerciseSolutionZipTask (ex );
133
- BgTask .start (
134
- "Downloading solution for " + ex .getName (),
135
- downloadTask ,
136
- new BgTaskListener <byte []>() {
137
- @ Override
138
- public void bgTaskReady (byte [] result ) {
139
- unzipSolution (ex , proj , result );
140
- }
141
-
142
- @ Override
143
- public void bgTaskCancelled () {}
144
-
145
- @ Override
146
- public void bgTaskFailed (Throwable ex ) {
147
- logger .log (Level .INFO , "Failed to download solution." , ex );
148
- dialogs .displayError (
149
- "Failed to download solution.\n "
150
- + ServerErrorHelper .getServerExceptionMsg (ex ));
151
- }
152
- });
153
- }
126
+ BgTask .start ("Downloading solution for " + ex .getName (), new CancellableCallable <Boolean >() {
127
+
128
+ ListenableFuture <Boolean > lf ;
129
+ @ Override
130
+ public Boolean call () throws Exception {
131
+ System .err .println ("loading" );
132
+ ListenableFuture <Boolean > lf = TmcCoreSingleton .getInstance ().downloadModelSolution (ex );
133
+ return lf .get ();
134
+ }
154
135
155
- private void unzipSolution (final Exercise ex , final TmcProjectInfo proj , final byte [] data ) {
156
- Callable <Object > task =
157
- new Callable <Object >() {
158
- @ Override
159
- public Object call () throws Exception {
160
- NbProjectUnzipper unzipper = new NbProjectUnzipper (solutionOverwriting );
161
- unzipper .unzipProject (data , proj .getProjectDirAsFile ());
162
- return null ;
163
- }
164
- };
165
-
166
- BgTask .start (
167
- "Extracting solution" ,
168
- task ,
169
- new BgTaskListener <Object >() {
170
- @ Override
171
- public void bgTaskReady (Object result ) {
172
- projectMediator .scanForExternalChanges (proj );
173
- }
174
-
175
- @ Override
176
- public void bgTaskCancelled () {}
177
-
178
- @ Override
179
- public void bgTaskFailed (Throwable ex ) {
180
- logger .log (Level .INFO , "Failed to extract solution." , ex );
181
- dialogs .displayError (
182
- "Failed to extract solution.\n "
183
- + ServerErrorHelper .getServerExceptionMsg (ex ));
184
- }
185
- });
186
- }
136
+ @ Override
137
+ public boolean cancel () {
138
+ return lf .cancel (true );
139
+ }
140
+ }, new BgTaskListener <Boolean >() {
187
141
188
- private OverwritingDecider solutionOverwriting =
189
- new OverwritingDecider () {
190
- @ Override
191
- public boolean mayOverwrite (String relPath ) {
192
- return true ;
193
- }
142
+ @ Override
143
+ public void bgTaskReady (Boolean result ) {
144
+ System .err .println ("ready" );
145
+ projectMediator .scanForExternalChanges (proj );
146
+ }
194
147
195
- @ Override
196
- public boolean mayDelete (String relPath ) {
197
- return false ;
198
- }
199
- };
148
+ @ Override
149
+ public void bgTaskCancelled () {
150
+ }
151
+
152
+ @ Override
153
+ public void bgTaskFailed (Throwable ex ) {
154
+ }
155
+ });
156
+ }
200
157
201
158
private class ActionMenuItem extends JMenuItem implements DynamicMenuContent {
159
+
202
160
public ActionMenuItem () {
203
161
super (DownloadSolutionAction .this );
204
162
}
205
163
206
164
@ Override
207
165
public JComponent [] getMenuPresenters () {
208
166
if (DownloadSolutionAction .this .isEnabled ()) {
209
- return new JComponent [] {getOriginalMenuPresenter ()};
167
+ return new JComponent []{getOriginalMenuPresenter ()};
210
168
} else {
211
169
return new JComponent [0 ];
212
170
}
@@ -217,4 +175,4 @@ public JComponent[] synchMenuPresenters(JComponent[] jcs) {
217
175
return getMenuPresenters ();
218
176
}
219
177
}
220
- }
178
+ }
0 commit comments