Package net.mygwt.ui.client.widget

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

package net.mygwt.ui.client.widget;

import java.util.ArrayList;
import java.util.List;

import net.mygwt.ui.client.MyDOM;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.util.Format;
import net.mygwt.ui.client.util.Markup;
import net.mygwt.ui.client.util.Rectangle;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.WidgetHelper;

public class Item extends Component {

  protected boolean overStyleEnabled = true;
  protected boolean selected;
  protected String text;
  protected String baseStyle, iconStyle;
  protected Element midleftElem, midRightElem;
  protected Element centerElem, textElem;
  protected IconButton iconBtn;
  protected List buttons;
  protected HorizontalPanel buttonPanel;
 
  protected String markup;

  private boolean hovering;

  public Item(String baseStyle) {
    this.baseStyle = baseStyle;
  }

  /**
   * Returns the item's icon style.
   *
   * @return the icon style
   */
  public String getIconStyle() {
    return iconStyle;
  }

  public void addWidget(Widget widget) {
    if (buttons == null) {
      buttons = new ArrayList();
    }
    buttons.add(widget);
    if (rendered) {
      if (buttonPanel == null) {
        buttonPanel = new HorizontalPanel();
        DOM.appendChild(midRightElem, buttonPanel.getElement());
        if (isAttached()) {
          WidgetHelper.doAttach(buttonPanel);
        }
      }
      buttonPanel.add(widget);
    }
  }

  protected void addButton(IconButton btn) {
    btn.addStyleName("my-tool");
    addWidget(btn);
  }

  /**
   * Returns the item's text.
   *
   * @return the text
   */
  public String getText() {
    return text;
  }

  protected boolean isSelected() {
    return selected;
  }

  public void onBaseEvent(BaseEvent be) {
    Rectangle rect = MyDOM.getBounds(getElement());
    if (rect.contains(be.getClientX(), be.getClientY())) {
      if (!hovering) {
        hovering = true;
        onMouseOver(be);
      }
    } else {
      hovering = false;
      onMouseOut(be);
    }

    switch (be.type) {
      case Event.ONMOUSEDOWN:
        onMouseDown(be);
        break;
      case Event.ONMOUSEUP:
        onMouseUp(be);
        break;
      case Event.ONCLICK:
        onClick(be);
        break;
    }

  }

  protected void setSelected(boolean select) {
    selected = select;
    if (selected) {
      addStyleName(baseStyle + "-sel");
    } else {
      removeStyleName(baseStyle + "-sel");
    }
  }

  /**
   * A CSS style which sets a background image to be used as the icon.
   *
   * @param iconStyle the CSS class name
   */
  public void setIconStyle(String iconStyle) {
    this.iconStyle = iconStyle;
    if (rendered) {
      if (iconBtn == null) {
        iconBtn = new IconButton(iconStyle);
        DOM.appendChild(midleftElem, iconBtn.getElement());
      }
      iconBtn.changeStyle(iconStyle);
    }
  }

  /**
   * Sets the item's text.
   *
   * @param text the new text
   */
  public void setText(String text) {
    this.text = text;
    if (rendered) {
      DOM.setInnerHTML(textElem, text);
    }
  }

  protected void onClick(BaseEvent be) {
    hovering = false;
    onMouseOut(be);
  }

  protected void onMouseDown(BaseEvent be) {
    addStyleName(baseStyle + "-down");
  }

  protected void onMouseOut(BaseEvent be) {
    if (overStyleEnabled) {
      removeStyleName(baseStyle + "-over");
      removeStyleName(baseStyle + "-down");
    }
  }

  protected void onMouseOver(BaseEvent be) {
    if (overStyleEnabled) {
      addStyleName(baseStyle + "-over");
    }
  }

  protected void onMouseUp(BaseEvent be) {
    removeStyleName(baseStyle + "-down");
  }

  protected void doAttachChildren() {
    if (buttonPanel != null) {
      WidgetHelper.doAttach(buttonPanel);
    }
  }

  protected void doDetachChildren() {
    if (buttonPanel != null) {
      WidgetHelper.doDetach(buttonPanel);
    }
  }

  protected void onRender() {
    if (markup == null) {
      markup = Markup.ITEM;
    }
    String html = Format.substitue(markup, baseStyle);
    setElement(MyDOM.create(html));

    midleftElem = MyDOM.findChild(baseStyle + "-ml", getElement());
    centerElem = DOM.getNextSibling(midleftElem);
    textElem = DOM.getFirstChild(centerElem);
    midRightElem = DOM.getNextSibling(centerElem);

    if (text != null) {
      setText(text);
    }

    if (iconStyle != null) {
      setIconStyle(iconStyle);
    }

    if (buttons != null) {
      for (int i = 0; i < buttons.size(); i++) {
        buttonPanel = new HorizontalPanel();
        IconButton btn = (IconButton) buttons.get(i);
        buttonPanel.add(btn);
      }
      DOM.appendChild(midRightElem, buttonPanel.getElement());
    }

    MyDOM.disableTextSelection(elem);
    sinkEvents(Event.ONCLICK | Event.ONDBLCLICK | Event.MOUSEEVENTS);
  }
}
TOP

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

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.