package rebelsky.text; /** * A simple representation of a multi-line block of text * surrounded by stars. * * @author Samuel A. Rebelsky * @author Everyone else in CSC152 2005S */ public class SurroundedText implements TextBlock { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The stuff that gets surrounded. */ TextBlock blob; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Surround a simple text block. * * @pre * The block is not null * @exception Exception * If the block is null. */ public SurroundedText(TextBlock _blob) throws Exception { if (_blob == null) throw new Exception("Cannot surround null text"); this.blob = _blob; } // SurroundedText(TextBlock) // +----------------+------------------------------------------ // | Public Methods | // +----------------+ /** * Determine the width of the surrounded block. */ public int getWidth() { return 4 + this.blob.getWidth(); } // getWidth() /** * Determine the number of lines of the surrounded block. */ public int getLines() { return 2 + this.blob.getLines(); } // getLines() /** * Determine the nth line of this block. Lines are numbered * starting with 0. * * @exception Exception * If an invalid line number is given. */ public TextLine getLine(int n) throws Exception { if ( (n >= 1) && (n <= this.blob.getLines()) ) { return new TextLine("* " + this.blob.getLine(n-1).atWidth(this.blob.getWidth()) + " *"); } else if ((n == 0) || (n == this.blob.getLines() + 1)) { String stars = "****"; while (stars.length() < this.getWidth()) { stars = stars + stars; } return new TextLine(stars.substring(0,this.getWidth())); } else { throw new Exception("Invalid line number."); } } // getLine() } // class SurroundedText