001 // This file is part of the Attempto Java Packages. 002 // Copyright 2008-2009, 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 number of text elements of this text container. 038 * 039 * @return The number of text elements. 040 */ 041 public int getTextElementsCount() { 042 return elements.size(); 043 } 044 045 /** 046 * Returns the text element with the given index. 047 * @param index The index of the text element to be returned. 048 * @return The text element. 049 */ 050 public TextElement getTextElement(int index) { 051 return elements.get(index); 052 } 053 054 /** 055 * Returns the sequence of text elements. 056 * 057 * @return A list containing the text elements. 058 */ 059 public ArrayList<TextElement> getTextElements() { 060 return new ArrayList<TextElement>(elements); 061 } 062 063 /** 064 * Sets the text elements. 065 * @param elements A list of text elements. 066 */ 067 public void setTextElements(List<TextElement> elements) { 068 this.elements = new ArrayList<TextElement>(elements); 069 updateConnections(); 070 } 071 072 /** 073 * Adds the text element to the end of the sequence. 074 * 075 * @param el The text element to be added. 076 */ 077 public void addElement(TextElement el) { 078 elements.add(el); 079 updateConnections(); 080 } 081 082 /** 083 * Removes all text elements. 084 */ 085 public void removeAllElements() { 086 elements.clear(); 087 } 088 089 /** 090 * Removes the last text element of the sequence if it is not empty. 091 */ 092 public void removeLastElement() { 093 if (elements.size() > 0) { 094 elements.remove(elements.size() - 1); 095 updateConnections(); 096 } 097 } 098 099 /** 100 * Returns the text that is represented by the sequence of text element as a string. 101 * 102 * @return The text. 103 */ 104 public String getText() { 105 String text = ""; 106 for (TextElement e : elements) { 107 if (e.getText().matches("[.?!]")) { 108 text += e.getText(); 109 } else { 110 text += " " + e.getText(); 111 } 112 } 113 if (text.startsWith(" ")) { 114 text = text.substring(1); 115 } 116 return text; 117 } 118 119 /** 120 * This method updates the connections between the text elements by calling their 121 * {@code checkNeighborTextElements}-method. 122 * 123 * @see TextElement#checkNeighborTextElements 124 */ 125 public void updateConnections() { 126 for (int i = 1; i < elements.size()-1; i++) { 127 elements.get(i).checkNeighborTextElements(elements.get(i-1), elements.get(i+1)); 128 } 129 if (elements.size() > 1) { 130 elements.get(0).checkNeighborTextElements(null, elements.get(1)); 131 elements.get(elements.size()-1).checkNeighborTextElements(elements.get(elements.size()-2), null); 132 } 133 if (elements.size() == 1) { 134 elements.get(0).checkNeighborTextElements(null, null); 135 } 136 } 137 138 }