package causation.lab; // packages import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.net.*; // custom classes import causation.lab.WorkbenchObject; import causation.lab.plaf.WBVariableUI; import causation.lab.WBVariableModel; import causation.lab.Randomizer; /** *
* A WBVariable maintains information about its name, possible values, whether * it is locked, and what its active value (i.e., the value to display) is. * If desired, the user can also set an icon to represent the WBVariable. * Note: In order to load the lock icon, the user must call setCodebase(URL) * at some point after construction, otherwise the WBVariable cannot load the * icon. *
* The following PropertyChangeEvent
s are created by this class:
*
* Copyright 1999 by David Danks. All rights reserved. *
* * @see WBVariableModel * @see BasicWBVariableUI * @version 0.9 Aug 30, 1999 * @author David Danks */ public class WBVariable extends WorkbenchObject { /////////////////// Static Class Variables /////////////////// // Note: it would be nice if we could eventually set all of these private static final Font font = new Font("SansSerif", Font.BOLD, 12); /////////////////// Instance Variables /////////////////// private WBVariableModel model; private String UIClassID = "causation.lab.plaf.WBVariableUI"; private boolean experimenting = true; private boolean highlight = false; /////////////////// Constructors /////////////////// /** * Null constructor. Creates an unlocked WBVariable with blank name, * one blank value, no active value, and no randomizer. */ public WBVariable() { init(new WBVariableModel()); } /** * Creates an unlocked WBVariable with given name, one blank value, no * active value, and no randomizer. * @param name The WBVariable name */ public WBVariable(String name) { init(new WBVariableModel(name)); } /** * Creates an unlocked WBVariable with blank name, one blank value, no * active value, no randomizer, and given icon. * @param icon The WBVariable icon */ public WBVariable(Icon icon) { init(new WBVariableModel()); setNameIcon(icon); } /** * Creates an unlocked WBVariable with blank name, given values, no * active value, and no randomizer. * @param values The possible WBVariable values */ public WBVariable(String[] values) { init(new WBVariableModel(values)); } /** * Creates an unlocked WBVariable with blank name, given values, no * active value, no randomizer, and given icon. * @param icon The WBVariable icon * @param values The possible WBVariable values */ public WBVariable(Icon icon, String[] values) { init(new WBVariableModel(values)); setNameIcon(icon); } /** * Creates an unlocked WBVariable with given name and values, no active * value, and no randomizer. * @param name The WBVariable name * @param values The possible WBVariable values */ public WBVariable(String name, String[] values) { init(new WBVariableModel(name, values)); } /** * Creates an unlocked WBVariable 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 WBVariable(String[] values, String activeValue) { init(new WBVariableModel(values, activeValue)); } /** * Creates an unlocked WBVariable 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 WBVariable(String name, String[] values, String activeValue) { init(new WBVariableModel(name, values, activeValue)); } /** * Creates an unlocked WBVariable with given name, values, randomizer, and no * active value. * @param name The WBVariable name * @param values The possible WBVariable values * @param randomizer The Randomizer associated with this WBVariable */ public WBVariable(String name, String[] values, Randomizer randomizer) { init(new WBVariableModel(name, values, randomizer)); } /** * Creates a WBVariable with given name, values, active value, lock state, and * randomizer. * @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 WBVariable(String name, String[] values, String activeValue, boolean locked, Randomizer randomizer) { init(new WBVariableModel(name, values, activeValue, locked, randomizer)); } /** * Creates a WBVariable with given name, values, active value, lock state, * randomizer, and given icon. * @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 * @param icon The WBVariable icon */ public WBVariable(String name, String[] values, String activeValue, boolean locked, Randomizer randomizer, Icon icon) { init(new WBVariableModel(name, values, activeValue, locked, randomizer)); setNameIcon(icon); } /** * Creates a WBVariable with the given WBVariableModel. * @param v The WBVariableModel for this WBVariable */ public WBVariable(WBVariableModel v) { init(v); } /** * This is a helper method that all of the different constructors call. * @param vModel The WBVariableModel for this WBVariable */ private void init(WBVariableModel vModel) { model = vModel; setFont(font); updateUI(); } /////////////////// Instance Methods /////////////////// // Appearance/painting methods /** * @return The preferred size of this WBVariable */ public Dimension getPreferredSize() { return ((WBVariableUI)ui).getPreferredSize(this); } /** * @return The preferred size of this WBVariable, since the two are the same */ public Dimension getMinimumSize() { return ((WBVariableUI)ui).getMinimumSize(this); } /** * @returntrue
, since the WBVariable fills in its whole size
*/
public boolean isOpaque() { return true; }
/**
* @return The bounding Dimension for this WBVariable
*/
public Shape getPerimeter() { return (Shape)getBounds(); }
/**
* @return The "name" icon
*/
public Icon getNameIcon() { return ((WBVariableUI)ui).getNameIcon(); }
/**
* @param newIcon The new icon for the WBVariable
*/
public void setNameIcon(Icon newIcon) {
((WBVariableUI)ui).setNameIcon(newIcon);
if (isShowing()) repaint();
}
/**
* @return The "grey" Icon used to indicate a lock (used to "break" the
* arrows in an independence drawing).
*/
public Icon getDisabledLockIcon() {
return ((WBVariableUI)ui).getDisabledLockIcon();
}
/**
* @return The codebase for the WBVariable to use in loading the lock icon
*/
public URL getCodebase() { return ((WBVariableUI)ui).getCodebase(); }
/**
* @param codebase The URL for the WBVariable to use when loading the
* lock icon
*/
public void setCodebase(URL codebase) {
((WBVariableUI)ui).setCodebase(codebase);
}
/**
* @return Whether the WBVariable can be experimented on
*/
public boolean isExperimenting() { return experimenting; }
/**
* @param b Whether the WBVariable can now be experimented on
*/
public void setExperimenting(boolean b) {
if (b == experimenting) return;
experimenting = b;
firePropertyChange("experimenting", b, !(b));
}
/**
* @param b Whether the mouse is over the WBVariable
*/
public void setHighlight(boolean b) {
if (b == highlight) return;
highlight = b;
firePropertyChange("highlight", b, !(b));
}
/**
* @return Whether the mouse is over the WBVariable
*/
public boolean isHighlight() { return highlight; }
// General accessor methods
/**
* @return Whether the WBVariable is locked
*/
public boolean isLocked() { return model.isLocked(); }
/**
* @param Whether the WBVariable should be locked
*/
public void setLocked(boolean b) {
if (b == model.isLocked()) return;
model.setLocked(b);
firePropertyChange("locked", !(b), b);
}
/**
* @return Whether the WBVariable is randomized
*/
public boolean isRandomized() { return model.isRandomized(); }
/**
* @return The Randomizer associated with the WBVariable
*/
public Randomizer getRandomizer() { return model.getRandomizer(); }
/**
* @param The Randomizer to associate with this WBVariable, or null
to
* remove the Randomizer
*/
public void setRandomizer(Randomizer newRandom) {
boolean old = model.isRandomized();
if ((newRandom == null) && (!(old))) return;
model.setRandomizer(newRandom);
firePropertyChange("randomized", old, (newRandom != null));
}
/**
* @return The name of the WBVariable
*/
public String getName() { return model.getName(); }
/**
* @param newName The new name for the WBVariable
*/
public void setName(String newName) {
String old = model.getName();
if (!(old.equals(newName))) {
model.setName(newName);
firePropertyChange("name", old, newName);
}
}
/**
* @return The possible values of the WBVariable
*/
public String[] getValues() { return model.getValues(); }
/**
* @param index The index of the value name
* @return The name of the index
-th value
*/
public String getValue(int index) { return model.getValue(index); }
/**
* @param newValues The new set of values for this WBVariable
*/
public void setValues(String[] newValues) {
String[] old = model.getValues();
if (old != newValues) {
model.setValues(newValues);
firePropertyChange("allvalues", null, null);
}
}
/**
* @param newValue The name for the new index
-th value
* @param index The index of the value to change
*/
public void setValue(String newValue, int index) {
String old = model.getValue(index);
if (!(old.equals(newValue))) {
model.setValue(newValue, index);
firePropertyChange("value:"+index, old, newValue);
}
}
/**
* @return The name of the active value, if there is one;
* null
, otherwise
*/
public String getActiveValue() { return model.getActiveValue(); }
/**
* @return The index of the active value, if there is one;
* -1
, otherwise
*/
public int getActiveValueIndex() { return model.getActiveValueIndex(); }
/**
* @param The name of the value to make the active one
*/
public void setActiveValue(String newValue) {
String oldValue = model.getActiveValue();
if (newValue == null) {
if (oldValue == null) return;
else {
model.setActiveValue(newValue);
firePropertyChange("activevalue", oldValue, null);
return;
}
}
if (newValue.equals(oldValue)) return;
model.setActiveValue(newValue);
firePropertyChange("activevalue", oldValue, model.getActiveValue());
}
// UI methods
/**
* @param ui The WBVariableUI to attach to this WBVariable
*/
public void setUI(WBVariableUI ui) { super.setUI(ui); }
/**
* Sets up the UI appropriately
*/
public void updateUI() {
setUI((WBVariableUI)UIManager.getUI(this));
invalidate();
}
/**
* @return The String identifying the UI for this Class
*/
public String getUIClassID() { return UIClassID; }
// Misc. Object methods
/**
* @return The String representation of the WBVariableModel
*/
public String toString() { return model.toString(); }
}