-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPastebinAction.java
More file actions
171 lines (151 loc) · 6.34 KB
/
PastebinAction.java
File metadata and controls
171 lines (151 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package fi.helsinki.cs.tmc.actions;
import fi.helsinki.cs.tmc.data.Exercise;
import fi.helsinki.cs.tmc.model.CourseDb;
import fi.helsinki.cs.tmc.model.ProjectMediator;
import fi.helsinki.cs.tmc.model.ServerAccess;
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
import fi.helsinki.cs.tmc.model.TmcSettings;
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
import fi.helsinki.cs.tmc.ui.PastebinDialog;
import fi.helsinki.cs.tmc.ui.PastebinResponseDialog;
import fi.helsinki.cs.tmc.utilities.BgTask;
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
import fi.helsinki.cs.tmc.utilities.CancellableCallable;
import fi.helsinki.cs.tmc.utilities.zip.RecursiveZipper;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.project.Project;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.nodes.Node;
import org.openide.util.NbBundle.Messages;
@ActionID(
category = "TMC",
id = "fi.helsinki.cs.tmc.actions.PastebinAction")
@ActionRegistration(
displayName = "#CTL_PastebinAction", lazy = false)
@ActionReferences({
@ActionReference(path = "Menu/TM&C", position = -17),
@ActionReference(path = "Projects/Actions", position = 1340, separatorBefore = 1330,
separatorAfter = 1360)
})
@Messages("CTL_PastebinAction=Send code to Pastebin")
//TODO: This is a horribly copypasted, then mangled version of RequestReviewAction
//plz remove everything that isn't needed here. --kviiri
public final class PastebinAction extends AbstractExerciseSensitiveAction {
private static final Logger log = Logger.getLogger(RequestReviewAction.class.getName());
private TmcSettings settings;
private CourseDb courseDb;
private ProjectMediator projectMediator;
private ConvenientDialogDisplayer dialogs;
public PastebinAction() {
this.settings = TmcSettings.getDefault();
this.courseDb = CourseDb.getInstance();
this.projectMediator = ProjectMediator.getInstance();
this.dialogs = ConvenientDialogDisplayer.getDefault();
}
@Override
protected ProjectMediator getProjectMediator() {
return projectMediator;
}
@Override
protected CourseDb getCourseDb() {
return courseDb;
}
@Override
public boolean enable(Project... projects) {
if (projects.length > 1) {
return false; // One at a time please
} else {
return super.enable(projects);
}
}
@Override
protected boolean enabledFor(Exercise exercise) {
return exercise.getReturnable();
}
@Override
protected void performAction(Node[] nodes) {
List<Project> project = projectsFromNodes(nodes);
if (project.size() == 1) {
TmcProjectInfo projectInfo = projectMediator.wrapProject(project.get(0));
Exercise exercise = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
if (exercise != null) {
showPasteRequestDialog(projectInfo, exercise);
} else {
log.log(Level.WARNING, "PastebinAction called in a context without a valid TMC project.");
}
} else {
log.log(Level.WARNING, "PastebinAction called in a context with {0} projects", project.size());
}
}
private void showPasteRequestDialog(final TmcProjectInfo projectInfo, final Exercise exercise) {
final PastebinDialog dialog = new PastebinDialog(exercise);
dialog.setOkListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = dialog.getMessageForReviewer().trim();
submitPaste(projectInfo, exercise, message);
}
});
dialog.setVisible(true);
}
private void submitPaste(final TmcProjectInfo projectInfo, final Exercise exercise,
final String messageForReviewer) {
projectMediator.saveAllFiles();
final String errorMsgLocale = settings.getErrorMsgLocale().toString();
BgTask.start("Zipping up " + exercise.getName(), new Callable<byte[]>() {
@Override
public byte[] call() throws Exception {
RecursiveZipper zipper = new RecursiveZipper(projectInfo.getProjectDirAsFile(), projectInfo.getZippingDecider());
return zipper.zipProjectSources();
}
}, new BgTaskListener<byte[]>() {
@Override
public void bgTaskReady(byte[] zipData) {
Map<String, String> extraParams = new HashMap<String, String>();
extraParams.put("error_msg_locale", errorMsgLocale);
extraParams.put("paste", "1");
if (!messageForReviewer.isEmpty()) {
extraParams.put("message_for_paste", messageForReviewer);
}
final ServerAccess sa = new ServerAccess();
CancellableCallable<ServerAccess.SubmissionResponse> submitTask = sa
.getSubmittingExerciseTask(exercise, zipData, extraParams);
BgTask.start("Sending " + exercise.getName(), submitTask, new BgTaskListener<ServerAccess.SubmissionResponse>() {
@Override
public void bgTaskReady(ServerAccess.SubmissionResponse result) {
new PastebinResponseDialog(result.pasteUrl.toString()).setVisible(true);
}
@Override
public void bgTaskCancelled() {
}
@Override
public void bgTaskFailed(Throwable ex) {
dialogs.displayError("Failed to send exercise to pastebin", ex);
}
});
}
@Override
public void bgTaskCancelled() {
}
@Override
public void bgTaskFailed(Throwable ex) {
dialogs.displayError("Failed to zip up exercise", ex);
}
});
}
@Override
public String getName() {
return "Send code to TMC Pastebin";
}
}