Skip to content

Commit 9364f7b

Browse files
committed
Send paste
1 parent 116a3f1 commit 9364f7b

File tree

1 file changed

+35
-77
lines changed

1 file changed

+35
-77
lines changed

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

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package fi.helsinki.cs.tmc.actions;
22

3+
import fi.helsinki.cs.tmc.core.TmcCore;
34
import fi.helsinki.cs.tmc.core.domain.Exercise;
5+
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
46
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
57
import fi.helsinki.cs.tmc.coreimpl.TmcCoreSettingsImpl;
68
import fi.helsinki.cs.tmc.events.TmcEvent;
79
import fi.helsinki.cs.tmc.events.TmcEventBus;
810
import fi.helsinki.cs.tmc.model.CourseDb;
911
import fi.helsinki.cs.tmc.model.ProjectMediator;
10-
import fi.helsinki.cs.tmc.model.ServerAccess;
1112
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
1213
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
1314
import fi.helsinki.cs.tmc.ui.PastebinDialog;
1415
import fi.helsinki.cs.tmc.ui.PastebinResponseDialog;
1516
import fi.helsinki.cs.tmc.utilities.BgTask;
1617
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
17-
import fi.helsinki.cs.tmc.utilities.CancellableCallable;
18-
import fi.helsinki.cs.tmc.utilities.zip.RecursiveZipper;
1918

2019
import org.netbeans.api.project.Project;
2120
import org.openide.awt.ActionID;
@@ -27,9 +26,8 @@
2726

2827
import java.awt.event.ActionEvent;
2928
import java.awt.event.ActionListener;
30-
import java.util.HashMap;
29+
import java.net.URI;
3130
import java.util.List;
32-
import java.util.Map;
3331
import java.util.concurrent.Callable;
3432
import java.util.logging.Level;
3533
import java.util.logging.Logger;
@@ -39,15 +37,13 @@
3937
@ActionReferences({
4038
@ActionReference(path = "Menu/TM&C", position = -17),
4139
@ActionReference(
42-
path = "Projects/Actions",
43-
position = 1340,
44-
separatorBefore = 1330,
45-
separatorAfter = 1360
40+
path = "Projects/Actions",
41+
position = 1340,
42+
separatorBefore = 1330,
43+
separatorAfter = 1360
4644
)
4745
})
4846
@Messages("CTL_PastebinAction=Send code to Pastebin")
49-
//TODO: This is a horribly copypasted, then mangled version of RequestReviewAction
50-
//plz remove everything that isn't needed here. --kviiri
5147
public final class PastebinAction extends AbstractExerciseSensitiveAction {
5248

5349
private static final Logger log = Logger.getLogger(RequestReviewAction.class.getName());
@@ -58,7 +54,7 @@ public final class PastebinAction extends AbstractExerciseSensitiveAction {
5854
private TmcEventBus eventBus;
5955

6056
public PastebinAction() {
61-
this.settings = ((TmcCoreSettingsImpl)TmcSettingsHolder.get());
57+
this.settings = ((TmcCoreSettingsImpl) TmcSettingsHolder.get());
6258
this.courseDb = CourseDb.getInstance();
6359
this.projectMediator = ProjectMediator.getInstance();
6460
this.dialogs = ConvenientDialogDisplayer.getDefault();
@@ -92,7 +88,7 @@ protected void performAction(Node[] nodes) {
9288
Exercise exercise = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
9389
if (exercise != null) {
9490
eventBus.post(new PastebinAction.InvokedEvent(projectInfo));
95-
showPasteRequestDialog(projectInfo, exercise);
91+
showPasteRequestDialog(exercise);
9692
} else {
9793
log.log(
9894
Level.WARNING,
@@ -106,83 +102,45 @@ protected void performAction(Node[] nodes) {
106102
}
107103
}
108104

109-
private void showPasteRequestDialog(final TmcProjectInfo projectInfo, final Exercise exercise) {
105+
private void showPasteRequestDialog(final Exercise exercise) {
110106
final PastebinDialog dialog = new PastebinDialog(exercise);
111107
dialog.setOkListener(
112108
new ActionListener() {
113-
@Override
114-
public void actionPerformed(ActionEvent e) {
115-
String message = dialog.getMessageForReviewer().trim();
116-
submitPaste(projectInfo, exercise, message);
117-
}
118-
});
109+
@Override
110+
public void actionPerformed(ActionEvent e) {
111+
String message = dialog.getMessageForReviewer().trim();
112+
submitPaste(exercise, message);
113+
}
114+
});
119115
dialog.setVisible(true);
120116
}
121117

122118
private void submitPaste(
123-
final TmcProjectInfo projectInfo,
124119
final Exercise exercise,
125120
final String messageForReviewer) {
126121
projectMediator.saveAllFiles();
127122

128-
final String errorMsgLocale = settings.getErrorMsgLocale().toString();
123+
Callable<URI> pasteTask = TmcCore.get()
124+
.pasteWithComment(ProgressObserver.NULL_OBSERVER, exercise, messageForReviewer);
129125

130126
BgTask.start(
131-
"Zipping up " + exercise.getName(),
132-
new Callable<byte[]>() {
133-
@Override
134-
public byte[] call() throws Exception {
135-
RecursiveZipper zipper =
136-
new RecursiveZipper(
137-
projectInfo.getProjectDirAsFile(),
138-
projectInfo.getZippingDecider());
139-
return zipper.zipProjectSources();
140-
}
141-
},
142-
new BgTaskListener<byte[]>() {
143-
@Override
144-
public void bgTaskReady(byte[] zipData) {
145-
Map<String, String> extraParams = new HashMap<String, String>();
146-
extraParams.put("error_msg_locale", errorMsgLocale);
147-
extraParams.put("paste", "1");
148-
if (!messageForReviewer.isEmpty()) {
149-
extraParams.put("message_for_paste", messageForReviewer);
150-
}
151-
152-
final ServerAccess sa = new ServerAccess();
153-
CancellableCallable<ServerAccess.SubmissionResponse> submitTask =
154-
sa.getSubmittingExerciseTask(exercise, zipData, extraParams);
155-
156-
BgTask.start(
157-
"Sending " + exercise.getName(),
158-
submitTask,
159-
new BgTaskListener<ServerAccess.SubmissionResponse>() {
160-
@Override
161-
public void bgTaskReady(
162-
ServerAccess.SubmissionResponse result) {
163-
new PastebinResponseDialog(result.pasteUrl.toString())
164-
.setVisible(true);
165-
}
166-
167-
@Override
168-
public void bgTaskCancelled() {}
169-
170-
@Override
171-
public void bgTaskFailed(Throwable ex) {
172-
dialogs.displayError(
173-
"Failed to send exercise to pastebin", ex);
174-
}
175-
});
176-
}
177-
178-
@Override
179-
public void bgTaskCancelled() {}
180-
181-
@Override
182-
public void bgTaskFailed(Throwable ex) {
183-
dialogs.displayError("Failed to zip up exercise", ex);
184-
}
185-
});
127+
"Sending " + exercise.getName(), pasteTask, new BgTaskListener<URI>() {
128+
@Override
129+
public void bgTaskReady(URI result) {
130+
new PastebinResponseDialog(result.toString())
131+
.setVisible(true);
132+
}
133+
134+
@Override
135+
public void bgTaskCancelled() {
136+
}
137+
138+
@Override
139+
public void bgTaskFailed(Throwable ex) {
140+
dialogs.displayError(
141+
"Failed to send exercise to pastebin", ex);
142+
}
143+
});
186144
}
187145

188146
@Override

0 commit comments

Comments
 (0)