All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.extjs.gxt.ui.client.widget.tree.Tree Maven / Gradle / Ivy

/*
 * Ext GWT 2.2.0 - Ext for GWT
 * Copyright(c) 2007-2010, Ext JS, LLC.
 * [email protected]
 * 
 * http://extjs.com/license
 */
package com.extjs.gxt.ui.client.widget.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.PartFactory;
import com.extjs.gxt.ui.client.PartProvider;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.aria.FocusFrame;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.ContainerEvent;
import com.extjs.gxt.ui.client.event.TreeEvent;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Container;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.selection.Selectable;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
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.Accessibility;

/**
 * A standard hierarchical tree widget. The tree contains a hierarchy of
 * TreeItems that the user can open, close, and select.
 * 
 * 

* The root item cannot be displayed. * *

Events:
* *
BeforeAdd : TreeEvent(item, child, index)
*
Fires before a item is added or inserted. Listeners can cancel the * action by calling {@link BaseEvent#setCancelled(boolean)}.
*
    *
  • item : this
  • *
  • child : the item being added
  • *
  • index : the index at which the item will be added
  • *
*
* *
BeforeRemove : TreeEvent(item, child)
*
Fires before a item is removed. Listeners can cancel the action by * calling {@link BaseEvent#setCancelled(boolean)}.
*
    *
  • item : this
  • *
  • child : the item being removed
  • *
*
* *
BeforeExpand : TreeEvent(item)
*
Fires before a item is expanded. Listeners can cancel the action by * calling {@link BaseEvent#setCancelled(boolean)}.
*
    *
  • item : this
  • *
*
* *
BeforeCollapse : TreeEvent(item)
*
Fires before a item is collapsed. Listeners can cancel the action by * calling {@link BaseEvent#setCancelled(boolean)}.
*
    *
  • item : this
  • *
*
* *
Add : TreeEvent(item, child, index)
*
Fires after a item has been added or inserted.
*
    *
  • item : this
  • *
  • child : the item that was added
  • *
  • index : the index at which the item will be added
  • *
*
* *
Remove : TreeEvent(tree, item, child)
*
Fires after a item has been removed.
*
    *
  • tree : this
  • *
  • item : item
  • *
  • child : the item being removed
  • *
*
* *
BeforeSelect : TreeEvent(tree, item)
*
Fires before a item is selected. Listeners can cancel the action by * calling {@link BaseEvent#setCancelled(boolean)}.
*
    *
  • tree : this
  • *
  • item : the selected item
  • *
*
* *
SelectionChange : TreeEvent(tree, selected)
*
Fires after the tree selection changes.
*
    *
  • tree : this
  • *
  • selected : the selected items
  • *
*
* *
Expand : TreeEvent(tree, item)
*
Fires after a item has been expanded.
*
    *
  • item : this
  • *
*
* *
Collapse : TreeEvent(tree, item)
*
Fires after a item is collapsed.
*
    *
  • item : this
  • *
*
* *
CheckChange : TreeEvent(tree, item)
*
Fires after a check state change.
*
    *
  • item : this
  • *
*
* *
ContextMenu : TreeEvent(tree)
*
Fires before the tree's context menu is shown.
*
    *
  • component : this
  • *
*
* *
KeyPress : TreeEvent(tree, event)
*
Fires when a key is pressed.
*
    *
  • event : dom event
  • *
*
* * *
*
Inherited Events:
*
BoxComponent Move
*
BoxComponent Resize
*
Component Enable
*
Component Disable
*
Component BeforeHide
*
Component Hide
*
Component BeforeShow
*
Component Show
*
Component Attach
*
Component Detach
*
Component BeforeRender
*
Component Render
*
Component BrowserEvent
*
Component BeforeStateRestore
*
Component StateRestore
*
Component BeforeStateSave
*
Component SaveState
*
* *
*
CSS:
*
.my-tree (the tree itself)
*
.my-tree-item-text span (the tree item text)
*
* * @deprecated see {@link TreePanel} */ public class Tree extends Container implements Selectable { /** * Check cascade enum. */ public enum CheckCascade { CHILDREN, NONE, PARENTS; } /** * Check nodes enum. */ public enum CheckNodes { BOTH, LEAF, PARENT; } /** * Joint enum. */ public enum Joint { NONE(0), COLLAPSED(1), EXPANDED(2); private int value; private Joint(int value) { this.value = value; } public int value() { return value; } } public static final String DEFAULT_TREE_ITEM_ID = "tree.item.default"; public static final String FAST_TREE_ITEM_ID = "tree.item.fast"; static { PartFactory.registerProvider(new PartProvider() { public Object createPart(String id) { if (id.equals(DEFAULT_TREE_ITEM_ID)) { return new DefaultTreeItemUI(); } else if (id.equals(FAST_TREE_ITEM_ID)) { return new FastTreeItemUI(); } return null; } }); } protected boolean isViewer; protected TreeItem root; protected TreeSelectionModel sm; private boolean animate = true; private boolean checkable; private CheckNodes checkNodes = CheckNodes.BOTH; private CheckCascade checkStyle = CheckCascade.PARENTS; private int indentWidth = 18; private Map nodeHash; private TreeStyle style = new TreeStyle(); private String treeItemPartId = DEFAULT_TREE_ITEM_ID; private String itemSelector = ".x-tree-item"; /** * Returns the item selector. * * @return the item selector */ public String getItemSelector() { return itemSelector; } /** * Sets the CSS selector used to retrieve tree items after bulk rendering * (defaults to '.x-tree-item'). * * @param itemSelector the item selector */ public void setItemSelector(String itemSelector) { this.itemSelector = itemSelector; } /** * Creates a new single select tree. */ public Tree() { attachChildren = false; baseStyle = "my-tree"; focusable = true; createRootItem(); root.root = true; nodeHash = new HashMap(); setSelectionModel(new TreeSelectionModel()); } /** * Collapses all item's. */ public void collapseAll() { boolean anim = animate; if (anim) animate = false; root.setExpanded(false, true); if (anim) animate = true; } /** * Expands all item's. */ public void expandAll() { boolean anim = animate; if (anim) animate = false; root.setExpanded(true, true); if (anim) animate = true; } /** * Expands a specified path. A path can be retrieved from a tree item with * {@link TreeItem#getPath()}. * * @param path the path to expand * @return true if all paths expanded */ public boolean expandPath(String path) { if (path == null) return false; String[] ids = path.split(","); if (ids.length == 0) return false; if (ids[0].equals(root.getId())) { root.setExpanded(true); TreeItem current = root; for (int i = 1; i < ids.length; i++) { String id = ids[i]; boolean match = false; for (int j = 0; j < current.getItemCount(); j++) { TreeItem child = current.getItem(j); if (!match && child.getId().equals(id)) { child.setExpanded(true); current = child; match = true; break; } } if (!match) { return false; } } } return true; } /** * Returns the tree whose element or child elements match the passed target. * * @param element the target element * @return the matching tree item or null if no match */ public TreeItem findItem(Element element) { Element elem = fly(element).findParentElement(itemSelector, 15); if (elem != null) { String id = elem.getId(); if (id != null && !id.equals("")) { TreeItem item = getItemById(id); return item; } } return null; } /** * Returns the total number of items contained in the tree excluding the root * item. * * @return the total item count */ public int getAllItemCount() { return nodeHash.size(); } /** * Returns all tree item's contained by the tree. * * @return all tree item's */ public List getAllItems() { List temp = new ArrayList(); temp.add(root); temp.addAll(nodeHash.values()); return temp; } /** * Returns true if animations are enabled. * * @return the animate state */ public boolean getAnimate() { return animate; } /** * Returns true if check boxs are enabled. * * @return the checkbox state */ public boolean getCheckable() { return checkable; } /** * Returns a list of id's for all checked items. * * @return the list of checked id's */ public List getChecked() { List list = new ArrayList(); Iterator it = nodeHash.values().iterator(); while (it.hasNext()) { TreeItem item = it.next(); if (item.isChecked()) { list.add(item); } } return list; } /** * Returns the child nodes value. * * @return the child nodes value */ public CheckNodes getCheckNodes() { return checkNodes; } /** * The check style value. * * @return the check style */ public CheckCascade getCheckStyle() { return checkStyle; } @Override public Menu getContextMenu() { // made public return super.getContextMenu(); } /** * Returns the indent width. * * @return the indent width */ public int getIndentWidth() { return indentWidth; } @Override public TreeItem getItem(int index) { return getRootItem().getItem(index); } /** * Returns the item by id. * * @param id the id of the element to return * @return the item */ public TreeItem getItemById(String id) { return nodeHash.get(id); } /** * Returns the item icon style. * * @return the icon style * @deprecated see {@link TreeStyle#getLeafIconStyle()} */ public String getItemIconStyle() { return style.getLeafIconStyle(); } /** * Returns the node icon style. * * @return the icon style * @deprecated see {@link TreeStyle#getNodeCloseIconStyle()} */ public String getNodeIconStyle() { return style.getNodeCloseIconStyle(); } /** * Returns the open node icon style. * * @return the icon style * @deprecated see {@link TreeStyle#getNodeOpenIconStyle()} */ public String getOpenNodeIconStyle() { return style.getNodeOpenIconStyle(); } /** * Returns the tree's root item. The root item cannot be displayed. * * @return the root item */ public TreeItem getRootItem() { return root; } /** * Returns the selected item. * * @return the item */ public TreeItem getSelectedItem() { return (TreeItem) sm.getSelectedItem(); } /** * Returns the selected items. * * @return the selected items */ public List getSelectedItems() { return sm.getSelectedItems(); } public SelectionMode getSelectionMode() { return sm.getSelectionMode(); } /** * Returns the tree's selection model. * * @return the selection model */ public TreeSelectionModel getSelectionModel() { return sm; } /** * Returns the tree's style. * * @return the tree style */ public TreeStyle getStyle() { return style; } /** * Returns the tree item part id. * * @return the part id */ public String getTreeItemPartId() { return treeItemPartId; } @Override public void onComponentEvent(ComponentEvent ce) { super.onComponentEvent(ce); TreeEvent te = (TreeEvent) ce; if (te.getItem() != null) { te.getItem().onComponentEvent(te); } int type = ce.getEventTypeInt(); switch (type) { case Event.ONFOCUS: onFocus(ce); break; } } protected void onFocus(ComponentEvent ce) { if (GXT.isAriaEnabled()) { FocusFrame.get().frame(this); } } public void onSelectChange(TreeItem item, boolean select) { item.getUI().onSelectedChange(select); } @Override public boolean removeAll() { getRootItem().removeAll(); nodeHash.clear(); return true; } /** * Sets whether expand /collapse should be animated (defaults to true). * * @param animate the animate state */ public void setAnimate(boolean animate) { this.animate = animate; } /** * Sets whether checkboxes are used in the tree. * * @param checkable true for checkboxes */ public void setCheckable(boolean checkable) { this.checkable = checkable; } /** * Sets which tree items will display a check box (defaults to BOTH). *

* Valid values are: *

    *
  • BOTH - both nodes and leafs
  • *
  • PARENT - only nodes with children
  • *
  • LEAF - only leafs
  • *
* * @param checkNodes the child nodes value */ public void setCheckNodes(CheckNodes checkNodes) { this.checkNodes = checkNodes; } /** * Sets the cascading behavior for check tree (defaults to PARENTS). *

* Valid values are: *

    *
  • NONE - no cascading
  • *
  • PARENTS - cascade to parents
  • *
  • CHILDREN - cascade to children
  • *
* * @param checkStyle the child style */ public void setCheckStyle(CheckCascade checkStyle) { this.checkStyle = checkStyle; } @Override public void setContextMenu(Menu menu) { super.setContextMenu(menu); } /** * Sets the number of pixels child items are indented. Default value is 18. * * @param indentWidth the indent width */ public void setIndentWidth(int indentWidth) { this.indentWidth = indentWidth; } /** * Sets the global icon style for leaf tree items. Individual tree items can * override this value by setting the the item's icon style. * * @param itemImageStyle the image style * @deprecated see {@link TreeStyle#setLeafIconStyle(String)} */ public void setItemIconStyle(String itemImageStyle) { style.setLeafIconStyle(itemImageStyle); } /** * The global icon style for tree items with children (defaults to * 'tree-folder'). Individual tree items can override this value by setting * the the item's icon style. * * @param nodeIconStyle the node icon style * @deprecated see {@link TreeStyle#setNodeCloseIconStyle(String)} */ public void setNodeIconStyle(String nodeIconStyle) { style.setNodeCloseIconStyle(nodeIconStyle); } /** * Sets the global icon style for expanded tree items (defaults to * 'tree-folder-open'). Individual tree items can override this value by * setting the the item's icon style. * * @param openNodeIconStyle the open node icon style * @deprecated see {@link TreeStyle#setNodeOpenIconStyle(String)} */ public void setOpenNodeIconStyle(String openNodeIconStyle) { style.setNodeOpenIconStyle(openNodeIconStyle); } public void setSelectedItem(TreeItem item) { sm.select(item, false); } public void setSelectedItems(List items) { sm.select(items, false); } /** * Sets the table's selection mode. * * @param mode the selection mode */ public void setSelectionMode(SelectionMode mode) { setSelectionModel(new TreeSelectionModel(mode)); } /** * Sets the tree's selection model. * * @param sm the tree selection model */ public void setSelectionModel(TreeSelectionModel sm) { assert sm != null; if (this.sm != null) { this.sm.bind(null); } this.sm = sm; sm.bind(this); } /** * Sets the part id used to obtain new tree item ui instances (defaults to * {@value #DEFAULT_TREE_ITEM_ID}. * * @param treeItemPartId the tree item part id */ public void setTreeItemPartId(String treeItemPartId) { this.treeItemPartId = treeItemPartId; } @Override protected ComponentEvent createComponentEvent(Event event) { return new TreeEvent(this, event == null ? null : findItem(DOM.eventGetTarget(event))); } @SuppressWarnings("rawtypes") @Override protected ContainerEvent createContainerEvent(TreeItem item) { return new TreeEvent(this, item); } protected void createRootItem() { root = new RootTreeItem(this); root.tree = this; } @Override protected void onRender(Element target, int index) { setElement(DOM.createDiv(), target, index); super.onRender(target, index); root.render(getElement()); if (!root.childrenRendered) { root.renderChildren(); } addStyleName("x-ftree-no-lines x-ftree-arrows"); disableTextSelection(true); if (GXT.isAriaEnabled()) { new KeyNav(this) { @Override public void onDown(ComponentEvent ce) { if (getSelectedItems().size() == 0 && getRootItem().getItemCount() > 0) { setSelectedItem(getRootItem().getItem(0)); } } }; setAnimate(false); } el().setTabIndex(0); el().setElementAttribute("hideFocus", "true"); Accessibility.setRole(getElement(), Accessibility.ROLE_TREE); sinkEvents(Event.ONCLICK | Event.ONDBLCLICK | Event.KEYEVENTS | Event.MOUSEEVENTS | Event.FOCUSEVENTS); } void registerItem(TreeItem item) { nodeHash.put(item.getId(), item); } void unregisterItem(TreeItem item) { int count = item.getItemCount(); for (int i = 0; i < count; i++) { unregisterItem(item.getItem(i)); } nodeHash.remove(item.getId()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy