package causation.lab; // packages import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; // no custom classes /** *

* This class contains the code for the two bars in the CausalityLab. * Its primary function is to lay out the buttons correctly, disable them * when the bar is disabled, and fire PropertyChangeEvents * when buttons are pressed. *

* The following PropertyChangeEvents are sent by this class: *

* Copyright 1999 by David Danks. All rights reserved.

* * @version 1.0 May 15, 1999 * @author David Danks */ public class ButtonBar extends JPanel implements ActionListener, LayoutManager { /////////////////// Class Variables /////////////////// // types of ButtonBars public static final int SETUP_GATHER_DATA_BAR = 0; public static final int HYPOTHESIZE_PREDICT_BAR = 1; // Label text private static final String[] SGD_LABEL = {"Setup Experiment", "& Gather Data"}; private static final String[] HP_LABEL = {"Hypothesize &", "Make Predictions"}; private static final Font FONT = new Font("Dialog", Font.BOLD, 11); // Tooltip text private static final String LOCK_TOOLTIP = "Lock a variable"; private static final String RAND_TOOLTIP = "Randomize a variable"; private static final String DATA_TOOLTIP = "Collect a dataset"; private static final String ARROW_TOOLTIP = "Draw an arrow"; private static final String LATENT_TOOLTIP = "Add a latent variable"; private static final String INDEP_TOOLTIP = "Get independence facts"; // icon directory private static final String DIR = "icons/"; // SGD Button icons private static final String LOCK_ENABLED = "lockup.gif"; private static final String LOCK_SELECTED = "lockdown.gif"; private static final String LOCK_ROLLOVER = "lockroll.gif"; private static final String LOCK_DISABLED = "lockoff.gif"; private static final String RAND_ENABLED = "randup.gif"; private static final String RAND_SELECTED = "randdown.gif"; private static final String RAND_ROLLOVER = "randroll.gif"; private static final String RAND_DISABLED = "randoff.gif"; private static final String DATA_ENABLED = "dataup.gif"; private static final String DATA_SELECTED = "datadown.gif"; private static final String DATA_ROLLOVER = "dataroll.gif"; private static final String DATA_DISABLED = "dataoff.gif"; // HP Button icons private static final String ARROW_ENABLED = "arrowup.gif"; private static final String ARROW_SELECTED = "arrowdown.gif"; private static final String ARROW_ROLLOVER = "arrowroll.gif"; private static final String ARROW_DISABLED = "arrowoff.gif"; private static final String LATENT_ENABLED = "latentup.gif"; private static final String LATENT_SELECTED = "latentdown.gif"; private static final String LATENT_ROLLOVER = "latentroll.gif"; private static final String LATENT_DISABLED = "latentoff.gif"; private static final String INDEP_ENABLED = "indepup.gif"; private static final String INDEP_SELECTED = "indepdown.gif"; private static final String INDEP_ROLLOVER = "indeproll.gif"; private static final String INDEP_DISABLED = "indepoff.gif"; // Background info private static final Color ENABLED_BACKGROUND = new Color(153, 204, 153); private static final Color DISABLED_BACKGROUND = new Color(204, 204, 204); private static final Color LABEL = new Color(102, 102, 102); private static final Insets MARGIN = new Insets(3, 3, 3, 3); private static final int UPPER_PIXEL_GAP = 15; private static final int LOWER_PIXEL_GAP = UPPER_PIXEL_GAP; /////////////////// Instance Variables /////////////////// // barEnabled tells us whether the bar is enabled; buttonActivated // tells us whether we can use the button or not. private boolean barEnabled = true; private boolean[] buttonActivated = new boolean[4]; private String[] label = null; private JToggleButton toggled = null; private int type; private URL codebase; /////////////////// Constructors /////////////////// // public ButtonBar() { this(SETUP_GATHER_DATA_BAR, new URL()); } // public ButtonBar(int type) { this(type, new URL()); } /** * This constructor creates a ButtonBar of type type, and * loads images from codebase * @param type The type of ButtonBar * @param codebase The codebase for loading images */ public ButtonBar(int type, URL codebase) { this.type = type; this.codebase = codebase; setLayout(this); setFont(FONT); for (int i=0; iPropertyChangeEvent. */ public void actionPerformed(ActionEvent a) { // if it's a toggle button, do the appropriate logic, and only // fire the needed PCEs if (a.getSource() instanceof JToggleButton) { if (toggled == null) { // none pressed toggled = (JToggleButton)a.getSource(); firePropertyChange(a.getActionCommand(), new Boolean(false), new Boolean(true)); } else if (toggled == a.getSource()) { // remove setting toggled = null; firePropertyChange(a.getActionCommand(), new Boolean(true), new Boolean(false)); } else { // unset toggled, and then send messages toggled.setSelected(false); firePropertyChange(toggled.getActionCommand(), new Boolean(true), new Boolean(false)); toggled = (JToggleButton)a.getSource(); firePropertyChange(a.getActionCommand(), new Boolean(false), new Boolean(true)); } } else { // otherwise, we just want to notify that it was clicked firePropertyChange(a.getActionCommand(), null, null); } } /** * @return Whether the bar is enabled */ public boolean isBarEnabled() { return barEnabled; } /** * @param b Whether the bar should be enabled */ public void setBarEnabled(boolean b) { if (b == barEnabled) return; // do the change barEnabled = b; Component[] children = getComponents(); for (int i=0; itrue always, since we fill the background */ public boolean isOpaque() { return true; } /** * Un-selects any of the JToggleButtons that might be selected. This * is here to make switching bars easier. */ public void deselectAll() { // if nothing's selected, just return if (toggled == null) return; // otherwise, call actionPerformed on toggled toggled.doClick(); } /** * This method paints the background and text for the ButtonBar. * @param g The Graphics context in which to paint the ButtonBar */ public void paintComponent(Graphics g) { int height = getHeight(); int width = getWidth(); FontMetrics fm = getFontMetrics(getFont()); // first, paint the background if (barEnabled) g.setColor(ENABLED_BACKGROUND); else g.setColor(DISABLED_BACKGROUND); g.fillRect(0, 0, width, height); // then paint the label g.setColor(LABEL); int topInset = (height-getPreferredSize().height)/2+MARGIN.top; for (int i=0; i