package causation.lab; // packages import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.awt.geom.*; // custom classes import causation.lab.WorkbenchObject; import causation.lab.plaf.LatentUI; /** *
* This class encodes the Latent variables for the CausalityLab. For now, * they cannot have values, so they are just graphical objects, and * nodes in a directed graph. *
* The following PropertyChangeEvent
s are created by this
* class:
*
* NOTE: It would be nice if this class actually used tetrad.graph.Node as * a model, since it would better integrate the CausalityLab with Tetrad * IV. However, this code was written before Tetrad IV was started, and * it would be more trouble than it would be worth to fix things right * now. *
* Copyright 1999 by David Danks. All rights reserved. *
* * @see WorkbenchObject * @see LatentUI * @version 0.9 June 28, 1999 * @author David Danks */ public class Latent extends WorkbenchObject { /////////////////// Class Variables /////////////////// private static final String UIClassID = "causation.lab.plaf.LatentUI"; private static final Font font = new Font("Serif", Font.ITALIC, 14); private static final Insets MARGIN = new Insets(3, 10, 3, 10); /////////////////// Instance Variables /////////////////// private String name; private boolean selected = false; private boolean experimenting = false; private boolean highlight = false; private Ellipse2D.Double ellipse = null; private Dimension size = null; /////////////////// Constructors /////////////////// /** * Creates a Latent named "default" */ public Latent() { this("default"); } /** * Creates a Latent with the given name * @param name The name of the Latent */ public Latent(String name) { this.name = name; setFont(font); updateParameters(); updateUI(); } /////////////////// Instance Methods /////////////////// /** * @return The name of the Latent */ public String getName() { return name; } /** * @param newName The new name of the Latent */ public void setName(String newName) { if (newName == null) newName = ""; if (newName.equals(name)) return; String oldName = name; updateParameters(); firePropertyChange("name", oldName, name); } /** * @return Whether the Workbench with the Latent is "experimenting". */ public boolean isExperimenting() { return experimenting; } /** * @param b Whether the Workbench with the Latent is "experimenting". */ public void setExperimenting(boolean b) { experimenting = b; } /** * @return Whether the Latent is currently highlighted */ public boolean isHighlight() { return highlight; } /** * @param b Whether the Latent should be highlighted */ public void setHighlight(boolean b) { highlight = b; } /** * @return The preferred size of the Latent */ public Dimension getPreferredSize() { return size; } /** * @return The minimum size of the Latent (currently the preferred size) */ public Dimension getMinimumSize() { return size; } /** * @returnfalse
always, since we want transparency
*/
public boolean isOpaque() { return false; }
/**
* @return The ellipse that bounds the Latent
*/
public Ellipse2D getEllipse() { return ellipse; }
/**
* Implemented since we are subclassing WorkbenchObject
* @return The bounding Shape for the Latent (an Ellipse2D.Double)
*/
public Shape getPerimeter() {
Ellipse2D.Double shift = new
Ellipse2D.Double(ellipse.getX()+getBounds().x,
ellipse.getY()+getBounds().y, ellipse.getWidth(),
ellipse.getHeight());
return (Shape)shift;
}
/**
* @return The Point of the lower left corner of the (centered) text
*/
public Point getTextOrigin() {
return new Point(MARGIN.left,
MARGIN.top+getFontMetrics(getFont()).getAscent());
}
/**
* A helper method that determines the size of the ellipse
*/
private void updateParameters() {
// first, determine the size of the text box
FontMetrics fm = getFontMetrics(getFont());
int intHeight = fm.getHeight();
int intWidth = fm.stringWidth(name);
// take the margin into account
intHeight += (MARGIN.top + MARGIN.bottom);
intWidth += (MARGIN.left + MARGIN.right);
size = new Dimension(intWidth+1, intHeight+1);
// now, create the ellipse
ellipse = new Ellipse2D.Double(0.0, 0.0, (double)intWidth,
(double)intHeight);
}
// UI methods
/**
* @param ui The LatentUI to attach to this Latent
*/
public void setUI(LatentUI ui) { super.setUI(ui); }
/**
* Sets up the UI appropriately
*/
public void updateUI() {
setUI((LatentUI)UIManager.getUI(this));
invalidate();
}
/**
* @return The String identifying the UI for this Class
*/
public String getUIClassID() { return UIClassID; }
}