001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.widget;
009:
010: import net.mygwt.ui.client.Events;
011: import net.mygwt.ui.client.event.BaseEvent;
012: import net.mygwt.ui.client.event.SelectionListener;
013: import net.mygwt.ui.client.event.TypedListener;
014:
015: import com.google.gwt.user.client.DOM;
016: import com.google.gwt.user.client.Event;
017:
018: /**
019: * A simple css styled button with 3 states: normal, over, and disabled.
020: *
021: * <p>
022: * Note: To change the icon style after construction use
023: * {@link #changeStyle(String)}.
024: * </p>
025: * <dl>
026: * <dt><b>Events:</b></dt>
027: *
028: * <dd><b>Select</b> : (widget, event)<br>
029: * <div>Fires after the item is selected.</div>
030: * <ul>
031: * <li>widget : this</li>
032: * <li>event : the dom event</li>
033: * </ul>
034: * </dd>
035: *
036: * <dt><b>CSS:</b></dt>
037: * <dd>.my-icon-btn (the button itself)</dd>
038: * </dl>
039: */
040: public class IconButton extends Component {
041:
042: protected String style;
043: protected boolean cancelBubble = false;
044:
045: /**
046: * Creates a new icon button. When using the default constructor,
047: * {@link #changeStyle(String)} must be called to initialize the button.
048: */
049: public IconButton() {
050: this ("none");
051: }
052:
053: /**
054: * Creates a new icon button. The 'over' style and 'disabled' style names
055: * determined by adding '-over' and '-disabled' to the base style name.
056: *
057: * @param style the base style
058: */
059: public IconButton(String style) {
060: this .style = style;
061: }
062:
063: /**
064: * Adds a listener interface to receive selection events.
065: *
066: * @param listener the listener to add
067: */
068: public void addSelectionListener(SelectionListener listener) {
069: TypedListener tl = new TypedListener(listener);
070: addListener(Events.Select, tl);
071: }
072:
073: /**
074: * Changes the icon style.
075: *
076: * @param style the new icon style
077: */
078: public void changeStyle(String style) {
079: removeStyleName(this .style);
080: removeStyleName(this .style + "-over");
081: removeStyleName(this .style + "-disabled");
082: addStyleName(style);
083: this .style = style;
084: }
085:
086: public void onBaseEvent(BaseEvent be) {
087: switch (be.type) {
088: case Event.ONMOUSEOVER:
089: addStyleName(style + "-over");
090: break;
091: case Event.ONMOUSEOUT:
092: removeStyleName(style + "-over");
093: break;
094: case Event.ONCLICK:
095: onClick(be);
096: break;
097: }
098: }
099:
100: /**
101: * Removes a previously added listener.
102: *
103: * @param listener the listener to be removed
104: */
105: public void removeSelectionListener(SelectionListener listener) {
106: unhook(Events.EffectStart, listener);
107: }
108:
109: protected void onClick(BaseEvent be) {
110: if (cancelBubble) {
111: be.cancelBubble();
112: }
113: removeStyleName(style + "-over");
114: fireEvent(Events.Select);
115: }
116:
117: protected void onDisable() {
118: super .onDisable();
119: addStyleName(style + "-disabled");
120: }
121:
122: protected void onEnable() {
123: super .onEnable();
124: removeStyleName(style + "-disabled");
125: }
126:
127: protected void onRender() {
128: setElement(DOM.createDiv());
129: addStyleName("my-icon-btn");
130: addStyleName("my-nodrag");
131: addStyleName(style);
132: sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS);
133: }
134:
135: }
|