package edu.gac.max.gened; import java.awt.TextField; import java.awt.Checkbox; import java.awt.List; import java.awt.Label; import java.awt.Panel; import java.awt.Button; import java.awt.FlowLayout; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Vector; public class Finder extends java.applet.Applet implements ActionListener { // Note that the HTML file containing this applet needs to provide // a parameter, called server, used to construct the URL for contacting // the RMI server. For example, if the server parameter is max-gened, // and this applet is served off of the kilpinen.mcs.gac.edu (i.e., // that is the "codebase"), then the RMI URL will be // rmi://kilpinen.mcs.max.edu/max-gened private boolean isAlive = true; private Server server; private List coursesDisplay; private Checkbox[] areaCheckboxes; private char[] dayNames = {'M', 'T', 'W', 'R', 'F'}; private char[] periodNames = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'W', 'X', 'Y', 'Z'}; private Checkbox[][] conflictCheckboxes; public void init(){ setLayout(new BorderLayout()); coursesDisplay = new List(); add(coursesDisplay, "Center"); Panel inputPanel = new Panel(); inputPanel.setLayout(new BorderLayout()); Panel areaPanel = new Panel(); areaPanel.setLayout(new FlowLayout()); String[] areaNames = {"A", "P", "B", "C", "D", "LD", "E", "F", "G", "GN"}; areaCheckboxes = new Checkbox[areaNames.length]; for(int i = 0; i < areaNames.length; i++){ areaCheckboxes[i] = new Checkbox(areaNames[i]); areaPanel.add(areaCheckboxes[i]); } inputPanel.add(areaPanel, "North"); Panel conflictPanel = new Panel(); conflictPanel.setLayout(new GridLayout(periodNames.length + 1, dayNames.length + 1)); conflictPanel.add(new Label()); // empty where row, col. labels intersect for(int i = 0; i < dayNames.length; i++){ conflictPanel.add(new Label(""+dayNames[i])); } conflictCheckboxes = new Checkbox[periodNames.length][dayNames.length]; for(int i = 0; i < periodNames.length; i++){ conflictPanel.add(new Label(""+periodNames[i])); for(int j = 0; j < dayNames.length; j++){ Checkbox c = new Checkbox(); conflictCheckboxes[i][j] = c; conflictPanel.add(c); } } Panel conflictPanelPanel = new Panel(); // prevents stretching grid to conflictPanelPanel.setLayout(new FlowLayout()); // full width of applet conflictPanelPanel.add(conflictPanel); inputPanel.add(conflictPanelPanel, "Center"); Panel buttonPanel = new Panel(); buttonPanel.setLayout(new FlowLayout()); Button b = new Button("Find"); b.addActionListener(this); buttonPanel.add(b); inputPanel.add(buttonPanel, "South"); add(inputPanel, "North"); try{ server = (Server) java.rmi.Naming.lookup ("rmi://" + getCodeBase().getHost() + "/" + getParameter("server")); } catch(Exception e){ showError("Internal problem: " + e); } } public void actionPerformed(ActionEvent evt){ if(isAlive){ try{ Vector areasVector = new Vector(); for(int i = 0; i < areaCheckboxes.length; i++){ if(areaCheckboxes[i].getState()){ areasVector.addElement(areaCheckboxes[i].getLabel()); } } String[] areas = new String[areasVector.size()]; areasVector.copyInto(areas); Vector conflictsVector = new Vector(); for(int i = 0; i < periodNames.length; i++){ for(int j = 0; j < dayNames.length; j++){ if(conflictCheckboxes[i][j].getState()){ conflictsVector.addElement (new Conflict(dayNames[j], periodNames[i])); } } } Conflict[] conflicts = new Conflict[conflictsVector.size()]; conflictsVector.copyInto(conflicts); Course[] courses = server.satisfyAnyAndAvoid(areas, conflicts); coursesDisplay.removeAll(); if(courses.length == 0){ coursesDisplay.add("None found"); } else { for(int i = 0; i < courses.length; i++){ Course c = courses[i]; coursesDisplay.add(c.getDepartment() + c.getNumber() + c.getSection() + ", " + c.getDescription() + ", satisfies " + c.getGeneds() + ", meets " + c.getDays() + " " + c.getPeriods()); } } } catch(Exception e){ showError("Internal problem: " + e); } } } private void showError(String message){ // using the coursesDisplay for this is a questionable hack coursesDisplay.removeAll(); coursesDisplay.add(message); coursesDisplay.add("This applet is now disabled."); coursesDisplay.add("To retry, resubmit the request for this web page."); isAlive = false; } }