/* * @(#)CDistributionChooserUI.java 0.0.1 99/09/07 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats.plaf.concrete; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.plaf.*; import stats.*; import stats.plaf.*; /** * @version 0.0.1 09/07/99 * @author Willie Wheeler */ public class CDistributionChooserUI extends DistributionChooserUI implements ActionListener { protected DistributionChooser chooser; protected JCheckBox[] independentChecks; protected JComboBox[] conditionCombos; protected JCheckBox[] conditionChecks; protected JRadioButton[] dependentRadios; public static ComponentUI createUI(JComponent c) { return new CDistributionChooserUI(); } public void installUI(JComponent c) { chooser = (DistributionChooser)c; // Install the defaults. chooser.setOpaque(true); chooser.setLayout(new BoxLayout(chooser, BoxLayout.X_AXIS)); // Install the components. installIndependentBox(); if (chooser.getType() == chooser.CONDITIONING_STATE) { installConditioningStateBox(); } else { installConditioningVariablesBox(); } installDependentBox(); } protected void installIndependentBox() { Box box = Box.createVerticalBox(); box.add(new JLabel("Independent variables:")); wrapAndInstall(box); // Add check boxes to the independent box. Vector independents = chooser.getIndependents(); independentChecks = new JCheckBox[independents.size()]; for (int i = 0; i < independents.size(); i++) { String name = ((Variable)independents.get(i)).getName(); independentChecks[i] = new JCheckBox(name); independentChecks[i].addActionListener(this); box.add(independentChecks[i]); } } protected void installConditioningStateBox() { Box box = Box.createVerticalBox(); box.add(new JLabel("Conditioning state:")); wrapAndInstall(box); // This panel contains the variable labels and the value combo boxes, // laid out with a GridBagLayout. We override getAlignmentX() in // order to obtain the correct horizontal relationship between the // "Conditioning state:" label and the panel. JPanel panel = new JPanel() { public float getAlignmentX() { return Component.LEFT_ALIGNMENT; } }; panel.setLayout(new GridBagLayout()); box.add(panel); // Add variable labels and value combo boxes. Vector independents = chooser.getIndependents(); conditionCombos = new JComboBox[independents.size()]; for (int i = 0; i < independents.size(); i++) { // Add the variable label. TextVariable variable = (TextVariable)independents.get(i); JLabel label = new JLabel(variable.getName() + "=", JLabel.RIGHT); addComponent(panel, label, 0, i, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH, GridBagConstraints.CENTER); // Add the value combo box. String[] values = variable.getValues(); String[] augValues = new String[values.length + 1]; augValues[0] = "Pooled"; System.arraycopy(values, 0, augValues, 1, values.length); conditionCombos[i] = new JComboBox(augValues); conditionCombos[i].addActionListener(this); addComponent(panel, conditionCombos[i], 1, i, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH, GridBagConstraints.CENTER); } } protected void installConditioningVariablesBox() { Box box = Box.createVerticalBox(); box.add(new JLabel("Conditioning variables:")); wrapAndInstall(box); // Add check boxes to the conditioning box. Vector independents = chooser.getIndependents(); conditionChecks = new JCheckBox[independents.size()]; for (int i = 0; i < independents.size(); i++) { String name = ((Variable)independents.get(i)).getName(); conditionChecks[i] = new JCheckBox(name); conditionChecks[i].addActionListener(this); box.add(conditionChecks[i]); } } protected void installDependentBox() { Box box = Box.createVerticalBox(); box.add(new JLabel("Dependent variable:")); wrapAndInstall(box); // Add radio buttons to the dependent box. Vector dependents = chooser.getDependents(); dependentRadios = new JRadioButton[dependents.size()]; ButtonGroup buttonGroup = new ButtonGroup(); for (int i = 0; i < dependents.size(); i++) { String name = ((Variable)dependents.get(i)).getName(); dependentRadios[i] = new JRadioButton(name); dependentRadios[i].addActionListener(this); buttonGroup.add(dependentRadios[i]); box.add(dependentRadios[i]); } dependentRadios[0].setSelected(true); } protected void wrapAndInstall(Component c) { // Build the panel, and set the layout and border. JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); panel.setBorder(new EmptyBorder(10, 10, 10, 10)); // Add the component to the panel, and the panel to the chooser. addComponent(panel, c, 0, 0, 1, 1, 0.0, 1.0, GridBagConstraints.NONE, GridBagConstraints.NORTH); chooser.add(panel); } /** * Adapted from O'Reilly's Java AWT Reference, p. 276, */ public static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor) { GridBagLayout gbl = (GridBagLayout)container.getLayout(); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = gridx; gbc.gridy = gridy; gbc.gridwidth = gridwidth; gbc.gridheight = gridheight; gbc.weightx = weightx; gbc.weighty = weighty; gbc.fill = fill; gbc.anchor = anchor; gbl.setConstraints(component, gbc); container.add(component); } public void uninstallUI(JComponent c) { } public void actionPerformed(ActionEvent e) { int numIndeps = chooser.getNumIndependents(); int numDeps = chooser.getNumDependents(); int[] temp, indices; // Set the selected independent variable indices. temp = new int[numIndeps]; int numSelIndeps = 0; for (int i = 0; i < numIndeps; i++) { if (independentChecks[i].isSelected()) { temp[numSelIndeps++] = i; } } indices = new int[numSelIndeps]; System.arraycopy(temp, 0, indices, 0, numSelIndeps); chooser.setSelectedIndependents(indices); // Set the selected dependent variable index. int index = 0; for (int i = 0; i < numDeps; i++) { if (dependentRadios[i].isSelected()) { index = i; break; } } chooser.setSelectedDependent(index); // Set the selected conditioning states, and variables if any. if (chooser.getType() == DistributionChooser.CONDITIONING_STATE) { // Selected conditioning state. int[] state = new int[numIndeps]; for (int i = 0; i < numIndeps; i++) { // This works since pooled values are represented by -1. state[i] = conditionCombos[i].getSelectedIndex() - 1; } chooser.setSelectedConditioningStates(new int[][] { state }); chooser.setSelectedConditioningVariables(null); } else { // Selected conditioning variables. temp = new int[numIndeps]; int numSelConds = 0; for (int i = 0; i < numIndeps; i++) { if (conditionChecks[i].isSelected()) { temp[numSelConds++] = i; } } indices = new int[numSelConds]; System.arraycopy(temp, 0, indices, 0, numSelConds); chooser.setSelectedConditioningVariables(indices); ContingencyTableModel model = chooser.getModel(); int[][] states = model.states(indices); chooser.setSelectedConditioningStates(states); } } }