1
1
package fi .helsinki .cs .tmc .actions ;
2
2
3
- import com .google .common .util .concurrent .FutureCallback ;
4
- import com .google .common .util .concurrent .Futures ;
5
- import com .google .common .util .concurrent .ListenableFuture ;
3
+ import static java .util .logging .Level .INFO ;
4
+
6
5
import fi .helsinki .cs .tmc .core .domain .Course ;
7
6
import fi .helsinki .cs .tmc .model .CourseDb ;
8
7
import fi .helsinki .cs .tmc .model .LocalExerciseStatus ;
9
8
import fi .helsinki .cs .tmc .model .ObsoleteClientException ;
10
- import fi .helsinki .cs .tmc .model .NBTmcSettings ;
11
- import fi .helsinki .cs .tmc .model .ServerAccess ;
9
+ import fi .helsinki .cs .tmc .model .NbTmcSettings ;
12
10
import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
13
11
import fi .helsinki .cs .tmc .ui .DownloadOrUpdateExercisesDialog ;
14
12
import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
26
24
import javax .swing .AbstractAction ;
27
25
import javax .swing .Icon ;
28
26
import javax .swing .SwingUtilities ;
27
+ import fi .helsinki .cs .tmc .utilities .BgTask ;
28
+ import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
29
+ import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
30
+
31
+ import com .google .common .util .concurrent .ListenableFuture ;
32
+
29
33
import org .apache .commons .lang3 .StringUtils ;
30
- import org .netbeans .api .progress .ProgressHandle ;
31
- import org .netbeans .api .progress .ProgressHandleFactory ;
34
+
32
35
import org .openide .awt .ActionID ;
33
36
import org .openide .awt .ActionReference ;
34
37
import org .openide .awt .ActionReferences ;
35
38
import org .openide .awt .ActionRegistration ;
36
- import org .openide .util .Exceptions ;
37
39
import org .openide .util .ImageUtilities ;
38
40
import org .openide .util .NbBundle .Messages ;
39
41
42
+ import java .util .logging .Logger ;
43
+ import java .awt .event .ActionEvent ;
44
+ import java .awt .event .ActionListener ;
45
+ import java .util .ArrayList ;
46
+ import javax .swing .AbstractAction ;
47
+ import javax .swing .Icon ;
48
+
40
49
@ ActionID (category = "TMC" ,
41
50
id = "fi.helsinki.cs.tmc.actions.CheckForNewExercisesOrUpdates" )
42
51
@ ActionRegistration (displayName = "#CTL_CheckForNewExercisesOrUpdates" )
@@ -53,14 +62,16 @@ public static void startTimer() {
53
62
timer .start ();
54
63
}
55
64
65
+ private static final Logger logger = Logger .getLogger (CheckForNewExercisesOrUpdates .class .getName ());
66
+
56
67
private static final TmcNotificationDisplayer .SingletonToken notifierToken = TmcNotificationDisplayer .createSingletonToken ();
57
68
58
69
private CourseDb courseDb ;
59
70
private TmcNotificationDisplayer notifier ;
60
71
private ConvenientDialogDisplayer dialogs ;
61
72
private boolean beQuiet ;
62
73
private boolean backgroundCheck ;
63
- private TmcCore tmcCore ;
74
+ private final TmcCore tmcCore ;
64
75
65
76
public CheckForNewExercisesOrUpdates () {
66
77
this (false , false );
@@ -82,6 +93,7 @@ public void actionPerformed(ActionEvent e) {
82
93
83
94
public void run () {
84
95
final Course currentCourseBeforeUpdate = courseDb .getCurrentCourse ();
96
+ <<<<<<< HEAD
85
97
try {
86
98
if (backgroundProcessingOrNoCurrentCourse (currentCourseBeforeUpdate )) {
87
99
return ;
@@ -112,87 +124,72 @@ private URI detailUrl(final Course currentCourseBeforeUpdate) throws URISyntaxEx
112
124
* return true.
113
125
*/
114
126
private boolean backgroundProcessingOrNoCurrentCourse (final Course currentCourseBeforeUpdate ) {
115
- if (backgroundCheck && !NBTmcSettings .getDefault ().isCheckingForUpdatesInTheBackground ()) {
116
- return true ;
127
+ if (backgroundCheck && !NbTmcSettings .getDefault ().isCheckingForUpdatesInTheBackground ()) {
128
+ return ;
117
129
}
130
+
118
131
if (currentCourseBeforeUpdate == null ) {
119
132
if (!beQuiet ) {
120
133
dialogs .displayMessage ("Please select a course in TMC -> Settings." );
121
134
}
122
- return true ;
135
+ return ;
123
136
}
124
- return false ;
125
- }
126
137
127
- class UpdateCourseForExerciseUpdate implements FutureCallback <Course > {
138
+ BgTaskListener bgTaskListener = new BgTaskListener <Course >() {
139
+ @ Override
140
+ public void bgTaskReady (Course receivedCourse ) {
141
+ if (receivedCourse != null ) {
128
142
129
- private ProgressHandle lastAction ;
143
+ courseDb . putDetailedCourse ( receivedCourse ) ;
130
144
131
- /**
132
- * This should be attached to listenableFuture. When future is ready,
133
- * receivedCourse will be saved to courseDb and view will be updated.
134
- */
135
- public UpdateCourseForExerciseUpdate (ProgressHandle lastAction ) {
136
- this .lastAction = lastAction ;
137
- }
145
+ final LocalExerciseStatus status = LocalExerciseStatus .get (receivedCourse .getExercises ());
138
146
139
- @ Override
140
- public void onSuccess (final Course receivedCourse ) {
141
- SwingUtilities .invokeLater (new Runnable () {
142
-
143
- @ Override
144
- public void run () {
145
- lastAction .finish ();
146
- if (receivedCourse != null ) {
147
- setCourseNameToAllExercises (receivedCourse );
148
- courseDb .putDetailedCourse (receivedCourse );
149
- final LocalExerciseStatus status = LocalExerciseStatus .get (receivedCourse .getExercises ());
150
- updateGUI (status );
147
+ if (status .thereIsSomethingToDownload (false )) {
148
+ if (beQuiet ) {
149
+ displayNotification (status , new ActionListener () {
150
+ @ Override
151
+ public void actionPerformed (ActionEvent e ) {
152
+ DownloadOrUpdateExercisesDialog .display (status .unlockable , status .downloadableUncompleted , status .updateable );
153
+ }
154
+ });
155
+ } else {
156
+ DownloadOrUpdateExercisesDialog .display (status .unlockable , status .downloadableUncompleted , status .updateable );
157
+ }
158
+ } else if (!beQuiet ) {
159
+ dialogs .displayMessage ("No new exercises or updates to download." );
151
160
}
152
161
}
162
+ }
153
163
154
- });
155
-
156
- }
157
-
158
- private void setCourseNameToAllExercises (Course receivedCourse ) {
159
- for (Exercise exercise : receivedCourse .getExercises ()) {
160
- exercise .setCourseName (receivedCourse .getName ());
164
+ @ Override
165
+ public void bgTaskCancelled () {
161
166
}
162
- }
163
167
164
- private void updateGUI (final LocalExerciseStatus status ) {
165
- boolean thereIsSomethingToDownload = status .thereIsSomethingToDownload (false );
166
- if (thereIsSomethingToDownload ) {
167
- if (beQuiet ) {
168
- displayNotification (status , new ActionListener () {
169
- @ Override
170
- public void actionPerformed (ActionEvent e ) {
171
- DownloadOrUpdateExercisesDialog .display (status .unlockable , status .downloadableUncompleted , status .updateable );
172
- }
173
- });
174
- } else {
175
- DownloadOrUpdateExercisesDialog .display (status .unlockable , status .downloadableUncompleted , status .updateable );
168
+ @ Override
169
+ public void bgTaskFailed (Throwable ex ) {
170
+ if (!beQuiet || ex instanceof ObsoleteClientException ) {
171
+ dialogs .displayError ("Failed to check for new exercises.\n " + ServerErrorHelper .getServerExceptionMsg (ex ));
176
172
}
177
- } else if (!beQuiet ) {
178
- dialogs .displayMessage ("No new exercises or updates to download." );
179
173
}
180
- }
174
+ };
181
175
182
- @ Override
183
- public void onFailure (final Throwable ex ) {
184
- SwingUtilities .invokeLater (new Runnable () {
176
+ BgTask .start ("Checking for new exercises" , new CancellableCallable <Course >() {
177
+ ListenableFuture <Course > currentCourseFuture ;
185
178
186
- @ Override
187
- public void run () {
188
- lastAction .finish ();
189
- if (!beQuiet || ex instanceof ObsoleteClientException ) {
190
- dialogs .displayError ("Failed to check for new exercises.\n " + ServerErrorHelper .getServerExceptionMsg (ex ));
191
- }
192
- }
179
+ @ Override
180
+ public Course call () throws Exception {
181
+ logger .info ("Downloading course to refresh cache" );
182
+ currentCourseFuture = tmcCore .getCourse (currentCourseBeforeUpdate .getDetailsUrlAsUri ());
183
+ return currentCourseFuture .get ();
184
+ }
185
+
186
+ @ Override
187
+ public boolean cancel () {
188
+ logger .log (INFO , "Get course (refresh list) cancelled." );
189
+ return currentCourseFuture .cancel (true );
190
+ }
191
+ }, bgTaskListener );
193
192
194
- });
195
- }
196
193
}
197
194
198
195
private void displayNotification (LocalExerciseStatus status , ActionListener action ) {
0 commit comments