package causation.lab; import javax.swing.*; import java.awt.*; import java.util.StringTokenizer; /** * This class displays a set of independencies for a particular * dataset. As of right now, this display only shows DSeparationFacts. * However, in the future, we're going to add the ability to display * the results of statistical tests. * * @see IndepInternalFrame * @see DSeparationFacts * @version 0.9 Aug 18, 1999 * @author David Danks */ public class DataIndepDisplay extends JComponent { /////////////////// Static Class Variables /////////////////// private static final String TITLE_START = "Dataset: "; private static final String SIZE_START = "N="; private static final String INFINITY_STRING = "[whole pop]"; 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, 11); private static final Insets MARGIN = new Insets(10, 10, 10, 10); 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 String title; private String size; private DSeparationFacts dsep; /////////////////// Constructors /////////////////// /** * Creates a DataIndepDisplay for a dataset with given name, size, and * DSeparationFacts. * @param datasetName The name of the dataset * @param datasetSize The size of the dataset (or <0, if infinite) * @param dsepFacts The DSeparationFacts for the dataset. */ public DataIndepDisplay(String datasetName, int datasetSize, DSeparationFacts dsepFacts) { title = TITLE_START+datasetName; if (datasetSize < 0) size = SIZE_START+INFINITY_STRING; else size = SIZE_START+String.valueOf(datasetSize); dsep = dsepFacts; setFont(FONT); } /////////////////// Instance Methods /////////////////// /** * @return The preferred size of this DataIndepDisplay */ public Dimension getPreferredSize() { FontMetrics fm = getFontMetrics(getFont()); int width = 0; int height = 0; // account for the label width = Math.max(width, fm.stringWidth(title)); width = Math.max(width, fm.stringWidth(size)); height += 2*fm.getHeight(); // account for the line // account for the 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 draw the background */ public boolean isOpaque() { return true; } /** * Does all of the painting of the display. * @param g The Graphics context in which to draw this display */ public void paintComponent(Graphics g) { FontMetrics fm = getFontMetrics(getFont()); g.setColor(BACKGROUND); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(LABEL); // draw the label g.drawString(title, MARGIN.left, MARGIN.top+fm.getAscent()); g.drawString(size, MARGIN.left, MARGIN.top+fm.getHeight()+fm.getAscent()); // draw a dividing line int yOffset = MARGIN.top+2*fm.getHeight(); // 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