package org.dlect.ui;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import javax.swing.SwingUtilities;
import org.dlect.controller.MainController;
import org.dlect.controller.event.ControllerEvent;
import org.dlect.controller.event.ControllerState;
import org.dlect.controller.event.ControllerType;
import org.dlect.controller.helper.Controller;
import org.dlect.controller.helper.ControllerStateHelper;
import org.dlect.controller.helper.ControllerStateHelper.ControllerStateHelperEventID;
import org.dlect.controller.helper.subject.SubjectDataHelper.DownloadState;
import org.dlect.controller.helper.subject.SubjectInformation;
import org.dlect.controller.worker.ErrorDisplayable;
import org.dlect.controller.worker.SubjectWorker;
import org.dlect.controller.worker.download.DownloadErrorDisplayable;
import org.dlect.controller.worker.download.DownloadWorkerHelper;
import org.dlect.event.util.EventIdListings;
import org.dlect.events.Event;
import org.dlect.events.EventListener;
import org.dlect.events.wrapper.Wrappers;
import org.dlect.exception.DLectExceptionCause;
import org.dlect.model.Database;
import org.dlect.model.Lecture;
import org.dlect.model.LectureDownload;
import org.dlect.model.Semester;
import org.dlect.model.Subject;
import org.dlect.model.formatter.DownloadType;
import org.dlect.ui.decorator.DownloadButtonDotter;
import org.dlect.ui.helper.ControllerErrorBoxHelper;
import org.dlect.ui.prefs.PreferencesDialog;
import org.dlect.ui.prefs.PreferencesDialogImpl;
import org.dlect.ui.subject.MultiSubjectDisplayPanel;
import static org.dlect.controller.helper.subject.SubjectDataHelper.DownloadState.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author lee
*/
public class CoursesScreen extends javax.swing.JPanel implements
EventListener, ErrorDisplayable, DownloadErrorDisplayable {
private static final long serialVersionUID = 1L;
private final MainController controller;
private final DownloadButtonDotter dbd;
private PreferencesDialog prefsDialog;
private final MultiSubjectDisplayPanel msdp;
/**
* Creates new form CoursesScreen
*
* @param controller
*/
public CoursesScreen(MainController controller) {
this.controller = controller;
this.msdp = new MultiSubjectDisplayPanel(controller);
initComponents();
dbd = new DownloadButtonDotter(downloadAllButton);
dbd.start();
Wrappers.addSwingListenerTo(this, controller, Database.class, Semester.class, Subject.class, Lecture.class, LectureDownload.class, Controller.class, ControllerStateHelper.class);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
bottomPanel = new javax.swing.JPanel();
jSeparator1 = new javax.swing.JSeparator();
preferencesButton = new javax.swing.JButton();
downloadAllButton = new javax.swing.JButton();
coursesScrollPane = new javax.swing.JScrollPane();
javax.swing.JPanel scrollContainerPanel = new javax.swing.JPanel();
javax.swing.JPanel subjectDisplayPanel = msdp;
setMinimumSize(new java.awt.Dimension(400, 300));
setLayout(new java.awt.GridBagLayout());
bottomPanel.setLayout(new java.awt.GridBagLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 10;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
bottomPanel.add(jSeparator1, gridBagConstraints);
preferencesButton.setMnemonic('p');
preferencesButton.setText("Preferences");
preferencesButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
preferencesButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
bottomPanel.add(preferencesButton, gridBagConstraints);
downloadAllButton.setText("Please Wait");
downloadAllButton.setEnabled(false);
downloadAllButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
downloadAllButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 9;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
bottomPanel.add(downloadAllButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
add(bottomPanel, gridBagConstraints);
coursesScrollPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
scrollContainerPanel.setLayout(new java.awt.GridBagLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
scrollContainerPanel.add(subjectDisplayPanel, gridBagConstraints);
coursesScrollPane.setViewportView(scrollContainerPanel);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(coursesScrollPane, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void downloadAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadAllButtonActionPerformed
DownloadWorkerHelper.downloadAllSelectedIn(this, controller, msdp.getShownSubjects());
}//GEN-LAST:event_downloadAllButtonActionPerformed
private void preferencesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preferencesButtonActionPerformed
if (prefsDialog == null) {
prefsDialog = new PreferencesDialogImpl(SwingUtilities.getWindowAncestor(this), controller);
prefsDialog.setLocationRelativeTo(this);
}
prefsDialog.setLocationByPlatform(true);
prefsDialog.setVisible(true);
}//GEN-LAST:event_preferencesButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel bottomPanel;
private javax.swing.JScrollPane coursesScrollPane;
private javax.swing.JButton downloadAllButton;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JButton preferencesButton;
// End of variables declaration//GEN-END:variables
private void updateButtonState() {
// TODO(Later) update this method to react to events better.
ControllerStateHelper csh = controller.getControllerStateHelper();
if (!csh.hasCompleted(ControllerType.SUBJECT)) {
dbd.updateAndStart("Please Wait");
return;
}
Set<Subject> shown = ImmutableSet.copyOf(msdp.getShownSubjects());
for (Subject subject : shown) {
if (csh.isDownloading(subject)) {
dbd.updateAndStart("Downloading");
return;
}
if (!csh.hasCompleted(ControllerType.LECTURE, subject)) {
dbd.updateAndStart("Loading Data");
return;
}
}
if (shown.isEmpty()) {
dbd.stop();
downloadAllButton.setText("No Subjects Shown.");
downloadAllButton.setEnabled(false);
downloadAllButton.setPreferredSize(null);
return;
}
int notDownloaded = 0;
int selected = 0;
for (Subject subject : shown) {
SubjectInformation i = new SubjectInformation();
i.setSubject(subject);
// TODO(Later) simplify this by storing the information and invalidating it as events come in.
selected += i.getDownloadsSelected();
notDownloaded += i.getNotDownloadedCount();
if (SubjectInformation.getDownloadedStatusFromCounts(notDownloaded, selected) == NOT_ALL_DOWNLOADED) {
// >1 selected and >1 not downloaded.
break;
}
}
DownloadState s = SubjectInformation.getDownloadedStatusFromCounts(notDownloaded, selected);
switch (s) {
case ALL_DOWNLOADED:
dbd.stop();
downloadAllButton.setText("All Downloaded");
downloadAllButton.setEnabled(false);
downloadAllButton.setPreferredSize(null);
break;
case NONE_SELECTED:
dbd.stop();
downloadAllButton.setText("None Selected");
downloadAllButton.setEnabled(false);
downloadAllButton.setPreferredSize(null);
break;
default:
dbd.stop();
downloadAllButton.setText("Download All");
downloadAllButton.setPreferredSize(null);
}
}
@Override
public void processEvent(Event e) {
if (EventIdListings.DOWNLOAD_UPDATE_EVENT_IDS.contains(e.getEventID())) {
updateButtonState();
} else if (e.getEventID().equals(ControllerStateHelperEventID.DOWNLOAD)) {
updateButtonState();
} else if (e.getEventID().equals(ControllerStateHelperEventID.CONTROLLER)) {
updateButtonState();
}
if (e instanceof ControllerEvent) {
ControllerEvent ce = (ControllerEvent) e;
if (ce.getEventID() == ControllerType.LOGIN
&& ce.getAfter() == ControllerState.COMPLETED) {
new SubjectWorker(this, controller).execute();
}
}
}
@Override
public void showErrorBox(ControllerType type, Object parameter, DLectExceptionCause get) {
// TODO(Later) show try again button if type==Subject && get==NO_CONN.
ControllerErrorBoxHelper.showErrorBox(this, type, parameter, get);
}
@Override
public void showDownloadError(Subject subject, Lecture lecture, DownloadType downloadType, DLectExceptionCause failureCause) {
ControllerErrorBoxHelper.showDownloadError(this, subject, lecture, downloadType, failureCause);
}
}