Skip to content

Commit 4eb5424

Browse files
committed
Start using tmc-core for downloading model solution
1 parent 49f71bb commit 4eb5424

File tree

1 file changed

+40
-82
lines changed

1 file changed

+40
-82
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/actions/DownloadSolutionAction.java

Lines changed: 40 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import fi.helsinki.cs.tmc.core.domain.Exercise;
44
import fi.helsinki.cs.tmc.model.CourseDb;
55
import fi.helsinki.cs.tmc.model.ProjectMediator;
6-
import fi.helsinki.cs.tmc.model.ServerAccess;
76
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
8-
import fi.helsinki.cs.tmc.model.NbTmcSettings;
97
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
108
import fi.helsinki.cs.tmc.utilities.BgTask;
119
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
1210
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;
1512

1613
import com.google.common.base.Function;
14+
import com.google.common.util.concurrent.ListenableFuture;
1715

1816
import org.netbeans.api.project.Project;
1917
import org.openide.awt.ActionID;
@@ -25,17 +23,17 @@
2523
import org.openide.util.HelpCtx;
2624
import org.openide.util.NbBundle.Messages;
2725

28-
import java.util.concurrent.Callable;
29-
import java.util.logging.Level;
3026
import java.util.logging.Logger;
3127
import javax.swing.JComponent;
3228
import 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")
3835
public 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

Comments
 (0)