| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.widgets; | 
  | 17 |  |   | 
  | 18 |  |  import com.google.gwt.dom.client.Element; | 
  | 19 |  |  import com.google.gwt.event.dom.client.BlurEvent; | 
  | 20 |  |  import com.google.gwt.event.dom.client.BlurHandler; | 
  | 21 |  |  import com.google.gwt.event.dom.client.FocusEvent; | 
  | 22 |  |  import com.google.gwt.event.dom.client.FocusHandler; | 
  | 23 |  |  import com.google.gwt.event.dom.client.KeyDownEvent; | 
  | 24 |  |  import com.google.gwt.event.dom.client.KeyDownHandler; | 
  | 25 |  |  import com.google.gwt.event.dom.client.KeyUpEvent; | 
  | 26 |  |  import com.google.gwt.event.dom.client.KeyUpHandler; | 
  | 27 |  |  import com.google.gwt.event.dom.client.MouseOutEvent; | 
  | 28 |  |  import com.google.gwt.event.dom.client.MouseOutHandler; | 
  | 29 |  |  import com.google.gwt.event.dom.client.MouseOverEvent; | 
  | 30 |  |  import com.google.gwt.event.dom.client.MouseOverHandler; | 
  | 31 |  |  import com.google.gwt.user.client.ui.Label; | 
  | 32 |  |  import com.google.gwt.user.client.ui.TextArea; | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 | 0 |  public class KSTextArea extends TextArea implements HasWatermark{ | 
  | 42 | 0 |          private boolean hasWatermark = false; | 
  | 43 | 0 |          private boolean watermarkShowing = false; | 
  | 44 |  |          private String watermarkText; | 
  | 45 | 0 |          private int left = 0; | 
  | 46 |  |   | 
  | 47 |  |       | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |      public KSTextArea() { | 
  | 52 | 0 |          super(); | 
  | 53 | 0 |          setupDefaultStyle(); | 
  | 54 | 0 |      } | 
  | 55 |  |   | 
  | 56 |  |       | 
  | 57 |  |   | 
  | 58 |  |   | 
  | 59 |  |   | 
  | 60 |  |   | 
  | 61 |  |      public KSTextArea(Element element) { | 
  | 62 | 0 |          super(element); | 
  | 63 | 0 |          setupDefaultStyle(); | 
  | 64 | 0 |      } | 
  | 65 |  |       | 
  | 66 |  |      public KSTextArea(final Label countLabel, final int maximumChar){ | 
  | 67 | 0 |              super(); | 
  | 68 | 0 |          setupDefaultStyle(); | 
  | 69 | 0 |              left = maximumChar; | 
  | 70 | 0 |              countLabel.setText(left + " characters left" ); | 
  | 71 | 0 |              this.addKeyUpHandler(new KeyUpHandler(){ | 
  | 72 |  |   | 
  | 73 |  |                          @Override | 
  | 74 |  |                          public void onKeyUp(KeyUpEvent event) { | 
  | 75 | 0 |                                  if(KSTextArea.this.getText().length() > maximumChar){ | 
  | 76 | 0 |                                          KSTextArea.this.setText(KSTextArea.this.getText().substring(0, maximumChar)); | 
  | 77 | 0 |                                          left = 0; | 
  | 78 |  |                                  } | 
  | 79 |  |                                  else{ | 
  | 80 | 0 |                                          left = maximumChar - KSTextArea.this.getText().length(); | 
  | 81 |  |                                  } | 
  | 82 | 0 |                                  if(left <= 10){ | 
  | 83 | 0 |                                          countLabel.getElement().setAttribute("style", "color: red;"); | 
  | 84 |  |                                  } | 
  | 85 |  |                                  else{ | 
  | 86 | 0 |                                          countLabel.getElement().removeAttribute("style"); | 
  | 87 |  |                                  } | 
  | 88 | 0 |                                  countLabel.setText(left + " characters left" ); | 
  | 89 | 0 |                  }}); | 
  | 90 | 0 |      } | 
  | 91 |  |   | 
  | 92 |  |       | 
  | 93 |  |   | 
  | 94 |  |   | 
  | 95 |  |   | 
  | 96 |  |      private void setupDefaultStyle() { | 
  | 97 | 0 |          addStyleName("KS-Textarea"); | 
  | 98 |  |   | 
  | 99 | 0 |          this.addBlurHandler(new BlurHandler(){ | 
  | 100 |  |              public void onBlur(BlurEvent event) { | 
  | 101 | 0 |                  KSTextArea.this.removeStyleName("KS-Textarea-Focus"); | 
  | 102 |  |   | 
  | 103 | 0 |              } | 
  | 104 |  |          }); | 
  | 105 |  |   | 
  | 106 | 0 |          this.addFocusHandler(new FocusHandler(){ | 
  | 107 |  |              public void onFocus(FocusEvent event) { | 
  | 108 | 0 |                  KSTextArea.this.addStyleName("KS-Textarea-Focus"); | 
  | 109 |  |   | 
  | 110 | 0 |              } | 
  | 111 |  |          }); | 
  | 112 |  |   | 
  | 113 | 0 |          this.addMouseOverHandler(new MouseOverHandler(){ | 
  | 114 |  |              public void onMouseOver(MouseOverEvent event) { | 
  | 115 | 0 |                  KSTextArea.this.addStyleName("KS-Textarea-Hover"); | 
  | 116 |  |   | 
  | 117 | 0 |              } | 
  | 118 |  |          }); | 
  | 119 |  |   | 
  | 120 | 0 |          this.addMouseOutHandler(new MouseOutHandler(){ | 
  | 121 |  |   | 
  | 122 |  |              public void onMouseOut(MouseOutEvent event) { | 
  | 123 | 0 |                  KSTextArea.this.removeStyleName("KS-Textarea-Hover"); | 
  | 124 |  |   | 
  | 125 | 0 |              } | 
  | 126 |  |   | 
  | 127 |  |          }); | 
  | 128 |  |   | 
  | 129 | 0 |      } | 
  | 130 |  |   | 
  | 131 |  |          @Override | 
  | 132 |  |          public void setWatermarkText(String text) { | 
  | 133 | 0 |                  if(!hasWatermark){ | 
  | 134 | 0 |                          hasWatermark = true; | 
  | 135 | 0 |                          watermarkText = text; | 
  | 136 | 0 |                          if(getText() == null || getText().isEmpty()){ | 
  | 137 | 0 |                                  addStyleName("watermark-text"); | 
  | 138 | 0 |                                  KSTextArea.super.setText(watermarkText); | 
  | 139 | 0 |                                  watermarkShowing = true; | 
  | 140 |  |                          } | 
  | 141 |  |                           | 
  | 142 | 0 |                          this.addFocusHandler(new FocusHandler(){ | 
  | 143 |  |           | 
  | 144 |  |                                  @Override | 
  | 145 |  |                                  public void onFocus(FocusEvent event) { | 
  | 146 | 0 |                                          if(watermarkShowing){ | 
  | 147 | 0 |                                                  removeStyleName("watermark-text"); | 
  | 148 | 0 |                                                  KSTextArea.super.setText(""); | 
  | 149 | 0 |                                                  watermarkShowing = false; | 
  | 150 |  |                                          } | 
  | 151 | 0 |                                  } | 
  | 152 |  |                          }); | 
  | 153 |  |                           | 
  | 154 | 0 |                          this.addBlurHandler(new BlurHandler(){ | 
  | 155 |  |           | 
  | 156 |  |                                  @Override | 
  | 157 |  |                                  public void onBlur(BlurEvent event) { | 
  | 158 | 0 |                                          if(getText() == null || getText().isEmpty()){ | 
  | 159 | 0 |                                                  addStyleName("watermark-text"); | 
  | 160 | 0 |                                                  KSTextArea.super.setText(watermarkText); | 
  | 161 | 0 |                                                  watermarkShowing = true; | 
  | 162 |  |                                          } | 
  | 163 | 0 |                                  } | 
  | 164 |  |                          }); | 
  | 165 |  |                  } | 
  | 166 |  |                  else{ | 
  | 167 | 0 |                          watermarkText = text; | 
  | 168 | 0 |                          if(getText() == null || getText().isEmpty()){ | 
  | 169 | 0 |                                  addStyleName("watermark-text"); | 
  | 170 | 0 |                                  KSTextArea.super.setText(watermarkText); | 
  | 171 | 0 |                                  watermarkShowing = true; | 
  | 172 |  |                          } | 
  | 173 |  |                  } | 
  | 174 | 0 |          } | 
  | 175 |  |           | 
  | 176 |  |          @Override | 
  | 177 |  |          public boolean hasWatermark(){ | 
  | 178 | 0 |                  return hasWatermark; | 
  | 179 |  |          } | 
  | 180 |  |           | 
  | 181 |  |          @Override | 
  | 182 |  |          public boolean watermarkShowing() { | 
  | 183 | 0 |                  return watermarkShowing; | 
  | 184 |  |          } | 
  | 185 |  |           | 
  | 186 |  |          @Override | 
  | 187 |  |          public String getText() { | 
  | 188 | 0 |                  if(!watermarkShowing){ | 
  | 189 | 0 |                          return super.getText(); | 
  | 190 |  |                  } | 
  | 191 | 0 |                  return null; | 
  | 192 |  |          } | 
  | 193 |  |           | 
  | 194 |  |          @Override | 
  | 195 |  |          public String getValue() { | 
  | 196 | 0 |                  if(!watermarkShowing){ | 
  | 197 | 0 |                          return super.getValue(); | 
  | 198 |  |                  } | 
  | 199 | 0 |                  return null; | 
  | 200 |  |          } | 
  | 201 |  |   | 
  | 202 |  |          @Override | 
  | 203 |  |          public void setValue(String value) { | 
  | 204 | 0 |                  if(hasWatermark){ | 
  | 205 | 0 |                          if(value == null || (value != null && value.isEmpty())){ | 
  | 206 | 0 |                                  super.setValue(watermarkText); | 
  | 207 | 0 |                                  addStyleName("watermark-text"); | 
  | 208 | 0 |                                  watermarkShowing = true; | 
  | 209 |  |                          } | 
  | 210 |  |                          else{ | 
  | 211 | 0 |                                  super.setValue(value); | 
  | 212 |  |                          } | 
  | 213 |  |                  } | 
  | 214 |  |                  else{ | 
  | 215 | 0 |                          super.setValue(value); | 
  | 216 |  |                  } | 
  | 217 | 0 |          } | 
  | 218 |  |           | 
  | 219 |  |          @Override | 
  | 220 |  |          public void setText(String text) { | 
  | 221 | 0 |                  if(hasWatermark){ | 
  | 222 | 0 |                          if(text == null || (text != null && text.isEmpty())){ | 
  | 223 | 0 |                                  super.setText(watermarkText); | 
  | 224 | 0 |                                  addStyleName("watermark-text"); | 
  | 225 | 0 |                                  watermarkShowing = true; | 
  | 226 |  |                          } | 
  | 227 |  |                          else{ | 
  | 228 | 0 |                                  super.setText(text); | 
  | 229 |  |                          } | 
  | 230 |  |                  } | 
  | 231 |  |                  else{ | 
  | 232 | 0 |                          super.setText(text); | 
  | 233 |  |                  } | 
  | 234 | 0 |          } | 
  | 235 |  |  } |