package org.gwtoolbox.sample.widget.client.button;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import org.gwtoolbox.ioc.core.client.annotation.Component;
import org.gwtoolbox.sample.widget.client.SamplePanel;
import org.gwtoolbox.widget.client.WidgetImages;
import org.gwtoolbox.widget.client.button.SimpleButton;
import org.gwtoolbox.widget.client.button.version2.Button;
import org.gwtoolbox.commons.ui.client.ggrowl.GGrowl;
/**
* Demonstrates some different uses of a {@code Button}.
*
* @author Uri Boness
* @author Tom van Zummeren
* @see org.gwtoolbox.widget.client.button.version2.Button
*/
@Component
@ButtonSample
public class SimpleButtonSample extends Composite implements SamplePanel {
private VerticalPanel container;
private int currentTabIndex;
/**
* Constructs a new {@code SimpleButtonSample}.
*/
public SimpleButtonSample() {
container = new VerticalPanel();
container.setSpacing(10);
addToContainer(captionOnlyButton());
addToContainer(iconWithCaptionButton());
addToContainer(iconOnlyButton());
addToContainer(customWidthButton());
addToContainer(disabledButton());
addToContainer(disabledIconWithCaptionButton());
initWidget(container);
}
/**
* Adds a widget to the container (vertical panel) and sets the tab index of that widget to a proper value.
*
* @param widget widget to add
*/
private void addToContainer(FocusWidget widget) {
widget.setTabIndex(currentTabIndex ++);
container.add(widget);
}
/**
* Creates a button showing only a caption.
*
* @return created button
*/
private Button captionOnlyButton() {
return new Button("Button", new ClickHandler() {
public void onClick(ClickEvent event) {
showMessage("Button was clicked");
}
});
}
/**
* Creates a button showing both a caption and an icon.
*
* @return created button
*/
private Button iconWithCaptionButton() {
Image icon = WidgetImages.Instance.get().icon_Printer().createImage();
return new Button("Icon button", icon, new ClickHandler() {
public void onClick(ClickEvent event) {
showMessage("Icon button was clicked");
}
});
}
/**
* Creates a button showing only an icon.
*
* @return created button
*/
private Button iconOnlyButton() {
Image icon = WidgetImages.Instance.get().icon_CubeBlue().createImage();
return new Button(icon, new ClickHandler() {
public void onClick(ClickEvent event) {
showMessage("Icon button was clicked");
}
});
}
/**
* Creates a button with a caption and a custom set width of "250px".
*
* @return created button
*/
private SimpleButton customWidthButton() {
Image icon = WidgetImages.Instance.get().icon_Printer().createImage();
SimpleButton button = new SimpleButton("Button custom width", icon, new ClickHandler() {
public void onClick(ClickEvent event) {
showMessage("Custom width button was clicked");
}
});
button.setWidth("250px");
return button;
}
/**
* Creates a disabled button with only a caption.
*
* @return created button
*/
private Button disabledButton() {
Button button = new Button("Disabled Button", new ClickHandler() {
public void onClick(ClickEvent event) {
showMessage("This should not be shown");
}
});
button.setEnabled(false);
return button;
}
/**
* Creates a disabled button with an icon and a caption.
*
* @return created button
*/
private Button disabledIconWithCaptionButton() {
Image icon = WidgetImages.Instance.get().icon_CubeGreen().createImage();
Button button = new Button("Disabled Button", icon, new ClickHandler() {
public void onClick(ClickEvent event) {
showMessage("This should not be shown");
}
});
button.setEnabled(false);
return button;
}
/**
* {@inheritDoc}
*/
public String getName() {
return "Button";
}
/**
* {@inheritDoc}
*/
public Widget getContentWidget() {
return this;
}
/**
* {@inheritDoc}
*/
public void reset() {
// do nothing
}
//============================================== Helper Methods ====================================================
/**
* Shows a tray notification message.
*
* @param message message text to show
*/
private void showMessage(String message) {
GGrowl.showMessage("Message", message);
}
}