/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dlect.ui.panel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import org.dlect.controller.MainController;
import org.dlect.controller.helper.ControllerStateHelper;
import org.dlect.controller.helper.subject.SubjectInformation;
import org.dlect.events.Event;
import org.dlect.events.EventListener;
import org.dlect.events.wrapper.Wrappers;
import org.dlect.model.Lecture;
import org.dlect.model.LectureDownload;
import org.dlect.model.Subject;
import org.dlect.model.Subject.SubjectEventID;
import org.dlect.ui.decorator.ArrowBorder;
import static org.dlect.event.util.EventIdListings.DOWNLOAD_UPDATE_EVENT_IDS;
/**
*
* @author lee
*/
public class CourseHeader extends javax.swing.JPanel implements EventListener {
private static final Color COMPLETED_PROGRESS_COLOR = new Color(0x7295E6);
private static final Color ALL_COMPLETED_PROGRESS_COLOR = new Color(0x68ED68);
private static final Color DOWNLOADING_PROGRESS_COLOR = new Color(0xF9CA6D);
private static final long serialVersionUID = 1L;
private Subject subject;
private double progress = 0;
private double dlInProgress = 0;
private final MainController ctl;
/**
* Creates new form CourseHeader
*
* @param ctl
*/
public CourseHeader(MainController ctl) {
initComponents();
this.ctl = ctl;
Wrappers.addSwingListenerTo(this, ctl, ControllerStateHelper.class);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
final int w = this.getWidth();
final int h = this.getHeight();
final double p = progress;
final double dp = dlInProgress;
if (p >= 0) {
if (p >= 1) {
g2.setColor(ALL_COMPLETED_PROGRESS_COLOR);
g2.fillRect(0, 0, w, h);
} else {
g2.setColor(COMPLETED_PROGRESS_COLOR);
g2.fillRect(0, 0, (int) (w * p), h);
if (dp > 0) {
g2.setColor(DOWNLOADING_PROGRESS_COLOR);
g2.fillRect((int) (w * p), 0, (int) (w * dp), h);
}
}
}
g2.setColor(this.getBackground().darker());
g2.drawLine(0, h - 1, w, h - 1);
g2.setColor(this.getBackground().brighter());
g2.drawLine(0, 0, w, 0);
}
/**
* 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;
courseNameLabel = new javax.swing.JLabel();
lectureCountLabel = new javax.swing.JLabel();
setBorder(new ArrowBorder());
setMaximumSize(new java.awt.Dimension(2147483647, 28));
setMinimumSize(new java.awt.Dimension(38, 28));
setPreferredSize(new java.awt.Dimension(38, 28));
setLayout(new java.awt.GridBagLayout());
courseNameLabel.setText("Course Name");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 0.5;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
add(courseNameLabel, gridBagConstraints);
lectureCountLabel.setText("No Lecures");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 0.5;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
add(lectureCountLabel, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel courseNameLabel;
private javax.swing.JLabel lectureCountLabel;
// End of variables declaration//GEN-END:variables
public void setSubject(Subject subject) {
if (this.subject == subject) {
return;
}
if (this.subject != null) {
this.subject.removeListener(this);
}
this.subject = subject;
Wrappers.addSwingListenerTo(this, subject, Subject.class, Lecture.class, LectureDownload.class);
refresh();
}
@Override
public void processEvent(Event e) {
if (subject == null) {
return;
}
if (e.getEventID().equals(SubjectEventID.NAME) || e.getEventID().equals(SubjectEventID.LECTURE)) {
updateDescriptions();
}
if (DOWNLOAD_UPDATE_EVENT_IDS.contains(e.getEventID())) {
updateNums(ctl.getSubjectDataHelper().getSubjectInformation(e));
}
}
public void refresh() {
updateNums(new SubjectInformation());
updateDescriptions();
}
private void updateNums(SubjectInformation i) {
if (subject == null) {
return;
}
i.setSubject(subject);
updateDescriptions();
int downloadsInProgress = ctl.getControllerStateHelper().getDownloadingIn(subject).size();
int numberDownloaded = i.getDownloadedCount();
int totalDownloads = i.getDownloadsSelected();
if (totalDownloads > 0) {
progress = ((double) numberDownloaded) / totalDownloads;
dlInProgress = ((double) downloadsInProgress) / totalDownloads;
} else {
progress = -1;
dlInProgress = 0;
}
repaint();
}
private void updateDescriptions() {
final int numLectures = subject.getLectures().size();
if (numLectures > 0) {
lectureCountLabel.setText(numLectures + " Lecture" + (numLectures == 1 ? "" : "s") + " Found");
} else {
lectureCountLabel.setText("No Lectures Found");
}
courseNameLabel.setText(subject.getName());
}
}