/* * @(#)DistributionChooser.java 0.0.1 99/08/06 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package causation.lab; import java.util.*; import javax.swing.*; import stats.*; /** * A JComponent to allow the user to specify a distribution. In * particular, the chooser is designed to provide the user with an easy way * to specify Chart constructor parameters. *
* The DistributionChooser allows the user to set three types of * information: *
* In the sample code that follows, we show how to build a chooser that allows * the user to pick multiple distributions. We place it inside of a * JOptionPane, and then generate a BarChart for each of * the distributions. *
* // Build a chooser and put it in an option pane.
* ContingencyTableModel model = tabulator.getDataOut();
* DistributionChooser chooser = new DistributionChooser(
* model, DistributionChooser.CONDITIONING_VARIABLES);
* int option = JOptionPane.showInternalOptionDialog(
* desktopPane, chooser, "Distribution Chooser",
* JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
* null, null, null);
*
* // Store the user-provided distribution specification.
* int[] independents = chooser.getSelectedIndependents();
* int dependent = chooser.getSelectedDependent();
* int[][] conditioningStates = chooser.getSelectedConditioningStates();
*
* // Return if the user cancels. S/he can do this either by pressing
* // 'Cancel' explicitly, or by pressing 'Ok' but not selecting any
* // independent variables.
* if (option == JOptionPane.CANCEL_OPTION || independents.length == 0) {
* return;
* }
*
* // User hasn't cancelled, so let's build the charts.
* Vector charts = new Vector();
* for (int i = 0; i < conditioningStates.length; i++) {
* charts.add(new BarChart(model, independents, dependent,
* conditioningStates[i]));
* }
*
* Supposing that the model's independent variables are Sex,
* Hair Color, and Smoker?, the above code produces the
* following distribution chooser:
*
*
*
* @version 0.0.1 08/06/99
* @author Willie Wheeler
* @see Chart
*/
public class DistributionChooser extends JComponent {
// Chooser types.
/**
* Chooser type associated with choosers that allow the user to specify
* a single conditioning state, and hence a single distribution.
*/
public static final int CONDITIONING_STATE = 0;
/**
* Chooser type associated with choosers that allow the user to specify
* multiple conditioning states, and hence multiple distributions. This
* type is called 'conditioning variables' because the multiple states
* are generated from a user-specifiable set of conditioning variables
* by considering all conditioning states in which the conditioning
* variables have specific values.
*/
public static final int CONDITIONING_VARIABLES = 1;
protected ContingencyTableModel model;
protected int type;
protected int[] selIndepVarIndices;
protected int selDepVarIndex;
protected int[] selCondVarIndices;
protected int[][] selCondStates;
public DistributionChooser(ContingencyTableModel model, int type) {
this.model = model;
this.type = type;
updateUI();
}
public void updateUI() { setUI(UIManager.getUI(this)); }
public String getUIClassID() {
return "causation.lab.plaf.DistributionChooserUI";
}
public ContingencyTableModel getModel() { return model; }
public int getNumIndependents() { return getIndependents().size(); }
public Vector getIndependents() { return model.getIndependents(); }
public int getNumDependents() { return getDependents().size(); }
public Vector getDependents() { return model.getDependents(); }
public int getType() { return type; }
public void setSelectedIndependents(int[] indices) {
this.selIndepVarIndices = indices;
}
public int[] getSelectedIndependents() { return selIndepVarIndices; }
public void setSelectedDependent(int index) {
this.selDepVarIndex = index;
}
public int getSelectedDependent() {
// Had to do this to meet Richard's CDistributionChooserUI specs...
// return selDepVarIndex;
return 0;
}
public void setSelectedConditioningVariables(int[] indices) {
this.selCondVarIndices = indices;
}
public int[] getSelectedConditioningVariables() {
return selCondVarIndices;
}
public void setSelectedConditioningStates(int[][] states) {
this.selCondStates = states;
}
public int[][] getSelectedConditioningStates() { return selCondStates; }
}