/* Containment.java * * Copyright (c) 2002, Benja Fallenstein * * You may use and distribute under the terms of either the GNU Lesser * General Public License, either version 2 of the license or, * at your choice, any later version. Alternatively, you may use and * distribute under the terms of the XPL. * * See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of * the licenses. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README * file for more details. * */ /* * Written by Benja Fallenstein */ package gzz.zzutil; import gzz.*; import gzz.media.*; /** Convenience methods for Ted's containment mechanism. */ public class Containment { /** Get the contents of this cell as a String, as specified * by the containment mechanism. This interprets the containment * structure and joins the text of the different cells * together. */ public static String getContainedText(Cell c) { Dim d_contain = c.space.getDim(Ids.d_contain); Dim d_contain_list = c.space.getDim(Ids.d_contain_list); String s = c.t(); Cell head = c.s(d_contain); if(head == null) return s; Cell x = head; do { s += getContainedText(x); x = x.s(d_contain_list); } while(x != null && !x.equals(head)); return s; } /** Get the contents of this cell as an Enfilade1D, * as specified by the containment mechanism. */ public static Enfilade1D getContainedEnfilade(Cell c) { VStreamCellTexter t = (VStreamCellTexter)c.space.getCellTexter(); Dim d_contain = c.space.getDim(Ids.d_contain); Dim d_contain_list = c.space.getDim(Ids.d_contain_list); Enfilade1D enf = t.getEnfilade(c, null); Cell head = c.s(d_contain); if(head == null) return enf; Cell x = head; do { enf = enf.plus(getContainedEnfilade(x)); x = x.s(d_contain_list); } while(x != null && !x.equals(head)); return enf; } /** Add the 'add' cell to the end of the list of cells * inside 'into'. This means that 'add''s contents * will appear after the contents of all other cells * already contained in 'into.' * * @throws IllegalArgumentException * if 'add' is already contained in another cell * (i.e., has a connection on d..contain-list, or * a connection negwards on d.contain). */ public static void addContainedCell(Cell into, Cell add) throws IllegalArgumentException { Dim d_contain = into.space.getDim(Ids.d_contain); Dim d_contain_list = into.space.getDim(Ids.d_contain_list); if(add.s(d_contain, -1) != null || add.s(d_contain_list, -1) != null || add.s(d_contain_list) != null) { throw new IllegalArgumentException("Already contained: "+add); } if(into.s(d_contain) == null) into.connect(d_contain, add); else { Cell c = into.s(d_contain, 1); while(c.s(d_contain_list) != null && c.s(d_contain_list).s(d_contain, -1) == null) { c = c.s(d_contain_list); } c.insert(d_contain_list, 1, add); } } }