33import fi .helsinki .cs .tmc .core .domain .Exercise ;
44import fi .helsinki .cs .tmc .model .CourseDb ;
55import fi .helsinki .cs .tmc .model .ProjectMediator ;
6- import fi .helsinki .cs .tmc .model .ServerAccess ;
76import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
8- import fi .helsinki .cs .tmc .model .NbTmcSettings ;
97import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
108import fi .helsinki .cs .tmc .utilities .BgTask ;
119import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
1210import 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 ;
1512
1613import com .google .common .base .Function ;
14+ import com .google .common .util .concurrent .ListenableFuture ;
1715
1816import org .netbeans .api .project .Project ;
1917import org .openide .awt .ActionID ;
2523import org .openide .util .HelpCtx ;
2624import org .openide .util .NbBundle .Messages ;
2725
28- import java .util .concurrent .Callable ;
29- import java .util .logging .Level ;
3026import java .util .logging .Logger ;
3127import javax .swing .JComponent ;
3228import javax .swing .JMenuItem ;
3329
3430@ ActionID (category = "TMC" , id = "fi.helsinki.cs.tmc.actions.DownloadSolutionAction" )
3531@ 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 )})
3734@ Messages ("CTL_DownloadSolutionAction=Download suggested &solution" )
3835public class DownloadSolutionAction extends AbstractExerciseSensitiveAction {
36+
3937 private static final Logger logger = Logger .getLogger (DownloadSolutionAction .class .getName ());
4038 private ProjectMediator projectMediator ;
4139 private CourseDb courseDb ;
@@ -104,10 +102,10 @@ protected void performAction(Node[] nodes) {
104102 return ;
105103 }
106104
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?" ;
111109 String title = "Replace with solution?" ;
112110 dialogs .askYesNo (
113111 question ,
@@ -125,88 +123,48 @@ public Void apply(Boolean yes) {
125123 }
126124
127125 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+ }
154135
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 >() {
187141
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+ }
194147
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+ }
200157
201158 private class ActionMenuItem extends JMenuItem implements DynamicMenuContent {
159+
202160 public ActionMenuItem () {
203161 super (DownloadSolutionAction .this );
204162 }
205163
206164 @ Override
207165 public JComponent [] getMenuPresenters () {
208166 if (DownloadSolutionAction .this .isEnabled ()) {
209- return new JComponent [] {getOriginalMenuPresenter ()};
167+ return new JComponent []{getOriginalMenuPresenter ()};
210168 } else {
211169 return new JComponent [0 ];
212170 }
@@ -217,4 +175,4 @@ public JComponent[] synchMenuPresenters(JComponent[] jcs) {
217175 return getMenuPresenters ();
218176 }
219177 }
220- }
178+ }
0 commit comments