package causation.lab;
import javax.swing.*;
import java.awt.*;
import java.util.StringTokenizer;
/**
* This class displays a set of independencies for a particular
* hypothesis.
*
* @see IndepInternalFrame
* @see DSeparationFacts
* @version 0.9 Aug 18, 1999
* @author David Danks
*/
public class HypIndepDisplay extends JComponent {
/////////////////// Static Class Variables ///////////////////
private static final String TITLE_START = "Hypothesis";
private static final Color LABEL = new Color(102, 102, 102);
private static final Color BACKGROUND = Color.white;
private static final Font FONT = new Font("Dialog", Font.BOLD, 12);
private static final Insets MARGIN = new Insets(10, 10, 10, 10);
private static final int BORDER_WIDTH = 2;
private static final int SPACE = 4;
private static final int INSET = 4; // constants for drawing the
private static final int CENTER = 5; // independence symbol
private static final int BASE = 2*INSET+CENTER;
private static final int DROP = 7;
private static final String NO_INDEPS_STRING = "No independencies.";
/////////////////// Instance Variables ///////////////////
private Image im;
private DSeparationFacts dsep;
private String title;
/////////////////// Constructors ///////////////////
/**
* This creates a HypIndepDisplay with given Image and DSeparationFacts,
* and with the title "Hypothesis".
* @param display The Image to display
* @param dsepFacts The DSeparationFacts to display
*/
public HypIndepDisplay(Image display, DSeparationFacts dsepFacts) {
this(display, dsepFacts, -1);
}
/**
* This creates a HypIndepDisplay with given Image and DSeparationFacts,
* and with the title "Hypothesis #count
".
* @param display The Image to display
* @param dsepFacts The DSeparationFacts to display
* @param count The hypothesis number (for the title)
*/
public HypIndepDisplay(Image display, DSeparationFacts dsepFacts,
int count) {
im = display;
dsep = dsepFacts;
if (count < 0) title = TITLE_START;
else title = TITLE_START+" #"+String.valueOf(count);
setFont(FONT);
}
/////////////////// Instance Methods ///////////////////
/**
* @return The preferred size of this HypIndepDisplay
*/
public Dimension getPreferredSize() {
FontMetrics fm = getFontMetrics(getFont());
int width = 0; int height = 0;
// account for text
width = Math.max(width, fm.stringWidth(title));
height += fm.getHeight();
// account for image
width = Math.max(width, im.getWidth(this));
height += im.getHeight(this);
// account for line
// account for facts
String[] facts = dsep.getDSepFacts();
if (facts.length == 0) {
height += fm.getHeight();
width = Math.max(width, fm.stringWidth(NO_INDEPS_STRING));
}
else {
height += (facts.length+1)*fm.getHeight();
for (int i=0; itrue, since we paint the background
*/
public boolean isOpaque() { return true; }
/**
* Does the painting of the display.
* @param g The Graphics context in which to paint the HypIndepDisplay
*/
public void paintComponent(Graphics g) {
FontMetrics fm = getFontMetrics(getFont());
g.setColor(BACKGROUND);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(LABEL);
// draw the label
int xInset = (getWidth()-fm.stringWidth(title))/2;
g.drawString(title, xInset, MARGIN.top+fm.getAscent());
// draw the image
xInset = (getWidth()-im.getWidth(this))/2;
g.drawImage(im, xInset, MARGIN.top+fm.getHeight(), this);
// draw a dividing line
int yOffset = MARGIN.top+fm.getHeight()+im.getHeight(this);
// draw the facts
String[] facts = dsep.getDSepFacts();
if (facts.length == 0) {
g.drawString(NO_INDEPS_STRING, MARGIN.left,
yOffset+fm.getAscent());
return;
}
for (int i=0; i