Instantly share code, notes, and snippets.
Created
September 6, 2013 09:06
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save bugabinga/6461373 to your computer and use it in GitHub Desktop.
A better mgwt button. Accepts a widget.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* <p> | |
* WXButton.java | |
* </p> | |
* <p> | |
* ©2013 isp-insoft GmbH | |
* </p> | |
* <p> | |
* Created by Oliver Krylow (10:10:17h) | |
* </p> | |
*/ | |
package com.bugabinga.mgwt.widgets; | |
import com.google.gwt.event.shared.GwtEvent; | |
import com.google.gwt.user.client.DOM; | |
import com.google.gwt.user.client.ui.HasOneWidget; | |
import com.google.gwt.user.client.ui.HasText; | |
import com.google.gwt.user.client.ui.IsWidget; | |
import com.google.gwt.user.client.ui.Label; | |
import com.google.gwt.user.client.ui.Widget; | |
import com.googlecode.mgwt.ui.client.MGWTStyle; | |
import com.googlecode.mgwt.ui.client.theme.base.ButtonCss; | |
import com.googlecode.mgwt.ui.client.widget.Button; | |
import com.googlecode.mgwt.ui.client.widget.base.ButtonBase; | |
/** | |
* Enhanced version of MGWT {@link Button}. Extended to be a widget container. | |
* * <h1>A simple button class</h1> This is a simple class for rendering button in | |
* mgwt. | |
* | |
* <h2>Styling notes:</h2> The button consists of a simple button element like | |
* this: | |
* | |
* <pre> | |
* <button class="mgwt-Button">ButtonWidget</button> | |
* </pre> | |
* | |
* The following classes are added as needed: | |
* | |
* <ul> | |
* <li>.mgwt-Button-active - Button is pressed</li> | |
* <li>.mgwt-Button-small - Button should be rendered small</li> | |
* <li>.mgwt-Button-confirm - Button should be rendered as a confirm button | |
* (e.g. green on iOS)</li> | |
* <li>.mgwt-Button-important - Button should be rendered as important (e.g. red | |
* on iOS)</li> | |
* <li>.mgwt-Button-round - Button should be rendered with rounded corners</li> | |
* </ul> | |
* | |
* | |
* | |
* @author Daniel Kurka | |
* @author Oliver Krylow | |
*/ | |
public class WXButton extends ButtonBase implements HasOneWidget | |
{ | |
private Widget widget; | |
private final ButtonCss css; | |
private boolean round; | |
private boolean small; | |
private boolean confirm; | |
private boolean important; | |
private boolean disabled; | |
/** | |
* Constructs a button with custom css. | |
* | |
* @param css | |
*/ | |
public WXButton( final ButtonCss css ) | |
{ | |
super( DOM.createButton(), css ); | |
this.css = css; | |
css.ensureInjected(); | |
addStyleName( css.button() ); | |
} | |
/** | |
* Construct a button with a given css and text | |
* | |
* @param css the css to use | |
* @param text the text to use | |
*/ | |
public WXButton( final ButtonCss css, final String text ) | |
{ | |
this( css ); | |
setText( text ); | |
} | |
/** | |
* Construct a button with a given text | |
* | |
* @param text the text to use in the button | |
*/ | |
public WXButton( final String text ) | |
{ | |
this( MGWTStyle.getTheme().getMGWTClientBundle().getButtonCss(), text ); | |
} | |
/** | |
* Construct a button with a given css and widget | |
* | |
* @param css the css to use | |
* @param widget the widget | |
*/ | |
public WXButton( final ButtonCss css, final Widget widget ) | |
{ | |
this( css ); | |
setWidget( widget ); | |
} | |
/** | |
* Construct a button with a given widget | |
* | |
* @param widget the widget to use in the button | |
*/ | |
public WXButton( final Widget widget ) | |
{ | |
this( MGWTStyle.getTheme().getMGWTClientBundle().getButtonCss(), widget ); | |
} | |
/** | |
* Constructs a default button. | |
*/ | |
public WXButton() | |
{ | |
this( MGWTStyle.getTheme().getMGWTClientBundle().getButtonCss() ); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void setWidget( final IsWidget w ) | |
{ | |
setWidget( (Widget) w ); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public Widget getWidget() | |
{ | |
return widget; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void setWidget( final Widget w ) | |
{ | |
widget = w; | |
getElement().setInnerHTML( "" );// clear previous widget | |
getElement().appendChild( w.getElement() ); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void setText( final String text ) | |
{ | |
setWidget( new Label( text ) ); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public String getText() | |
{ | |
if ( widget != null ) | |
{ | |
try | |
{ | |
final HasText label = (HasText) widget; | |
return label.getText(); | |
} | |
catch ( final ClassCastException cce ) | |
{ | |
// intentionally ignored, empty string will be returned. | |
} | |
} | |
return ""; | |
} | |
/** | |
* Should the button have rounded corners | |
* | |
* @return true if the button has rounded corners, otherwise false | |
*/ | |
public boolean isRound() | |
{ | |
return round; | |
} | |
/** | |
* Should the button have rounded corners | |
* | |
* @param round true if the button should have rounded corners, otherwise false | |
*/ | |
public void setRound( final boolean round ) | |
{ | |
if ( round ) | |
{ | |
addStyleName( css.round() ); | |
} | |
else | |
{ | |
removeStyleName( css.round() ); | |
} | |
this.round = round; | |
} | |
/** | |
* Should the button be rendered small | |
* | |
* @param small true if the button should be rendered small, otherwise false | |
*/ | |
public void setSmall( final boolean small ) | |
{ | |
if ( small ) | |
{ | |
addStyleName( css.small() ); | |
} | |
else | |
{ | |
removeStyleName( css.small() ); | |
} | |
this.small = small; | |
} | |
/** | |
* Should the button be rendered small | |
* | |
* @return true if the button should be rendered small, otherwise false | |
*/ | |
public boolean isSmall() | |
{ | |
return small; | |
} | |
/** | |
* Should the button be rendered as important | |
* | |
* @return true if the button should be rendered as important, otherwise false | |
*/ | |
public boolean isImportant() | |
{ | |
return important; | |
} | |
/** | |
* Should the button be rendered as important | |
* | |
* @param important true if the button should be rendered as important, otherwise false | |
*/ | |
public void setImportant( final boolean important ) | |
{ | |
if ( important ) | |
{ | |
addStyleName( css.important() ); | |
} | |
else | |
{ | |
removeStyleName( css.important() ); | |
} | |
this.important = important; | |
} | |
/** | |
* Should the button be rendered as a confirm button | |
* | |
* @return a boolean. | |
*/ | |
public boolean isConfirm() | |
{ | |
return confirm; | |
} | |
/** | |
* Should the button be rendered as a confirm button | |
* | |
* @param confirm true if the button should be rendered as a confirm button, otherwise false | |
*/ | |
public void setConfirm( final boolean confirm ) | |
{ | |
if ( confirm ) | |
{ | |
addStyleName( css.confirm() ); | |
} | |
else | |
{ | |
removeStyleName( css.confirm() ); | |
} | |
this.confirm = confirm; | |
} | |
/** | |
* Should the button be disabled. By default, the button will be grayed out. | |
* | |
* @param disabled true if the button should be disabled, otherwise false | |
*/ | |
public void setDisabled( final boolean disabled ) | |
{ | |
if ( disabled ) | |
{ | |
addStyleName( css.disabled() ); | |
} | |
else | |
{ | |
removeStyleName( css.disabled() ); | |
} | |
this.disabled = disabled; | |
} | |
/** | |
* @see com.google.gwt.user.client.ui.Widget#fireEvent(com.google.gwt.event.shared.GwtEvent) | |
*/ | |
@Override | |
public void fireEvent( final GwtEvent<?> event ) | |
{ | |
if ( !disabled ) | |
{ | |
// only fire events if the button is currently enabled | |
super.fireEvent( event ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment