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.MyGWT;
012: import net.mygwt.ui.client.Style;
013: import net.mygwt.ui.client.event.BaseEvent;
014: import net.mygwt.ui.client.event.Listener;
015:
016: import com.google.gwt.user.client.DOM;
017: import com.google.gwt.user.client.Element;
018: import com.google.gwt.user.client.ui.HorizontalPanel;
019: import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
020:
021: /**
022: * A horizontal row of buttons.
023: *
024: * <dl>
025: * <dt><b>Styles:</b></dt>
026: * <dd>LEFT, CENTER, RIGHT</dd>
027: *
028: * <dt><b>Events:</b></dt>
029: * <dd><b>Click</b> : (widget, item)<br>
030: * <div>Fires when a button is clicked.</div>
031: * <ul>
032: * <li>widget : the button bar</li>
033: * <li>item : the button that was clicked</li>
034: * </ul>
035: * </dd>
036: *
037: * <dd><b>BeforeAdd</b> : (widget, item, index)<br>
038: * <div>Fires before a widget is added or inserted. Listeners can set the
039: * <code>doit</code> field to <code>false</code> to cancel the action.</div>
040: * <ul>
041: * <li>widget : this</li>
042: * <li>item : the widget being added</li>
043: * <li>index : the index at which the widget will be added</li>
044: * </ul>
045: * </dd>
046: *
047: * <dd><b>BeforeRemove</b> : (widget, item)<br>
048: * <div>Fires before a widget is removed. Listeners can set the
049: * <code>doit</code> field to <code>false</code> to cancel the action.</div>
050: * <ul>
051: * <li>widget : this</li>
052: * <li>item : the widget being removed</li>
053: * </ul>
054: * </dd>
055: *
056: * <dd><b>Add</b> : (widget, item, index)<br>
057: * <div>Fires after a widget has been added or inserted.</div>
058: * <ul>
059: * <li>widget : this</li>
060: * <li>item : the widget that was added</li>
061: * <li>index : the index at which the item was added</li>
062: * </ul>
063: * </dd>
064: *
065: * <dd><b>Remove</b> : (widget, item)<br>
066: * <div>Fires after a widget has been removed.</div>
067: * <ul>
068: * <li>widget : this</li>
069: * <li>item : the widget that was removed</li>
070: * </ul>
071: * </dd>
072: *
073: * <dt><b>CSS:</b></dt>
074: * <dd>.my-btn-bar (the button bar itself)</dd>
075: * </dl>
076: */
077: public class ButtonBar extends Container {
078:
079: private int buttonWidth = 75;
080: private Button buttonPressed;
081: private HorizontalPanel wrap, panel;
082: private int alignment = Style.LEFT;
083: private Listener listener = new Listener() {
084: public void handleEvent(BaseEvent be) {
085: onButtonPressed(be);
086: }
087: };
088:
089: /**
090: * Creates a left aligned button bar.
091: */
092: public ButtonBar() {
093: this (Style.LEFT);
094: }
095:
096: /**
097: * Creates a new button bar.
098: *
099: * @param style the style
100: */
101: public ButtonBar(int style) {
102: this .style = style;
103: this .alignment = style;
104: baseStyle = "my-btn-bar";
105: }
106:
107: /**
108: * Adds a button to the bar.
109: *
110: * @param button the button to be added
111: */
112: public void add(Button button) {
113: insert(button, getButtonCount());
114: }
115:
116: /**
117: * Returns the button alignment.
118: *
119: * @return the alignment
120: */
121: public int getAlignment() {
122: return alignment;
123: }
124:
125: /**
126: * Returns the button at the specified index.
127: *
128: * @param index the index
129: * @return the button or <code>null</code>
130: */
131: public Button getButton(int index) {
132: return (Button) items.get(index);
133: }
134:
135: /**
136: * Returns the button with the specified button id.
137: *
138: * @param buttonId the button id
139: * @return the button or <code>null</code> if no match
140: */
141: public Button getButtonById(int buttonId) {
142: int count = getButtonCount();
143: for (int i = 0; i < count; i++) {
144: Button btn = getButton(i);
145: if (btn.getButtonId() == buttonId) {
146: return btn;
147: }
148: }
149: return null;
150: }
151:
152: /**
153: * Returns the button count.
154: *
155: * @return the button count
156: */
157: public int getButtonCount() {
158: return items.size();
159: }
160:
161: /**
162: * Returns the last button that was selected.
163: *
164: * @return the last button or <codee>null</code>
165: */
166: public Button getButtonPressed() {
167: return buttonPressed;
168: }
169:
170: /**
171: * Returns the default button width.
172: *
173: * @return the button width
174: */
175: public int getButtonWidth() {
176: return buttonWidth;
177: }
178:
179: /**
180: * Inserts a button at the specified location.
181: *
182: * @param button the button to be inserted
183: * @param index the insert location
184: */
185: public void insert(Button button, int index) {
186: if (fireEvent(Events.BeforeAdd, this , button, index)) {
187: items.add(index, button);
188: button.addListener(Events.Click, listener);
189: if (rendered) {
190: renderButton(button, index);
191: }
192: fireEvent(Events.Add, this , button, index);
193: }
194: }
195:
196: /**
197: * Removes a button from the bar.
198: *
199: * @param button the button to be removed
200: */
201: public void remove(Button button) {
202: if (fireEvent(Events.BeforeRemove, this , button)) {
203: button.removeListener(Events.Click, listener);
204: items.remove(button);
205: if (rendered) {
206: panel.remove(button);
207: }
208: fireEvent(Events.Remove, this , button);
209: }
210: }
211:
212: /**
213: * Removes all the buttons.
214: */
215: public void removeAll() {
216: while (getButtonCount() > 0) {
217: remove(getButton(0));
218: }
219: }
220:
221: /**
222: * Sets the button alignment. Valid values are LEFT, CENTER, and RIGHT.
223: *
224: * @param align the alignment
225: */
226: public void setAlignment(int align) {
227: this .alignment = align;
228: if (isRendered()) {
229: HorizontalAlignmentConstant dir = HorizontalPanel.ALIGN_LEFT;
230: switch (align) {
231: case Style.CENTER:
232: dir = HorizontalPanel.ALIGN_CENTER;
233: break;
234: case Style.RIGHT:
235: dir = HorizontalPanel.ALIGN_RIGHT;
236: }
237: wrap.setCellHorizontalAlignment(panel, dir);
238: wrap.setCellVerticalAlignment(panel,
239: HorizontalPanel.ALIGN_MIDDLE);
240: }
241: }
242:
243: /**
244: * Sets the default button width. Default value is 75.
245: *
246: * @param width the button width
247: */
248: public void setButtonWidth(int width) {
249: buttonWidth = width;
250: }
251:
252: protected void onButtonPressed(BaseEvent be) {
253: Button btn = (Button) be.widget;
254: buttonPressed = btn;
255: fireEvent(Events.Click, this , btn);
256: }
257:
258: protected void onDisable() {
259: super .onDisable();
260: for (int i = 0; i < getButtonCount(); i++) {
261: getButton(i).disable();
262: }
263: }
264:
265: protected void onEnable() {
266: super .onEnable();
267: for (int i = 0; i < getButtonCount(); i++) {
268: getButton(i).enable();
269: }
270: }
271:
272: protected void onRender() {
273: setElement(DOM.createDiv());
274: setStyleName(baseStyle);
275:
276: int height = MyGWT.isIE ? 32 : 28;
277: setHeight(height);
278:
279: wrap = new HorizontalPanel();
280: wrap.setWidth("100%");
281: wrap.setHeight("100%");
282: DOM.appendChild(getElement(), wrap.getElement());
283:
284: panel = new HorizontalPanel();
285: panel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
286:
287: wrap.add(panel);
288: wrap.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
289:
290: int count = getButtonCount();
291: for (int i = 0; i < count; i++) {
292: Button button = getButton(i);
293: renderButton(button, i);
294: }
295:
296: setAlignment(style);
297: }
298:
299: protected void renderButton(Button button, int index) {
300: panel.insert(button, index);
301: button.setWidth(buttonWidth);
302: Element td = DOM.getParent(button.getElement());
303: String p = "0 3 0 3px";
304: DOM.setStyleAttribute(td, "padding", p);
305: }
306:
307: }
|