001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or
005: * without modification, are permitted provided that the following
006: * conditions are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above
012: * copyright notice, this list of conditions and the following
013: * disclaimer in the documentation and/or other materials
014: * provided with the distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
025: * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
026: * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
027: * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
028: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
029: * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
030: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
031: * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
032: * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
033: *
034: * You acknowledge that this software is not designed, licensed or
035: * intended for use in the design, construction, operation or
036: * maintenance of any nuclear facility.
037: */
038:
039: package guessnumber;
040:
041: import javax.faces.component.UIComponent;
042: import javax.faces.context.FacesContext;
043: import javax.faces.validator.LongRangeValidator;
044: import javax.faces.validator.Validator;
045: import javax.faces.validator.ValidatorException;
046: import javax.faces.application.Application;
047: import javax.faces.application.FacesMessage;
048:
049: import java.util.Random;
050: import java.util.ResourceBundle;
051: import java.util.Locale;
052:
053: public class UserNumberBean {
054:
055: private Integer userNumber;
056: private String response;
057: private int random;
058: private Random randomGR;
059: protected String[] status;
060: private int minimum;
061: private boolean minimumSet;
062: private int maximum;
063: private boolean maximumSet;
064: private int newMaximum = -1;
065: private int newMinimum = -1;
066:
067: public UserNumberBean() {
068: randomGR = new Random();
069: }
070:
071: public void setUserNumber(Integer userNumber) {
072: this .userNumber = userNumber;
073: }
074:
075: public Integer getUserNumber() {
076: return userNumber;
077: }
078:
079: public String[] getStatus() {
080: return status;
081: }
082:
083: public void setStatus(String[] newStatus) {
084: status = newStatus;
085: }
086:
087: public int getMaximum() {
088: return (this .maximum);
089: }
090:
091: public void setMaximum(int maximum) {
092: this .maximum = maximum;
093: this .maximumSet = true;
094: random = getRandomNumber(this .maximum);
095: }
096:
097: public int getMinimum() {
098: return (this .minimum);
099: }
100:
101: public void setMinimum(int minimum) {
102: this .minimum = minimum;
103: this .minimumSet = true;
104: }
105:
106: public int getNewMinimum() {
107: if (newMinimum == -1)
108: return getMinimum();
109: return newMinimum;
110: }
111:
112: public void setNewMinimum(int newMinimum) {
113: this .newMinimum = newMinimum;
114: }
115:
116: public int getNewMaximum() {
117: if (newMaximum == -1)
118: return getMaximum();
119: return newMaximum;
120: }
121:
122: public void setNewMaximum(int newMaximum) {
123: this .newMaximum = newMaximum;
124: }
125:
126: public String getResponse() {
127: FacesContext context = FacesContext.getCurrentInstance();
128: ResourceBundle rb = getResourceBundle(context);
129: int randomLocal = random + this .minimum;
130: if (randomLocal < this .minimum)
131: randomLocal = this .minimum;
132: else if (randomLocal > this .maximum)
133: randomLocal = this .maximum;
134: Integer randomInt = new Integer(randomLocal);
135: int guessNumber = -1;
136: if (userNumber != null) {
137: guessNumber = userNumber.compareTo(randomInt);
138: }
139: if (guessNumber == 0) {
140: // Guess is correct. Generate a new random number
141: random = getRandomNumber(this .maximum);
142: return rb.getString("correct");
143: } else {
144: StringBuffer message = new StringBuffer();
145: message.append(rb.getString("wrong1"));
146: message.append(" ");
147: message.append(userNumber);
148: message.append(" ");
149: message.append(rb.getString("wrong2"));
150: message.append(" ");
151: if (guessNumber < 0) {
152: message.append(rb.getString("higher"));
153: } else {
154: message.append(rb.getString("lower"));
155: }
156: return message.toString();
157: }
158: }
159:
160: public String submit() {
161: FacesContext context = FacesContext.getCurrentInstance();
162: if (this .newMaximum < this .newMinimum) {
163: ResourceBundle rb = getResourceBundle(context);
164: String msg = rb.getString("less_than");
165: FacesMessage facesMsg = new FacesMessage(
166: FacesMessage.SEVERITY_FATAL, msg, msg);
167: context.addMessage(null, facesMsg);
168: return "error";
169: }
170: if (this .newMaximum != -1)
171: this .maximum = this .newMaximum;
172: if (this .newMinimum != -1)
173: this .minimum = this .newMinimum;
174: if (context.getRenderResponse()
175: || context.getResponseComplete()) {
176: return "error";
177: }
178: return "success";
179: }
180:
181: public String cancel() {
182: this .newMaximum = -1;
183: this .newMinimum = -1;
184: return "cancel";
185: }
186:
187: public void validate(FacesContext context, UIComponent component,
188: Object value) throws ValidatorException {
189:
190: if ((context == null) || (component == null)) {
191: throw new NullPointerException();
192: }
193: if (value != null) {
194: try {
195: int converted = intValue(value);
196: if (maximumSet && (converted > maximum)) {
197: if (minimumSet) {
198: throw new ValidatorException(
199: MessageFactory
200: .getMessage(
201: context,
202: Validator.NOT_IN_RANGE_MESSAGE_ID,
203: new Object[] {
204: new Integer(
205: minimum),
206: new Integer(
207: maximum) }));
208:
209: } else {
210: throw new ValidatorException(
211: MessageFactory
212: .getMessage(
213: context,
214: LongRangeValidator.MAXIMUM_MESSAGE_ID,
215: new Object[] { new Integer(
216: maximum) }));
217: }
218: }
219: if (minimumSet && (converted < minimum)) {
220: if (maximumSet) {
221: throw new ValidatorException(
222: MessageFactory
223: .getMessage(
224: context,
225: Validator.NOT_IN_RANGE_MESSAGE_ID,
226: new Object[] {
227: new Double(
228: minimum),
229: new Double(
230: maximum) }));
231:
232: } else {
233: throw new ValidatorException(
234: MessageFactory
235: .getMessage(
236: context,
237: LongRangeValidator.MINIMUM_MESSAGE_ID,
238: new Object[] { new Integer(
239: minimum) }));
240: }
241: }
242: } catch (NumberFormatException e) {
243: throw new ValidatorException(MessageFactory.getMessage(
244: context, LongRangeValidator.TYPE_MESSAGE_ID));
245: }
246: }
247:
248: }
249:
250: private int intValue(Object attributeValue)
251: throws NumberFormatException {
252:
253: if (attributeValue instanceof Number) {
254: return (((Number) attributeValue).intValue());
255: } else {
256: return (Integer.parseInt(attributeValue.toString()));
257: }
258:
259: }
260:
261: private int getRandomNumber(int number) {
262: int random;
263: try {
264: random = randomGR.nextInt(number);
265: } catch (IllegalArgumentException iae) {
266: random = 0;
267: }
268: return random;
269: }
270:
271: private ResourceBundle getResourceBundle(FacesContext context) {
272: Application application = context.getApplication();
273: // The bundle name specified by the <message-bundle> is returned by Application
274: String messageBundleName = application.getMessageBundle();
275: Locale locale = context.getViewRoot().getLocale();
276: ResourceBundle rb = ResourceBundle.getBundle(messageBundleName,
277: locale);
278: return rb;
279: }
280: }
|