-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPastebinAction.java
More file actions
166 lines (145 loc) · 5.67 KB
/
Copy pathPastebinAction.java
File metadata and controls
166 lines (145 loc) · 5.67 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
package fi.helsinki.cs.tmc.actions;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.model.CourseDb;
import fi.helsinki.cs.tmc.model.ProjectMediator;
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
import fi.helsinki.cs.tmc.model.NbTmcSettings;
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
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 com.google.common.util.concurrent.ListenableFuture;
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;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@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 NbTmcSettings settings;
private CourseDb courseDb;
private ProjectMediator projectMediator;
private final ConvenientDialogDisplayer dialogs;
public PastebinAction() {
this.settings = NbTmcSettings.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 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();
BgTask.start(
"Sending tmc-paste",
new CancellableCallable<URI>() {
ListenableFuture<URI> result;
@Override
public URI call() throws Exception {
log.log(Level.INFO, "Pre submit");
result =
TmcCoreSingleton.getInstance()
.pasteWithComment(
projectInfo.getProjectDirAsPath(),
messageForReviewer);
return result.get();
}
@Override
public boolean cancel() {
return result.cancel(true);
}
},
new PasteResult());
}
class PasteResult implements BgTaskListener<URI> {
@Override
public void bgTaskReady(URI uri) {
new PastebinResponseDialog(uri.toString()).setVisible(true);
}
@Override
public void bgTaskCancelled() {}
@Override
public void bgTaskFailed(Throwable ex) {
dialogs.displayError("Failed to send exercise to pastebin. \n"
+ ServerErrorHelper.getServerExceptionMsg(ex));
}
}
@Override
public String getName() {
return "Send code to TMC Pastebin";
}
}