001package edu.pdx.cs410J.grader.poa.ui;
002
003import com.google.common.eventbus.EventBus;
004import com.google.common.eventbus.Subscribe;
005import com.google.inject.Guice;
006import com.google.inject.Inject;
007import com.google.inject.Injector;
008import com.google.inject.Singleton;
009import edu.pdx.cs410J.grader.poa.DownloadPOASubmissionsRequest;
010import edu.pdx.cs410J.grader.poa.LoadGradeBook;
011import edu.pdx.cs410J.grader.poa.POASubmission;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015import java.awt.*;
016import java.io.*;
017import java.time.LocalDateTime;
018
019@Singleton
020public class PlanOfAttackGrader {
021  private static final Logger logger = LoggerFactory.getLogger(PlanOfAttackGrader.class);
022  private final TopLevelJFrame parent;
023
024  @Inject
025  public PlanOfAttackGrader(TopLevelJFrame parent,
026                            POASubmissionsPanel submissions,
027                            POASubmissionInformationPanel submissionInfo,
028                            StatusMessageWidget statusMessage) {
029    this.parent = parent;
030
031    parent.setTitle("Plan Of Attack Grader");
032
033    Container content = parent.getContentPane();
034    content.setLayout(new BorderLayout());
035    content.add(submissions, BorderLayout.WEST);
036    content.add(submissionInfo, BorderLayout.CENTER);
037    content.add(statusMessage, BorderLayout.SOUTH);
038  }
039
040  public static void main(String[] args) {
041    Thread.currentThread().setUncaughtExceptionHandler((t, e) -> printStackTrace(e));
042
043    Injector injector = Guice.createInjector(new POAGraderUIModule());
044
045    EventBus bus = injector.getInstance(EventBus.class);
046    registerEventLogger(bus);
047
048    PlanOfAttackGrader ui = injector.getInstance(PlanOfAttackGrader.class);
049    ui.display();
050
051    if (args.length >= 1) {
052      String gradeBookFileName = args[0];
053      File gradeBookFile = new File(gradeBookFileName);
054      bus.post(new LoadGradeBook(gradeBookFile));
055    }
056
057    DownloadPOASubmissionsRequest downloadPOASubmissionsRequest;
058    if (args.length >= 3) {
059      String email = args[1];
060      String password = loadPasswordFromFile(args[2]);
061      downloadPOASubmissionsRequest = new DownloadPOASubmissionsRequest(email, password);
062
063    } else {
064      downloadPOASubmissionsRequest = new DownloadPOASubmissionsRequest();
065    }
066
067    bus.post(downloadPOASubmissionsRequest);
068  }
069
070  private static String loadPasswordFromFile(String fileName) {
071    File file = new File(fileName);
072    if (!file.exists()) {
073      System.err.println("Password file does not exist: " + file);
074      System.exit(1);
075    }
076
077    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
078      return br.readLine();
079
080    } catch (IOException e) {
081      System.err.println("While reading password from " + file);
082      e.printStackTrace(System.err);
083      System.exit(1);
084      return null;
085    }
086  }
087
088  private static void printStackTrace(Throwable e) {
089    e.fillInStackTrace();
090    e.printStackTrace(System.err);
091  }
092
093  private void display() {
094    Dimension fullScreen = Toolkit.getDefaultToolkit().getScreenSize();
095    parent.setPreferredSize(fullScreen);
096    int width = (int) (fullScreen.getWidth());
097    int height = (int) (fullScreen.getHeight());
098    parent.setPreferredSize(new Dimension(width, height));
099
100    parent.pack();
101    parent.setVisible(true);
102  }
103
104  private static void registerEventLogger(EventBus bus) {
105    bus.register(new Object() {
106      @Subscribe
107      public void logEvent(Object event) {
108        logger.debug("Event " + event);
109      }
110    });
111
112  }
113
114  private static POASubmission createPOASubmission(String subject) {
115    POASubmission.Builder builder = POASubmission.builder();
116
117    builder.setSubject(subject);
118    builder.setSubmitter("Submitter");
119    builder.setSubmitTime(LocalDateTime.now());
120
121    return builder.create();
122  }
123
124}