1 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 ; 23 24 25 public class UserNumberBean { 26 27 Integer userNumber = null; 28 Integer randomInt = null; 29 String response = null; 30 31 32 public UserNumberBean() { 33 Random randomGR = new Random (); 34 randomInt = new Integer (randomGR.nextInt(10)); 35 System.out.println("Duke's number: " + randomInt); 36 } 37 38 39 public void setUserNumber(Integer user_number) { 40 userNumber = user_number; 41 System.out.println("Set userNumber " + userNumber); 42 } 43 44 45 public Integer getUserNumber() { 46 System.out.println("get userNumber " + userNumber); 47 return userNumber; 48 } 49 50 51 public String 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 [] status = null; 61 62 63 public String [] getStatus() { 64 return status; 65 } 66 67 68 public void setStatus(String [] 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 value) throws ValidatorException { 106 107 if ((context == null) || (component == null)) { 108 throw new NullPointerException (); 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 []{ 121 new Integer (minimum), 122 new Integer (maximum) 123 })); 124 125 } else { 126 throw new ValidatorException( 127 MessageFactory.getMessage 128 (context, 129 LongRangeValidator.MAXIMUM_MESSAGE_ID, 130 new Object []{ 131 new Integer (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 []{ 142 new Double (minimum), 143 new Double (maximum) 144 })); 145 146 } else { 147 throw new ValidatorException( 148 MessageFactory.getMessage 149 (context, 150 LongRangeValidator.MINIMUM_MESSAGE_ID, 151 new Object []{ 152 new Integer (minimum) 153 })); 154 } 155 } 156 } catch (NumberFormatException 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 attributeValue) 167 throws NumberFormatException { 168 169 if (attributeValue instanceof Number ) { 170 return (((Number ) attributeValue).intValue()); 171 } else { 172 return (Integer.parseInt(attributeValue.toString())); 173 } 174 175 } 176 177 178 } 179 | Popular Tags |