Class CellTree
- java.lang.Object
-
- com.google.gwt.user.client.ui.UIObject
-
- com.google.gwt.user.client.ui.Widget
-
- com.google.gwt.user.client.ui.Composite
-
- com.google.gwt.user.cellview.client.AbstractCellTree
-
- com.google.gwt.user.cellview.client.CellTree
-
- All Implemented Interfaces:
HasAttachHandlers,HasCloseHandlers<TreeNode>,HasOpenHandlers<TreeNode>,HasHandlers,HasKeyboardSelectionPolicy,EventListener,Focusable,HasAnimation,HasVisibility,IsRenderable,IsWidget
public class CellTree extends AbstractCellTree implements HasAnimation, Focusable
A view of a tree.This widget will only work in standards mode, which requires that the HTML page in which it is run have an explicit <!DOCTYPE> declaration.
Examples
- Trivial example
public class CellTreeExample implements EntryPoint { /** * The model that defines the nodes in the tree. */ private static class CustomTreeModel implements TreeViewModel { /** * Get the {@link NodeInfo} that provides the children of the specified * value. */ public <T> NodeInfo<?> getNodeInfo(T value) { /* * Create some data in a data provider. Use the parent value as a prefix * for the next level. */ ListDataProvider<String> dataProvider = new ListDataProvider<String>(); for (int i = 0; i < 2; i++) { dataProvider.getList().add(value + "." + String.valueOf(i)); } // Return a node info that pairs the data with a cell. return new DefaultNodeInfo<String>(dataProvider, new TextCell()); } /** * Check if the specified value represents a leaf node. Leaf nodes cannot be * opened. */ public boolean isLeaf(Object value) { // The maximum length of a value is ten characters. return value.toString().length() > 10; } } public void onModuleLoad() { // Create a model for the tree. TreeViewModel model = new CustomTreeModel(); /* * Create the tree using the model. We specify the default value of the * hidden root node as "Item 1". */ CellTree tree = new CellTree(model, "Item 1"); // Add the tree to the root layout panel. RootLayoutPanel.get().add(tree); } }- Complex example
public class CellTreeExample2 implements EntryPoint { /** * A list of songs. */ private static class Playlist { private final String name; private final List<String> songs = new ArrayList<String>(); public Playlist(String name) { this.name = name; } /** * Add a song to the playlist. * * @param name the name of the song */ public void addSong(String name) { songs.add(name); } public String getName() { return name; } /** * Return the list of songs in the playlist. */ public List<String> getSongs() { return songs; } } /** * A composer of classical music. */ private static class Composer { private final String name; private final List<Playlist> playlists = new ArrayList<Playlist>(); public Composer(String name) { this.name = name; } /** * Add a playlist to the composer. * * @param playlist the playlist to add */ public Playlist addPlaylist(Playlist playlist) { playlists.add(playlist); return playlist; } public String getName() { return name; } /** * Return the rockin' playlist for this composer. */ public List<Playlist> getPlaylists() { return playlists; } } /** * The model that defines the nodes in the tree. */ private static class CustomTreeModel implements TreeViewModel { private final List<Composer> composers; /** * This selection model is shared across all leaf nodes. A selection model * can also be shared across all nodes in the tree, or each set of child * nodes can have its own instance. This gives you flexibility to determine * how nodes are selected. */ private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>(); public CustomTreeModel() { // Create a database of information. composers = new ArrayList<Composer>(); // Add compositions by Beethoven. { Composer beethoven = new Composer("Beethoven"); composers.add(beethoven); Playlist concertos = beethoven.addPlaylist(new Playlist("Concertos")); concertos.addSong("No. 1 - C"); concertos.addSong("No. 2 - B-Flat Major"); concertos.addSong("No. 3 - C Minor"); concertos.addSong("No. 4 - G Major"); concertos.addSong("No. 5 - E-Flat Major"); Playlist quartets = beethoven.addPlaylist(new Playlist("Quartets")); quartets.addSong("Six String Quartets"); quartets.addSong("Three String Quartets"); quartets.addSong("Grosse Fugue for String Quartets"); Playlist sonatas = beethoven.addPlaylist(new Playlist("Sonatas")); sonatas.addSong("Sonata in A Minor"); sonatas.addSong("Sonata in F Major"); Playlist symphonies = beethoven.addPlaylist(new Playlist("Symphonies")); symphonies.addSong("No. 2 - D Major"); symphonies.addSong("No. 2 - D Major"); symphonies.addSong("No. 3 - E-Flat Major"); symphonies.addSong("No. 4 - B-Flat Major"); symphonies.addSong("No. 5 - C Minor"); symphonies.addSong("No. 6 - F Major"); symphonies.addSong("No. 7 - A Major"); symphonies.addSong("No. 8 - F Major"); symphonies.addSong("No. 9 - D Minor"); } // Add compositions by Brahms. { Composer brahms = new Composer("Brahms"); composers.add(brahms); Playlist concertos = brahms.addPlaylist(new Playlist("Concertos")); concertos.addSong("Violin Concerto"); concertos.addSong("Double Concerto - A Minor"); concertos.addSong("Piano Concerto No. 1 - D Minor"); concertos.addSong("Piano Concerto No. 2 - B-Flat Major"); Playlist quartets = brahms.addPlaylist(new Playlist("Quartets")); quartets.addSong("Piano Quartet No. 1 - G Minor"); quartets.addSong("Piano Quartet No. 2 - A Major"); quartets.addSong("Piano Quartet No. 3 - C Minor"); quartets.addSong("String Quartet No. 3 - B-Flat Minor"); Playlist sonatas = brahms.addPlaylist(new Playlist("Sonatas")); sonatas.addSong("Two Sonatas for Clarinet - F Minor"); sonatas.addSong("Two Sonatas for Clarinet - E-Flat Major"); Playlist symphonies = brahms.addPlaylist(new Playlist("Symphonies")); symphonies.addSong("No. 1 - C Minor"); symphonies.addSong("No. 2 - D Minor"); symphonies.addSong("No. 3 - F Major"); symphonies.addSong("No. 4 - E Minor"); } // Add compositions by Mozart. { Composer mozart = new Composer("Mozart"); composers.add(mozart); Playlist concertos = mozart.addPlaylist(new Playlist("Concertos")); concertos.addSong("Piano Concerto No. 12"); concertos.addSong("Piano Concerto No. 17"); concertos.addSong("Clarinet Concerto"); concertos.addSong("Violin Concerto No. 5"); concertos.addSong("Violin Concerto No. 4"); } } /** * Get the {@link NodeInfo} that provides the children of the specified * value. */ public <T> NodeInfo<?> getNodeInfo(T value) { if (value == null) { // LEVEL 0. // We passed null as the root value. Return the composers. // Create a data provider that contains the list of composers. ListDataProvider<Composer> dataProvider = new ListDataProvider<CellTreeExample2.Composer>( composers); // Create a cell to display a composer. Cell<Composer> cell = new AbstractCell<Composer>() { @Override public void render(Context context, Composer value, SafeHtmlBuilder sb) { if (value != null) { sb.appendEscaped(value.getName()); } } }; // Return a node info that pairs the data provider and the cell. return new DefaultNodeInfo<Composer>(dataProvider, cell); } else if (value instanceof Composer) { // LEVEL 1. // We want the children of the composer. Return the playlists. ListDataProvider<Playlist> dataProvider = new ListDataProvider<Playlist>( ((Composer) value).getPlaylists()); Cell<Playlist> cell = new AbstractCell<Playlist>() { @Override public void render(Context context, Playlist value, SafeHtmlBuilder sb) { if (value != null) { sb.appendEscaped(value.getName()); } } }; return new DefaultNodeInfo<Playlist>(dataProvider, cell); } else if (value instanceof Playlist) { // LEVEL 2 - LEAF. // We want the children of the playlist. Return the songs. ListDataProvider<String> dataProvider = new ListDataProvider<String>( ((Playlist) value).getSongs()); // Use the shared selection model. return new DefaultNodeInfo<String>(dataProvider, new TextCell(), selectionModel, null); } return null; } /** * Check if the specified value represents a leaf node. Leaf nodes cannot be * opened. */ public boolean isLeaf(Object value) { // The leaf nodes are the songs, which are Strings. if (value instanceof String) { return true; } return false; } } public void onModuleLoad() { // Create a model for the tree. TreeViewModel model = new CustomTreeModel(); /* * Create the tree using the model. We use <code>null</code> as the default * value of the root node. The default value will be passed to * CustomTreeModel#getNodeInfo(); */ CellTree tree = new CellTree(model, null); tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); // Open the first playlist by default. TreeNode rootNode = tree.getRootTreeNode(); TreeNode firstPlaylist = rootNode.setChildOpen(0, true); firstPlaylist.setChildOpen(0, true); // Add the tree to the root layout panel. RootLayoutPanel.get().add(tree); } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCellTree.BasicResourcesResources that match the GWT standard style theme.(package private) static interfaceCellTree.BasicStyleStyles used byCellTree.BasicResources.static interfaceCellTree.CellTreeMessagesConstants for labeling the cell tree.static classCellTree.NodeAnimationA node animation.static interfaceCellTree.ResourcesA ClientBundle that provides images for this widget.static classCellTree.RevealAnimationACellTree.NodeAnimationthat reveals the contents of child nodes.static classCellTree.SlideAnimationACellTree.NodeAnimationthat slides children into view.static interfaceCellTree.StyleStyles used by this widget.(package private) static interfaceCellTree.Template-
Nested classes/interfaces inherited from class com.google.gwt.user.client.ui.UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabled
-
Nested classes/interfaces inherited from interface com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy
HasKeyboardSelectionPolicy.KeyboardSelectionPolicy
-
-
Field Summary
Fields Modifier and Type Field Description (package private) booleancellIsEditingA boolean indicating whether or not a cell is being edited.(package private) booleanisFocusedA boolean indicating that the widget has focus.(package private) booleanisRefreshingSet to true while the elements are being refreshed.(package private) CellTreeNodeView<?>rootNodeThe hidden root node in the tree.-
Fields inherited from class com.google.gwt.user.client.ui.UIObject
DEBUG_ID_PREFIX
-
-
Constructor Summary
Constructors Constructor Description CellTree(TreeViewModel viewModel, T rootValue)Construct a newCellTree.CellTree(TreeViewModel viewModel, T rootValue, CellTree.Resources resources)Construct a newCellTree.CellTree(TreeViewModel viewModel, T rootValue, CellTree.Resources resources, CellTree.CellTreeMessages messages)Construct a newCellTree.CellTree(TreeViewModel viewModel, T rootValue, CellTree.Resources resources, CellTree.CellTreeMessages messages, int defaultNodeSize)Construct a newCellTree.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidcancelTreeNodeAnimation()Cancel a pending animation.protected chargetAccessKey()Get the access key.CellTree.NodeAnimationgetAnimation()Get the animation used to open and close nodes in this tree if animations are enabled.(package private) SafeHtmlgetClosedImageHtml(boolean isTop)Get the HTML to render the closed image.intgetDefaultNodeSize()Get the default maximum number of children to display under each tree node.(package private) intgetImageWidth()Get the width required for the images.(package private) CellTreeNodeView<?>getKeyboardSelectedNode()Return the node that has keyboard selection.TreeNodegetKeyboardSelectedTreeNode()Returns the TreeNode that is selected when the CellTree has keyboard focus.(package private) SafeHtmlgetLoadingImageHtml()Return the HTML to render the loading image.(package private) SafeHtmlgetOpenImageHtml(boolean isTop)Get the HTML to render the open image.TreeNodegetRootTreeNode()Get the rootTreeNode.(package private) CellTree.StylegetStyle()Return the Style used by the tree.intgetTabIndex()Gets the widget's position in the tab index.(package private) voidhandleKeyNavigation(int keyCode)Handle keyboard navigation.booleanisAnimationEnabled()Returns true if animations are enabled, false if not.(package private) voidkeyboardSelect(CellTreeNodeView<?> node, boolean stealFocus)Select a node using the keyboard.(package private) voidmaybeAnimateTreeNode(CellTreeNodeView<?> node)Animate the current state of aCellTreeNodeViewin this tree.protected voidonBlur()Called when the keyboard selected node loses focus.voidonBrowserEvent(Event event)Fired whenever a browser event is received.protected voidonFocus()Called when the keyboard selected node gains focus.(package private) voidresetFocus()If this widget has focus, reset it.voidsetAccessKey(char key)Sets the widget's 'access key'.voidsetAnimation(CellTree.NodeAnimation animation)Set the animation used to open and close nodes in this tree.voidsetAnimationEnabled(boolean enable)Enable or disable animations.voidsetDefaultNodeSize(int defaultNodeSize)Set the default number of children to display beneath each child node.voidsetFocus(boolean focused)Explicitly focus/unfocus this widget.voidsetKeyboardSelectedTreeNode(TreeNode parentNode, int childIndex, boolean stealFocus)Sets the node that will be selected when the CellTree gains keyboard focus.voidsetTabIndex(int index)Sets the widget's position in the tab index.-
Methods inherited from class com.google.gwt.user.cellview.client.AbstractCellTree
addCloseHandler, addOpenHandler, getKeyboardSelectionPolicy, getNodeInfo, getTreeViewModel, isKeyboardSelectionDisabled, isLeaf, setKeyboardSelectionPolicy
-
Methods inherited from class com.google.gwt.user.client.ui.Composite
claimElement, getWidget, initializeClaimedElement, initWidget, isAttached, onAttach, onDetach, render, render, resolvePotentialElement, setWidget
-
Methods inherited from class com.google.gwt.user.client.ui.Widget
addAttachHandler, addBitlessDomHandler, addDomHandler, addHandler, asWidget, asWidgetOrNull, createHandlerManager, delegateEvent, doAttachChildren, doDetachChildren, fireEvent, getHandlerCount, getLayoutData, getParent, isOrWasAttached, onLoad, onUnload, removeFromParent, setLayoutData, sinkEvents, unsinkEvents
-
Methods inherited from class com.google.gwt.user.client.ui.UIObject
addStyleDependentName, addStyleName, ensureDebugId, ensureDebugId, ensureDebugId, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, onEnsureDebugId, removeStyleDependentName, removeStyleName, setElement, setElement, setHeight, setPixelSize, setSize, setStyleDependentName, setStyleName, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkBitlessEvent, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.google.gwt.event.shared.HasHandlers
fireEvent
-
-
-
-
Field Detail
-
cellIsEditing
boolean cellIsEditing
A boolean indicating whether or not a cell is being edited.
-
isFocused
boolean isFocused
A boolean indicating that the widget has focus.
-
isRefreshing
boolean isRefreshing
Set to true while the elements are being refreshed. Events are ignored during this time.
-
rootNode
final CellTreeNodeView<?> rootNode
The hidden root node in the tree. Visible for testing.
-
-
Constructor Detail
-
CellTree
public CellTree(TreeViewModel viewModel, T rootValue)
Construct a newCellTree.- Type Parameters:
T- the type of data in the root node- Parameters:
viewModel- theTreeViewModelthat backs the treerootValue- the hidden root value of the tree
-
CellTree
public CellTree(TreeViewModel viewModel, T rootValue, CellTree.Resources resources)
Construct a newCellTree. Uses default translations that means that messages will be always in English.- Type Parameters:
T- the type of data in the root node- Parameters:
viewModel- theTreeViewModelthat backs the treerootValue- the hidden root value of the treeresources- the resources used to render the tree
-
CellTree
public CellTree(TreeViewModel viewModel, T rootValue, CellTree.Resources resources, CellTree.CellTreeMessages messages)
Construct a newCellTree.- Type Parameters:
T- the type of data in the root node- Parameters:
viewModel- theTreeViewModelthat backs the treerootValue- the hidden root value of the treeresources- the resources used to render the treemessages- translation messages. Users should inherit an empty interface fromCellTree.CellTreeMessagesand add annotations needed for their specific translation systems. Then create the new interface with GWT.create and pass as this argument.
-
CellTree
public CellTree(TreeViewModel viewModel, T rootValue, CellTree.Resources resources, CellTree.CellTreeMessages messages, int defaultNodeSize)
Construct a newCellTree.- Type Parameters:
T- the type of data in the root node- Parameters:
viewModel- theTreeViewModelthat backs the treerootValue- the hidden root value of the treeresources- the resources used to render the treemessages- translation messages. Users should inherit an empty interface fromCellTree.CellTreeMessagesand add annotations needed for their specific translation systems. Then create the new interface with GWT.create and pass as this argument.defaultNodeSize- default number of children to display beneath each child node
-
-
Method Detail
-
getAnimation
public CellTree.NodeAnimation getAnimation()
Get the animation used to open and close nodes in this tree if animations are enabled.- Returns:
- the animation
- See Also:
isAnimationEnabled(),setAnimation(NodeAnimation)
-
getDefaultNodeSize
public int getDefaultNodeSize()
Get the default maximum number of children to display under each tree node.- Returns:
- the default node size
- See Also:
setDefaultNodeSize(int)
-
getRootTreeNode
public TreeNode getRootTreeNode()
Description copied from class:AbstractCellTreeGet the rootTreeNode.- Specified by:
getRootTreeNodein classAbstractCellTree- Returns:
- the
TreeNodeat the root of the tree
-
getTabIndex
public int getTabIndex()
Description copied from interface:FocusableGets the widget's position in the tab index.- Specified by:
getTabIndexin interfaceFocusable- Returns:
- the widget's tab index
-
isAnimationEnabled
public boolean isAnimationEnabled()
Description copied from interface:HasAnimationReturns true if animations are enabled, false if not.- Specified by:
isAnimationEnabledin interfaceHasAnimation
-
onBrowserEvent
public void onBrowserEvent(Event event)
Description copied from interface:EventListenerFired whenever a browser event is received.- Specified by:
onBrowserEventin interfaceEventListener- Overrides:
onBrowserEventin classComposite- Parameters:
event- the event received
-
setAccessKey
public void setAccessKey(char key)
Sets the widget's 'access key'. This key is used (in conjunction with a browser-specific modifier key) to automatically focus the widget.Setting the key to (int) 0 will disable the access key.
- Specified by:
setAccessKeyin interfaceFocusable- Parameters:
key- the widget's access key- See Also:
getAccessKey()
-
setAnimation
public void setAnimation(CellTree.NodeAnimation animation)
Set the animation used to open and close nodes in this tree. You must callsetAnimationEnabled(boolean)to enable or disable animation.- Parameters:
animation- aCellTree.NodeAnimation- See Also:
setAnimationEnabled(boolean),getAnimation()
-
setAnimationEnabled
public void setAnimationEnabled(boolean enable)
Description copied from interface:HasAnimationEnable or disable animations.- Specified by:
setAnimationEnabledin interfaceHasAnimation- Parameters:
enable- true to enable, false to disable
-
setDefaultNodeSize
public void setDefaultNodeSize(int defaultNodeSize)
Set the default number of children to display beneath each child node. If more nodes are available, a button will appear at the end of the list allowing the user to show more items. Changing this value will not affect other tree nodes that are already open (including the hidden root node).- Parameters:
defaultNodeSize- the max- See Also:
getDefaultNodeSize()
-
setFocus
public void setFocus(boolean focused)
Description copied from interface:FocusableExplicitly focus/unfocus this widget. Only one widget can have focus at a time, and the widget that does will receive all keyboard events. NOTE: Most browsers fire FocusEvents asynchronously. Especially within GWT tests, you'll need to make your test asynchronous to properly do verifications. SeeGWTTestCase#delayTestFinishfor more information on how to do this.
-
setKeyboardSelectedTreeNode
public void setKeyboardSelectedTreeNode(TreeNode parentNode, int childIndex, boolean stealFocus)
Sets the node that will be selected when the CellTree gains keyboard focus.- Parameters:
parentNode- a node in the tree that is currently openchildIndex- the index of the child to selectstealFocus- if true, also change keyboard focus to this CellTree.
-
setTabIndex
public void setTabIndex(int index)
Description copied from interface:FocusableSets the widget's position in the tab index. If more than one widget has the same tab index, each such widget will receive focus in an arbitrary order. Setting the tab index to-1will cause this widget to be removed from the tab order.- Specified by:
setTabIndexin interfaceFocusable- Parameters:
index- the widget's tab index
-
getAccessKey
protected char getAccessKey()
Get the access key.- Returns:
- the access key, or -1 if not set
- See Also:
setAccessKey(char)
-
onBlur
protected void onBlur()
Called when the keyboard selected node loses focus.
-
onFocus
protected void onFocus()
Called when the keyboard selected node gains focus.
-
cancelTreeNodeAnimation
void cancelTreeNodeAnimation()
Cancel a pending animation.
-
getClosedImageHtml
SafeHtml getClosedImageHtml(boolean isTop)
Get the HTML to render the closed image.- Parameters:
isTop- true if the top element, false if not- Returns:
- the HTML string
-
getImageWidth
int getImageWidth()
Get the width required for the images.- Returns:
- the maximum width required for images.
-
getKeyboardSelectedNode
CellTreeNodeView<?> getKeyboardSelectedNode()
Return the node that has keyboard selection.
-
getKeyboardSelectedTreeNode
public TreeNode getKeyboardSelectedTreeNode()
Returns the TreeNode that is selected when the CellTree has keyboard focus.
-
getLoadingImageHtml
SafeHtml getLoadingImageHtml()
Return the HTML to render the loading image.
-
getOpenImageHtml
SafeHtml getOpenImageHtml(boolean isTop)
Get the HTML to render the open image.- Parameters:
isTop- true if the top element, false if not- Returns:
- the HTML string
-
getStyle
CellTree.Style getStyle()
Return the Style used by the tree.
-
keyboardSelect
void keyboardSelect(CellTreeNodeView<?> node, boolean stealFocus)
Select a node using the keyboard.- Parameters:
node- the new node to selectstealFocus- true to steal focus, false not to
-
maybeAnimateTreeNode
void maybeAnimateTreeNode(CellTreeNodeView<?> node)
Animate the current state of aCellTreeNodeViewin this tree.- Parameters:
node- the node to animate
-
resetFocus
void resetFocus()
If this widget has focus, reset it.
-
handleKeyNavigation
void handleKeyNavigation(int keyCode)
Handle keyboard navigation.- Parameters:
keyCode- the key code that was pressed
-
-