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: package com.gwtext.client.widgets;
009:
010: import com.google.gwt.core.client.JavaScriptObject;
011: import com.gwtext.client.widgets.event.ButtonListener;
012: import com.gwtext.client.widgets.event.CycleButtonListener;
013: import com.gwtext.client.widgets.menu.CheckItem;
014:
015: /**
016: * A specialized SplitButton that contains a menu of {@link CheckItem} elements. The button automatically cycles through
017: * each menu item on click, raising the button's change event (or calling the button's changeHandler function, if supplied)
018: * for the active menu item. Clicking on the arrow section of the button displays the dropdown menu just like a normal SplitButton.
019: * <p/>
020: * <pre>
021: * <code>
022: * <p/>
023: * CycleButtonConfig config = new CycleButtonConfig();
024: * config.setShowText(true);
025: * config.setPrependText("View as ");
026: * config.addItem(new CheckItem("text only", true));
027: * config.addItem(new CheckItem("HTML", false));
028: *
029: * config.setChangeHandler(new CycleButtonHandler() {
030: * public void execute(CycleButton self, CheckItem item) {
031: * MessageBox.alert("Change View", item.getText());
032: * }
033: * });
034: * <p/>
035: * CycleButton button = new CycleButton(config);
036: * </code>
037: * </pre>
038: */
039: public class CycleButton extends SplitButton {
040:
041: /* private static JavaScriptObject configPrototype;
042:
043: static {
044: init();
045: }
046:
047: private static native void init() *//*-{
048: var c = new $wnd.Ext.CycleButton();
049: @com.gwtext.client.widgets.CycleButton::configPrototype = c.initialConfig;
050: }-*//*;
051:
052:
053: protected JavaScriptObject getConfigPrototype() {
054: return configPrototype;
055: }*/
056:
057: public String getXType() {
058: return "cycle";
059: }
060:
061: /**
062: * Create a new CycleButton.
063: */
064: public CycleButton() {
065: }
066:
067: /**
068: * Create a new CycleButton.
069: *
070: * @param listener the button listener
071: */
072: public CycleButton(CycleButtonListener listener) {
073: super (null, listener);
074: }
075:
076: /**
077: * Create a new CycleButton.
078: *
079: * @param listener the button listener
080: * @param icon icon image path
081: */
082: public CycleButton(CycleButtonListener listener, String icon) {
083: super (null, listener, icon);
084: }
085:
086: public CycleButton(JavaScriptObject jsObj) {
087: super (jsObj);
088: }
089:
090: private static CycleButton instance(JavaScriptObject jsObj) {
091: return new CycleButton(jsObj);
092: }
093:
094: protected native JavaScriptObject create(JavaScriptObject config) /*-{
095: return new $wnd.Ext.CycleButton(config);
096: }-*/;
097:
098: /**
099: * Gets the currently active menu item.
100: *
101: * @return the active menu item
102: */
103: public native CheckItem getActiveItem() /*-{
104: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
105: var item = button.getActiveItem();
106: var itemJ = @com.gwtext.client.widgets.menu.CheckItem::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(item);
107: return itemJ;
108: }-*/;
109:
110: /**
111: * Sets the button's active menu item.
112: *
113: * @param item the item to activate
114: */
115: public native void setActiveItem(CheckItem item) /*-{
116: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
117: var itemJS = item.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
118: button.setActiveItem(itemJS);
119: }-*/;
120:
121: /**
122: * Sets the button's active menu item.
123: *
124: * @param item the item to activate
125: * @param supressEvent true to prevent the button's change event from firing (defaults to false)
126: */
127: public native void setActiveItem(CheckItem item,
128: boolean supressEvent) /*-{
129: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
130: var itemJS = item.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
131: button.setActiveItem(itemJS, supressEvent);
132: }-*/;
133:
134: /**
135: * This is normally called internally on button click, but can be called externally to advance the button's active
136: * item programmatically to the next one in the menu. If the current item is the last one in the menu the active item
137: * will be set to the first item in the menu.
138: */
139: public native void toggleSelected() /*-{
140: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
141: button.toggleSelected();
142: }-*/;
143:
144: /**
145: * Add a CycleButton Listener.
146: *
147: * @param listener the listener
148: */
149: public native void addListener(CycleButtonListener listener)/*-{
150:
151: this.@com.gwtext.client.widgets.SplitButton::addListener(Lcom/gwtext/client/widgets/event/SplitButtonListener;)(listener);
152: var buttonJ = this;
153:
154: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('change',
155: function(source, item) {
156: var itemJ = @com.gwtext.client.widgets.menu.CheckItem::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(item);
157: listener.@com.gwtext.client.widgets.event.CycleButtonListener::onChange(Lcom/gwtext/client/widgets/CycleButton;Lcom/gwtext/client/widgets/menu/CheckItem;)(buttonJ, itemJ);
158: }
159: );
160: }-*/;
161:
162: //--- config properties ---
163:
164: /**
165: * Add a CheckItem config object to be used when creating the button's menu items.
166: *
167: * @param item check item config
168: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
169: */
170: public void addItem(CheckItem item) throws IllegalStateException {
171: if (isRendered()) {
172: throw new IllegalStateException(
173: "Items can be added to the CycleButton only prior to render.");
174: }
175: addItemPreRender(item);
176: }
177:
178: private native void addItemPreRender(CheckItem item)
179: throws IllegalStateException/*-{
180: var config = this.@com.gwtext.client.widgets.Component::config;
181: var itemJS = item.@com.gwtext.client.widgets.Component::config;
182: if(!config.items) {
183: config.items = @com.gwtext.client.util.JavaScriptObjectHelper::createJavaScriptArray()();
184: }
185: config.items.push(itemJS);
186: }-*/;
187:
188: /**
189: * A static string to prepend before the active item's text when displayed as the button's text
190: * (only applies when showText = true, defaults to '').
191: *
192: * @param prependText the prepend text
193: */
194: public void setPrependText(String prependText) {
195: setAttribute("prependText", prependText, true, true);
196: }
197:
198: /**
199: * @return the prepend text
200: */
201: public String getPrependText() {
202: return getAttribute("prependText");
203: }
204:
205: /**
206: * True to display the active item's text as the button text (defaults to false).
207: *
208: * @param showText true to display item's text
209: */
210: public void setShowText(boolean showText) {
211: setAttribute("showText", showText, true, true);
212: }
213:
214: /**
215: * @return the show text
216: */
217: public String getShowText() {
218: return getAttribute("showText");
219: }
220: }
|