001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.widgets;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.google.gwt.user.client.DOM;
013: import com.google.gwt.user.client.Element;
014: import com.google.gwt.user.client.ui.Widget;
015: import com.gwtext.client.core.ExtElement;
016:
017: /**
018: * Base abstract Widget class.
019: */
020: public abstract class BaseExtWidget extends Widget {
021:
022: protected JavaScriptObject jsObj;
023:
024: protected BaseExtWidget() {
025: }
026:
027: protected BaseExtWidget(JavaScriptObject jsObj) {
028: this .jsObj = jsObj;
029: setElement(getElement(this .jsObj));
030: }
031:
032: public JavaScriptObject getJsObj() {
033: return jsObj;
034: }
035:
036: public void setJsObj(JavaScriptObject jsObj) {
037: this .jsObj = jsObj;
038: }
039:
040: public ExtElement getEl() {
041: return jsObj == null ? null : new ExtElement(getElement());
042: }
043:
044: //jsObj is JS object representing the UI Widget
045: //jsObj.el is the ExtElement of the Widget
046: //jsObj.el.dom is the DOM Element of the widget
047: protected native Element getElement(JavaScriptObject jsObj) /*-{
048: //var el = jsObj.el;
049: var el = jsObj.getEl().dom;
050: if(el == null || el === undefined) {
051: return null;
052: //forms buttons are detached when initially added
053: //throw new Error('Widget ' + jsObj + ' has no element property set');
054: } else {
055: //There's an inconsistency in Ext where most elements have the property 'el' set to Ext's Element
056: //with the exception of Menu->Item, Menu->Separator, Menu->TextItem, Toolbar.Item and subclasses
057: //(Toolbar.Separator, Toolbar.Spacer, Toolbar.TextItem) where the 'el' property is set to
058: //the DOM element itself. Therefore retruning 'el' if 'el' is not Ext's Element. See details in issue 39.
059: return el.dom || el ;
060: }
061: }-*/;
062:
063: protected void onLoad() {
064: if (getElement() == null) {
065: setElement(getElement(jsObj));
066: }
067: }
068:
069: protected void onAttach() {
070: super .onAttach();
071: }
072:
073: /*protected void onAttach() {
074: super.onAttach();
075: Widget parent = getParent();
076: if(!isRendered()) {
077: render(parent.getElement());
078: }
079:
080: }
081: */
082: /*public Element getElement() {
083: if (super.getElement() == null) {
084: if(!isRendered()) {
085: render(parent.getElement());
086: }
087: setElement(getElement(jsObj));
088: }
089: return super.getElement();
090: }*/
091:
092: public int getOffsetHeight() {
093: return DOM.getElementPropertyInt(getElement(), "offsetHeight");
094: }
095:
096: public int getOffsetWidth() {
097: return DOM.getElementPropertyInt(getElement(), "offsetWidth");
098: }
099:
100: protected Element getStyleElement() {
101: return getElement();
102: }
103:
104: public String getTitle() {
105: return DOM.getElementProperty(getElement(), "title");
106: }
107:
108: public boolean isVisible() {
109: return isVisible(getElement());
110: }
111:
112: public native void purgeListeners() /*-{
113: var widget = this.@com.gwtext.client.widgets.Component::getJsObj()();
114: widget.purgeListeners();
115: }-*/;
116:
117: public void setHeight(String height) {
118: // This exists to deal with an inconsistency in IE's implementation where
119: // it won't accept negative numbers in length measurements
120: assert extractLengthValue(height.trim().toLowerCase()) >= 0 : "CSS heights should not be negative";
121: DOM.setStyleAttribute(getElement(), "height", height);
122: }
123:
124: private native double extractLengthValue(String s) /*-{
125: if (s == "auto" || s == "inherit" || s == "") {
126: return 0;
127: } else {
128: return parseFloat(s);
129: }
130: }-*/;
131:
132: public void setTitle(String title) {
133: if (title == null || title.length() == 0) {
134: DOM.removeElementAttribute(getElement(), "title");
135: } else {
136: DOM.setElementAttribute(getElement(), "title", title);
137: }
138: }
139:
140: public void setVisible(boolean visible) {
141: setVisible(getElement(), visible);
142: }
143:
144: public void setWidth(String width) {
145: // This exists to deal with an inconsistency in IE's implementation where
146: // it won't accept negative numbers in length measurements
147: assert extractLengthValue(width.trim().toLowerCase()) >= 0 : "CSS widths should not be negative";
148: DOM.setStyleAttribute(getElement(), "width", width);
149: }
150:
151: public boolean equals(Object obj) {
152: if (obj instanceof BaseExtWidget) {
153: return getElement().equals(
154: ((BaseExtWidget) obj).getElement());
155: } else {
156: return false;
157: }
158: }
159:
160: public int hashCode() {
161: return getElement().hashCode();
162: }
163:
164: public String toString() {
165: return "element";
166: /*if (getElement() == null) {
167: return "(null handle)";
168: }
169: return DOM.toString(getElement());*/
170: }
171: }
|