1
1
package fi .helsinki .cs .tmc .actions ;
2
2
3
- import com .google .common .base .Function ;
4
3
import fi .helsinki .cs .tmc .core .domain .Exercise ;
5
4
import fi .helsinki .cs .tmc .model .CourseDb ;
6
5
import fi .helsinki .cs .tmc .model .ProjectMediator ;
13
12
import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
14
13
import fi .helsinki .cs .tmc .utilities .zip .NbProjectUnzipper ;
15
14
import 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
+
21
18
import org .netbeans .api .project .Project ;
22
19
import org .openide .awt .ActionID ;
23
20
import org .openide .awt .ActionReference ;
28
25
import org .openide .util .HelpCtx ;
29
26
import org .openide .util .NbBundle .Messages ;
30
27
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
+
31
34
@ ActionID (category = "TMC" ,
32
35
id = "fi.helsinki.cs.tmc.actions.DownloadSolutionAction" )
33
36
@ ActionRegistration (displayName = "#CTL_DownloadSolutionAction" , lazy = false )
@@ -46,7 +49,7 @@ public DownloadSolutionAction() {
46
49
this .courseDb = CourseDb .getInstance ();
47
50
this .dialogs = ConvenientDialogDisplayer .getDefault ();
48
51
}
49
-
52
+
50
53
@ Override
51
54
public String getName () {
52
55
return "Download suggested &solution" ;
@@ -66,7 +69,7 @@ protected boolean enabledFor(Exercise exercise) {
66
69
protected boolean enabledForMultipleProjects () {
67
70
return true ;
68
71
}
69
-
72
+
70
73
@ Override
71
74
protected boolean asynchronous () {
72
75
return false ;
@@ -76,7 +79,7 @@ protected boolean asynchronous() {
76
79
public JMenuItem getMenuPresenter () {
77
80
return new ActionMenuItem ();
78
81
}
79
-
82
+
80
83
private JMenuItem getOriginalMenuPresenter () {
81
84
return super .getMenuPresenter ();
82
85
}
@@ -93,9 +96,8 @@ protected CourseDb getCourseDb() {
93
96
94
97
@ Override
95
98
protected void performAction (Node [] nodes ) {
96
- // TODO(jamo): use bg task
97
99
projectMediator .saveAllFiles ();
98
-
100
+
99
101
for (final Project project : projectsFromNodes (nodes )) {
100
102
final Exercise ex = exerciseForProject (project );
101
103
if (ex .getSolutionDownloadUrl () == null ) {
@@ -104,7 +106,7 @@ protected void performAction(Node[] nodes) {
104
106
this .setEnabled (false );
105
107
return ;
106
108
}
107
-
109
+
108
110
String question = "Are you sure you want to OVERWRITE your copy of\n " + ex .getName () + " with the suggested solution?" ;
109
111
String title = "Replace with solution?" ;
110
112
dialogs .askYesNo (question , title , new Function <Boolean , Void >() {
@@ -121,6 +123,8 @@ public Void apply(Boolean yes) {
121
123
122
124
private void downloadSolution (final Exercise ex , final TmcProjectInfo proj ) {
123
125
ServerAccess serverAccess = new ServerAccess (NbTmcSettings .getDefault ());
126
+
127
+ // TODO: Use tmc-core.
124
128
CancellableCallable <byte []> downloadTask = serverAccess .getDownloadingExerciseSolutionZipTask (ex );
125
129
BgTask .start ("Downloading solution for " + ex .getName (), downloadTask , new BgTaskListener <byte []>() {
126
130
@ Override
@@ -139,7 +143,7 @@ public void bgTaskFailed(Throwable ex) {
139
143
}
140
144
});
141
145
}
142
-
146
+
143
147
private void unzipSolution (final Exercise ex , final TmcProjectInfo proj , final byte [] data ) {
144
148
Callable <Object > task = new Callable <Object >() {
145
149
@ Override
@@ -149,7 +153,7 @@ public Object call() throws Exception {
149
153
return null ;
150
154
}
151
155
};
152
-
156
+
153
157
BgTask .start ("Extracting solution" , task , new BgTaskListener <Object >() {
154
158
@ Override
155
159
public void bgTaskReady (Object result ) {
@@ -167,7 +171,7 @@ public void bgTaskFailed(Throwable ex) {
167
171
}
168
172
});
169
173
}
170
-
174
+
171
175
private OverwritingDecider solutionOverwriting = new OverwritingDecider () {
172
176
@ Override
173
177
public boolean mayOverwrite (String relPath ) {
@@ -178,12 +182,12 @@ public boolean mayDelete(String relPath) {
178
182
return false ;
179
183
}
180
184
};
181
-
185
+
182
186
private class ActionMenuItem extends JMenuItem implements DynamicMenuContent {
183
187
public ActionMenuItem () {
184
188
super (DownloadSolutionAction .this );
185
189
}
186
-
190
+
187
191
@ Override
188
192
public JComponent [] getMenuPresenters () {
189
193
if (DownloadSolutionAction .this .isEnabled ()) {
@@ -197,6 +201,6 @@ public JComponent[] getMenuPresenters() {
197
201
public JComponent [] synchMenuPresenters (JComponent [] jcs ) {
198
202
return getMenuPresenters ();
199
203
}
200
-
204
+
201
205
}
202
206
}
0 commit comments