KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > test > UserNumberBean


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13
14 package com.tonbeller.wcf.test;
15
16 import javax.faces.component.UIComponent;
17 import javax.faces.context.FacesContext;
18 import javax.faces.validator.LongRangeValidator;
19 import javax.faces.validator.Validator;
20 import javax.faces.validator.ValidatorException;
21
22 import java.util.Random JavaDoc;
23
24
25 public class UserNumberBean {
26
27     Integer JavaDoc userNumber = null;
28     Integer JavaDoc randomInt = null;
29     String JavaDoc response = null;
30
31
32     public UserNumberBean() {
33         Random JavaDoc randomGR = new Random JavaDoc();
34         randomInt = new Integer JavaDoc(randomGR.nextInt(10));
35         System.out.println("Duke's number: " + randomInt);
36     }
37
38
39     public void setUserNumber(Integer JavaDoc user_number) {
40         userNumber = user_number;
41         System.out.println("Set userNumber " + userNumber);
42     }
43
44
45     public Integer JavaDoc getUserNumber() {
46         System.out.println("get userNumber " + userNumber);
47         return userNumber;
48     }
49
50
51     public String JavaDoc getResponse() {
52         if (userNumber != null && userNumber.compareTo(randomInt) == 0) {
53             return "Yay! You got it!";
54         } else {
55             return "Sorry, " + userNumber + " is incorrect.";
56         }
57     }
58
59
60     protected String JavaDoc[] status = null;
61
62
63     public String JavaDoc[] getStatus() {
64         return status;
65     }
66
67
68     public void setStatus(String JavaDoc[] newStatus) {
69         status = newStatus;
70     }
71
72
73     private int maximum = 0;
74     private boolean maximumSet = false;
75
76
77     public int getMaximum() {
78         return (this.maximum);
79     }
80
81
82     public void setMaximum(int maximum) {
83         this.maximum = maximum;
84         this.maximumSet = true;
85     }
86
87
88     private int minimum = 0;
89     private boolean minimumSet = false;
90
91
92     public int getMinimum() {
93         return (this.minimum);
94     }
95
96
97     public void setMinimum(int minimum) {
98         this.minimum = minimum;
99         this.minimumSet = true;
100     }
101
102
103     public void validate(FacesContext context,
104                          UIComponent component,
105                          Object JavaDoc value) throws ValidatorException {
106
107         if ((context == null) || (component == null)) {
108             throw new NullPointerException JavaDoc();
109         }
110         if (value != null) {
111             try {
112                 int converted = intValue(value);
113                 if (maximumSet &&
114                     (converted > maximum)) {
115                     if (minimumSet) {
116                         throw new ValidatorException(
117                             MessageFactory.getMessage
118                             (context,
119                              Validator.NOT_IN_RANGE_MESSAGE_ID,
120                              new Object JavaDoc[]{
121                                  new Integer JavaDoc(minimum),
122                                  new Integer JavaDoc(maximum)
123                              }));
124
125                     } else {
126                         throw new ValidatorException(
127                             MessageFactory.getMessage
128                             (context,
129                              LongRangeValidator.MAXIMUM_MESSAGE_ID,
130                              new Object JavaDoc[]{
131                                  new Integer JavaDoc(maximum)
132                              }));
133                     }
134                 }
135                 if (minimumSet &&
136                     (converted < minimum)) {
137                     if (maximumSet) {
138                         throw new ValidatorException(MessageFactory.getMessage
139                                                      (context,
140                                                       Validator.NOT_IN_RANGE_MESSAGE_ID,
141                                                       new Object JavaDoc[]{
142                                                           new Double JavaDoc(minimum),
143                                                           new Double JavaDoc(maximum)
144                                                       }));
145
146                     } else {
147                         throw new ValidatorException(
148                             MessageFactory.getMessage
149                             (context,
150                              LongRangeValidator.MINIMUM_MESSAGE_ID,
151                              new Object JavaDoc[]{
152                                  new Integer JavaDoc(minimum)
153                              }));
154                     }
155                 }
156             } catch (NumberFormatException JavaDoc e) {
157                 throw new ValidatorException(
158                     MessageFactory.getMessage
159                     (context, LongRangeValidator.TYPE_MESSAGE_ID));
160             }
161         }
162
163     }
164
165
166     private int intValue(Object JavaDoc attributeValue)
167         throws NumberFormatException JavaDoc {
168
169         if (attributeValue instanceof Number JavaDoc) {
170             return (((Number JavaDoc) attributeValue).intValue());
171         } else {
172             return (Integer.parseInt(attributeValue.toString()));
173         }
174
175     }
176
177
178 }
179
Popular Tags