Skip to content

Commit 130624e

Browse files
committed
Merge pull request #20 from rage/submitPaste
Paste works
2 parents 5b47d2b + c536956 commit 130624e

File tree

1 file changed

+24
-47
lines changed

1 file changed

+24
-47
lines changed

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

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package fi.helsinki.cs.tmc.actions;
22

3+
import com.google.common.util.concurrent.FutureCallback;
4+
import com.google.common.util.concurrent.Futures;
5+
import com.google.common.util.concurrent.ListenableFuture;
36
import hy.tmc.core.domain.Exercise;
47
import fi.helsinki.cs.tmc.model.CourseDb;
58
import fi.helsinki.cs.tmc.model.ProjectMediator;
69
import fi.helsinki.cs.tmc.model.ServerAccess;
710
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
811
import fi.helsinki.cs.tmc.model.NBTmcSettings;
12+
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
913
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
1014
import fi.helsinki.cs.tmc.ui.PastebinDialog;
1115
import fi.helsinki.cs.tmc.ui.PastebinResponseDialog;
1216
import fi.helsinki.cs.tmc.utilities.BgTask;
1317
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
1418
import fi.helsinki.cs.tmc.utilities.CancellableCallable;
1519
import fi.helsinki.cs.tmc.utilities.zip.RecursiveZipper;
20+
import hy.tmc.core.exceptions.TmcCoreException;
1621
import java.awt.event.ActionEvent;
1722
import java.awt.event.ActionListener;
1823
import java.net.URI;
@@ -28,6 +33,7 @@
2833
import org.openide.awt.ActionReferences;
2934
import org.openide.awt.ActionRegistration;
3035
import org.openide.nodes.Node;
36+
import org.openide.util.Exceptions;
3137
import org.openide.util.NbBundle.Messages;
3238

3339
@ActionID(
@@ -38,7 +44,7 @@
3844
@ActionReferences({
3945
@ActionReference(path = "Menu/TM&C", position = -17),
4046
@ActionReference(path = "Projects/Actions", position = 1340, separatorBefore = 1330,
41-
separatorAfter = 1360)
47+
separatorAfter = 1360)
4248
})
4349
@Messages("CTL_PastebinAction=Send code to Pastebin")
4450
//TODO: This is a horribly copypasted, then mangled version of RequestReviewAction
@@ -108,59 +114,30 @@ public void actionPerformed(ActionEvent e) {
108114
private void submitPaste(final TmcProjectInfo projectInfo, final Exercise exercise,
109115
final String messageForReviewer) {
110116
projectMediator.saveAllFiles();
111-
112117
final String errorMsgLocale = settings.getErrorMsgLocale().toString();
118+
try {
119+
ListenableFuture<URI> result = TmcCoreSingleton.getInstance().pasteWithComment(projectInfo.getProjectDirAbsPath(), settings, messageForReviewer);
120+
Futures.addCallback(result, new PasteResult());
121+
} catch (TmcCoreException ex) {
122+
Exceptions.printStackTrace(ex);
123+
}
124+
}
113125

114-
BgTask.start("Zipping up " + exercise.getName(), new Callable<byte[]>() {
115-
@Override
116-
public byte[] call() throws Exception {
117-
RecursiveZipper zipper = new RecursiveZipper(projectInfo.getProjectDirAsFile(), projectInfo.getZippingDecider());
118-
return zipper.zipProjectSources();
119-
}
120-
}, new BgTaskListener<byte[]>() {
121-
@Override
122-
public void bgTaskReady(byte[] zipData) {
123-
Map<String, String> extraParams = new HashMap<String, String>();
124-
extraParams.put("error_msg_locale", errorMsgLocale);
125-
extraParams.put("paste", "1");
126-
if (!messageForReviewer.isEmpty()) {
127-
extraParams.put("message_for_paste", messageForReviewer);
128-
}
129-
130-
final ServerAccess sa = new ServerAccess();
131-
CancellableCallable<ServerAccess.SubmissionResponse> submitTask = sa
132-
.getSubmittingExerciseTask(exercise, zipData, extraParams);
133-
134-
BgTask.start("Sending " + exercise.getName(), submitTask, new BgTaskListener<ServerAccess.SubmissionResponse>() {
135-
@Override
136-
public void bgTaskReady(ServerAccess.SubmissionResponse result) {
137-
new PastebinResponseDialog(result.pasteUrl.toString()).setVisible(true);
138-
}
139-
140-
@Override
141-
public void bgTaskCancelled() {
142-
}
143-
144-
@Override
145-
public void bgTaskFailed(Throwable ex) {
146-
dialogs.displayError("Failed to send exercise to pastebin", ex);
147-
}
148-
});
149-
}
126+
class PasteResult implements FutureCallback<URI> {
150127

151-
@Override
152-
public void bgTaskCancelled() {
153-
}
128+
@Override
129+
public void onSuccess(URI ur) {
130+
new PastebinResponseDialog(ur.toString()).setVisible(true);
131+
}
154132

155-
@Override
156-
public void bgTaskFailed(Throwable ex) {
157-
dialogs.displayError("Failed to zip up exercise", ex);
158-
}
159-
});
133+
@Override
134+
public void onFailure(Throwable thrwbl) {
135+
dialogs.displayError("Failed to send exercise to pastebin", thrwbl);
136+
}
160137
}
161138

162139
@Override
163140
public String getName() {
164141
return "Send code to TMC Pastebin";
165142
}
166-
}
143+
}

0 commit comments

Comments
 (0)