1 16 package com.google.gwt.user.client.ui; 17 18 import com.google.gwt.user.client.DOM; 19 import com.google.gwt.user.client.Element; 20 21 83 public abstract class UIObject { 84 85 private static final String EMPTY_STYLENAME_MSG = "Style names cannot be empty"; 86 87 private static final String NULL_HANDLE_MSG = "Null widget handle. If you " 88 + "are creating a composite, ensure that initWidget() has been called."; 89 90 private static final String STYLE_EMPTY = "gwt-nostyle"; 91 92 public static native boolean isVisible(Element elem) ; 95 96 public static native void setVisible(Element elem, boolean visible) ; 99 100 107 protected static void resetStyleName(Element elem, String style) { 108 if (elem == null) { 109 throw new RuntimeException (NULL_HANDLE_MSG); 110 } 111 112 style = style.trim(); 115 if (style.length() == 0) { 116 throw new IllegalArgumentException (EMPTY_STYLENAME_MSG); 117 } 118 119 ensurePrimaryStyleName(elem); 120 updatePrimaryAndDependentStyleNames(elem, style); 121 } 122 123 133 protected static void setStyleName(Element elem, String style, boolean add) { 134 if (elem == null) { 135 throw new RuntimeException (NULL_HANDLE_MSG); 136 } 137 138 style = style.trim(); 139 if (style.length() == 0) { 140 throw new IllegalArgumentException (EMPTY_STYLENAME_MSG); 141 } 142 143 String oldStyle = ensurePrimaryStyleName(elem); 145 int idx; 146 if (oldStyle == null) { 147 idx = -1; 148 oldStyle = ""; 149 } else { 150 idx = oldStyle.indexOf(style); 151 } 152 153 while (idx != -1) { 155 if (idx == 0 || oldStyle.charAt(idx - 1) == ' ') { 156 int last = idx + style.length(); 157 int lastPos = oldStyle.length(); 158 if ((last == lastPos) 159 || ((last < lastPos) && (oldStyle.charAt(last) == ' '))) { 160 break; 161 } 162 } 163 idx = oldStyle.indexOf(style, idx + 1); 164 } 165 166 if (add) { 167 if (idx == -1) { 169 if (oldStyle.length() > 0) { 170 oldStyle += " "; 171 } 172 DOM.setElementProperty(elem, "className", oldStyle + style); 173 } 174 } else { 175 if (idx != -1) { 177 if (idx == 0) { 178 throw new IllegalArgumentException ("Cannot remove base style name"); 180 } 181 String begin = oldStyle.substring(0, idx); 182 String end = oldStyle.substring(idx + style.length()); 183 DOM.setElementProperty(elem, "className", begin + end); 184 } 185 } 186 } 187 188 194 private static String ensurePrimaryStyleName(Element elem) { 195 String className = DOM.getElementProperty(elem, "className").trim(); 196 197 if ("".equals(className)) { 198 className = STYLE_EMPTY; 199 DOM.setElementProperty(elem, "className", className); 200 } 201 202 return className; 203 } 204 205 208 private static native void updatePrimaryAndDependentStyleNames(Element elem, String newStyle) ; 239 240 private Element element; 241 242 321 public void addStyleName(String style) { 322 setStyleName(getStyleElement(), style, true); 323 } 324 325 331 public int getAbsoluteLeft() { 332 return DOM.getAbsoluteLeft(getElement()); 333 } 334 335 341 public int getAbsoluteTop() { 342 return DOM.getAbsoluteTop(getElement()); 343 } 344 345 350 public Element getElement() { 351 return element; 352 } 353 354 360 public int getOffsetHeight() { 361 return DOM.getElementPropertyInt(element, "offsetHeight"); 362 } 363 364 370 public int getOffsetWidth() { 371 return DOM.getElementPropertyInt(element, "offsetWidth"); 372 } 373 374 382 public String getStyleName() { 383 String fullClassName = ensurePrimaryStyleName(getStyleElement()); 384 385 int spaceIdx = fullClassName.indexOf(' '); 389 if (spaceIdx >= 0) { 390 return fullClassName.substring(0, spaceIdx); 391 } 392 return fullClassName; 393 } 394 395 401 public String getTitle() { 402 return DOM.getElementProperty(element, "title"); 403 } 404 405 410 public boolean isVisible() { 411 return isVisible(element); 412 } 413 414 420 public void removeStyleName(String style) { 421 setStyleName(getStyleElement(), style, false); 422 } 423 424 430 public void setHeight(String height) { 431 assert extractLengthValue(height.trim().toLowerCase()) >= 0 : 434 "CSS heights should not be negative"; 435 DOM.setStyleAttribute(element, "height", height); 436 } 437 438 445 public void setPixelSize(int width, int height) { 446 if (width >= 0) { 447 setWidth(width + "px"); 448 } 449 if (height >= 0) { 450 setHeight(height + "px"); 451 } 452 } 453 454 461 public void setSize(String width, String height) { 462 setWidth(width); 463 setHeight(height); 464 } 465 466 473 public void setStyleName(String style) { 474 resetStyleName(getStyleElement(), style); 475 } 476 477 483 public void setTitle(String title) { 484 if (title == null || title.length() == 0) { 485 DOM.removeElementAttribute(element, "title"); 486 } else { 487 DOM.setElementAttribute(element, "title", title); 488 } 489 } 490 491 497 public void setVisible(boolean visible) { 498 setVisible(element, visible); 499 } 500 501 507 public void setWidth(String width) { 508 assert extractLengthValue(width.trim().toLowerCase()) >= 0 : 511 "CSS widths should not be negative"; 512 DOM.setStyleAttribute(element, "width", width); 513 } 514 515 524 public void sinkEvents(int eventBitsToAdd) { 525 DOM.sinkEvents(getElement(), eventBitsToAdd 526 | DOM.getEventsSunk(getElement())); 527 } 528 529 535 public String toString() { 536 if (element == null) { 537 return "(null handle)"; 538 } 539 return DOM.toString(element); 540 } 541 542 550 public void unsinkEvents(int eventBitsToRemove) { 551 DOM.sinkEvents(getElement(), DOM.getEventsSunk(getElement()) 552 & (~eventBitsToRemove)); 553 } 554 555 562 protected Element getStyleElement() { 563 return element; 564 } 565 566 576 protected void setElement(Element elem) { 577 if (this.element != null) { 578 replaceNode(this.element, elem); 580 } 581 582 this.element = elem; 583 584 } 588 589 598 private native double extractLengthValue(String s) ; 605 606 private native void replaceNode(Element node, Element newNode) ; 614 } 615 | Popular Tags |