package causation.lab; // packages import javax.swing.*; import javax.swing.event.*; import java.beans.*; // custom classes import causation.lab.WBVariableModelInterface; import causation.lab.Randomizer; /** *
* This is the underlying model for WBVariables. It maintains information about: *
* The following PropertyChangeEvent
s are fired by this class:
*
* NOTE: this class should really just be tetrad.model.Variable (pkg?). * That would make a lot of other pieces easier to code. However, this * code was written before Tetrad IV was started, and so I had to write * my own WBVariableModel class. Perhaps someday this code can be updated * to use tetrad.model.Variable. *
* Copyright 1999 by David Danks. All rights reserved. *
* * @see WBVariable * @see WBVariableModelInterface * @version 0.9 Mar 2, 1999 * @author David Danks */ public class WBVariableModel implements WBVariableModelInterface { /////////////////// Instance Variables /////////////////// // does the work of firing PCEs for us protected EventListenerList listenerList = new EventListenerList(); private boolean locked = false; private String name = ""; private String[] values = null; private int activeIndex = -1; private Randomizer randomizer = null; /////////////////// Constructors /////////////////// /** * Null constructor. Creates an unlocked WBVariableModel with blank name, * one blank value, no active value, and no randomizer. */ public WBVariableModel() { this("", new String[]{""}, null, false, null); } /** * Creates an unlocked WBVariableModel with given name, one blank value, no * active value, and no randomizer. * @param name The WBVariable name */ public WBVariableModel(String name) { this(name, new String[]{""}, null, false, null); } /** * Creates an unlocked WBVariableModel with blank name, given values, no * active value, and no randomizer. * @param values The possible WBVariable values */ public WBVariableModel(String[] values) { this("", values, null, false, null); } /** * Creates an unlocked WBVariableModel with given name and values, no active * value, and no randomizer. * @param name The WBVariable name * @param values The possible WBVariable values */ public WBVariableModel(String name, String[] values) { this(name, values, null, false, null); } /** * Creates an unlocked WBVariableModel with blank name, given values and * active value, and no randomizer. * @param values The possible WBVariable values * @param activeValue The value the WBVariable is "set" to */ public WBVariableModel(String[] values, String activeValue) { this("", values, activeValue, false, null); } /** * Creates an unlocked WBVariableModel with given name, values, active value, and no * randomizer. * @param name The WBVariable name * @param values The possible WBVariable values * @param activeValue The value the WBVariable is "set" to */ public WBVariableModel(String name, String[] values, String activeValue) { this(name, values, activeValue, false, null); } /** * Creates an unlocked WBVariableModel with given name, values, randomizer, and no * active value. * @param name The WBVariable name * @param values The possible WBVariable values * @param randomizer The associated Randomizer */ public WBVariableModel(String name, String[] values, Randomizer randomizer) { this(name, values, null, false, randomizer); } /** * Creates a WBVariableModel with given name, values, active value, lock * state, and randomizer. All other constructors call this one. * @param name The WBVariable name * @param values The possible WBVariable values * @param activeValue The value the WBVariable is "set" to * @param locked Whether the WBVariable is locked * @param randomizer The Randomizer associated with this WBVariable */ public WBVariableModel(String name, String[] values, String activeValue, boolean locked, Randomizer randomizer) { this.name = name; this.values = values; this.locked = locked; this.randomizer = randomizer; int i; for (i=0; iindex
-th possible value of the WBVariable
*/
public String getValue(int index) { return values[index]; }
/**
* @param newValues The new set of possible values for the WBVariable.
*/
public synchronized void setValues(String[] newValues) {
if (newValues == null) // don't allow null; make it almost empty
newValues = new String[]{""};
for (int i=0; inull
, otherwise.
*/
public String getActiveValue() {
if (activeIndex == -1) return null;
else return values[activeIndex];
}
/**
* @return The index of the current active value, if there is one,
* -1
, otherwise.
*/
public int getActiveValueIndex() { return activeIndex; }
/**
* @param The new value for the active value (possibly null
).
*/
public synchronized void setActiveValue(String newValue) {
if (newValue == null) {
int oldIndex = activeIndex;
activeIndex = -1;
firePropertyChange("activevalue", values[oldIndex], null);
return;
}
int i; for (i=0; i