001: package org.andromda.andromdapp;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: /**
008: * Represents a user prompt used by AndroMDApp.
009: *
010: * @author Chad Brandon
011: */
012: public class Prompt {
013: /**
014: * The unique prompt identifier.
015: */
016: private String id;
017:
018: /**
019: * Gets the unique id of this prompt.
020: *
021: * @return Returns the id.
022: */
023: public String getId() {
024: return id;
025: }
026:
027: /**
028: * Sets the unique id of this prompt.
029: *
030: * @param id The id to set.
031: */
032: public void setId(String id) {
033: this .id = id;
034: }
035:
036: /**
037: * Whether or not this prompt is required.
038: */
039: private boolean required = true;
040:
041: /**
042: * Sets whether or not this prompt is required,
043: * by default the prompt is <strong>required</strong>.
044: *
045: * @param required whether or not this prompt is required
046: */
047: public void setRequired(final boolean required) {
048: this .required = required;
049: }
050:
051: /**
052: * Indicates whether or not this prompt is required.
053: *
054: * @return true/false
055: */
056: public boolean isRequired() {
057: return this .required;
058: }
059:
060: /**
061: * Stores the actual text of the prompt.
062: */
063: private String text;
064:
065: /**
066: * Gets the text of the prompt.
067: *
068: * @return Returns the text.
069: */
070: public String getText() {
071: final StringBuffer text = new StringBuffer();
072: if (this .text != null) {
073: text.append(this .text);
074: }
075: if (!this .responses.isEmpty()) {
076: text.append(" " + this .getResponsesAsString());
077: }
078: text.append(": ");
079: return text.toString();
080: }
081:
082: /**
083: * Sets the prompt text.
084: *
085: * @param text The text to set.
086: */
087: public void setText(String text) {
088: this .text = text != null ? text.trim() : null;
089: }
090:
091: /**
092: * Stores the possible responses of the prompt.
093: */
094: private List responses = new ArrayList();
095:
096: /**
097: * Adds a reponse to the possible responses.
098: *
099: * @param response the response to add.
100: * @param type the full qualified type of the response (if undefined
101: * the type is left as a string).
102: */
103: public void addResponse(final String response) {
104: if (response != null && response.trim().length() > 0) {
105: this .responses.add(response.trim());
106: }
107: }
108:
109: /**
110: * Indicates whether or not the given <code>response</code> is valid
111: * according to the valid responses contained in this prompt instance.
112: *
113: * @param response the response to check.
114: * @return true/false
115: */
116: public boolean isValidResponse(final String response) {
117: return this .responses.contains(response)
118: || (this .responses.isEmpty() && (!this .isRequired() || (response != null && response
119: .trim().length() > 0)));
120: }
121:
122: /**
123: * Gets the response object converted to the appropriate
124: * type or just as it is (if no conversion took place or conversion
125: * failed).
126: *
127: * @param response the response to convert.
128: * @return the response as an object.
129: */
130: public Object getResponse(final Object response) {
131: return AndroMDAppUtils.convert(response, this .responseType);
132: }
133:
134: /**
135: * Stores the response type.
136: */
137: private String responseType;
138:
139: /**
140: * Sets the response type to use (i.e the fully qualified name of the
141: * type to which it should be converted when placed into the the template context).
142: *
143: * @param responseType the fully qualified response type name.
144: */
145: public void setResponseType(final String responseType) {
146: this .responseType = responseType;
147: }
148:
149: /**
150: * Gets the responses as a formatted string.
151: *
152: * @return the responses list as a string.
153: */
154: private String getResponsesAsString() {
155: final StringBuffer responses = new StringBuffer("[");
156: for (final Iterator iterator = this .responses.iterator(); iterator
157: .hasNext();) {
158: responses.append(iterator.next());
159: if (iterator.hasNext()) {
160: responses.append(", ");
161: }
162: }
163: responses.append("]");
164: return responses.toString();
165: }
166:
167: /**
168: * The conditions that apply to this prompt.
169: */
170: private List conditions = new ArrayList();
171:
172: /**
173: * Adds a condition to this prompt.
174: *
175: * @param condition the condition which must apply to this prompt.
176: */
177: public void addCondition(final Condition condition) {
178: this .conditions.add(condition);
179: }
180:
181: /**
182: * Gets the conditions defined in this prompt.
183: *
184: * @return the conditions that are defined within this prompt.
185: */
186: public List getConditions() {
187: return this .conditions;
188: }
189:
190: /**
191: * The preconditions that must be valid for this prompt
192: * in order for it to be executed.
193: */
194: private List preconditions = new ArrayList();
195:
196: /**
197: * Adds preconditions to this prompt.
198: *
199: * @param preconditions the preconditions to add.
200: */
201: public void addPreconditions(final Conditions preconditions) {
202: this .preconditions.add(preconditions);
203: }
204:
205: /**
206: * Gets the preconditions for this prompt.
207: *
208: * @return the prompt preconditions.
209: */
210: public List getPreconditions() {
211: return this .preconditions;
212: }
213:
214: /**
215: * Whether or not the value of the response should be
216: * set to a boolean value of true.
217: */
218: private boolean setResponseAsTrue;
219:
220: /**
221: * Whether or not the response should be set to a boolean value of <code>true</code>.
222: *
223: * @return Returns the setResponseAsTrue.
224: */
225: public boolean isSetResponseAsTrue() {
226: return setResponseAsTrue;
227: }
228:
229: /**
230: * Sets whether or not the response should be set to a boolean value of true.
231: *
232: * @param setResponseAsTrue The setResponseAsTrue to set.
233: */
234: public void setSetResponseAsTrue(boolean setResponseAsBoolean) {
235: this.setResponseAsTrue = setResponseAsBoolean;
236: }
237: }
|