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.menu;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013: import com.gwtext.client.widgets.menu.event.BaseItemListener;
014:
015: /**
016: * A base class for all menu items that require menu-related functionality (like sub-menus) and are not static display
017: * items. Item extends the base functionality of {@link BaseItem} by adding menu-specific activation and click handling.
018: */
019: public class Item extends BaseItem {
020:
021: private static JavaScriptObject configPrototype;
022:
023: static {
024: init();
025: }
026:
027: private static native void init()/*-{
028: $wnd.Ext.reg('menu-item', $wnd.Ext.menu.Item);
029: var c = new $wnd.Ext.menu.Item();
030: @com.gwtext.client.widgets.menu.Item::configPrototype = c.initialConfig;
031: }-*/;
032:
033: protected JavaScriptObject getConfigPrototype() {
034: return configPrototype;
035: }
036:
037: public String getXType() {
038: return "menu-tem";
039: }
040:
041: public Item() {
042: }
043:
044: public Item(String text) {
045: if (text != null)
046: setText(text);
047: }
048:
049: public Item(String text, BaseItemListener listener) {
050: if (text != null)
051: setText(text);
052: addListener(listener);
053: }
054:
055: public Item(String text, BaseItemListener listener, String icon) {
056: if (text != null)
057: setText(text);
058: addListener(listener);
059: setIcon(icon);
060: }
061:
062: public Item(JavaScriptObject jsObj) {
063: super (jsObj);
064: }
065:
066: protected native JavaScriptObject create(JavaScriptObject config) /*-{
067: return new $wnd.Ext.menu.Item(config);
068: }-*/;
069:
070: /**
071: * The text of the check item.
072: *
073: * @param text the text
074: */
075: public void setText(String text) {
076: if (!isRendered()) {
077: setAttribute("text", text, true);
078: } else {
079: setTextRendered(text);
080: }
081: }
082:
083: /**
084: * Sets the text of the item.
085: *
086: * @param text the item text
087: */
088: private native void setTextRendered(String text) /*-{
089: var item = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
090: item.setText(text);
091: }-*/;
092:
093: /**
094: * Retrun the text of the item.
095: *
096: * @return the item text
097: */
098: public String getText() {
099: if (!isRendered()) {
100: return JavaScriptObjectHelper.getAttribute(config, "text");
101: } else {
102: return getTextRendered();
103: }
104: }
105:
106: /**
107: * Return the item text.
108: *
109: * @return the item text
110: */
111: private native String getTextRendered() /*-{
112: var item = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
113: return item.text;
114: }-*/;
115:
116: public void setIconCls(String iconCls) {
117: if (!isRendered()) {
118: setAttribute("iconCls", iconCls, true);
119: } else {
120: setIconClsRendered(iconCls);
121: }
122:
123: }
124:
125: private native void setIconClsRendered(String iconCls) /*-{
126: var item = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
127: item.setIconClass(iconCls);
128: }-*/;
129:
130: /**
131: * @return the icon CSS class
132: */
133: public String getIconCls() {
134: if (!isRendered()) {
135: return JavaScriptObjectHelper.getAttribute(config,
136: "iconCls");
137: } else {
138: return JavaScriptObjectHelper.getAttribute(getJsObj(),
139: "iconCls");
140: }
141: }
142:
143: // -- config properties ---
144:
145: /**
146: * The href attribute to use for the underlying anchor link (defaults to '#').
147: *
148: * @param href the href attribute
149: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
150: */
151: public void setHref(String href) throws IllegalStateException {
152: JavaScriptObjectHelper.setAttribute(config, "href", href);
153: }
154:
155: /**
156: * The href attribute to use for the underlying anchor link (defaults to '#').
157: *
158: * @return the href attribute
159: *
160: */
161: public String getHref() {
162: return JavaScriptObjectHelper.getAttribute(config, "href");
163: }
164:
165: /**
166: * The target attribute to use for the underlying anchor link (defaults to '').
167: *
168: * @param hrefTarget the href target
169: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
170: */
171: public void setHrefTarget(String hrefTarget)
172: throws IllegalStateException {
173: JavaScriptObjectHelper.setAttribute(config, "hrefTarget",
174: hrefTarget);
175: }
176:
177: /**
178: * The target attribute to use for the underlying anchor link (defaults to '').
179: *
180: * @return the href target
181: */
182: public String getHrefTarget() {
183: return JavaScriptObjectHelper
184: .getAttribute(config, "hrefTarget");
185: }
186:
187: /**
188: * The item's icon.
189: *
190: * @param icon the icon
191: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
192: */
193: public void setIcon(String icon) throws IllegalStateException {
194: JavaScriptObjectHelper.setAttribute(config, "icon", icon);
195: }
196:
197: /**
198: * The default CSS class to use for menu items (defaults to 'x-menu-item')
199: *
200: * @param itemCls the CSS clas
201: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
202: */
203: public void setItemCls(String itemCls) throws IllegalStateException {
204: JavaScriptObjectHelper.setAttribute(config, "itemCls", itemCls);
205: }
206:
207: /**
208: * The default CSS class to use for menu items.
209: *
210: * @return the default CSS class to use for menu items
211: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
212: */
213: public String getItemCls() throws IllegalStateException {
214: return JavaScriptObjectHelper.getAttribute(config, "itemCls");
215: }
216: }
|