com.extjs.gxt.ui.client.widget.menu.Item Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gxt Show documentation
Show all versions of gxt Show documentation
Rich Internet Application Framework for GWT
/*
* Sencha GXT 2.3.1a - Sencha for GWT
* Copyright(c) 2007-2013, Sencha, Inc.
* [email protected]
*
* http://www.sencha.com/products/gxt/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;
}
}