-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCheckForNewExercisesOrUpdates.java
More file actions
191 lines (160 loc) · 7.78 KB
/
CheckForNewExercisesOrUpdates.java
File metadata and controls
191 lines (160 loc) · 7.78 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package fi.helsinki.cs.tmc.actions;
import static java.util.logging.Level.INFO;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.model.CourseDb;
import fi.helsinki.cs.tmc.model.LocalExerciseStatus;
import fi.helsinki.cs.tmc.model.ObsoleteClientException;
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
import fi.helsinki.cs.tmc.ui.DownloadOrUpdateExercisesDialog;
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
import fi.helsinki.cs.tmc.ui.TmcNotificationDisplayer;
import fi.helsinki.cs.tmc.utilities.Inflector;
import fi.helsinki.cs.tmc.utilities.TmcStringUtils;
import fi.helsinki.cs.tmc.core.TmcCore;
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.apache.commons.lang3.StringUtils;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle.Messages;
import java.util.logging.Logger;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.Icon;
@ActionID(category = "TMC", id = "fi.helsinki.cs.tmc.actions.CheckForNewExercisesOrUpdates")
@ActionRegistration(displayName = "#CTL_CheckForNewExercisesOrUpdates")
@ActionReferences({@ActionReference(path = "Menu/TM&C", position = -50)})
@Messages("CTL_CheckForNewExercisesOrUpdates=&Download/update exercises")
public class CheckForNewExercisesOrUpdates extends AbstractAction {
public static void startTimer() {
int interval = 20 * 60 * 1000; // 20 minutes
javax.swing.Timer timer =
new javax.swing.Timer(interval, new CheckForNewExercisesOrUpdates(true, true));
timer.setRepeats(true);
timer.start();
}
private static final Logger logger =
Logger.getLogger(CheckForNewExercisesOrUpdates.class.getName());
private static final TmcNotificationDisplayer.SingletonToken notifierToken =
TmcNotificationDisplayer.createSingletonToken();
private CourseDb courseDb;
private TmcNotificationDisplayer notifier;
private ConvenientDialogDisplayer dialogs;
private boolean beQuiet;
private boolean backgroundCheck;
private final TmcCore tmcCore;
public CheckForNewExercisesOrUpdates() {
this(false, false);
}
public CheckForNewExercisesOrUpdates(boolean beQuiet, boolean backgroundCheck) {
this.courseDb = CourseDb.getInstance();
this.notifier = TmcNotificationDisplayer.getDefault();
this.dialogs = ConvenientDialogDisplayer.getDefault();
this.beQuiet = beQuiet;
this.backgroundCheck = backgroundCheck;
this.tmcCore = TmcCoreSingleton.getInstance();
}
@Override
public void actionPerformed(ActionEvent e) {
run();
}
public void run() {
final Course currentCourseBeforeUpdate = courseDb.getCurrentCourse();
BgTaskListener bgTaskListener =
new BgTaskListener<Course>() {
@Override
public void bgTaskReady(Course receivedCourse) {
if (receivedCourse != null) {
courseDb.putDetailedCourse(receivedCourse);
final LocalExerciseStatus status =
LocalExerciseStatus.get(receivedCourse.getExercises());
if (status.thereIsSomethingToDownload(false)) {
if (beQuiet) {
displayNotification(
status,
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DownloadOrUpdateExercisesDialog.display(
status.unlockable,
status.downloadableUncompleted,
status.updateable);
}
});
} else {
DownloadOrUpdateExercisesDialog.display(
status.unlockable,
status.downloadableUncompleted,
status.updateable);
}
} else if (!beQuiet) {
dialogs.displayMessage("No new exercises or updates to download.");
}
}
}
@Override
public void bgTaskCancelled() {}
@Override
public void bgTaskFailed(Throwable ex) {
if (!beQuiet || ex instanceof ObsoleteClientException) {
dialogs.displayError(
"Failed to check for new exercises.\n"
+ ServerErrorHelper.getServerExceptionMsg(ex));
}
}
};
BgTask.start(
"Checking for new exercises",
new CancellableCallable<Course>() {
ListenableFuture<Course> currentCourseFuture;
@Override
public Course call() throws Exception {
logger.info("Downloading course to refresh cache");
currentCourseFuture =
tmcCore.getCourse(currentCourseBeforeUpdate.getDetailsUrl());
return currentCourseFuture.get();
}
@Override
public boolean cancel() {
logger.log(INFO, "Get course (refresh list) cancelled.");
return currentCourseFuture.cancel(true);
}
},
bgTaskListener);
}
private void displayNotification(LocalExerciseStatus status, ActionListener action) {
ArrayList<String> items = new ArrayList<String>();
ArrayList<String> actions = new ArrayList<String>();
if (!status.unlockable.isEmpty()) {
items.add(Inflector.pluralize(status.unlockable.size(), "an unlockable exercise"));
actions.add("unlock");
}
if (!status.downloadableUncompleted.isEmpty()) {
items.add(Inflector.pluralize(status.downloadableUncompleted.size(), "a new exercise"));
actions.add("download");
}
if (!status.updateable.isEmpty()) {
items.add(Inflector.pluralize(status.updateable.size(), "an update"));
actions.add("update");
}
int total =
status.unlockable.size()
+ status.downloadableUncompleted.size()
+ status.updateable.size();
String msg = TmcStringUtils.joinCommaAnd(items);
msg += " " + Inflector.pluralize(total, "is") + " available.";
msg = StringUtils.capitalize(msg);
String prompt = "Click here to " + TmcStringUtils.joinCommaAnd(actions) + ".";
notifier.notify(notifierToken, msg, getNotificationIcon(), prompt, action);
}
private Icon getNotificationIcon() {
return ImageUtilities.loadImageIcon("fi/helsinki/cs/tmc/smile.gif", false);
}
}