Skip to content

Commit 88fd961

Browse files
committed
Merge branch 'core_integration' of github.com:rage/tmc-netbeans into core_integration
2 parents c352090 + 1bd2088 commit 88fd961

File tree

7 files changed

+142
-134
lines changed

7 files changed

+142
-134
lines changed

maven-wrapper/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<repository>
2525
<id>netbeans</id>
2626
<name>NetBeans</name>
27-
<url>http://bits.netbeans.org/maven2/</url>
27+
<url>http://bits.netbeans.org/nexus/content/groups/netbeans/</url>
2828
<snapshots>
2929
<enabled>false</enabled>
3030
</snapshots>
@@ -321,7 +321,6 @@
321321
<artifactId>javax-websocket-client-impl</artifactId>
322322
<version>9.2.5.v20141112</version>
323323
</dependency>
324-
325324
</dependencies>
326325

327326
<build>

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

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

3+
import com.google.common.util.concurrent.FutureCallback;
4+
import com.google.common.util.concurrent.Futures;
5+
import com.google.common.util.concurrent.ListenableFuture;
36
import hy.tmc.core.domain.Course;
4-
import fi.helsinki.cs.tmc.data.CourseListUtils;
57
import fi.helsinki.cs.tmc.model.CourseDb;
68
import fi.helsinki.cs.tmc.model.LocalExerciseStatus;
79
import fi.helsinki.cs.tmc.model.ObsoleteClientException;
810
import fi.helsinki.cs.tmc.model.ServerAccess;
911
import fi.helsinki.cs.tmc.model.NBTmcSettings;
12+
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
1013
import fi.helsinki.cs.tmc.ui.DownloadOrUpdateExercisesDialog;
1114
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
1215
import fi.helsinki.cs.tmc.ui.TmcNotificationDisplayer;
13-
import fi.helsinki.cs.tmc.utilities.BgTask;
14-
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
1516
import fi.helsinki.cs.tmc.utilities.Inflector;
1617
import fi.helsinki.cs.tmc.utilities.TmcStringUtils;
18+
import hy.tmc.core.TmcCore;
19+
import hy.tmc.core.exceptions.TmcCoreException;
1720
import java.awt.event.ActionEvent;
1821
import java.awt.event.ActionListener;
1922
import java.util.ArrayList;
20-
import java.util.List;
2123
import javax.swing.AbstractAction;
2224
import javax.swing.Icon;
2325
import org.apache.commons.lang3.StringUtils;
2426
import org.openide.awt.ActionID;
2527
import org.openide.awt.ActionReference;
2628
import org.openide.awt.ActionReferences;
2729
import org.openide.awt.ActionRegistration;
30+
import org.openide.util.Exceptions;
2831
import org.openide.util.ImageUtilities;
2932
import org.openide.util.NbBundle.Messages;
3033

3134
@ActionID(category = "TMC",
32-
id = "fi.helsinki.cs.tmc.actions.CheckForNewExercisesOrUpdates")
35+
id = "fi.helsinki.cs.tmc.actions.CheckForNewExercisesOrUpdates")
3336
@ActionRegistration(displayName = "#CTL_CheckForNewExercisesOrUpdates")
3437
@ActionReferences({
3538
@ActionReference(path = "Menu/TM&C", position = -50)
@@ -38,94 +41,124 @@
3841
public class CheckForNewExercisesOrUpdates extends AbstractAction {
3942

4043
public static void startTimer() {
41-
int interval = 20*60*1000; // 20 minutes
44+
int interval = 20 * 60 * 1000; // 20 minutes
4245
javax.swing.Timer timer = new javax.swing.Timer(interval, new CheckForNewExercisesOrUpdates(true, true));
4346
timer.setRepeats(true);
4447
timer.start();
4548
}
46-
49+
4750
private static final TmcNotificationDisplayer.SingletonToken notifierToken = TmcNotificationDisplayer.createSingletonToken();
48-
51+
4952
private CourseDb courseDb;
5053
private ServerAccess serverAccess;
5154
private TmcNotificationDisplayer notifier;
5255
private ConvenientDialogDisplayer dialogs;
5356
private boolean beQuiet;
5457
private boolean backgroundCheck;
58+
private TmcCore tmcCore;
5559

5660
public CheckForNewExercisesOrUpdates() {
5761
this(false, false);
5862
}
59-
63+
6064
public CheckForNewExercisesOrUpdates(boolean beQuiet, boolean backgroundCheck) {
6165
this.courseDb = CourseDb.getInstance();
6266
this.serverAccess = new ServerAccess();
6367
this.notifier = TmcNotificationDisplayer.getDefault();
6468
this.dialogs = ConvenientDialogDisplayer.getDefault();
6569
this.beQuiet = beQuiet;
6670
this.backgroundCheck = backgroundCheck;
71+
this.tmcCore = TmcCoreSingleton.getInstance();
6772
}
6873

6974
@Override
7075
public void actionPerformed(ActionEvent e) {
7176
run();
7277
}
73-
78+
79+
//"Checking for new exercises" päivitä tämä näkymään progressHandleen myöhemmin
80+
/**
81+
*
82+
*/
7483
public void run() {
75-
final Course currentCourseBeforeUpdate = courseDb.getCurrentCourse();
76-
84+
try {
85+
final Course currentCourseBeforeUpdate = courseDb.getCurrentCourse();
86+
if (backgroundProcessingOrNoCurrentCourse(currentCourseBeforeUpdate)) {
87+
return;
88+
}
89+
ListenableFuture<Course> currentCourseFuture = this.tmcCore.getCourse(
90+
NBTmcSettings.getDefault(), currentCourseBeforeUpdate.getDetailsUrl()
91+
);
92+
Futures.addCallback(currentCourseFuture, new UpdateCourseForExerciseUpdate());
93+
} catch (TmcCoreException ex) {
94+
Exceptions.printStackTrace(ex);
95+
}
96+
}
97+
98+
/**
99+
* If there is something at background or no current course is chosen,
100+
* return true.
101+
*/
102+
private boolean backgroundProcessingOrNoCurrentCourse(final Course currentCourseBeforeUpdate) {
77103
if (backgroundCheck && !NBTmcSettings.getDefault().isCheckingForUpdatesInTheBackground()) {
78-
return;
104+
return true;
79105
}
80-
81106
if (currentCourseBeforeUpdate == null) {
82107
if (!beQuiet) {
83108
dialogs.displayMessage("Please select a course in TMC -> Settings.");
84109
}
85-
return;
110+
return true;
111+
}
112+
return false;
113+
}
114+
115+
class UpdateCourseForExerciseUpdate implements FutureCallback<Course> {
116+
117+
/**
118+
* This should be attached to listenableFuture. When future is ready,
119+
* receivedCourse will be saved to courseDb and view will be updated.
120+
*/
121+
public UpdateCourseForExerciseUpdate() {
122+
}
123+
124+
@Override
125+
public void onSuccess(Course receivedCourse) {
126+
if (receivedCourse != null) {
127+
courseDb.putDetailedCourse(receivedCourse);
128+
final LocalExerciseStatus status = LocalExerciseStatus.get(receivedCourse.getExercises());
129+
updateGUI(status);
130+
}
86131
}
87-
88-
BgTask.start("Checking for new exercises", serverAccess.getFullCourseInfoTask(currentCourseBeforeUpdate), new BgTaskListener<Course>() {
89-
@Override
90-
public void bgTaskReady(Course receivedCourse) {
91-
if (receivedCourse != null) {
92-
courseDb.putDetailedCourse(receivedCourse);
93-
94-
final LocalExerciseStatus status = LocalExerciseStatus.get(receivedCourse.getExercises());
95-
if (status.thereIsSomethingToDownload(false)) {
96-
if (beQuiet) {
97-
displayNotification(status, new ActionListener() {
98-
@Override
99-
public void actionPerformed(ActionEvent e) {
100-
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
101-
}
102-
});
103-
} else {
132+
133+
private void updateGUI(final LocalExerciseStatus status) {
134+
if (status.thereIsSomethingToDownload(false)) {
135+
if (beQuiet) {
136+
displayNotification(status, new ActionListener() {
137+
@Override
138+
public void actionPerformed(ActionEvent e) {
104139
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
105140
}
106-
} else if (!beQuiet) {
107-
dialogs.displayMessage("No new exercises or updates to download.");
108-
}
141+
});
142+
} else {
143+
DownloadOrUpdateExercisesDialog.display(status.unlockable, status.downloadableUncompleted, status.updateable);
109144
}
145+
} else if (!beQuiet) {
146+
dialogs.displayMessage("No new exercises or updates to download.");
110147
}
148+
}
111149

112-
@Override
113-
public void bgTaskCancelled() {
114-
}
115-
116-
@Override
117-
public void bgTaskFailed(Throwable ex) {
118-
if (!beQuiet || ex instanceof ObsoleteClientException) {
119-
dialogs.displayError("Failed to check for new exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
120-
}
150+
@Override
151+
public void onFailure(Throwable ex) {
152+
if (!beQuiet || ex instanceof ObsoleteClientException) {
153+
dialogs.displayError("Failed to check for new exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
121154
}
122-
});
155+
}
123156
}
124157

125158
private void displayNotification(LocalExerciseStatus status, ActionListener action) {
126159
ArrayList<String> items = new ArrayList<String>();
127160
ArrayList<String> actions = new ArrayList<String>();
128-
161+
129162
if (!status.unlockable.isEmpty()) {
130163
items.add(Inflector.pluralize(status.unlockable.size(), "an unlockable exercise"));
131164
actions.add("unlock");
@@ -138,18 +171,18 @@ private void displayNotification(LocalExerciseStatus status, ActionListener acti
138171
items.add(Inflector.pluralize(status.updateable.size(), "an update"));
139172
actions.add("update");
140173
}
141-
142-
int total =
143-
status.unlockable.size() +
144-
status.downloadableUncompleted.size() +
145-
status.updateable.size();
146-
174+
175+
int total
176+
= status.unlockable.size()
177+
+ status.downloadableUncompleted.size()
178+
+ status.updateable.size();
179+
147180
String msg = TmcStringUtils.joinCommaAnd(items);
148181
msg += " " + Inflector.pluralize(total, "is") + " available.";
149182
msg = StringUtils.capitalize(msg);
150-
183+
151184
String prompt = "Click here to " + TmcStringUtils.joinCommaAnd(actions) + ".";
152-
185+
153186
notifier.notify(notifierToken, msg, getNotificationIcon(), prompt, action);
154187
}
155188

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,34 @@ public final class RefreshCoursesAction {
3737
public RefreshCoursesAction() {
3838
this(NBTmcSettings.getDefault());
3939
}
40-
40+
41+
/**
42+
* Default constructor.
43+
*/
4144
public RefreshCoursesAction(NBTmcSettings settings) {
45+
this(settings, TmcCoreSingleton.getInstance());
46+
}
47+
48+
/**
49+
* Dependency inject TmcCore for tests.
50+
*/
51+
public RefreshCoursesAction(NBTmcSettings settings, TmcCore core) {
4252
this.tmcSettings = settings;
4353
this.serverAccess = new ServerAccess(settings);
4454
this.serverAccess.setSettings(settings);
4555
this.courseDb = CourseDb.getInstance();
4656
this.dialogs = ConvenientDialogDisplayer.getDefault();
47-
48-
//this.listeners = new BgTaskListenerList<List<Course>>();
4957
this.callbacks = new FutureCallbackList<List<Course>>();
50-
this.tmcCore = TmcCoreSingleton.getInstance();
58+
this.tmcCore = core;
5159
}
5260

5361
public RefreshCoursesAction addDefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
5462
this.callbacks.addListener(new DefaultListener(showDialogOnError, updateCourseDb));
5563
return this;
5664
}
5765

58-
public RefreshCoursesAction addListener(FutureCallback<List<Course>> callbacks) {
59-
this.callbacks.addListener(callbacks);
66+
public RefreshCoursesAction addListener(FutureCallback<List<Course>> callback) {
67+
this.callbacks.addListener(callback);
6068
return this;
6169
}
6270

tmc-plugin/src/fi/helsinki/cs/tmc/model/NBTmcSettings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public void save() {
8989
}
9090
settings.saveAll();
9191
eventBus.post(new SavedEvent());
92-
9392
}
9493

9594
@Override

0 commit comments

Comments
 (0)