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.gwtext.client.core.Template;
015: import com.gwtext.client.util.ClickRepeaterConfig;
016: import com.gwtext.client.widgets.event.ButtonListener;
017: import com.gwtext.client.widgets.menu.Menu;
018:
019: /**
020: * Simple Button class.
021: */
022: public class Button extends Component {
023:
024: private static JavaScriptObject configPrototype;
025:
026: static {
027: init();
028: }
029:
030: private static native void init()/*-{
031: var c = new $wnd.Ext.Button();
032: @com.gwtext.client.widgets.Button::configPrototype = c.initialConfig;
033: }-*/;
034:
035: protected JavaScriptObject getConfigPrototype() {
036: return configPrototype;
037: }
038:
039: /**
040: * Create a new Button.
041: */
042: public Button() {
043: }
044:
045: /**
046: * Create a new Button
047: *
048: * @param text the button label
049: */
050: public Button(String text) {
051: if (text != null)
052: setText(text);
053: }
054:
055: /**
056: * Create a new Button.
057: *
058: * @param text the Button label
059: * @param menu the Button menu
060: */
061: public Button(String text, Menu menu) {
062: if (text != null)
063: setText(text);
064: setMenu(menu);
065: }
066:
067: /**
068: * Create a new Button.
069: *
070: * @param text the button label
071: * @param listener the Button listener.
072: * @see com.gwtext.client.widgets.event.ButtonListenerAdapter
073: */
074: public Button(String text, ButtonListener listener) {
075: if (text != null)
076: setText(text);
077: addListener(listener);
078: }
079:
080: /**
081: * Create a new Button.
082: *
083: * @param text the button label
084: * @param listener the Button listener
085: * @param icon the button icon
086: * @see com.gwtext.client.widgets.event.ButtonListenerAdapter
087: * @see #setIcon(String)
088: */
089: public Button(String text, ButtonListener listener, String icon) {
090: if (text != null)
091: setText(text);
092: if (icon != null)
093: setIcon(icon);
094: addListener(listener);
095:
096: if (text == null && icon != null) {
097: setCls("x-btn-icon");
098: } else {
099: setCls("x-btn-text-icon");
100: }
101: }
102:
103: public Button(JavaScriptObject jsObj) {
104: super (jsObj);
105: }
106:
107: private static Button instance(JavaScriptObject jsObj) {
108: return new Button(jsObj);
109: }
110:
111: protected native JavaScriptObject create(JavaScriptObject config) /*-{
112: return new $wnd.Ext.Button(config);
113: }-*/;
114:
115: /**
116: * Returns true if button is disabled.
117: *
118: * @return true if disabled
119: */
120: public boolean isDisabled() {
121: return super .isDisabled();
122: }
123:
124: /**
125: * Focus the button.
126: */
127: public void focus() {
128: super .focus();
129: }
130:
131: /**
132: * Gets the text of this button.
133: *
134: * @return the button text, null if not set
135: */
136: private native String getRenderedText() /*-{
137: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
138: var text = button.getText();
139: return text === undefined ? null : text;
140: }-*/;
141:
142: /**
143: * An Ext Button consists of a table that wraps a button element. This method returns the underlying
144: * Button element.
145: * <br>
146: * <b>Note:</b> This method should be called only after the Button has been Rendered.
147: *
148: * @return the Button Element
149: */
150: public native Element getButtonElement() /*-{
151: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
152: var but = button.el.child('button:first').dom;
153: return but;
154: }-*/;
155:
156: /**
157: * Returns true if the button has a menu and it is visible.
158: *
159: * @return true if menu visible
160: */
161: public native boolean hasVisibleMenu() /*-{
162: var button = this.@com.gwtext.client.widgets.Component::getJsObj()();
163: if(button == null) return false;
164: return button.hasVisibleMenu();
165: }-*/;
166:
167: /**
168: * Hide this button's menu (if it has one).
169: */
170: public native void hideMenu() /*-{
171: var button = this.@com.gwtext.client.widgets.Component::getJsObj()();
172: if(button != null) button.hideMenu();
173: }-*/;
174:
175: /**
176: * Sets this button's text.
177: *
178: * @param text the text
179: */
180: private native void setRenderedText(String text) /*-{
181: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
182: button.setText(text);
183: }-*/;
184:
185: /**
186: * Show this button's menu (if it has one).
187: */
188: public native void showMenu() /*-{
189: var button = this.@com.gwtext.client.widgets.Component::getJsObj()();
190: if(button != null) button.showMenu();
191: }-*/;
192:
193: /**
194: * Toggle the current state.
195: */
196: public void toggle() {
197: if (!isRendered()) {
198: setPressed(!isPressed());
199: } else {
200: toggleRendered();
201: }
202: }
203:
204: private native void toggleRendered() /*-{
205: var button = this.@com.gwtext.client.widgets.Component::getJsObj()();
206: if(button != null) button.toggle();
207: }-*/;
208:
209: /**
210: * Toggle the button to the passed state.
211: *
212: * @param state true to toggle pressed
213: */
214: public void toggle(boolean state) {
215: if (!isRendered()) {
216: setPressed(state);
217: } else {
218: toggleRendered(state);
219: }
220: }
221:
222: private native void toggleRendered(boolean state) /*-{
223: var button = this.@com.gwtext.client.widgets.Component::getJsObj()();
224: if(button != null) button.toggle(state);
225: }-*/;
226:
227: /**
228: * Sets the Button's tooltip.
229: *
230: * @param tooltip Button's toolip
231: */
232: private native void setRenderedTooltip(String tooltip) /*-{
233: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
234: var but = button.el.child('button:first').dom;
235: but.qtip = tooltip;
236: }-*/;
237:
238: /**
239: * Add a Button listener.
240: *
241: * @param listener the listener
242: */
243: public native void addListener(ButtonListener listener) /*-{
244: this.@com.gwtext.client.widgets.Component::addListener(Lcom/gwtext/client/widgets/event/ComponentListener;)(listener);
245: var buttonJ = this;
246:
247: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('click',
248: function(source, event) {
249: var e = (event === undefined || event == null) ? null : @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
250: listener.@com.gwtext.client.widgets.event.ButtonListener::onClick(Lcom/gwtext/client/widgets/Button;Lcom/gwtext/client/core/EventObject;)(buttonJ, e);
251: }
252: );
253:
254: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('menuhide',
255: function(source, menu) {
256: var menuJ = @com.gwtext.client.widgets.menu.Menu::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(menu);
257: listener.@com.gwtext.client.widgets.event.ButtonListener::onMenuHide(Lcom/gwtext/client/widgets/Button;Lcom/gwtext/client/widgets/menu/Menu;)(buttonJ, menuJ);
258: }
259: );
260:
261: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('menushow',
262: function(source, menu) {
263: var menuJ = @com.gwtext.client.widgets.menu.Menu::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(menu);
264: listener.@com.gwtext.client.widgets.event.ButtonListener::onMenuShow(Lcom/gwtext/client/widgets/Button;Lcom/gwtext/client/widgets/menu/Menu;)(buttonJ, menuJ);
265: }
266: );
267:
268: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('menutriggerout',
269: function(source, menu, event) {
270: var e = (event === undefined || event == null) ? null : @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
271: var menuJ = @com.gwtext.client.widgets.menu.Menu::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(menu);
272: listener.@com.gwtext.client.widgets.event.ButtonListener::onMenuTriggerOut(Lcom/gwtext/client/widgets/Button;Lcom/gwtext/client/widgets/menu/Menu;Lcom/gwtext/client/core/EventObject;)(buttonJ, menuJ, e);
273: }
274: );
275:
276: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('menutriggerover',
277: function(source, menu, event) {
278: var e = (event === undefined || event == null) ? null : @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
279: var menuJ = @com.gwtext.client.widgets.menu.Menu::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(menu);
280: listener.@com.gwtext.client.widgets.event.ButtonListener::onMenuTriggerOver(Lcom/gwtext/client/widgets/Button;Lcom/gwtext/client/widgets/menu/Menu;Lcom/gwtext/client/core/EventObject;)(buttonJ, menuJ, e);
281: }
282: );
283: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('mouseout',
284: function(source, event) {
285: var e = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
286: listener.@com.gwtext.client.widgets.event.ButtonListener::onMouseOut(Lcom/gwtext/client/widgets/Button;Lcom/gwtext/client/core/EventObject;)(buttonJ, e);
287: }
288: );
289:
290: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('mouseover',
291: function(source, event) {
292: var e = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
293: listener.@com.gwtext.client.widgets.event.ButtonListener::onMouseOver(Lcom/gwtext/client/widgets/Button;Lcom/gwtext/client/core/EventObject;)(buttonJ, e);
294: }
295: );
296:
297: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('toggle',
298: function(source,pressed) {
299: listener.@com.gwtext.client.widgets.event.ButtonListener::onToggle(Lcom/gwtext/client/widgets/Button;Z)(buttonJ, pressed);
300: }
301: );
302: }-*/;
303:
304: // --- config properties ---
305: public String getXType() {
306: return "button";
307: }
308:
309: /**
310: * The type of event to map to the button's event handler (defaults to 'click')
311: *
312: * @param clickEvent the click event
313: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
314: */
315: public void setClickEvent(String clickEvent)
316: throws IllegalStateException {
317: setAttribute("clickEvent", clickEvent, true);
318: }
319:
320: /**
321: * @return the type of event to map to the button's event handler (defaults to 'click')
322: */
323: public String getClickEvent() {
324: return getAttribute("clickEvent");
325: }
326:
327: /**
328: * True to enable pressed/not pressed toggling (defaults to false).
329: *
330: * @param enableToggle true to enable toggle
331: */
332: public void setEnableToggle(boolean enableToggle) {
333: if (isRendered()) {
334: toggle(enableToggle);
335: } else {
336: setAttribute("enableToggle", enableToggle, true);
337: }
338: }
339:
340: /**
341: * @return true if toggle enabled
342: */
343: public boolean isEnableToggle() {
344: return getAttributeAsBoolean("enableToggle");
345: }
346:
347: /**
348: * You can bind your form buttons to the valid state of the form. Note: the Form's monitorValid config must be set to true.
349: * To bind a button(s) enabled state to the valid state set this property to true.
350: *
351: * @param bindToForm to to bind to form's valid state
352: */
353: public void setFormBind(boolean bindToForm) {
354: setAttribute("formBind", bindToForm, true);
355: }
356:
357: /**
358: * @return true if bind to form is enabled
359: */
360: public boolean getBindToForm() {
361: return getAttributeAsBoolean("formBind");
362: }
363:
364: /**
365: * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true).
366: *
367: * @param handleMouseEvents false to diable visual cues
368: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
369: */
370: public void setHandleMouseEvents(boolean handleMouseEvents)
371: throws IllegalStateException {
372: setAttribute("handleMouseEvents", handleMouseEvents, true);
373: }
374:
375: /**
376: * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true).
377: *
378: * @return true if visual cues on mouseover, mouseout and mousedown are enabled
379: */
380: public boolean getHandleMouseEvents() {
381: return getAttributeAsBoolean("handleMouseEvents");
382: }
383:
384: /**
385: * True to start hidden or hide rendered button (defaults to false).
386: *
387: * @param hidden true for hidden
388: */
389: public void setHidden(boolean hidden) {
390: if (!isRendered()) {
391: setAttribute("hidden", hidden, true);
392: } else {
393: hide();
394: }
395: }
396:
397: //TODO doesnt seem to be working for toolbar menu button
398: /**
399: * The path to an image to display in the button (the image will be set as the background-image CSS property of the
400: * button by default, so if you want a mixed icon/text button, set cls:"x-btn-text-icon")
401: *
402: * @param icon the icon
403: */
404: public void setIcon(String icon) {
405: if (!isRendered()) {
406: setAttribute("icon", icon, true);
407: } else {
408: Element buttonEl = getButtonElement();
409: DOM.setStyleAttribute(buttonEl, "backgroundImage", "url("
410: + icon + ")");
411: }
412: if (getText() == null) {
413: addClass("x-btn-icon");
414: } else {
415: addClass("x-btn-text-icon");
416: }
417: }
418:
419: /**
420: * @return the path of the Button icon image
421: */
422: public String getIcon() {
423: return getAttribute("icon");
424: }
425:
426: /**
427: * A css class which sets a background image to be used as the icon for this button (defaults to undefined).
428: *
429: * @param iconCls the icon CSS class
430: */
431: public void setIconCls(String iconCls) {
432: if (isCreated()) {
433: if (getCls() == null) {
434: addClass(getText() != null ? "x-btn-text-icon"
435: : "x-btn-icon");
436: }
437: setIconClsCreated(iconCls);
438: } else {
439: setAttribute("iconCls", iconCls, false);
440: }
441: }
442:
443: private native void setIconClsCreated(String iconCls) /*-{
444: var button = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
445: return button.setIconClass(iconCls);
446: }-*/;
447:
448: /**
449: * @return the icon CSS class for this Button
450: */
451: public String getIconCls() {
452: return getAttribute("iconCls");
453: }
454:
455: /**
456: * Assign a button menu.
457: *
458: * @param menu the button menu
459: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
460: */
461: public void setMenu(Menu menu) throws IllegalStateException {
462: setAttribute("menu", menu.getOrCreateJsObj(), false);
463: }
464:
465: /**
466: * The position to align the menu to. Defaults to tl-bl?
467: *
468: * @param anchorPosition the element's anchor position
469: */
470: public void setMenuAlign(String anchorPosition) {
471: setAttribute("menuAlign", anchorPosition, true, true);
472: }
473:
474: /**
475: * The position to align the menu to. Defaults to tl-bl?
476: *
477: * @return the position to align the menu to
478: */
479: public String getMenuAlign() {
480: return getAttribute("menuAlign");
481: }
482:
483: /**
484: * The minimum width for this button.
485: *
486: * @param minWidth the min width
487: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
488: */
489: public void setMinWidth(int minWidth) throws IllegalStateException {
490: setAttribute("minWidth", minWidth, true);
491: }
492:
493: /**
494: * The minimum width for this button.
495: *
496: * @return the Button min width
497: */
498: public int getMinWidth() {
499: return getAttributeAsInt("minWidth");
500: }
501:
502: /**
503: * True to start pressed (only if enableToggle = true).
504: *
505: * @param pressed true for pressed
506: */
507: public void setPressed(boolean pressed) {
508: if (!isRendered()) {
509: setAttribute("pressed", pressed, true);
510: } else {
511: toggle(pressed);
512: }
513: }
514:
515: /**
516: * True if the Button is pressed.
517: *
518: * @return true if start Button pressed (only if enableToggle = true)
519: * @see #toggle(boolean)
520: */
521: public boolean isPressed() {
522: return getAttributeAsBoolean("pressed");
523: }
524:
525: /**
526: * True to repeat fire the click event while the mouse is down. (defaults to false).
527: *
528: * @param repeat true to repeat
529: */
530: public void setRepeat(boolean repeat) {
531: setAttribute("repeat", repeat, true);
532: }
533:
534: /**
535: * @return true to repeat fire the click event while the mouse is down
536: */
537: public native boolean isRepeat() /*-{
538: var config = this.@com.gwtext.client.widgets.Component::config;
539: return config['repeat'] != null && config['repeat'] !== undefined;
540: }-*/;
541:
542: /**
543: * Fire click event when mouse is down.
544: *
545: * @param qconfig the click repeat configuration
546: */
547: public void setRepeat(ClickRepeaterConfig qconfig) {
548: setAttribute("repeat", qconfig.getJsObj(), true);
549: }
550:
551: /**
552: * Set a DOM tabIndex for this button.
553: *
554: * @param tabIndex the tab index
555: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
556: */
557: public void setTabIndex(int tabIndex) throws IllegalStateException {
558: setAttribute("tabIndex", tabIndex, true);
559: }
560:
561: /**
562: * @return the tab index, 0 if undefined
563: */
564: public int getTabIndex() {
565: return getAttributeAsInt("tabIndex");
566: }
567:
568: //TODO inconsistent since others use autoCreate with template overridden?
569: /**
570: * A {@link Template} with which to create the Button's main element. This Template must
571: * contain numeric substitution parameter 0 if it is to display the text property. Changing the template could
572: * require code modifications if required elements (e.g. a button) aren't present.
573: *
574: * The defaut tempalte is
575: *
576: * <pre>
577: * <code>
578: *
579: * <table border="0" cellpadding="0" cellspacing="0" class="x-btn-wrap"><tbody><tr>
580: * <td class="x-btn-left"><i> </i></td><td class="x-btn-center"><em unselectable="on"><button class="x-btn-text" type="{1}">{0}</button></em></td><td class="x-btn-right"><i> </i></td>
581: * </tr></tbody></table>
582: * </code>
583: * </pre>
584: * @param template the button template
585: */
586: public void setTemplate(Template template) {
587: setAttribute("template", template.getJsObj(), true);
588: }
589:
590: /**
591: * The button text.
592: *
593: * @param text the buttons text
594: */
595: public void setText(String text) {
596: if (isRendered()) {
597: setRenderedText(text);
598: } else {
599: setAttribute("text", text, true);
600: }
601: }
602:
603: /**
604: * @return the Button's text
605: */
606: public String getText() {
607: if (isRendered()) {
608: return getRenderedText();
609: } else {
610: return getAttribute("text");
611: }
612: }
613:
614: /**
615: * The group this toggle button is a member of (only 1 per group can be pressed, only applies if enableToggle = true).
616: *
617: * @param toggleGroup the button's toggle group
618: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
619: */
620: public void setToggleGroup(String toggleGroup)
621: throws IllegalStateException {
622: setAttribute("toggleGroup", toggleGroup, true);
623: setEnableToggle(true);
624: }
625:
626: /**
627: * @return the Button's toggle group
628: */
629: public String getToggleGroup() {
630: return getAttribute("toggleGroup");
631: }
632:
633: /**
634: * The tooltip for the button.
635: *
636: * @param tooltip the tooltip
637: */
638: public void setTooltip(String tooltip) {
639: if (isRendered()) {
640: setRenderedTooltip(tooltip);
641: } else {
642: setAttribute("tooltip", tooltip, true);
643: }
644: }
645:
646: /**
647: * @return the Button tooltip
648: */
649: public String getTooltip() {
650: return getAttribute("tooltip");
651: }
652:
653: /**
654: * The tooltip for the button.
655: *
656: * @param title the tootlip title
657: * @param text the tootlip text
658: */
659: public void setTooltip(final String title, final String text) {
660: setAttribute("tooltip", new QuickTipsConfig() {
661: {
662: setTitle(title);
663: setText(text);
664: }
665: }.getJsObj(), true);
666: }
667:
668: /**
669: * The tooltip for the button.
670: *
671: * @param qtipConfig the tooltip config
672: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
673: */
674: public void setTooltip(QuickTipsConfig qtipConfig)
675: throws IllegalStateException {
676: setAttribute("tooltip", qtipConfig.getJsObj(), true);
677: }
678:
679: /**
680: * The type of tooltip to use.
681: *
682: * @param quickTip true for quicktips, false for the "title" attribute.
683: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
684: */
685: public void setTooltipType(boolean quickTip)
686: throws IllegalStateException {
687: if (quickTip) {
688: setAttribute("tooltipType", "qtip", true);
689: } else {
690: setAttribute("tooltipType", "title", true);
691: }
692: }
693:
694: /**
695: * The type of tooltip to use.
696: *
697: * @return the tooltip type
698: */
699: public String getTooltipType() {
700: return getAttribute("tooltipType");
701: }
702:
703: /**
704: * The button's type, corresponding to the DOM input element type attribute. Either "submit," "reset" or "button" (default).
705: *
706: * @param type the button type
707: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
708: */
709: public void setType(String type) throws IllegalStateException {
710: setAttribute("type", type, true);
711: }
712:
713: /**
714: * The button's type, corresponding to the DOM input element type attribute. Either "submit," "reset" or "button" (default).
715: *
716: * @return the Button type
717: */
718: public String getType() {
719: return getAttribute("type");
720: }
721: }
|