0% found this document useful (0 votes)
4 views

tree

The document describes a tree component that displays hierarchical employee data, allowing users to navigate and manipulate nodes. It includes Java classes for managing the tree structure, such as EmployeeUserObject and TreeController, which handle node selection and movement. The tree is built using DefaultMutableTreeNode instances and is designed for applications requiring a visual representation of hierarchical data.

Uploaded by

Roberto Bravo
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

tree

The document describes a tree component that displays hierarchical employee data, allowing users to navigate and manipulate nodes. It includes Java classes for managing the tree structure, such as EmployeeUserObject and TreeController, which handle node selection and movement. The tree is built using DefaultMutableTreeNode instances and is designed for applications requiring a visual representation of hierarchical data.

Uploaded by

Roberto Bravo
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

El Árbol de componentes muestra datos jerárquicos como un "árbol" de ramas y nodos de

hojas. Mueve los nodos arriba y abajo usando los Botones de Comandos de la parte inferior
de la página.

Employees

Western

Smith, Ethan

Smith, Jacob

Williams, Daniel

Brown, Andrew

Central

Brown, Joshua

Brown, Christopher

Miller, Vincent

Davis, Matthew

Eastern

Garcia, Alexander
Garcia, Jacob

Garcia, Benjamin

Rodriguez, Joshua

Valores de Bean de Respaldo en el Servidor:


Ruta: Employees
Id de Empleado: 0
Nombre:
Teléfono:

EmployeeUserObject.java
/* MPL License text (see http://www.mozilla.org/MPL/) */

package org.icefaces.application.showcase.view.bean.examples.component.tr
ee;

import org.icefaces.application.showcase.model.entity.Employee;

import javax.swing.tree.DefaultMutableTreeNode;

public class EmployeeUserObject extends NodeUserObject {

private Employee employee;

public EmployeeUserObject(DefaultMutableTreeNode defaultMutableTreeNo


de) {
super(defaultMutableTreeNode);
}

public Employee getEmployee() {


return employee;
}

public void setEmployee(Employee employee) {


this.employee = employee;
}
}
NodeUserObject.java
/* MPL License text (see http://www.mozilla.org/MPL/) */

package org.icefaces.application.showcase.view.bean.examples.component.tr
ee;

import com.icesoft.faces.component.tree.IceUserObject;

import javax.swing.tree.DefaultMutableTreeNode;

public class NodeUserObject extends IceUserObject {

public NodeUserObject(DefaultMutableTreeNode defaultMutableTreeNode)


{
super(defaultMutableTreeNode);

setLeafIcon("tree_document.gif");
setBranchContractedIcon("tree_folder_closed.gif");
setBranchExpandedIcon("tree_folder_open.gif");
setExpanded(true);
}

TreeController.java
/* MPL License text (see http://www.mozilla.org/MPL/) */

package org.icefaces.application.showcase.view.bean.examples.component.tr
ee;

import com.icesoft.faces.component.dragdrop.DropEvent;
import com.icesoft.faces.component.ext.HtmlPanelGroup;
import com.icesoft.faces.component.tree.IceUserObject;
import org.icefaces.application.showcase.model.entity.Employee;
import org.icefaces.application.showcase.util.FacesUtils;
import org.icefaces.application.showcase.view.bean.BaseBean;

import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import java.util.ArrayList;
import java.util.Enumeration;
import java.io.Serializable;

/**
* <p>The TreeBean class is the backing bean for the Tree Component showc
ase
* demonstration. It is used to store and display the selected tree node<
/p>
*
* @see NodeUserObject
* @since 0.3.0
*/
public class TreeController extends BaseBean implements Serializable {

// tree default model, used as a value for the tree component


private DefaultTreeModel model;
private EmployeeUserObject selectedUserObject;

/**
* Construct the default tree structure by combining tree nodes.
*/
public TreeController() {
init();
}

public DefaultTreeModel getModel() {


return model;
}

public void setModel(DefaultTreeModel model) {


this.model = model;
}

public NodeUserObject getSelectedUserObject() {


return selectedUserObject;
}

public void employeeNodeSelected(ActionEvent event) {


// get employee id
String employeeId = FacesUtils.getRequestParameter("employeeId");

// find employee node by id and make it the selected node


DefaultMutableTreeNode node = findTreeNode(employeeId);
selectedUserObject = (EmployeeUserObject) node.getUserObject();
// fire effects.);
valueChangeEffect.setFired(false);
}

public ArrayList getSelectedTreePath() {


Object[] objectPath = selectedUserObject.getWrapper().getUserObje
ctPath();
ArrayList treePath = new ArrayList();
Object anObjectPath;
for(int i= 0, max = objectPath.length; i < max; i++){
anObjectPath = objectPath[i];
IceUserObject userObject = (IceUserObject) anObjectPath;
treePath.add(userObject.getText());
}
return treePath;
}
public boolean isMoveUpDisabled() {
DefaultMutableTreeNode selectedNode = selectedUserObject.getWrapp
er();
return isMoveDisabled(selectedNode, selectedNode.getPreviousNode(
));
}

public boolean isMoveDownDisabled() {


DefaultMutableTreeNode selectedNode = selectedUserObject.getWrapp
er();
return isMoveDisabled(selectedNode, selectedNode.getNextNode());
}

public boolean isMoveDisabled(DefaultMutableTreeNode selected, Defaul


tMutableTreeNode swapper) {
return selected == null || swapper == null || selected.getAllowsChi
ldren() || swapper.isRoot();
}

public void moveUp(ActionEvent event) {


DefaultMutableTreeNode selectedNode = selectedUserObject.getWrapp
er();
exchangeNodes(selectedNode.getPreviousNode(), selectedNode);
}

public void moveDown(ActionEvent event) {


DefaultMutableTreeNode selectedNode = selectedUserObject.getWrapp
er();
exchangeNodes(selectedNode, selectedNode.getNextNode());
}

public void exchangeNodes(DefaultMutableTreeNode node1, DefaultMutabl


eTreeNode node2) {
DefaultMutableTreeNode node1Parent = (DefaultMutableTreeNode) nod
e1.getParent();
DefaultMutableTreeNode node2Parent = (DefaultMutableTreeNode) nod
e2.getParent();
DefaultMutableTreeNode node1PrevNode = node1.getPreviousNode();
DefaultMutableTreeNode node1PrevNodeParent = (DefaultMutableTreeN
ode) node1PrevNode.getParent();
int childCount = 0;

if (node1.isNodeDescendant(node2)) {
while (node2.getChildCount() > 0) {
node1.insert((MutableTreeNode) node2.getFirstChild(), chi
ldCount++);
}
if (node1PrevNode == node1Parent ||
(node1PrevNode.isNodeSibling(node1) && !
node1PrevNode.getAllowsChildren())) {
node1Parent.insert(node2, node1Parent.getIndex(node1));
} else if (node1PrevNode.getAllowsChildren()) {
node1PrevNode.add(node2);
} else {
node1PrevNodeParent.add(node2);
}

return;
}

if (node2.getAllowsChildren()) {
node2.insert(node1, 0);
} else {
node1.removeFromParent();
node2Parent.insert(node1, node2Parent.getIndex(node2) + 1);
}
}

public void dropListener(DropEvent event) {


HtmlPanelGroup panelGroup = (HtmlPanelGroup) event.getComponent()
;

DefaultMutableTreeNode dragNode = (DefaultMutableTreeNode) event.


getTargetDragValue();
DefaultMutableTreeNode dropNode = (DefaultMutableTreeNode) panelG
roup.getDropValue();
DefaultMutableTreeNode dropNodeParent = (DefaultMutableTreeNode)
dropNode.getParent();

if (dragNode.isNodeDescendant(dropNode)) return;

if (dropNode.getAllowsChildren()) {
dropNode.insert(dragNode, 0);
} else {
dragNode.removeFromParent();
dropNodeParent.insert(dragNode, dropNodeParent.getIndex(dropN
ode) + 1);
}
}

private DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent,


String title,
Employee employee) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode();
EmployeeUserObject userObject = new EmployeeUserObject(node);
node.setUserObject(userObject);
userObject.setEmployee(employee);

// non-employee node or branch


if (title != null) {
userObject.setText(title);
userObject.setLeaf(false);
userObject.setExpanded(true);
node.setAllowsChildren(true);
}
// employee node
else {
userObject.setText(employee.getLastName() + ", " +
employee.getFirstName());
userObject.setLeaf(true);
node.setAllowsChildren(false);
}
// finally add the node to the parent.
if (parent != null) {
parent.add(node);
}

return node;
}

protected void init() {


DefaultMutableTreeNode rootNode = addNode(null, "Employees",
new Employee(0, "", "", "", "", ""));
model = new DefaultTreeModel(rootNode);
selectedUserObject = (EmployeeUserObject) rootNode.getUserObject(
);
selectedUserObject.setExpanded(true);
DefaultMutableTreeNode regionNode = addNode(rootNode, "Western",
new Employee(1, "", "", "", "", ""));
addNode(regionNode, null,
new Employee(10, "Western", "Calgary", "Ethan", "Smith",
"555-4562"));
addNode(regionNode, null,
new Employee(15, "Western", "Calgary", "Jacob", "Smith",
"555-4563"));
addNode(regionNode, null,
new Employee(50, "Western", "Victoria", "Daniel", "Willia
ms", "555-4572"));
addNode(regionNode, null,
new Employee(120, "Western", "Victoria", "Andrew", "Brown
", "555-4577"));

regionNode = addNode(rootNode, "Central", new Employee(2, "", "",


"", "", ""));
addNode(regionNode, null,
new Employee(140, "Central", "Kitchener", "Joshua", "Brow
n", "555-4579"));
addNode(regionNode, null,
new Employee(150, "Central", "Kitchener", "Christopher",
"Brown", "555-4580"));
addNode(regionNode, null,
new Employee(260, "Central", "Toronto", "Vincent", "Mille
r", "555-4591"));
addNode(regionNode, null,
new Employee(300, "Central", "Waterloo", "Matthew", "Davi
s", "555-4595"));

regionNode = addNode(rootNode, "Eastern", new Employee(3, "", "",


"", "", ""));
addNode(regionNode, null,
new Employee(340, "Eastern", "Ottawa", "Alexander", "Garc
ia", "555-4590"));
addNode(regionNode, null,
new Employee(360, "Eastern", "Ottawa", "Jacob", "Garcia",
"555-4592"));
addNode(regionNode, null,
new Employee(350, "Eastern", "Ottawa", "Benjamin", "Garci
a", "555-4591"));
addNode(regionNode, null,
new Employee(420, "Eastern", "Halifax", "Joshua", "Rodrig
uez", "555-4598"));

private DefaultMutableTreeNode findTreeNode(String nodeId) {


DefaultMutableTreeNode rootNode =
(DefaultMutableTreeNode) model.getRoot();
DefaultMutableTreeNode node;
EmployeeUserObject tmp;
Enumeration nodes = rootNode.depthFirstEnumeration();
while (nodes.hasMoreElements()) {
node = (DefaultMutableTreeNode) nodes.nextElement();
tmp = (EmployeeUserObject) node.getUserObject();
if (nodeId.equals(String.valueOf(tmp.getEmployee().getId())))
{
return node;
}
}
return null;
}
}

tree.jspx
// MPL License text (see http://www.mozilla.org/MPL/)

<ice:panelGroup styleClass="componentBox"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ice="http://www.icesoft.com/icefaces/component">

<ice:panelGroup styleClass="synopsisBox">
<ice:outputText value="#{msgs['page.tree.synopsis']}"/>
<ice:outputText value="#{msgs['page.tree.explanation']}"/>
</ice:panelGroup>

<ice:panelGroup styleClass="exampleBox">
<ice:tree id="tree"
value="#{treeController.model}"
var="node"
hideRootNode="false"
hideNavigation="false"
imageDir="#{styleBean.imageDirectory}">
<ice:treeNode>
<f:facet name="icon">
<ice:panelGroup
style="display: inline" >
<ice:graphicImage
value="/xmlhttp/css/#{styleBean.currentStyle}/
css-images/#{node.userObject.icon}"/>
</ice:panelGroup>
</f:facet>
<f:facet name="content">
<ice:panelGroup
styleClass="selectedNode#{node.userObject eq tree
Controller.selectedUserObject}"
style="display: inline" >
<ice:commandLink
actionListener="#{treeController.employeeNode
Selected}">
<f:param name="employeeId" value="#{node.userObje
ct.employee.id}" />
<ice:outputText id="TreeNode"
value="#{node.userObject.text}"/>
</ice:commandLink>
</ice:panelGroup>
</f:facet>
</ice:treeNode>
</ice:tree>
<br/>
<ice:commandButton id="moveUp"
value="#{msgs['page.tree.move.up.label']}"
disabled="#{treeController.moveUpDisabled}"
actionListener="#{treeController.moveUp}"/>
<ice:commandButton id="moveDown"
value="#{msgs['page.tree.move.down.label']}"
disabled="#{treeController.moveDownDisabled}"
actionListener="#{treeController.moveDown}"/>
</ice:panelGroup>

<!-- Server-side values-->


<ice:panelGroup
styleClass="exampleBox firstChildTitle backBeanChangeContainer">

<!-- backing bean title box -->


<ice:panelGroup styleClass="backBeanChangeTitle" style="width:350px;"
>
<ice:outputText value="#{msgs['page.global.serverFeedback']}"/>
</ice:panelGroup>

<!-- actual backing bean values. -->


<ice:panelGroup styleClass="backBeanChangeBody"
effect="#{treeController.valueChangeEffect}">
<ice:panelGroup style="margin: 0; padding: 0;width:350px;">
<ice:panelGrid cellpadding="0" cellspacing="3" columns="2
"
rendered="#{treeController.selectedUserObject.emp
loyee != null}">

<ice:outputLabel value="#{msgs['page.tree.path.label'
]}"/>
<ice:panelSeries id="icePnlSrs"
value="#{treeController.selectedTree
Path}"
var="treePath">
<ice:graphicImage
width="11" height="8"
style="margin-left:3px;margin-right:3px;"
value="/xmlhttp/css/rime/css-images/
bullet.gif" />
<ice:outputText id="treePath" value="#{treePath}"
/>
</ice:panelSeries>

<ice:outputLabel value="#{msgs['page.tree.employee.id
.label']}"/>
<ice:outputText id="employeeID"
value="#{treeController.selectedUserObject.em
ployee.id}"/>

<ice:outputLabel value="#{msgs['page.tree.employee.na
me.label']}"/>
<ice:outputText id="employeeName"
value="#{treeController.selectedUserObject.em
ployee.firstName} #{treeController.selectedUserObject.employee.lastName}"
/>

<ice:outputLabel value="#{msgs['page.tree.employee.ph
one.label']}"/>
<ice:outputText id="employeePhone"
value="#{treeController.selectedUserObject.em
ployee.phone}"/>
</ice:panelGrid>
</ice:panelGroup>
</ice:panelGroup>
</ice:panelGroup>

</ice:panelGroup>

tree

The tree component displays hierarchical data as a "tree" of branches and leaf nodes.
Optionally, the tree may also display navigation controls for the dynamic expansion and
collapse of branch nodes. Nodes may also support an action event when clicked that can be
used to respond to user click events.

The tree component uses a TreeModel defined in an application backing bean. The
TreeModel must contain a tree of DefaultMutableTreeNode instances. Each
DefaultMutableTreeNode instance encapsultes an IceUserObject. The IceUserObject is the
extension point for the application developer. The leaf attribute of the IceUserObject
defines the type of the tree node. By default the leaf attribute is false and tree nodes will be
folders unless the leaf attribute is set to true.

The tree component can be used in cases where a hierarchical data structure must be
viewed and navigated. It is typically used for menu-style applications, where the user
selects a tree node and the application responds with an action related to the selected node.

Example

The following code shows how to create a basic tree component:

<ice:tree
id="myTree"
value="#{treeBean.model}"
hideRootNode="false"
action="#{treeBean.treeSelectionAction}"
binding="#{treeBean.treeComponent}"
/>
Tag Summary

tag-name: <ice:tree>

tag-class: com.icesoft.faces.component.tree.TreeTag

component-class: com.icesoft.faces.component.tree.Tree

component-type: com.icesoft.faces.TreeView

component-family: com.icesoft.faces.TreeView

renderer-class: com.icesoft.faces.component.tree.TreeRenderer

renderer-type: com.icesoft.faces.View

ICEfaces Component Suite


Tag tree
The tree component displays hierarchical data as a "tree" of branches and leaf nodes.
Optionally, the tree may also display navigation controls for the dynamic expansion and
collapse of branch nodes. Tree navigation events are available so that an application can
respond to these events. Nodes may also support an action event that can be used to
respond to user click events.
The tree component can be used in cases where a hierarchical data structure must be
viewed and navigated. It is typically used for menu-style applications, where the user
selects a tree node and the application responds with an action related to the selected node.
In implementing a tree tag, the application developer must provide declare in the JSP a tree
tag and a single treeNode tag as a child of the tree tag. The tree tag should declare the
"value" attribute to be a value binding to a backing bean that will return an object that
implements the javax.swing.tree.TreeModel interface. The TreeModel must contain a tree
of DefaultMutableTreeNode instances. Each DefaultMutableTreeNode instance encapsultes
an IceUserObject. The IceUserObject is the extension point for the application developer. If
the IceUserObject does not provide sufficient state for representation of the tree's nodes,
then the application developer should extend the IceUserObject and add state as required to
their extension. When creating an IceUserObject, the DefaultMutableTreeNode wrapper
must be provided to the constructor. Then the node's state can be set to the attributes on the
IceUserObject including: text, a convenience field that will typically represent the text that
will be displayed somewhere in the content facet; expanded, whether the node is expanded
on its first rendering and until the user initiates a navigation event to change this value;
tooltip, the text that will appear in the tooltip that appears when the user hovers over a
node; leafIcon, the application-relative path to an image that will be used to represent this
node when it has no children (is a leaf), typically referred to in the icon facet;
branchExpandedIcon, the application-relative path to an image that will be used to
represent this node when it has children (is a branch) and is expanded, typically referred to
in the icon facet; branchContractedIcon, the application-relative path to an image that will
be used to represent this node when it has children (is a branch) and is not expanded,
typically referred to in the icon facet. The "binding" attribute can be defined so that the
application developer will have access to the Tree component in the application's backing
bean. The "var" attribute can be declared on the tree tag such that the treeNode tag's
children have access to the state of the TreeModel's node that it represents.
See the documentation for the treeNode tag for a description of supported facets.

Tag Information
Tag Class com.icesoft.faces.component.tree.TreeTag
TagExtraInfo Class None
Body Content JSP
Display Name None

Attributes
Require Reques
Name Type Description
d t-time
action false false java.lang.Stri MethodBinding
ng
representing the
application action to
invoke when this
component is activated by
the user. The expression
must evaluate to a either a
String or a public method
that takes no parameters,
and returns a String (the
logical outcome) which is
passed to the
NavigationHandler for
this application.
actionListener false false java.lang.Stri MethodBinding
ng
representing an action
listener method that will
be notified when this
component is activated by
the user. The expression
must evaluate to a public
method that takes an
ActionEvent parameter,
with a return type of void.
binding false false java.lang.Stri The value binding
ng
expression linking this
component to a property
in a backing bean
documentImage false false java.lang.Stri
ng

folderImage false false java.lang.Stri


ng

folderOpenImage false false java.lang.Stri


ng

hideNavigation false false java.lang.Stri Declares whether the


ng
navigation links will be
rendered. Valid values are
true and false.
hideRootNode false false java.lang.Stri Declares whether the root
ng
tree node will be rendered.
Valid values are true and
false.
id false false java.lang.Stri The component identifier
ng
for this component. This
value must be unique
within the closest parent
component that is a
naming container.
imageDir false false java.lang.Stri Set directory for location
ng
of the tree images.
immediate false false java.lang.Stri Flag indicating that this
ng
component's value must
be converted and
validated immediately
(that is, during Apply
Request Values phase),
rather than waiting until
Process Validations phase.
keyboardNavigationEna false false java.lang.Stri Enables keyboard support,
ng
bled default value is true.
navCloseTop false false java.lang.Stri No Description
ng

navOpenTop false false java.lang.Stri Name of the image that


ng
overrides the default
image.
navOpenTopNoSiblings false false java.lang.Stri Name of the image that
ng
overrides the default
image.
rendered false false java.lang.Stri Flag indicating whether or
ng
not this component should
be rendered (during
Render Response Phase),
or processed on any
subsequent form submit.
style false false java.lang.Stri CSS style(s) to be applied
ng
when this component is
rendered.
styleClass false false java.lang.Stri Space-separated list of
ng
CSS style class(es) to be
applied when this element
is rendered. This value
must be passed through as
the "class" attribute on
generated markup.

Defines the base class


name for all style classes.
Default value is iceTree

 iceTree

 iceTreeRow
value false false java.lang.Stri Sets the component's
ng
model value, which must
be a JSF value binding
expression to an
implementation of the
"javax.swing.tree.TreeMo
del" interface.
var false false java.lang.Stri Name of a request-scope
ng
attribute under which the
model data for the row
selected by the current
value of the "rowIndex"
property (i.e. also the
current value of the
"rowData" property) will
be exposed.

Variables
No Variables Defined.

Overview
Library Tag Help
FRAMES NO FRAMES All Tags

Output Generated by Tag Library Documentation Generator. Java, JSP, and JavaServer Pages are
trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries. Copyright
2002-4 Sun Microsystems, Inc. 4150 Network Circle Santa Clara, CA 95054, U.S.A. All Rights Reserved.

ICEfaces Component Suite


Tag treeNode
The treeNode tag provides the template that be applied in rendering each node in the
backing data model. The treeNode tag supports two facets: the icon facet and the content
facet. The icon facet is intended to contain a graphic image that will serve as the icon for
the node it represents. This image can be customized for each node, or default icons for leaf
nodes, expand branch nodes, and contracted branch nodes will be used. The content facet
can contain any collection of components. For each node in the tree's backing data model,
the child components of the two facets will be rendered with state retrieved from the data
model as configured in the JSP by the application developer.

Tag Information
Tag Class com.icesoft.faces.component.tree.TreeNodeTag
TagExtraInfo Class None
Body Content JSP
Display Name None

Attributes
Request-
Name Required Type Description
time
binding false false java.lang.String The value binding expression linking
this component to a property in a
backing bean
id false false java.lang.String The component identifier for this
component. This value must be
unique within the closest parent
component that is a naming
container.
rendered false false java.lang.String Flag indicating whether or not this
component should be rendered
(during Render Response Phase), or
processed on any subsequent form
submit.

Variables
No Variables Defined.

Overview
Library Tag Help
FRAMES NO FRAMES All Tags

Output Generated by Tag Library Documentation Generator. Java, JSP, and JavaServer Pages are
trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries. Copyright
2002-4 Sun Microsystems, Inc. 4150 Network Circle Santa Clara, CA 95054, U.S.A. All Rights Reserved.

You might also like