package rebelsky.text; /** * A simple representation of the vertical composition of two * blocks of text. * * @author Samuel A. Rebelsky * @author Everyone else in CSC152 2005S */ public class VerticallyCompose implements TextBlock { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The stuff on top. */ TextBlock top; /** * The stuff on the bottom. */ TextBlock bottom; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Surround a simple text block. * * @pre * The block is not null * @exception Exception * If the block is null. */ public VerticallyCompose(TextBlock _top, TextBlock _bottom) throws Exception { if ((_top == null) || (_bottom == null)) throw new Exception("Cannot compose null text"); this.top = _top; this.bottom = _bottom; } // VerticallyCompose(TextBlock) // +----------------+------------------------------------------ // | Public Methods | // +----------------+ /** * Determine the width of the composed block. */ public int getWidth() { if (this.top.getWidth() > this.bottom.getWidth()) return this.top.getWidth(); else return this.bottom.getWidth(); } // getWidth() /** * Determine the number of lines of the composed block. */ public int getLines() { return this.top.getLines() + this.bottom.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 { // Compute the number of lines in the top, because we use // it repeatedly. int toplines = this.top.getLines(); // The first toplines lines belong to the top. if (n < toplines) return this.top.getLine(n); // The next this.bottom.getLines() lines belong to this.bottom else if (n < toplines + this.bottom.getLines()) return this.bottom.getLine(n-toplines); else throw new Exception("Invalid line number."); } // getLine() } // class VerticallyCompose