source: trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/gui/TextInput.java @ 830

Last change on this file since 830 was 830, checked in by sherbold, 12 years ago
  • TextInput? now returns an unmodifiable view of the internal event list
  • code documentation and formating
File size: 5.3 KB
Line 
1
2package de.ugoe.cs.quest.eventcore.gui;
3
4import java.util.Collections;
5import java.util.List;
6
7import de.ugoe.cs.quest.eventcore.Event;
8
9/**
10 * <p>
11 * A text input represents a list of key events that together represent entering text into a text
12 * field or text area.
13 * </p>
14 *
15 * @version 1.0
16 * @author Patrick Harms, Steffen Herbold
17 */
18public class TextInput implements IInteraction {
19
20    /**
21     * <p>
22     * Defines how the {@link TextInput}s are evaluated as equal.
23     * </p>
24     *
25     * @version 1.0
26     * @author Steffen Herbold
27     */
28    public enum TextEquality {
29        /**
30         * <p>
31         * Two text inputs are equal if their {@link TextInput#textInputEvents} are equal.
32         * </p>
33         */
34        LEXICAL,
35        /**
36         * <p>
37         * Two text inputs are equal if their {@link TextInput#enteredText}s are equal.
38         * </p>
39         */
40        SYNTACTICAL,
41        /**
42         * <p>
43         * All text inputs are equal.
44         * </p>
45         */
46        SEMANTICAL
47    };
48
49    /**
50     * <p>
51     * Id for object serialization.
52     * </p>
53     */
54    private static final long serialVersionUID = 1L;
55
56    /**
57     * <p>
58     * The text resulting from the text input events.
59     * </p>
60     */
61    private String enteredText;
62
63    /**
64     * <p>
65     * The text input events that caused the entering of the text.
66     * </p>
67     */
68    private List<Event> textInputEvents;
69
70    /**
71     * <p>
72     * Defines how this TextInput event evaluates the equality.
73     * </p>
74     */
75    private final TextEquality equalityType;
76
77    /**
78     * <p>
79     * Constructor. Creates a new {@link TextInput} with {@link TextEquality#LEXICAL} equality.
80     * </p>
81     *
82     * @param enteredText
83     *            text resulting from the inputs
84     * @param textInputEvents
85     *            text input events of which this input consists
86     */
87    public TextInput(String enteredText, List<Event> textInputEvents) {
88        this(enteredText, textInputEvents, TextEquality.LEXICAL);
89    }
90
91    /**
92     * <p>
93     * Constructor. Creates a new {@link TextInput}..
94     * </p>
95     *
96     * @param enteredText
97     *            text resulting from the inputs
98     * @param textInputEvents
99     *            text input events of which this input consists
100     * @param equalityType
101     *            defines how this event evaluates the equality (@see {@link TextEquality})
102     */
103    public TextInput(String enteredText, List<Event> textInputEvents, TextEquality equalityType) {
104        this.enteredText = enteredText;
105        this.textInputEvents = textInputEvents;
106        this.equalityType = equalityType;
107    }
108
109    /*
110     * (non-Javadoc)
111     *
112     * @see de.harms.attef.userinteraction.Interaction#getName()
113     */
114    public String getName() {
115        return "TextInput(\"" + enteredText + "\")";
116    }
117
118    /*
119     * (non-Javadoc)
120     *
121     * @see java.lang.Object#toString()
122     */
123    @Override
124    public String toString() {
125        return "text input \"" + enteredText + "\"";
126    }
127
128    /**
129     * <p>
130     * Returns the entered text.
131     * </p>
132     *
133     * @return the entered text
134     */
135    public String getEnteredText() {
136        return enteredText;
137    }
138
139    /**
140     * <p>
141     * Returns the events of which this {@link TextInput} consists. The returned list is immutable.
142     * </p>
143     *
144     * @return the textInputEvents
145     */
146    public List<Event> getTextInputEvents() {
147        return Collections.unmodifiableList(textInputEvents);
148    }
149
150    /*
151     * (non-Javadoc)
152     *
153     * @see de.harms.attef.userinteraction.Interaction#startsLogicalSequence()
154     */
155    public boolean startsLogicalSequence() {
156        return false;
157    }
158
159    /*
160     * (non-Javadoc)
161     *
162     * @see de.harms.attef.userinteraction.Interaction#finishesLogicalSequence()
163     */
164    public boolean finishesLogicalSequence() {
165        return false;
166    }
167
168    /*
169     * (non-Javadoc)
170     *
171     * @see java.lang.Object#equals(java.lang.Object)
172     */
173    @Override
174    public boolean equals(Object obj) {
175        if (this == obj) {
176            return true;
177        }
178        else if (obj instanceof TextInput) {
179            switch (equalityType)
180            {
181                case LEXICAL:
182                    return textInputEvents.equals(((TextInput) obj).textInputEvents);
183                case SYNTACTICAL:
184                    return enteredText.equals(((TextInput) obj).enteredText);
185                case SEMANTICAL:
186                    return true;
187                default:
188                    throw new AssertionError("reached source code that should be unreachable");
189            }
190        }
191        return false;
192    }
193
194    /*
195     * (non-Javadoc)
196     *
197     * @see java.lang.Object#hashCode()
198     */
199    @Override
200    public int hashCode() {
201        int hashCode = getClass().hashCode();
202        if (equalityType == TextEquality.LEXICAL) {
203            hashCode += enteredText.hashCode() + textInputEvents.size();
204        }
205        else if (equalityType == TextEquality.SYNTACTICAL) {
206            hashCode += enteredText.hashCode();
207        }
208        return hashCode;
209    }
210
211}
Note: See TracBrowser for help on using the repository browser.