Skip to content

Commit 077cfe6

Browse files
committed
Don't run whole BgTask in ui-thread, only bgTaskReady, -Failed and -Cancelled
1 parent c23c6e9 commit 077cfe6

20 files changed

+186
-290
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import fi.helsinki.cs.tmc.core.domain.Course;
55
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
66
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
7-
import fi.helsinki.cs.tmc.core.utilities.ServerErrorHelper;
87
import fi.helsinki.cs.tmc.coreimpl.TmcCoreSettingsImpl;
98
import fi.helsinki.cs.tmc.core.events.TmcEvent;
10-
import fi.helsinki.cs.tmc.core.exceptions.ObsoleteClientException;
119
import fi.helsinki.cs.tmc.coreimpl.BridgingProgressObserver;
1210
import fi.helsinki.cs.tmc.core.events.TmcEventBus;
1311
import fi.helsinki.cs.tmc.model.CourseDb;
@@ -26,6 +24,8 @@
2624
import java.util.concurrent.Callable;
2725
import javax.swing.AbstractAction;
2826
import javax.swing.Icon;
27+
import javax.swing.SwingUtilities;
28+
2929
import org.apache.commons.lang3.StringUtils;
3030
import org.openide.awt.ActionID;
3131
import org.openide.awt.ActionReference;
@@ -106,11 +106,15 @@ public void bgTaskReady(Course receivedCourse) {
106106
displayNotification(status, new ActionListener() {
107107
@Override
108108
public void actionPerformed(ActionEvent e) {
109-
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
109+
SwingUtilities.invokeLater(() -> {
110+
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
111+
});
110112
}
111113
});
112114
} else {
113-
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
115+
SwingUtilities.invokeLater(() -> {
116+
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
117+
});
114118
}
115119
} else if (!beQuiet) {
116120
dialogs.displayMessage("No new exercises or updates to download.");
@@ -125,7 +129,9 @@ public void bgTaskCancelled() {
125129
@Override
126130
public void bgTaskFailed(Throwable ex) {
127131
if (!beQuiet) {
128-
dialogs.displayError("Failed to check for new exercises.\nPlease check your internet connection.");
132+
SwingUtilities.invokeLater(() -> {
133+
dialogs.displayError("Failed to check for new exercises.\nPlease check your internet connection.");
134+
});
129135
}
130136
}
131137
});

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.logging.Level;
1919
import java.util.logging.Logger;
2020
import javax.swing.SwingUtilities;
21+
2122
import org.openide.awt.ActionID;
2223
import org.openide.awt.ActionReference;
2324
import org.openide.awt.ActionReferences;
@@ -98,7 +99,9 @@ public void run() {
9899
public void bgTaskReady(List<Review> result) {
99100
boolean newReviews = reviewDb.setReviews(result);
100101
if (!newReviews && notifyAboutNoNewReviews) {
101-
dialogs.displayMessage("You have no unread code reviews.");
102+
SwingUtilities.invokeLater(() -> {
103+
dialogs.displayMessage("You have no unread code reviews.");
104+
});
102105
}
103106
}
104107

@@ -107,7 +110,9 @@ public void bgTaskFailed(final Throwable ex) {
107110
final String msg = "Failed to check for code reviews.\nPlease check your internet connection.";
108111
log.log(Level.INFO, msg, ex);
109112
if (!beQuiet) {
110-
dialogs.displayError(msg);
113+
SwingUtilities.invokeLater(() -> {
114+
dialogs.displayError(msg);
115+
});
111116
}
112117
}
113118

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.ArrayList;
1515
import java.util.List;
1616
import javax.swing.Icon;
17+
1718
import org.openide.awt.NotificationDisplayer;
1819
import org.openide.util.ImageUtilities;
1920

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import fi.helsinki.cs.tmc.core.domain.Course;
55
import fi.helsinki.cs.tmc.core.domain.Exercise;
66
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
7-
import fi.helsinki.cs.tmc.core.utilities.ServerErrorHelper;
87
import fi.helsinki.cs.tmc.core.events.TmcEvent;
98
import fi.helsinki.cs.tmc.coreimpl.BridgingProgressObserver;
109
import fi.helsinki.cs.tmc.core.events.TmcEventBus;
@@ -25,6 +24,7 @@
2524
import java.util.Collections;
2625
import java.util.List;
2726
import java.util.concurrent.Callable;
27+
import javax.swing.SwingUtilities;
2828

2929
@ActionID(category = "TMC", id = "fi.helsinki.cs.tmc.actions.DownloadCompletedExercises")
3030
@ActionRegistration(displayName = "#CTL_DownloadCompletedExercises")
@@ -64,9 +64,13 @@ public void bgTaskReady(Course receivedCourse) {
6464

6565
if (!status.downloadableCompleted.isEmpty()) {
6666
List<Exercise> emptyList = Collections.emptyList();
67-
DownloadOrUpdateExercisesDialog.display(emptyList, status.downloadableCompleted, emptyList);
67+
SwingUtilities.invokeLater(() -> {
68+
DownloadOrUpdateExercisesDialog.display(emptyList, status.downloadableCompleted, emptyList);
69+
});
6870
} else {
69-
dialogs.displayMessage("No completed exercises to download.\nDid you only close them and not delete them?");
71+
SwingUtilities.invokeLater(() -> {
72+
dialogs.displayMessage("No completed exercises to download.\nDid you only close them and not delete them?");
73+
});
7074
}
7175
}
7276
}
@@ -77,7 +81,9 @@ public void bgTaskCancelled() {
7781

7882
@Override
7983
public void bgTaskFailed(Throwable ex) {
80-
dialogs.displayError("Failed to check for new exercises.\nPlease check your internet connection.");
84+
SwingUtilities.invokeLater(() -> {
85+
dialogs.displayError("Failed to check for new exercises.\nPlease check your internet connection.");
86+
});
8187
}
8288
});
8389
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import fi.helsinki.cs.tmc.utilities.TmcSwingUtilities;
1818

1919
import com.google.common.collect.Lists;
20+
2021
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.concurrent.Callable;
2324
import java.util.logging.Level;
2425
import java.util.logging.Logger;
26+
import javax.swing.SwingUtilities;
27+
2528
import org.openide.util.Exceptions;
2629

2730
/**
@@ -80,11 +83,8 @@ public void bgTaskReady(List<Exercise> result) {
8083
}
8184

8285
// Need to invoke courseDb in swing thread to avoid races
83-
TmcSwingUtilities.ensureEdt(new Runnable() {
84-
@Override
85-
public void run() {
86-
courseDb.exerciseDownloaded(exercise);
87-
}
86+
TmcSwingUtilities.ensureEdt(() -> {
87+
courseDb.exerciseDownloaded(exercise);
8888
});
8989
listener.bgTaskReady(proj);
9090

@@ -109,7 +109,9 @@ public void bgTaskFailed(Throwable ex) {
109109
@Override
110110
public void bgTaskReady(Collection<TmcProjectInfo> projects) {
111111
projectMediator.openProjects(projects);
112-
new CheckProjectCount().checkAndNotifyIfOver();
112+
SwingUtilities.invokeLater(() -> {
113+
new CheckProjectCount().checkAndNotifyIfOver();
114+
});
113115
}
114116

115117
@Override
@@ -119,7 +121,9 @@ public void bgTaskCancelled() {
119121
@Override
120122
public void bgTaskFailed(Throwable ex) {
121123
logger.log(Level.INFO, "Failed to download exercise file.", ex);
122-
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
124+
SwingUtilities.invokeLater(() -> {
125+
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
126+
});
123127
}
124128
};
125129

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

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
import java.util.logging.Logger;
3232
import javax.swing.JComponent;
3333
import javax.swing.JMenuItem;
34+
import javax.swing.SwingUtilities;
3435

3536
@ActionID(category = "TMC", id = "fi.helsinki.cs.tmc.actions.DownloadSolutionAction")
3637
@ActionRegistration(displayName = "#CTL_DownloadSolutionAction", lazy = false)
37-
@ActionReferences({@ActionReference(path = "Menu/TM&C", position = -35, separatorAfter = -30)})
38+
@ActionReferences({
39+
@ActionReference(path = "Menu/TM&C", position = -35, separatorAfter = -30)})
3840
@Messages("CTL_DownloadSolutionAction=Download suggested &solution")
3941
public class DownloadSolutionAction extends AbstractExerciseSensitiveAction {
4042

@@ -108,24 +110,24 @@ protected void performAction(Node[] nodes) {
108110
return;
109111
}
110112

111-
String question =
112-
"Are you sure you want to OVERWRITE your copy of\n"
113-
+ ex.getName()
114-
+ " with the suggested solution?";
113+
String question
114+
= "Are you sure you want to OVERWRITE your copy of\n"
115+
+ ex.getName()
116+
+ " with the suggested solution?";
115117
String title = "Replace with solution?";
116118
dialogs.askYesNo(
117119
question,
118120
title,
119121
new Function<Boolean, Void>() {
120-
@Override
121-
public Void apply(Boolean yes) {
122-
if (yes) {
123-
eventBus.post(new InvokedEvent(ex));
124-
downloadSolution(ex, projectMediator.wrapProject(project));
125-
}
126-
return null;
127-
}
128-
});
122+
@Override
123+
public Void apply(Boolean yes) {
124+
if (yes) {
125+
eventBus.post(new InvokedEvent(ex));
126+
downloadSolution(ex, projectMediator.wrapProject(project));
127+
}
128+
return null;
129+
}
130+
});
129131
}
130132
}
131133

@@ -135,25 +137,27 @@ private void downloadSolution(final Exercise ex, final TmcProjectInfo proj) {
135137
Callable<Exercise> dlModelSolutionTask = TmcCore.get().downloadModelSolution(observer, exercise);
136138
BgTask.start("Downloading suggested solution", dlModelSolutionTask, observer,
137139
new BgTaskListener<Object>() {
138-
@Override
139-
public void bgTaskReady(Object result) {
140-
projectMediator.scanForExternalChanges(proj);
141-
}
140+
@Override
141+
public void bgTaskReady(Object result) {
142+
projectMediator.scanForExternalChanges(proj);
143+
}
142144

143-
@Override
144-
public void bgTaskCancelled() {}
145+
@Override
146+
public void bgTaskCancelled() {
147+
}
145148

146-
@Override
147-
public void bgTaskFailed(Throwable ex) {
148-
logger.log(Level.INFO, "Failed to extract solution.", ex);
149-
dialogs.displayError(
150-
"Failed to extract solution.\n"
151-
+ ServerErrorHelper.getServerExceptionMsg(ex));
152-
}
149+
@Override
150+
public void bgTaskFailed(Throwable ex) {
151+
logger.log(Level.INFO, "Failed to extract solution.", ex);
152+
SwingUtilities.invokeLater(() -> {
153+
dialogs.displayError(
154+
"Failed to extract solution.\n"
155+
+ ServerErrorHelper.getServerExceptionMsg(ex));
153156
});
157+
}
158+
});
154159
}
155160

156-
157161
private class ActionMenuItem extends JMenuItem implements DynamicMenuContent {
158162

159163
public ActionMenuItem() {
@@ -163,7 +167,7 @@ public ActionMenuItem() {
163167
@Override
164168
public JComponent[] getMenuPresenters() {
165169
if (DownloadSolutionAction.this.isEnabled()) {
166-
return new JComponent[] {getOriginalMenuPresenter()};
170+
return new JComponent[]{getOriginalMenuPresenter()};
167171
} else {
168172
return new JComponent[0];
169173
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
import java.util.concurrent.Callable;
3333
import java.util.logging.Level;
3434
import java.util.logging.Logger;
35+
import javax.swing.SwingUtilities;
3536

3637
@ActionID(category = "TMC", id = "fi.helsinki.cs.tmc.actions.PastebinAction")
3738
@ActionRegistration(displayName = "#CTL_PastebinAction", lazy = false)
3839
@ActionReferences({
39-
@ActionReference(path = "Menu/TM&C", position = 30),
40+
@ActionReference(path = "Menu/TM&C", position = 30)
41+
,
4042
@ActionReference(
4143
path = "Projects/Actions",
4244
position = 1340,
@@ -126,8 +128,10 @@ private void submitPaste(
126128
"Sending " + exercise.getName(), pasteTask, observer, new BgTaskListener<URI>() {
127129
@Override
128130
public void bgTaskReady(URI result) {
129-
new PastebinResponseDialog(result.toString())
130-
.setVisible(true);
131+
SwingUtilities.invokeLater(() -> {
132+
new PastebinResponseDialog(result.toString())
133+
.setVisible(true);
134+
});
131135
}
132136

133137
@Override
@@ -136,8 +140,10 @@ public void bgTaskCancelled() {
136140

137141
@Override
138142
public void bgTaskFailed(Throwable ex) {
139-
dialogs.displayError(
140-
"Failed to send exercise to pastebin", ex);
143+
SwingUtilities.invokeLater(() -> {
144+
dialogs.displayError(
145+
"Failed to send exercise to pastebin", ex);
146+
});
141147
}
142148
});
143149
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.concurrent.Callable;
1919
import java.util.logging.Level;
2020
import java.util.logging.Logger;
21+
import javax.swing.SwingUtilities;
2122

2223
/**
2324
* Refreshes the course list in the background.
@@ -133,7 +134,9 @@ public void bgTaskCancelled() {
133134
@Override
134135
public void bgTaskFailed(Throwable ex) {
135136
if (showDialogOnError) {
136-
dialogs.displayError("Course refresh failed.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
137+
SwingUtilities.invokeLater(() -> {
138+
dialogs.displayError("Course refresh failed.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
139+
});
137140
}
138141
}
139142
}

0 commit comments

Comments
 (0)