Package net.mygwt.ui.client.widget

Source Code of net.mygwt.ui.client.widget.ButtonBar

/*
* MyGWT Widget Library
* Copyright(c) 2007, MyGWT.
* licensing@mygwt.net
*
* http://mygwt.net/license
*/
package net.mygwt.ui.client.widget;

import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.MyGWT;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.Listener;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

/**
* A horizontal row of buttons.
*
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>LEFT, CENTER, RIGHT</dd>
*
* <dt><b>Events:</b></dt>
* <dd><b>Click</b> : (widget, item)<br>
* <div>Fires when a button is clicked.</div>
* <ul>
* <li>widget : the button bar</li>
* <li>item : the button that was clicked</li>
* </ul>
* </dd>
*
* <dt><b>CSS:</b></dt>
* <dd>.my-btn-bar (the button bar itself)</dd>
* </dl>
*/
public class ButtonBar extends Container {

  /**
   * Specifies the button width for buttons. Default value is 75.
   */
  public int buttonWidth = 75;

  protected Button buttonPressed;
 
  private int style;
  private HorizontalPanel wrap;
  private HorizontalPanel panel;

  private Listener listener = new Listener() {
    public void handleEvent(BaseEvent be) {
      onButtonPressed(be);
    }

  };

  /**
   * Creates a left aligned button bar.
   */
  public ButtonBar() {
    this(Style.LEFT);
  }

  /**
   * Creates a new button bar instance.
   */
  public ButtonBar(int style) {
    this.style = style;
    disableLayout = true;
  }

  /**
   * Adds a button to the bar.
   *
   * @param button the button to be added
   */
  public void add(Button button) {
    insert(button, getButtonCount());
  }

  /**
   * Removes all the buttons.
   */
  public void removeAll() {
    while (getButtonCount() > 0) {
      remove(getButton(0));
    }
  }

  /**
   * Returns the button at the specified index.
   *
   * @param index the index
   * @return the button or <code>null</code>
   */
  public Button getButton(int index) {
    return (Button) items.get(index);
  }

  /**
   * Returns the button count.
   *
   * @return the button count
   */
  public int getButtonCount() {
    return items.size();
  }

  /**
   * Returns the last button that was selected.
   *
   * @return the last button or <codee>null</code>
   */
  public Button getButtonPressed() {
    return buttonPressed;
  }

  /**
   * Inserts a button at the specified location.
   *
   * @param button the button to be inserted
   * @param index the insert location
   */
  public void insert(Button button, int index) {
    if (fireEvent(Events.BeforeAdd, this, button, index)) {
      items.add(index, button);
      button.addListener(Events.Click, listener);
      if (rendered) {
        renderButton(button, index);
      }
      fireEvent(Events.Add, this, button, index);
    }
  }

  /**
   * Removes a button from the bar.
   *
   * @param button the button to be removed
   */
  public void remove(Button button) {
    if (fireEvent(Events.BeforeRemove, this, button)) {
      button.removeListener(Events.Click, listener);
      panel.remove(button);
      fireEvent(Events.Remove, this, button);
    }
  }

  protected void onButtonPressed(BaseEvent be) {
    Button btn = (Button) be.widget;
    buttonPressed = btn;
    fireEvent(Events.Click, this, btn);
  }

  protected void onRender() {
    setElement(DOM.createDiv());
    setStyleName("my-btn-bar");
   
    int height = MyGWT.isIE ? 32 : 28;
    setHeight(height);
   

    wrap = new HorizontalPanel();
    wrap.setWidth("100%");
    wrap.setHeight("100%");
    DOM.appendChild(elem, wrap.getElement());

    panel = new HorizontalPanel();
    panel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);

    wrap.add(panel);
    wrap.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);

    int count = getButtonCount();
    for (int i = 0; i < count; i++) {
      Button button = getButton(i);
      renderButton(button, i);
    }

    setAlignment(style);
  }

  private void renderButton(Button button, int index) {
    panel.insert(button, index);
    button.setWidth(buttonWidth);
    Element td = DOM.getParent(button.getElement());
    String p = "0 3 0 3px";
   
    DOM.setStyleAttribute(td, "padding", p);
  }

  private void setAlignment(int align) {
    HorizontalAlignmentConstant dir = HorizontalPanel.ALIGN_LEFT;
    switch (align) {
      case Style.CENTER:
        dir = HorizontalPanel.ALIGN_CENTER;
        break;
      case Style.RIGHT:
        dir = HorizontalPanel.ALIGN_RIGHT;
    }
    wrap.setCellHorizontalAlignment(panel, dir);
    wrap.setCellVerticalAlignment(panel, HorizontalPanel.ALIGN_MIDDLE);
  }

}
TOP

Related Classes of net.mygwt.ui.client.widget.ButtonBar

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.