11package fi .helsinki .cs .tmc .actions ;
22
3- import com .google .common .base .Function ;
43import fi .helsinki .cs .tmc .core .domain .Exercise ;
54import fi .helsinki .cs .tmc .model .CourseDb ;
65import fi .helsinki .cs .tmc .model .ProjectMediator ;
1312import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
1413import fi .helsinki .cs .tmc .utilities .zip .NbProjectUnzipper ;
1514import fi .helsinki .cs .tmc .utilities .zip .NbProjectUnzipper .OverwritingDecider ;
16- import java .util .concurrent .Callable ;
17- import java .util .logging .Level ;
18- import java .util .logging .Logger ;
19- import javax .swing .JComponent ;
20- import javax .swing .JMenuItem ;
15+
16+ import com .google .common .base .Function ;
17+
2118import org .netbeans .api .project .Project ;
2219import org .openide .awt .ActionID ;
2320import org .openide .awt .ActionReference ;
2825import org .openide .util .HelpCtx ;
2926import org .openide .util .NbBundle .Messages ;
3027
28+ import java .util .concurrent .Callable ;
29+ import java .util .logging .Level ;
30+ import java .util .logging .Logger ;
31+ import javax .swing .JComponent ;
32+ import javax .swing .JMenuItem ;
33+
3134@ ActionID (category = "TMC" ,
3235id = "fi.helsinki.cs.tmc.actions.DownloadSolutionAction" )
3336@ ActionRegistration (displayName = "#CTL_DownloadSolutionAction" , lazy = false )
@@ -46,7 +49,7 @@ public DownloadSolutionAction() {
4649 this .courseDb = CourseDb .getInstance ();
4750 this .dialogs = ConvenientDialogDisplayer .getDefault ();
4851 }
49-
52+
5053 @ Override
5154 public String getName () {
5255 return "Download suggested &solution" ;
@@ -66,7 +69,7 @@ protected boolean enabledFor(Exercise exercise) {
6669 protected boolean enabledForMultipleProjects () {
6770 return true ;
6871 }
69-
72+
7073 @ Override
7174 protected boolean asynchronous () {
7275 return false ;
@@ -76,7 +79,7 @@ protected boolean asynchronous() {
7679 public JMenuItem getMenuPresenter () {
7780 return new ActionMenuItem ();
7881 }
79-
82+
8083 private JMenuItem getOriginalMenuPresenter () {
8184 return super .getMenuPresenter ();
8285 }
@@ -93,9 +96,8 @@ protected CourseDb getCourseDb() {
9396
9497 @ Override
9598 protected void performAction (Node [] nodes ) {
96- // TODO(jamo): use bg task
9799 projectMediator .saveAllFiles ();
98-
100+
99101 for (final Project project : projectsFromNodes (nodes )) {
100102 final Exercise ex = exerciseForProject (project );
101103 if (ex .getSolutionDownloadUrl () == null ) {
@@ -104,7 +106,7 @@ protected void performAction(Node[] nodes) {
104106 this .setEnabled (false );
105107 return ;
106108 }
107-
109+
108110 String question = "Are you sure you want to OVERWRITE your copy of\n " + ex .getName () + " with the suggested solution?" ;
109111 String title = "Replace with solution?" ;
110112 dialogs .askYesNo (question , title , new Function <Boolean , Void >() {
@@ -121,6 +123,8 @@ public Void apply(Boolean yes) {
121123
122124 private void downloadSolution (final Exercise ex , final TmcProjectInfo proj ) {
123125 ServerAccess serverAccess = new ServerAccess (NbTmcSettings .getDefault ());
126+
127+ // TODO: Use tmc-core.
124128 CancellableCallable <byte []> downloadTask = serverAccess .getDownloadingExerciseSolutionZipTask (ex );
125129 BgTask .start ("Downloading solution for " + ex .getName (), downloadTask , new BgTaskListener <byte []>() {
126130 @ Override
@@ -139,7 +143,7 @@ public void bgTaskFailed(Throwable ex) {
139143 }
140144 });
141145 }
142-
146+
143147 private void unzipSolution (final Exercise ex , final TmcProjectInfo proj , final byte [] data ) {
144148 Callable <Object > task = new Callable <Object >() {
145149 @ Override
@@ -149,7 +153,7 @@ public Object call() throws Exception {
149153 return null ;
150154 }
151155 };
152-
156+
153157 BgTask .start ("Extracting solution" , task , new BgTaskListener <Object >() {
154158 @ Override
155159 public void bgTaskReady (Object result ) {
@@ -167,7 +171,7 @@ public void bgTaskFailed(Throwable ex) {
167171 }
168172 });
169173 }
170-
174+
171175 private OverwritingDecider solutionOverwriting = new OverwritingDecider () {
172176 @ Override
173177 public boolean mayOverwrite (String relPath ) {
@@ -178,12 +182,12 @@ public boolean mayDelete(String relPath) {
178182 return false ;
179183 }
180184 };
181-
185+
182186 private class ActionMenuItem extends JMenuItem implements DynamicMenuContent {
183187 public ActionMenuItem () {
184188 super (DownloadSolutionAction .this );
185189 }
186-
190+
187191 @ Override
188192 public JComponent [] getMenuPresenters () {
189193 if (DownloadSolutionAction .this .isEnabled ()) {
@@ -197,6 +201,6 @@ public JComponent[] getMenuPresenters() {
197201 public JComponent [] synchMenuPresenters (JComponent [] jcs ) {
198202 return getMenuPresenters ();
199203 }
200-
204+
201205 }
202206}
0 commit comments