001package edu.pdx.cs410J.grader.poa.ui;
002
003import com.google.common.eventbus.EventBus;
004import com.google.inject.AbstractModule;
005import com.google.inject.Provides;
006import com.google.inject.Singleton;
007import com.google.inject.name.Named;
008import edu.pdx.cs410J.grader.poa.*;
009
010import java.util.concurrent.*;
011
012public class POAGraderUIModule extends AbstractModule {
013  @Override
014  protected void configure() {
015
016    bind(POASubmissionsView.class).to(POASubmissionsPanel.class);
017    bind(POASubmissionView.class).to(POASubmissionInformationWidgets.class);
018    bind(GradeBookView.class).to(GradeBookWidget.class);
019    bind(UnhandledExceptionView.class).to(UnhandledExceptionDialog.class).asEagerSingleton();
020    bind(StudentsView.class).to(StudentsWidget.class);
021    bind(POAAssignmentsView.class).to(POAAssignmentsWidget.class);
022    bind(POAGradeView.class).to(POAGradeWidgets.class);
023    bind(EmailCredentialsView.class).to(EmailCredentialsDialog.class).asEagerSingleton();
024    bind(StatusMessageView.class).to(StatusMessageWidget.class);
025
026    bind(POASubmissionsPresenter.class).asEagerSingleton();
027    bind(POASubmissionPresenter.class).asEagerSingleton();
028    bind(GradeBookPresenter.class).asEagerSingleton();
029    bind(UnhandledExceptionPresenter.class).asEagerSingleton();
030    bind(StudentsPresenter.class).asEagerSingleton();
031    bind(POAAssignmentsPresenter.class).asEagerSingleton();
032    bind(POAGradePresenter.class).asEagerSingleton();
033    bind(EmailCredentialsPresenter.class).asEagerSingleton();
034    bind(StatusMessagePresenter.class).asEagerSingleton();
035
036    bind(GradeBookFileManager.class).asEagerSingleton();
037    bind(POASubmissionsDownloader.class).asEagerSingleton();
038  }
039
040  @Provides
041  @Singleton
042  public EventBus provideEventBus() {
043    return new EventBusThatPublishesUnhandledExceptionEvents();
044  }
045
046  @Provides
047  @Singleton
048  @Named("POADownloaderExecutor")
049  public Executor provideDownloaderExecutor(EventBus bus) {
050    return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new ThreadFactory() {
051      @Override
052      public Thread newThread(Runnable runnable) {
053        return new Thread(runnable, "POA Downloader");
054      }
055    }) {
056      @Override
057      protected void afterExecute(Runnable r, Throwable t) {
058        super.afterExecute(r, t);
059        if (t == null && r instanceof Future<?> && ((Future<?>) r).isDone()) {
060          try {
061            ((Future<?>) r).get();
062          } catch (CancellationException ce) {
063            t = ce;
064          } catch (ExecutionException ee) {
065            t = ee.getCause();
066          } catch (InterruptedException ie) {
067            // ignore/reset
068            Thread.currentThread().interrupt();
069          }
070        }
071        if (t != null) {
072          bus.post(new UnhandledExceptionEvent(t));
073        }
074      }
075    };
076  }
077
078}