11package 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 ;
3+ import static java .util .logging .Level .INFO ;
4+
65import fi .helsinki .cs .tmc .core .domain .Course ;
76import fi .helsinki .cs .tmc .model .CourseDb ;
87import fi .helsinki .cs .tmc .model .LocalExerciseStatus ;
98import 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 ;
1210import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
1311import fi .helsinki .cs .tmc .ui .DownloadOrUpdateExercisesDialog ;
1412import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
2624import javax .swing .AbstractAction ;
2725import javax .swing .Icon ;
2826import 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+
2933import org .apache .commons .lang3 .StringUtils ;
30- import org .netbeans .api .progress .ProgressHandle ;
31- import org .netbeans .api .progress .ProgressHandleFactory ;
34+
3235import org .openide .awt .ActionID ;
3336import org .openide .awt .ActionReference ;
3437import org .openide .awt .ActionReferences ;
3538import org .openide .awt .ActionRegistration ;
36- import org .openide .util .Exceptions ;
3739import org .openide .util .ImageUtilities ;
3840import org .openide .util .NbBundle .Messages ;
3941
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+
4049@ ActionID (category = "TMC" ,
4150 id = "fi.helsinki.cs.tmc.actions.CheckForNewExercisesOrUpdates" )
4251@ ActionRegistration (displayName = "#CTL_CheckForNewExercisesOrUpdates" )
@@ -53,14 +62,16 @@ public static void startTimer() {
5362 timer .start ();
5463 }
5564
65+ private static final Logger logger = Logger .getLogger (CheckForNewExercisesOrUpdates .class .getName ());
66+
5667 private static final TmcNotificationDisplayer .SingletonToken notifierToken = TmcNotificationDisplayer .createSingletonToken ();
5768
5869 private CourseDb courseDb ;
5970 private TmcNotificationDisplayer notifier ;
6071 private ConvenientDialogDisplayer dialogs ;
6172 private boolean beQuiet ;
6273 private boolean backgroundCheck ;
63- private TmcCore tmcCore ;
74+ private final TmcCore tmcCore ;
6475
6576 public CheckForNewExercisesOrUpdates () {
6677 this (false , false );
@@ -82,6 +93,7 @@ public void actionPerformed(ActionEvent e) {
8293
8394 public void run () {
8495 final Course currentCourseBeforeUpdate = courseDb .getCurrentCourse ();
96+ <<<<<<< HEAD
8597 try {
8698 if (backgroundProcessingOrNoCurrentCourse (currentCourseBeforeUpdate )) {
8799 return ;
@@ -112,87 +124,72 @@ private URI detailUrl(final Course currentCourseBeforeUpdate) throws URISyntaxEx
112124 * return true.
113125 */
114126 private boolean backgroundProcessingOrNoCurrentCourse (final Course currentCourseBeforeUpdate ) {
115- if (backgroundCheck && !NBTmcSettings .getDefault ().isCheckingForUpdatesInTheBackground ()) {
116- return true ;
127+ if (backgroundCheck && !NbTmcSettings .getDefault ().isCheckingForUpdatesInTheBackground ()) {
128+ return ;
117129 }
130+
118131 if (currentCourseBeforeUpdate == null ) {
119132 if (!beQuiet ) {
120133 dialogs .displayMessage ("Please select a course in TMC -> Settings." );
121134 }
122- return true ;
135+ return ;
123136 }
124- return false ;
125- }
126137
127- class UpdateCourseForExerciseUpdate implements FutureCallback <Course > {
138+ BgTaskListener bgTaskListener = new BgTaskListener <Course >() {
139+ @ Override
140+ public void bgTaskReady (Course receivedCourse ) {
141+ if (receivedCourse != null ) {
128142
129- private ProgressHandle lastAction ;
143+ courseDb . putDetailedCourse ( receivedCourse ) ;
130144
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 ());
138146
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." );
151160 }
152161 }
162+ }
153163
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 () {
161166 }
162- }
163167
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 ));
176172 }
177- } else if (!beQuiet ) {
178- dialogs .displayMessage ("No new exercises or updates to download." );
179173 }
180- }
174+ };
181175
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 ;
185178
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 );
193192
194- });
195- }
196193 }
197194
198195 private void displayNotification (LocalExerciseStatus status , ActionListener action ) {
0 commit comments