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.MyDOM;
012: import net.mygwt.ui.client.Style;
013: import net.mygwt.ui.client.event.BaseEvent;
014: import net.mygwt.ui.client.event.SelectionListener;
015: import net.mygwt.ui.client.event.TypedListener;
016: import net.mygwt.ui.client.util.Markup;
017:
018: import com.google.gwt.user.client.DOM;
019: import com.google.gwt.user.client.Element;
020:
021: /**
022: * A standard push button.
023: *
024: * <dl>
025: * <dt>Events:</dt>
026: *
027: * <dd><b>Select</b> : (widget)<br>
028: * <div>Fires after the button is selected.</div>
029: * <ul>
030: * <li>widget : this</li>
031: * </ul>
032: * </dd>
033: *
034: * <dt><b>CSS:</b></dt>
035: * <dd>.my-btn (the button itself)</dd>
036: * <dd>.my-btn .my-btn-text (the button text)</dd>
037: * </dl>
038: */
039: public class Button extends Item {
040:
041: private int buttonId;
042: private String name;
043: private int tabIndex = Style.DEFAULT;
044:
045: /**
046: * Creates a new button.
047: */
048: public Button() {
049: baseStyle = "my-btn";
050: }
051:
052: /**
053: * Creates a new button with the given text.
054: *
055: * @param text the button's text
056: */
057: public Button(String text) {
058: this ();
059: setText(text);
060: }
061:
062: /**
063: * Creates a new button with the given text and specified event listener.
064: *
065: * @param text the button's text
066: * @param listener the selection listener
067: */
068: public Button(String text, SelectionListener listener) {
069: this (text);
070: addSelectionListener(listener);
071: }
072:
073: /**
074: * Adds a listener interface to receive selection events.
075: *
076: * @param listener the listener to add
077: */
078: public void addSelectionListener(SelectionListener listener) {
079: TypedListener tl = new TypedListener(listener);
080: addListener(Events.Select, tl);
081: }
082:
083: /**
084: * Returns the button's id. The button id differs from the component id as it
085: * does not have to be unique and is typically used when grouping sets of buttons.
086: *
087: * @return the button id
088: */
089: public int getButtonId() {
090: return buttonId;
091: }
092:
093: /**
094: * Returns button's name.
095: *
096: * @return the button name
097: */
098: public String getName() {
099: return name;
100: }
101:
102: /**
103: * Removes a previously added listener.
104: *
105: * @param listener the listener to be removed
106: */
107: public void removeSelectionListener(SelectionListener listener) {
108: unhook(Events.Select, listener);
109: }
110:
111: /**
112: * Sets the button's id.
113: *
114: * @param id the button id
115: */
116: public void setButtonId(int id) {
117: this .buttonId = id;
118: }
119:
120: public void setIconStyle(String style) {
121: addStyleName("my-btn-icon");
122: super .setIconStyle(style);
123: }
124:
125: /**
126: * Sets the button's name.
127: *
128: * @param name the button name
129: */
130: public void setName(String name) {
131: this .name = name;
132: if (rendered) {
133: DOM.setElementProperty(getElement(), "name", name);
134: }
135: }
136:
137: /**
138: * Sets the button's tab index.
139: *
140: * @param index the tab index
141: */
142: public void setTabIndex(int index) {
143: this .tabIndex = index;
144: if (isRendered()) {
145: DOM.setElementPropertyInt(textElem, "tabIndex", index);
146: }
147: }
148:
149: protected Element getFocusElement() {
150: return textElem;
151: }
152:
153: protected String getTemplate() {
154: return Markup.BUTTON;
155: }
156:
157: protected void onClick(BaseEvent be) {
158: super .onClick(be);
159: fireEvent(Events.Select);
160: }
161:
162: protected void onDisable() {
163: super .onDisable();
164: DOM.setElementProperty(textElem, "disabled", "true");
165: }
166:
167: protected void onEnable() {
168: super .onEnable();
169: DOM.setElementProperty(textElem, "disabled", "");
170: }
171:
172: protected void onMouseDown(BaseEvent be) {
173: super .onMouseDown(be);
174: MyDOM.setFocus(textElem, true);
175: }
176:
177: protected void onRender() {
178: super .onRender();
179: setDisabledStyle(baseStyle + "-disabled");
180: if (name != null) {
181: setName(name);
182: }
183: if (tabIndex != Style.DEFAULT) {
184: setTabIndex(tabIndex);
185: }
186: }
187:
188: }
|