Skip to content

Commit

Permalink
now course list is sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
skarahoda committed Sep 6, 2015
1 parent c50b079 commit 6ede23f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/io/scheduler/data/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "courses")
public class Course {
public class Course implements Comparable<Course> {

public static final String CODE_FIELD_NAME = "code";
public static final String NAME_FIELD_NAME = "name";
Expand Down Expand Up @@ -178,4 +178,9 @@ public boolean hasCoRequisiteRestriction(Schedule s) throws SQLException {
public boolean hasCoRequisite() {
return coReq != null;
}

@Override
public int compareTo(Course o) {
return toString().compareTo(o.toString());
}
}
6 changes: 3 additions & 3 deletions src/io/scheduler/gui/OptionSUClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class OptionSUClass {
private int option;
private DefaultListModel<SUClass> listModelSUClass;
private DefaultListModel<Course> listModelCourse;
private SortedListModel<Course> listModelCourse;
private DefaultListModel<Meeting> listModelMeeting;
private JList<Course> jListCourse;
private JList<SUClass> jListSUClass;
Expand Down Expand Up @@ -284,7 +284,7 @@ private JScrollPane createScrollMeeting() {
}

private JScrollPane createScrollCourse() {
listModelCourse = new DefaultListModel<Course>();
listModelCourse = new SortedListModel<Course>();
jListCourse = new JList<Course>(listModelCourse);
jListCourse.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane returnVal = new JScrollPane(jListCourse);
Expand All @@ -296,7 +296,7 @@ private void fillScrollCourse() {
for (SUClass suClass : filteredSuClasses) {
Course course = suClass.getCourse();
if (course != null && !listModelCourse.contains(course)) {
listModelCourse.addElement(course);
listModelCourse.add(course);
}
}
if (!listModelCourse.isEmpty()) {
Expand Down
77 changes: 77 additions & 0 deletions src/io/scheduler/gui/SortedListModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.scheduler.gui;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractListModel;

public class SortedListModel<E> extends AbstractListModel<E> {
/**
*
*/
private static final long serialVersionUID = 6150903636354738067L;
SortedSet<E> model;

public SortedListModel() {
model = new TreeSet<E>();
}

@Override
public int getSize() {
return model.size();
}

@Override
@SuppressWarnings("unchecked")
public E getElementAt(int index) {
return (E) model.toArray()[index];
}

public void add(E element) {
if (model.add(element)) {
fireContentsChanged(this, 0, getSize());
}
}

public void addAll(E elements[]) {
Collection<E> c = Arrays.asList(elements);
model.addAll(c);
fireContentsChanged(this, 0, getSize());
}

public void clear() {
model.clear();
fireContentsChanged(this, 0, getSize());
}

public boolean contains(E element) {
return model.contains(element);
}

public Object firstElement() {
return model.first();
}

public Iterator<E> iterator() {
return model.iterator();
}

public E lastElement() {
return model.last();
}

public boolean removeElement(E element) {
boolean removed = model.remove(element);
if (removed) {
fireContentsChanged(this, 0, getSize());
}
return removed;
}

public boolean isEmpty() {
return model.isEmpty();
}
}

0 comments on commit 6ede23f

Please sign in to comment.