    001    // This file is part of the Attempto Java Packages.
    002    // Copyright 2008, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
    003    //
    004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
    005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
    006    // either version 3 of the License, or (at your option) any later version.
    007    //
    008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
    009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    010    // PURPOSE. See the GNU Lesser General Public License for more details.
    011    //
    012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
    013    // Java Packages. If not, see http://www.gnu.org/licenses/.
    014
    015    package ch.uzh.ifi.attempto.preditor.text;
    016
    017    import java.util.ArrayList;
    018    import java.util.List;
    019
    020    /**
    021     * This class represents a text container that stores a sequence of text elements that represent a
    022     * (partial) text or sentence.
    023     *
    024     * @author Tobias Kuhn
    025     */
    026    public class TextContainer {
    027
    028            private ArrayList<TextElement> elements = new ArrayList<TextElement>();
    029
    030            /**
    031             * Creates a new text container.
    032             */
    033            public TextContainer() {
    034            }
    035
    036            /**
    037             * Returns the sequence of text elements.
    038             *
    039             * @return A list containing the text elements.
    040             */
    041            public ArrayList<TextElement> getTextElements() {
    042                    return new ArrayList<TextElement>(elements);
    043            }
    044
    045            /**
    046             * Sets the text elements.
    047             * @param elements A list of text elements.
    048             */
    049            public void setTextElements(List<TextElement> elements) {
    050                    this.elements = new ArrayList<TextElement>(elements);
    051                    updateConnections();
    052            }
    053
    054            /**
    055             * Adds the text element to the end of the sequence.
    056             *
    057             * @param el The text element to be added.
    058             */
    059            public void addElement(TextElement el) {
    060                    elements.add(el);
    061                    updateConnections();
    062            }
    063
    064            /**
    065             * Removes all text elements.
    066             */
    067            public void removeAllElements() {
    068                    elements.clear();
    069            }
    070
    071            /**
    072             * Removes the last text element of the sequence if it is not empty.
    073             */
    074            public void removeLastElement() {
    075                    if (elements.size() > 0) {
    076                            elements.remove(elements.size() - 1);
    077                            updateConnections();
    078                    }
    079            }
    080
    081            /**
    082             * Returns the text that is represented by the sequence of text element as a string.
    083             *
    084             * @return The text.
    085             */
    086            public String getText() {
    087                    String text = "";
    088                    for (TextElement e : elements) {
    089                            if (e.getText().matches("[.?!]")) {
    090                                    text += e.getText();
    091                            } else {
    092                                    text += " " + e.getText();
    093                            }
    094                    }
    095                    if (text.startsWith(" ")) {
    096                            text = text.substring(1);
    097                    }
    098                    return text;
    099            }
    100
    101            private void updateConnections() {
    102                    for (int i = 1; i < elements.size()-1; i++) {
    103                            elements.get(i).checkNeighborTextElements(elements.get(i-1), elements.get(i+1));
    104                    }
    105                    if (elements.size() > 1) {
    106                            elements.get(0).checkNeighborTextElements(null, elements.get(1));
    107                            elements.get(elements.size()-1).checkNeighborTextElements(elements.get(elements.size()-2), null);
    108                    }
    109                    if (elements.size() == 1) {
    110                            elements.get(0).checkNeighborTextElements(null, null);
    111                    }
    112            }
    113
    114    }