📄 schedule.java
字号:
/**
* Classroom Scheduler
* Copyright (C) 2004 Colin Archibald, Ph.D.
* https://sourceforge.net/projects/cr-scheduler/
*
* Licensed under the Academic Free License version 2.0
*/
package application;
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import java.text.DateFormat;
import java.util.*;
import resources.*;
/**
* The Schedule maintains the data for a particular Schedule being manipulated
* by the application.
*
* Uses Singleton model. When someone asks for it, the method will either
* create a new one or give the existing one.
*
*/
public class Schedule extends Observable
implements Constants, Serializable, Printable, Pageable, Observer {
static final long serialVersionUID = 911;
public static final String DEFAULT_FILE_NAME = "Untitled.crs";
private static Schedule schedule = null;
private static boolean changed = false; // used for save warning.
private Date lastUpdated = new Date();
private String scheduleName;
private String directory; // Directory for this document
private ArrayList professors;
private ArrayList classrooms;
private ArrayList timeSlots;
private ArrayList courses;
private ArrayList schedCourses;
private ArrayList conflicts;
private ArrayList books;
private Summary summary = null; // The Summary object for printing and display
/**
* There is no public constructor for a schedule. Only the static
* singleton method "getSchedule" can create a schedule using the
* default constructor for a Schedule
*/
private Schedule() {
scheduleName = DEFAULT_FILE_NAME;
directory = "."; // Directory for this document
professors = new ArrayList(20);
classrooms = new ArrayList(20);
timeSlots = new ArrayList(20);
courses = new ArrayList(20);
schedCourses = new ArrayList(20);
conflicts = new ArrayList(20);
books = new ArrayList(20);
addTheTBAs();
}
/**
* Any time the schedule changes, we will set a changed flag. The
* schedule will be an observer of itself.
*
*/
synchronized public void update(Observable self, Object obj) {
changed = true;
lastUpdated = new Date();
}
private void setLastUpdated(Date d) {
lastUpdated = d;
}
private Date getLastUpdated() {
return lastUpdated;
}
public void setChanged(boolean b) {
changed = b;
}
public boolean getChanged() {
return changed;
}
public String updated() {
DateFormat df =
DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT);
return " Updated: " + df.format(lastUpdated);
}
synchronized public void resetSchedule(Schedule localSchedule, String name) {
if (localSchedule != null) {
schedule.setScheduleName(name);
schedule.setDirectory(localSchedule.getDirectory());
schedule.setProfessors(localSchedule.getProfessors());
schedule.setClassrooms(localSchedule.getClassrooms());
schedule.setTimeSlots(localSchedule.getTimeSlots());
schedule.setCourses(localSchedule.getCourses());
schedule.setBooks(localSchedule.getBooks());
schedule.setSchedCourses(localSchedule.getSchedCourses());
schedule.setLastUpdated(localSchedule.getLastUpdated());
schedule.setChanged(false);
} else {
scheduleName = DEFAULT_FILE_NAME;
directory = "."; // Directory for this document
professors = new ArrayList(20);
classrooms = new ArrayList(20);
timeSlots = new ArrayList(20);
courses = new ArrayList(20);
books = new ArrayList(20);
schedCourses = new ArrayList(20);
conflicts = new ArrayList(20);
summary = new Summary();
addTheTBAs();
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
}
private void addTheTBAs() {
professors.add(new Professor(" ", " STAFF", Professor.ADJUNCT));
classrooms.add(new Classroom(" ", "TBA", 0));
timeSlots.add( new TimeSlot().setTba() );
timeSlots.add( new TimeSlot().setOnline() );
}
/** Allows access to the schedule to anyone who wants it without needing to pass
* it around as a parameter.
*/
synchronized public static Schedule getSchedule() {
if (schedule == null) {
schedule = new Schedule();
return schedule;
} else {
return schedule;
}
}
synchronized public String getScheduleName() {
return scheduleName;
}
synchronized public void setScheduleName(String scheduleName) {
this.scheduleName = scheduleName;
}
// Get the directory where the document is stored
synchronized public String getDirectory() {
return directory;
}
// Set the directory for the document
/**
* Sets the directory for the document
* @param String directory the directory
*/
synchronized public void setDirectory(String directory) {
this.directory = directory;
}
/**
* Gets the instructors.
* @return a <code>Vector</code> specifying the instructors
*/
public ArrayList getProfessors() {
return professors;
}
synchronized public void setProfessors(ArrayList professors) {
this.professors = professors;
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
/**
* Gets the class rooms.
* @return a <code>Vector</code> specifying the class rooms.
*/
synchronized public ArrayList getClassrooms() {
return classrooms;
}
synchronized public void setClassrooms(ArrayList classrooms) {
this.classrooms = classrooms;
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
/**
* Gets the Courses.
* @return Vector specifying the course sections.
*/
synchronized public ArrayList getCourses() {
return courses;
}
synchronized public void setCourses(ArrayList courses) {
this.courses = courses;
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
/**
* Gets the time slots.
* @return Vector specifying the course sections.
*/
public ArrayList getTimeSlots() {
return timeSlots;
}
synchronized public void setTimeSlots(ArrayList timeSlots) {
this.timeSlots = timeSlots;
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
public ArrayList getBooks(){
return books;
}
public void setBooks(ArrayList books){
this.books = books;
setChanged();
notifyObservers();
}
/**
* Gets the Scheduled Courses.
* @return Vector specifying the scheduled courses.
*/
public ArrayList getSchedCourses() {
return schedCourses;
}
synchronized public void setSchedCourses(ArrayList schedCourses) {
this.schedCourses = schedCourses;
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
/**
* Gets the Conflicts.
* @return Vector specifying the conflicts.
*/
synchronized public ArrayList getConflicts() {
return conflicts;
}
synchronized public Summary getSummary() {
if(summary == null) {
summary = new Summary();
}
return summary;
}
/**
* Adds an Professor to the document
* @param Professor to be added
*/
synchronized public void addProfessor(Professor prof) {
if(!professors.contains(prof)){
professors.add(prof);
Collections.sort(professors);
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
}
/**
* Removes a Professor from the document
* @param Professor to be removed
*/
synchronized public void removeProfessor(Professor prof) {
professors.remove(prof);
setChanged();
notifyObservers();
}
/**
* Adds a Classroom to the document
* @param Classroom to be added
*/
synchronized public void addClassroom(Classroom classroom) {
if(!classrooms.contains(classroom)){
classrooms.add(classroom);
Collections.sort(classrooms);
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
}
/**
* Removes a Classroom from the document
* @param Classroom to be removed
*/
synchronized public void removeClassroom(Classroom classroom) {
classrooms.remove(classroom);
setChanged();
notifyObservers();
}
/**
* Adds a TimeSlot to the document
* @param TimeSlot to be added
*/
synchronized public void addTimeSlot(TimeSlot timeSlot) {
if(!timeSlots.contains(timeSlot)){
timeSlots.add(timeSlot);
Collections.sort(timeSlots);
setChanged(); // Mark the document as changed
notifyObservers(); // Tell everybody
}
}
/**
* Removes a TimeSlot from the document
* @param TimeSlot to be removed
*/
synchronized public void removeTimeSlot(TimeSlot timeSlot) {
timeSlots.remove(timeSlot);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -