package causation.lab; // packages import javax.swing.*; import java.awt.*; import java.awt.event.*; // no custom classes /** * This is a utility Object so that we can get information from the user about * what kind of dataset they want to collect in the CausalityLab. * Copyright 1999 by David Danks. All rights reserved. * * @see Workbench * @version 1.0 June 27, 1999 * @author David Danks */ public class ExperimentInfoPanel extends JPanel implements ItemListener { /////////////////// Class Variables /////////////////// // how much data will be displayed for an "infinite" sample private static final int INFINITE_SAMPLE_SIZE = 5; /////////////////// Instance Variables /////////////////// private ButtonGroup group; private JRadioButton infinite; private JRadioButton finite; private JTextField sampleSize; private JLabel sizeLabel; private JTextField sampleName; /////////////////// Constructors /////////////////// /** * Creates and lays out the JPanel, with it preset to "Finite sample", * and with the initial sample name of "experiment". */ public ExperimentInfoPanel() { this("experiment"); } /** * Creates and lays out the JPanel, with it preset to "Finite sample", * and with the given initial sample name. * @param initialName The initial name for the experiment */ public ExperimentInfoPanel(String initialName) { sampleSize = new JTextField(6); sizeLabel = new JLabel("Sample size (1-5000):"); sampleName = new JTextField(15); sampleName.setText(initialName); group = new ButtonGroup(); infinite = new JRadioButton("Whole population"); infinite.setSelected(false); infinite.addItemListener(this); finite = new JRadioButton("Sample"); finite.setSelected(true); finite.addItemListener(this); group.add(infinite); group.add(finite); setInfinite(false); setLayout(new GridBagLayout()); addComponent(infinite, 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST); addComponent(finite, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST); addComponent(sizeLabel, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST); addComponent(sampleSize, 1, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST); addComponent(new JLabel("Sample name:"), 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST); addComponent(sampleName, 1, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST); } /////////////////// Instance Methods /////////////////// /** * @return The default infinite sample size, if the user chose that; * 0, if the field did not actually contain a number; * otherwise, the number (even if negative) */ public int getSampleSize() { if (infinite.isSelected()) return INFINITE_SAMPLE_SIZE; int sample; try { sample = Integer.parseInt(sampleSize.getText()); } catch (NumberFormatException n) { return 0; } return sample; } /** * @return Whether the user requested an infinite sample */ public boolean isSampleInfinite() { return infinite.isSelected(); } /** * @return The sample name */ public String getSampleName() { return sampleName.getText(); } /** * Sets all of the Component.enabled properties correctly when a radio button * is clicked */ public void itemStateChanged(ItemEvent e) { if (e.getSource() == infinite) setInfinite(true); else if (e.getSource() == finite) setInfinite(false); } /** * A helper method that sets the Component.enabled properties */ private void setInfinite(boolean b) { if (b) { sampleSize.setEnabled(false); sizeLabel.setEnabled(false); } else { sampleSize.setEnabled(true); sizeLabel.setEnabled(true); } } /** * Puts the focus onto the sample size, so that the user can just type */ public void setCursor() { sampleSize.requestFocus(); } /** * addComponent is a helper method so that we can add lots of components with * single-line calls. */ protected void addComponent(Component obj, int posx, int posy, int width, int height, int fill, int anchor) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = posx; gbc.gridy = posy; gbc.gridwidth = width; gbc.gridheight = height; gbc.fill = fill; gbc.anchor = anchor; this.add(obj, gbc); } }