001 // This file is part of AceWiki.
002 // Copyright 2008-2012, AceWiki developers.
003 //
004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006 // the License, or (at your option) any later version.
007 //
008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010 // Lesser General Public License for more details.
011 //
012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013 // not, see http://www.gnu.org/licenses/.
014
015 package ch.uzh.ifi.attempto.base;
016
017
018 /**
019 * This class describes a text element (a word or a phrase) to be used by the predictive editor.
020 * Every text element contains a text and one or more grammatical categories. If a text element
021 * contains more than one category then this means that it can stand for any of these categories.
022 *
023 * @author Tobias Kuhn
024 */
025 public class TextElement {
026
027 private String text;
028 private TextContainer textContainer;
029
030 /**
031 * Creates a new text element.
032 *
033 * @param text The text of the new text element.
034 */
035 public TextElement(String text) {
036 this.text = text;
037 }
038
039 /**
040 * Creates a new text element without initializing the fields of the object.
041 */
042 protected TextElement() {
043 }
044
045 /**
046 * Returns the original text of this text element. This is the unchanged text before the
047 * context is checked by the text operator.
048 *
049 * @return The original text (before context checking).
050 */
051 public String getOriginalText() {
052 return text;
053 }
054
055 /**
056 * Returns the text of this text element. The text might have been changed by the context
057 * checking of the text operator.
058 *
059 * @return The text (after context checking).
060 */
061 public String getText() {
062 if (textContainer != null) {
063 return textContainer.getTextElementText(this);
064 }
065 return getOriginalText();
066 }
067
068 /**
069 * Sets the text container that contains this text element.
070 *
071 * @param textContainer The text container.
072 */
073 void setTextContainer(TextContainer textContainer) {
074 this.textContainer = textContainer;
075 }
076
077 /**
078 * Removes the text container.
079 */
080 void removeTextContainer() {
081 this.textContainer = null;
082 }
083
084 /**
085 * Two text elements are equals if they share the same text.
086 */
087 public boolean equals(Object obj) {
088 if (!(obj instanceof TextElement)) return false;
089 TextElement other = (TextElement) obj;
090 if (!toString().equals(other.toString())) return false;
091 return true;
092 }
093
094 public String toString() {
095 return text;
096 }
097
098 }