From 6ede23ffcbb6bd9ee652934b7a6c4b4b1f2ef7b8 Mon Sep 17 00:00:00 2001 From: skarahoda Date: Mon, 7 Sep 2015 02:28:10 +0300 Subject: [PATCH] now course list is sorted --- src/io/scheduler/data/Course.java | 7 ++- src/io/scheduler/gui/OptionSUClass.java | 6 +- src/io/scheduler/gui/SortedListModel.java | 77 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/io/scheduler/gui/SortedListModel.java diff --git a/src/io/scheduler/data/Course.java b/src/io/scheduler/data/Course.java index af04428..5fd788c 100644 --- a/src/io/scheduler/data/Course.java +++ b/src/io/scheduler/data/Course.java @@ -9,7 +9,7 @@ import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName = "courses") -public class Course { +public class Course implements Comparable { public static final String CODE_FIELD_NAME = "code"; public static final String NAME_FIELD_NAME = "name"; @@ -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()); + } } diff --git a/src/io/scheduler/gui/OptionSUClass.java b/src/io/scheduler/gui/OptionSUClass.java index 81331a7..c580eb5 100644 --- a/src/io/scheduler/gui/OptionSUClass.java +++ b/src/io/scheduler/gui/OptionSUClass.java @@ -40,7 +40,7 @@ public class OptionSUClass { private int option; private DefaultListModel listModelSUClass; - private DefaultListModel listModelCourse; + private SortedListModel listModelCourse; private DefaultListModel listModelMeeting; private JList jListCourse; private JList jListSUClass; @@ -284,7 +284,7 @@ private JScrollPane createScrollMeeting() { } private JScrollPane createScrollCourse() { - listModelCourse = new DefaultListModel(); + listModelCourse = new SortedListModel(); jListCourse = new JList(listModelCourse); jListCourse.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane returnVal = new JScrollPane(jListCourse); @@ -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()) { diff --git a/src/io/scheduler/gui/SortedListModel.java b/src/io/scheduler/gui/SortedListModel.java new file mode 100644 index 0000000..6ba3e8f --- /dev/null +++ b/src/io/scheduler/gui/SortedListModel.java @@ -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 extends AbstractListModel { + /** + * + */ + private static final long serialVersionUID = 6150903636354738067L; + SortedSet model; + + public SortedListModel() { + model = new TreeSet(); + } + + @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 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 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(); + } +}