/*
* @(#)SetBuilderExerciseApplet.java 0.0.2 99/08/10
*
* Copyright (c) 1999 by Willie Wheeler. All rights reserved.
*/
package stats.exercises;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.help.*;
import javax.swing.*;
import exercise.*;
import stats.*;
/**
* Exercise applet for set builder exercises.
*
* Known bugs:
*
* - We need to remove table model listeners when internal frames are
* closed, or else the listeners will not be garbage collected.
* Try adding println()'s to a event callback to see what is
* happening.
*
- Under appletviewer, the help system throws exceptions when trying
* to paint an image onto the content view. This does not occur when
* running the applet in a browser equipped with the Java plug-in.
*
*
* @version 0.0.2 08/10/99
* @author Willie Wheeler
*/
public class SetBuilderExerciseApplet extends ExerciseApplet
implements ActionListener {
/** @serial */
protected JDesktopPane desktopPane;
/** @serial */
protected SetBuilderExercise exercise;
/** @serial */
protected SetBuilder builder;
/** @serial */
protected Tabulator tabulator;
/** @serial */
protected int chartFrameX = CHART_FRAME_X_MIN;
/** @serial */
protected int chartFrameY = CHART_FRAME_Y_MIN;
// Action commands
protected static final String SUBMIT = "submit";
protected static final String EXIT = "exit";
protected static final String ADD_BAR = "add bar";
protected static final String ADD_PIE = "add pie";
protected static final String ADD_MULTIPLE_BARS = "add multi bars";
protected static final String ADD_MULTIPLE_PIES = "add multi pies";
protected static final String HELP_COURSEWARE = "help courseware";
protected static final String HELP_ABOUT = "help about";
// Fields for managing placement of chart frames.
protected static final int CHART_FRAME_X_MIN = 420;
protected static final int CHART_FRAME_X_MAX = 600;
protected static final int CHART_FRAME_Y_MIN = 10;
protected static final int CHART_FRAME_Y_MAX = 400;
public void init() {
super.init(); // Map to exercise servlet.
ConcretePLAF.install(); // Install the Concrete PLAF.
this.desktopPane = new JDesktopPane();
// Install the builder.
String exerciseID = getParameter("exercise_id");
this.exercise = (SetBuilderExercise)requestExercise(exerciseID);
this.builder =
new SetBuilder(exercise.getVariables(), exercise.getAtoms());
JInternalFrame builderFrame =
new JInternalFrame("Set Builder", true, true, true, true);
builderFrame.getContentPane().add(builder);
builderFrame.setBounds(10, 10, 400, 550);
desktopPane.add(builderFrame);
this.tabulator = new Tabulator(builder.getData());
installInstructions();
// Frame the desktop pane, and add a menu bar to the frame.
JFrame desktopFrame = new JFrame("Set Builder Exercise");
desktopFrame.setJMenuBar(buildMenuBar());
desktopFrame.setContentPane(desktopPane);
desktopFrame.setBounds(0, 0, 650, 650);
desktopFrame.setVisible(true);
}
/** Helper method for init(). Installs instructions panel. */
protected void installInstructions() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
panel.setBackground(Color.white);
JTextArea textArea = new JTextArea("Build a set as follows:\n");
Vector constraints = exercise.getConstraints();
for (int i = 0; i < constraints.size(); i++) {
SetBuilderExercise.Constraint constraint =
(SetBuilderExercise.Constraint)constraints.get(i);
textArea.append("\n " + constraint);
}
textArea.append("\n\nThen submit your solution.");
textArea.setFont(new Font("SansSerif", Font.PLAIN, 12));
textArea.setEditable(false);
gbc.gridy = 0;
gbc.insets = new Insets(5, 5, 5, 5);
panel.add(textArea, gbc);
JButton button = new JButton("Submit solution");
button.setActionCommand(SUBMIT);
button.addActionListener(this);
gbc.gridy = 1;
gbc.insets = new Insets(0, 5, 5, 5);
panel.add(button, gbc);
JInternalFrame fr = new JInternalFrame();
fr.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
fr.getContentPane().add(panel);
fr.setLocation(420, 350);
fr.pack();
desktopPane.add(fr, JDesktopPane.PALETTE_LAYER);
}
/** Helper method for init(). Builds exercise menu bar. */
protected JMenuBar buildMenuBar() {
// Step 1. Build icons for chart menu.
String base = "resources/images/icons/";
Icon[] icons = new ImageIcon[2];
try {
icons[0] =
new ImageIcon(new URL(getCodeBase(), base + "hist_icon.gif"));
icons[1] =
new ImageIcon(new URL(getCodeBase(), base + "pie_icon.gif"));
} catch (MalformedURLException e) { System.out.println(e); }
// Step 2. Build courseware help system and help listener.
CSH.DisplayHelpFromSource helpListener = null;
try {
URL hsURL = new URL(getCodeBase(), "resources/help/cwHS.hs");
HelpSet hs = new HelpSet(null, hsURL);
HelpBroker hb = hs.createHelpBroker();
helpListener = new CSH.DisplayHelpFromSource(hb);
} catch (Exception e) { e.printStackTrace(); }
// Step 3. Use the icons and help system to build the menu bar.
JMenuBar bar = new JMenuBar();
JMenu menu;
// Step 3.1. Build "File" menu.
menu = buildMenu("File", 'f', new String[] { "Exit" },
new char[] { 'x' }, new String[] { EXIT }, this, bar);
menu.getItem(0).setEnabled(false); // temp
// Step 3.2. Build "Chart" menu.
String[] chartItems = new String[] { "Add bar chart", "Add pie chart",
"Add multiple bar charts",
"Add multiple pie charts" };
String[] chartCmds = new String[] { ADD_BAR, ADD_PIE,
ADD_MULTIPLE_BARS,
ADD_MULTIPLE_PIES };
menu = buildMenu("Chart", 'c', chartItems,
new char[] { 'b', 'p', 'm', 'u' },
chartCmds, this, bar);
menu.getItem(0).setIcon(icons[0]);
menu.getItem(1).setIcon(icons[1]);
menu.insertSeparator(2);
// Step 3.3. Build "Help" menu.
menu = buildMenu("Help", 'h',
new String[] { "Courseware Help System" },
new char[] { 'c' }, new String[] { HELP_COURSEWARE },
helpListener, bar);
JMenuItem item = new JMenuItem("About", 'a');
item.setActionCommand(HELP_ABOUT);
item.addActionListener(this);
menu.add(item);
// Step 4. Return menu bar.
return bar;
}
/** Helper method for buildMenuBar(). */
protected static JMenu buildMenu(String s,
char mnemonic,
String[] items,
char[] mnemonics,
String[] cmds,
ActionListener listener,
JMenuBar bar) {
JMenu menu = new JMenu(s);
menu.setMnemonic(mnemonic);
for (int i=0; i CHART_FRAME_Y_MAX) {
chartFrameY = CHART_FRAME_Y_MIN;
chartFrameX += 20;
if (chartFrameX > CHART_FRAME_X_MAX) {
chartFrameX = CHART_FRAME_X_MIN;
}
}
return bounds;
}
public String getExerciseServletClassID() {
return "stats.exercises.SetBuilderExerciseServlet";
}
}