Package com.extjs.gxt.ui.client.widget.menu

Source Code of com.extjs.gxt.ui.client.widget.menu.Item

/*
* Ext GWT 2.2.0 - Ext for GWT
* Copyright(c) 2007-2010, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
package com.extjs.gxt.ui.client.widget.menu;

import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Component;

/**
* The base class for all items that render into menus. BaseItem provides
* default rendering, activated state management.
*/
public abstract class Item extends Component {

  /**
   * True if this item can be visually activated (defaults to false).
   */
  protected boolean canActivate;

  /**
   * True to hide the containing menu after this item is clicked (defaults to
   * true).
   */
  protected boolean hideOnClick = true;

  /**
   * The item's containing menu.
   */
  protected Menu parentMenu;

  private String activeStyle = "x-menu-item-active";

  public Item() {
    disabledStyle = "x-item-disabled";
  }

  /**
   * Adds a selection listener.
   *
   * @param listener the listener to add
   */
  public void addSelectionListener(SelectionListener<? extends MenuEvent> listener) {
    addListener(Events.Select, listener);
  }

  /**
   * Returns the active style.
   *
   * @return the style
   */
  public String getActiveStyle() {
    return activeStyle;
  }

  /**
   * Returns the hide on click state.
   *
   * @return the hide on click state
   */
  public boolean getHideOnClick() {
    return hideOnClick;
  }

  /**
   * Returns the item's containing menu.
   *
   * @return the menu
   */
  public Menu getParentMenu() {
    return parentMenu;
  }

  /**
   * Returns true if the component can be activated.
   *
   * @return true if can be activated
   */
  public boolean isCanActivate() {
    return this.canActivate;
  }

  /**
   * Removes a previously added listener.
   *
   * @param listener the listener to be removed
   */
  public void removeSelectionListener(SelectionListener<? extends ComponentEvent> listener) {
    removeListener(Events.Select, listener);
  }

  /**
   * The CSS class to use when the item becomes activated (defaults to
   * "x-menu-item-active").
   *
   * @param activeStyle the active style
   */
  public void setActiveStyle(String activeStyle) {
    this.activeStyle = activeStyle;
  }

  /**
   * Sets whether the item can be activated (defaults to false).
   *
   * @param canActivate true to activate
   */
  public void setCanActivate(boolean canActivate) {
    this.canActivate = canActivate;
  }

  /**
   * True to hide the containing menu after this item is clicked (defaults to
   * true).
   *
   * @param hideOnClick true to hide, otherwise false
   */
  public void setHideOnClick(boolean hideOnClick) {
    this.hideOnClick = hideOnClick;
  }

  protected void activate(boolean autoExpand) {
    if (disabled) {
      return;
    }

    El li = el().getParent();
    if (li != null) {
      li.addStyleName(activeStyle);
    }
    MenuEvent me = new MenuEvent(parentMenu);
    me.setItem(this);
    fireEvent(Events.Activate, me);
  }
 
  protected boolean onEscape() {
    return true;
  }

  protected void deactivate() {
    El li = el().getParent();
    if (li != null) {
      li.removeStyleName(activeStyle);
    }
    MenuEvent me = new MenuEvent(parentMenu);
    me.setItem(this);
    fireEvent(Events.Deactivate, me);
  }

  protected void expandMenu(boolean autoActivate) {

  }

  protected void handleClick(ComponentEvent be) {
    if (hideOnClick && parentMenu != null) {
      parentMenu.hide(true);
    }
  }

  protected void onClick(ComponentEvent be) {
    be.stopEvent();
    MenuEvent me = new MenuEvent(parentMenu);
    me.setItem(this);
    me.setEvent(be.getEvent());
    if (!disabled && fireEvent(Events.Select, me)) {
      handleClick(be);
    }
  }

  @Override
  protected void onDisable() {
    super.onDisable();
    El li = el().getParent();
    if (li != null) {
      li.addStyleName(disabledStyle);
    }
  }

  @Override
  protected void onEnable() {
    super.onEnable();
    El li = el().getParent();
    if (li != null) {
      li.removeStyleName(disabledStyle);
    }
  }

  protected boolean shouldDeactivate(ComponentEvent ce) {
    return true;
  }

}
TOP

Related Classes of com.extjs.gxt.ui.client.widget.menu.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.