/*
 * File:     InterlinearBlock.java
 * Project:  MPI Linguistic Application
 * Date:     03 April 2006
 *
 * Copyright (C) 2001-2006  Max Planck Institute for Psycholinguistics
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package mpi.eudico.client.annotator.interlinear;

import java.util.ArrayList;


/**
 * A class that represents a block of positioned printable, interlinear
 * annotations.  It is in fact a group of interlinear tiers, occupying an area
 * that spans from the left border to the right border of the output area and
 * that optionally includes a margin for tier labels.
 *
 * @author HS
 * @version 1.0
 */
public class InterlinearBlock {
    private ArrayList printTiers;
    private int occBlockWidth;

    /**
     * Creates a new InterlinearBlock instance
     *
     * @param printTiers a list of InterlinearTiers that can receive
     *        InterlinearAnnotations
     */
    public InterlinearBlock(ArrayList printTiers) {
        this.printTiers = printTiers;
    }

    /**
     * Returns the InterlinearTier of the specified name.
     *
     * @param tierName the name of the tier
     *
     * @return the <code>InterlinearTier</code> for the specified tier
     */
    public InterlinearTier getPrintTier(String tierName) {
        InterlinearTier pt;

        for (int i = 0; i < printTiers.size(); i++) {
            pt = (InterlinearTier) printTiers.get(i);

            if (pt.getTierName().equals(tierName)) {
                return pt;
            }
        }

        return null;
    }

    /**
     * Returns a list of the InterlinearTiers in this block.
     *
     * @return a list of the InterlinearTiers in this block
     */
    public ArrayList getPrintTiers() {
        return printTiers;
    }

    /**
     * Returns the total numbers of lines on the tiers in this block.
     *
     * @return the total numbers of lines on the tiers in this block
     */
    public int getNumberOfLines() {
        int lines = 0;
        InterlinearTier pt;

        for (int i = 0; i < printTiers.size(); i++) {
            pt = (InterlinearTier) printTiers.get(i);
            lines += pt.getNumLines();
        }

        return lines;
    }

    /**
     * Invokes the removal of tiers that contain no annotations.
     */
    public void removeEmptyTiers() {
        InterlinearTier pt;

        for (int i = printTiers.size() - 1; i >= 0; i--) {
            pt = (InterlinearTier) printTiers.get(i);

            if (pt.getAnnotations().size() == 0) {
                printTiers.remove(i);
            }
        }
    }

    /**
     * Returns the width currently occupied by the annotations in the tiers.
     *
     * @return the width
     */
    public int getOccupiedBlockWidth() {
        return occBlockWidth;
    }

    /**
     * The occupied block width can be set explicitely from the outside world,
     * irrespective of the annotations present on the tiers.
     *
     * @param occBlockWidth the new block width
     */
    public void setOccupiedBlockWidth(int occBlockWidth) {
        this.occBlockWidth = occBlockWidth;

        for (int i = 0; i < printTiers.size(); i++) {
            ((InterlinearTier) printTiers.get(i)).setPrintWidth(occBlockWidth);
        }
    }

    /**
     * Calculates the current max. occupied horizontal space by any annotation
     * on  any tier in the block.
     *
     * @return the current occupied horizontal space
     */
    public int calculateOccupiedBlockWidth() {
        int curWidth = 0;

        for (int i = 0; i < printTiers.size(); i++) {
            int tWidth = ((InterlinearTier) printTiers.get(i)).getPrintAdvance();

            if (tWidth > curWidth) {
                curWidth = tWidth;
            }
        }

        return curWidth;
    }
}
