package causality; // packages import javax.swing.*; import java.awt.*; import java.util.*; /** *
* This class is a JPanel that displays an instance of DSeparationFacts, along * with an (optional) Image. *
* Copyright 1999 by David Danks. All rights reserved. *
* * @see DSeparationFacts * @version 1.0; Mar 20, 1999 * @author David Danks */ public class DSeparationPanel extends JPanel { /////////////////// Class Variable /////////////////// private static final Font FONT = new Font("Serif", Font.PLAIN, 18); private static final int MARGIN = 10; private static final int LINE_WIDTH = 3; 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_FACTS_STRING = "No independencies."; private static final String NULL_IMAGE_STRING = "No image."; /////////////////// Instance Variables /////////////////// private Image image; private DSeparationFacts dsf; private String[] facts; // private double scale = 1.0; /** * Determines the d-separation facts for the given DiGraph. Note: the * DSeparationPanel does not automatically pop up. * @param dg The DiGraph whose d-separation facts are requested * @param im An (optional) Image to display with the independencies */ public DSeparationPanel(DiGraph dg, Image im) { this(new DSeparationFacts(dg), im); } /** * Displays the given DSeparationFacts. Note: the DSeparationPanel does not * automatically pop up. * @param dsf The DSeparationFacts to display * @param im An (optional) Image to display */ public DSeparationPanel(DSeparationFacts dsf, Image im) { this.dsf = dsf; image = im; facts = dsf.getDSepFacts(); setFont(FONT); } /** * @return The DSeparationFacts to be displayed */ public DSeparationFacts getDSeparationFacts() { return dsf; } /** * @return The preferred size of the DSeparationPanel */ public Dimension getPreferredSize() { FontMetrics fm = getFontMetrics(getFont()); int width = 0; // space for line and gap around line (the line length is the panel width) int height = 3*LINE_WIDTH; // space for facts if (facts.length == 0) { height += fm.getHeight(); width = Math.max(width, fm.stringWidth(NO_FACTS_STRING)+2*MARGIN); } else { height += (facts.length+1)*fm.getHeight(); for (int i=0; i