The following is a complete example--with all sources included--of a
working tree in which the treeState is mananaged such that it can start
in the fully expanded state. To get it to start expanded the really
critical files are the 'enterprises.html' page (which needs to have
the KeyProvider specified (see below) and the
initializeEnterpriseTreeContentProvider
method, which has logic to force the tree to begin expanded.
The example consists of these files:
enterprises.page
enterprises.html
Enterprises.java
EnterpriseTreeContentProvider.java
Folder.java
Item.java
KeyProvider.java
-------------------------------------------------------------------------------------
enterprises.page
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE page-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 4.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
<page-specification class="com.tyco.web.pages.enterprise.Enterprises">
<description>Tree</description>
<property name="item" />
<bean name="evenOdd" class="org.apache.tapestry.bean.EvenOdd"/>
<component id="tree" type="tacos:Tree">
<binding name="value" value="item"/>
<binding name="nodeLinkAjax" value="ognl:false" />
<binding name="evenOdd" value="ognl:page.beans.evenOdd"
/>
<binding name="rowStyle" value="ognl:beans.evenOdd"/>
<binding name="state" value="enterpriseTreeState"/>
</component>
</page-specification>
-------------------------------------------------------------------------------------
from enterprises.html
Some of the stuff inside the tree
(the 'if' and 'else') are not
required except for my particular
application.
-------------------------------------------------------------------------------------
<table width="100%" class="body" border="0">
<TR>
<TD>
<div
jwcid="tree"
id="tree"
keyProvider="ognl:keyProvider"
contentProvider="ognl:contentProvider"
style="overflow: auto; width: auto; height: auto;">
<span jwcid="@If" condition="ognl:item.active" conditionValue="true">
<a
style="active"
href="#"
jwcid="@DirectLink"
listener="ognl:listeners.selectedEnterprise"
parameters="ognl:{item.keyId}">
<span jwcid="@Insert" value="ognl:item.name"
style="active"></span>
<span jwcid="@If" condition="ognl:item.routing"
conditionValue="!null">
(<span jwcid="@Insert" value="ognl:item.routing"></span>)
</span>
</a>
</span>
<span jwcid="@Else">
<span jwcid="@Insert" value="ognl:item.name"
style="inactive"></span>
<span jwcid="@If" condition="ognl:item.routing"
conditionValue="!null">
(<span jwcid="@Insert" value="ognl:item.routing"
style="inactive"></span>)
</span>
</span>
</div>
</TD>
</TR>
</TABLE>
-------------------------------------------------------------------------------------
Enterprises.java
-------------------------------------------------------------------------------------
package com.tyco.web.pages.enterprise;
import org.apache.tapestry.IRequestCycle;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import net.sf.tacos.model.IKeyProvider;
import net.sf.tacos.tree.ITreeManager;
import net.sf.tacos.tree.TreeManager;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.Persist;
import com.tyco.web.components.enterprise.EnterpriseTreeContentProvider;
import com.tyco.web.pages.common.ActivatePage;
import com.tyco.api.authorization.User;
import com.tyco.api.enterprise.Enterprise;
import com.tyco.api.enterprise.EnterpriseService;
import com.tyco.api.enterprise.EnterpriseServiceAccessor;
import com.tyco.api.util.ReturnResult;
public abstract class Enterprises
{
/**
* This class contains the common tree-related functionality.
*
* */
public abstract class EnterpriseTreeBase extends ActivatePage
{
@InjectState( "user" )
public abstract User getUser();
public abstract void setUser( User pUser );
@InjectState( "productID" )
public abstract Long getProductID();
@InjectState( "contentProvider" )
public abstract EnterpriseTreeContentProvider getContentProvider();
public abstract void setContentProvider( EnterpriseTreeContentProvider
pEnterpriseTreeContentProvider );
@InjectState( "enterpriseTreeState" )
public abstract Set<Long> getEnterpriseTreeState();
public abstract void setEnterpriseTreeState( Set pEnterpriseTreeState );
@Persist( "client" )
public abstract String getStatus();
public abstract void setStatus( String pStatus );
@Persist
public abstract Enterprise getEnterprise();
public abstract void setEnterprise(Enterprise pEnterprise);
@Persist
public abstract Enterprise getParentEnterprise();
public abstract void setParentEnterprise(Enterprise pParentEnterprise);
/**
* This statement will create the enterprise tree.
* This is critical for causing the tree to be
* expanded by default.
*/
public void initializeEnterpriseTreeContentProvider( User pUser )
{
EnterpriseTreeContentProvider contentProvider = new
EnterpriseTreeContentProvider();
contentProvider.setToggleActiveOnly( true );
contentProvider.resetTree( getUser() );
setContentProvider( contentProvider );
ITreeManager tm = this.getTreeManager();
tm.expandAll();
}
/**
* Gets the key provider
* @return
*/
public IKeyProvider getKeyProvider()
{
return KeyProvider.INSTANCE;
}
/**
* Makes all nodes visible
* @param cycle
*/
public void expandAll(IRequestCycle cycle)
{
getTreeManager().expandAll();
}
/**
* Collapses all nodes
* @param cycle
*/
public void collapseAll(IRequestCycle cycle)
{
getTreeManager().collapseAll();
}
/**
* The tree manager
* @return
*/
public ITreeManager getTreeManager()
{
return new TreeManager(
getEnterpriseTreeState(),
getContentProvider(),
getKeyProvider()
);
}
/**
* This listener method is called in response to
* a user clicking on one of the nodes of the tree.
* Doing so implies that the user wants to switch
* to that enterprise. For now, we are only switching
* the Name of the enterprise.
*/
public void selectedEnterprise()
{
IRequestCycle cycle = getRequestCycle();
Object[] parameters = (Object[]) cycle.getListenerParameters();
if( parameters != null )
{
Long entID = (Long) parameters[ 0 ];
if( entID != null )
{
EnterpriseService enterpriseService =
EnterpriseServiceAccessor.getService();
Enterprise enterprise = enterpriseService.getEnterpriseByID( entID );
User loggedInUser = this.getUser();
loggedInUser.setDefaultEnterpriseID( enterprise.getID() );
ReturnResult rr = getAuthorizationService().updateUser( loggedInUser
);
if( rr.isError() )
{
this.setValidationMessages( rr.getMessages() );
}
else
{
// Only udate the current enterprise if we were
// successful updating the user.
setCurrentEnterprise( enterprise );
cycle.activate(productPageMap.get(getProductID()));
}
}
}
}
}
}
-------------------------------------------------------------------------------------
EnterpriseTreeContentProvider.java
-------------------------------------------------------------------------------------
package com.tyco.web.components.enterprise;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import net.sf.tacos.model.ITreeContentProvider;
import com.tyco.api.authorization.AuthorizationService;
import com.tyco.api.authorization.AuthorizationServiceAccessor;
import com.tyco.api.authorization.User;
import com.tyco.api.enterprise.Enterprise;
import com.tyco.api.enterprise.EnterpriseLite;
import com.tyco.api.enterprise.EnterpriseService;
import com.tyco.api.enterprise.EnterpriseServiceAccessor;
import com.tyco.api.util.Identifiable;
/**
* This class gets a List of the current Enterprises available.
* These nodes are then presented within the TACOS Tree component.
*
*/
public class EnterpriseTreeContentProvider implements ITreeContentProvider
{
private boolean mToggleActiveOnly = false;
private User mUser = null;
private List<Item> roots = Collections.EMPTY_LIST;
public EnterpriseTreeContentProvider()
{
}
public void setUser( User pUser )
{
mUser = pUser;
}
public User getUser()
{
return mUser;
}
/** This method is only used by the test.*/
public List getTreeRoots()
{
return roots;
}
/** This method is only used by the test.*/
public void setTreeRoots( List pRoots )
{
roots = pRoots;
}
public User getUserByID( Long pUserID )
{
AuthorizationService auth = AuthorizationServiceAccessor.getService();
return auth.getUserByID( pUserID );
}
public void resetTree( User pUser )
{
User updatedUser = getUserByID( pUser.getID() );
setUser( updatedUser );
populateTree( updatedUser );
}
public void setToggleActiveOnly( boolean pToggleActiveOnly )
{
mToggleActiveOnly = pToggleActiveOnly;
}
public boolean getToggleActiveOnly()
{
return mToggleActiveOnly;
}
/** This method is here to allow us to check externally the state of the
roots.
* The tree will not be consumed through this. */
public boolean isTreeNull()
{
if( roots == null )
{
return true;
}
return false;
}
/** This method just executes the service
* call to return the List of Enterprises. */
protected List<Enterprise> getEnterprises()
{
EnterpriseService enterpriseService =
EnterpriseServiceAccessor.getService();
List<Enterprise> topLevelEnterps =
enterpriseService.getEnterprisesByParentID( null );
return topLevelEnterps;
}
private void populateTree( User pUser )
{
List<Enterprise> topLevelEnterps = getEnterprises();
roots = new ArrayList<Item>();
if( null == topLevelEnterps )
{
Folder root = new Folder( "No Enterprises", "No routing", "No id",
false );
roots.add( root );
}
else
{
Folder root = new Folder( "Enterprises", null, "", false );
addChildNodes( topLevelEnterps.iterator(), root, pUser );
roots.add( root );
}
}
/** This method will recursively add child nodes.
* This method supports and infinite level
* of recursion but the underlying widget
* [TACOS (Dojo) Tree] may not.
*
private void addChildNodes( Iterator pChildIterator, Folder pParentFolder,
User pUser )
{
/** Populate the User's Enterprises to limit
* visible Enterprises. */
List<EnterpriseLite> usersEnterprises = null;
if( pUser != null )
{
usersEnterprises = pUser.getEnterprises();
}
else
{
usersEnterprises = Collections.EMPTY_LIST;
}
/** Add, recursively, child nodes. */
while( pChildIterator.hasNext() )
{
Enterprise aChildEnterprise = (Enterprise) pChildIterator.next();
/** The following code will allow this method to respond
* to the 'Show Active/Inactive button. */
if( getToggleActiveOnly() && Identifiable.INACTIVE.equals(
aChildEnterprise.getStatus() ) )
{
continue; // continue out IF you encounter an INactive enterprise
}
/** The following code will allow us to exclude enterprises
* that this user does not have rights to see. */
boolean excludeEnterprise = true;
for( EnterpriseLite aUsersEnteprise : usersEnterprises )
{
Long idAllowed = aUsersEnteprise.getID();
Long idUnderTest = aChildEnterprise.getID();
if( idAllowed.longValue() == idUnderTest.longValue() )
{
excludeEnterprise = false;
break;
}
}
if( excludeEnterprise )
{
continue;
}
Iterator aGrandchildEnterprise = null;
List grandKids = aChildEnterprise.getChildrenEnterprise();
if( grandKids != null )
{
aGrandchildEnterprise = grandKids.iterator();
}
boolean status = false;
/** Special requirement: Although the two 'System' enterprises are
active,
* users should not be able to log into them. */
if( Identifiable.ACTIVE.equals( aChildEnterprise.getStatus() )
&& !( aChildEnterprise.getID().equals( new Long( 1 ) )
|| aChildEnterprise.getID().equals( new Long( 2 ) ) ) )
{
status = true;
}
else
{
status = false;
}
/** If there ARE children, we must add a Folder. */
if( aGrandchildEnterprise != null && aGrandchildEnterprise.hasNext() )
{
Folder childFolder = pParentFolder.folder(
aChildEnterprise.getName(),
aChildEnterprise.getRoutingID(),
aChildEnterprise.getID().toString(),
status );
addChildNodes( aGrandchildEnterprise, childFolder, pUser );
}
else
{
/** If there are NO children, we must add Items. */
pParentFolder.item(
aChildEnterprise.getName(),
aChildEnterprise.getRoutingID(),
aChildEnterprise.getID().toString(),
status
);
}
}
}
/**
*
* [EMAIL PROTECTED]
*/
public boolean hasChildren( Object parentElement )
{
if (parentElement instanceof Folder)
{
Folder folder = (Folder)parentElement;
return !(folder.getFolders().isEmpty() &&
folder.getItems().isEmpty());
}
return false;
}
/**
*
* [EMAIL PROTECTED]
*/
public List getElements()
{
if( roots == null )
{
populateTree( getUser() );
}
return roots;
}
/**
*
* [EMAIL PROTECTED]
*/
public Collection getChildren(Object parentElement)
{
if (parentElement instanceof Folder)
{
Folder folder = (Folder)parentElement;
List<Item> l = new ArrayList<Item>();
l.addAll( folder.getFolders() );
l.addAll( folder.getItems() );
return l;
}
return Collections.EMPTY_LIST;
}
/**
*
* [EMAIL PROTECTED]
*/
public Object getParent(Object childElement)
{
return ((Item)childElement).getParent();
}
}
-------------------------------------------------------------------------------------
Folder.java
-------------------------------------------------------------------------------------
package com.tyco.web.components.enterprise;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class Folder extends Item implements java.io.Serializable
{
/** serialVersionUID */
private static final long serialVersionUID = 7097320612652218327L;
private final List<Folder> mFolders = new ArrayList<Folder>();
private final List<Item> mItems = new ArrayList<Item>();
/**
* Creates a new folder
* @param name
* @param routing
*/
public Folder( String pName, String pRouting, String pId, boolean pActive )
{
super( pName, pRouting, pId, pActive );
}
/**
* Create new folder with parent
* @param parent
* @param name
* @param routing
*/
public Folder( Folder pParent, String pName, String pRouting, String pId,
boolean pActive )
{
super( pParent, pName, pRouting, pId, pActive );
}
/**
* New folder with name
* @param name
* @param routing
* @return
*/
public Folder folder( String pName, String pRouting, String pId, boolean
pActive )
{
Folder f = new Folder( this, pName, pRouting, pId, pActive );
mFolders.add( f );
return f;
}
/**
* Creates folder item
* @param name
* @param routing
* @return
*/
public Folder item( String pName, String pRouting, String pId, boolean
pActive )
{
Item i = new Item( this, pName, pRouting, pId, pActive );
mItems.add(i);
return this;
}
/**
* Returns list of folders
* @return
*/
public List<Folder> getFolders()
{
return mFolders;
}
/**
* List of items
* @return
*/
public List<Item> getItems()
{
return mItems;
}
/**
* Debug string
* [EMAIL PROTECTED]
*/
public String toString()
{
return "Folder " + getName();
}
}
-------------------------------------------------------------------------------------
Item.java
-------------------------------------------------------------------------------------
package com.tyco.web.components.enterprise;
/**
* This class forms the basis of the nodes in the
* TACOS tree. It is subclassed by Folder.
*/
public class Item implements java.io.Serializable
{
/** serialVersionUID */
private static final long serialVersionUID = -6014868077202988099L;
private final Folder mParent;
private final String mName;
private final String mRouting;
private final String mId;
private final boolean mActive;
private boolean mChecked = false;
private long mKeyId;
/** These two constants are used on the page to alternately
* change the color of the links. */
public long getKeyId()
{
if( mId.equals( "" ) )
{
return 0;
}
else
{
Integer keyInteger = Integer.parseInt( mId );
return keyInteger.intValue();
}
}
public void setKeyId( long pKeyId )
{
mKeyId = pKeyId;
}
public Item()
{
this( null, null, null, null, false );
}
/**
* Creates an item
* @param name
* @param routing
*/
public Item( String pName, String pRouting,
String pId, boolean pActive )
{
this( null, pName, pRouting, pId, pActive );
}
/**
* Creates an item with a parent
* @param parent
* @param name
*/
public Item( Folder pParent, String pName,
String pRouting, String pId, boolean pActive )
{
this.mParent = pParent;
this.mName = pName;
this.mRouting = pRouting;
this.mId = pId;
this.mActive = pActive;
}
/**
* Name of item
* @return
*/
public String getName()
{
return mName;
}
/**
* Routing
* @return
*/
public String getRouting()
{
return mRouting;
}
/**
* Id of this row
* @return
*/
public String getId()
{
return mId;
}
/**
* Active flag--whether or not to grey out the item.
* @return
*/
public boolean getActive()
{
return mActive;
}
/**
* Items parent
* @return
*/
public Folder getParent()
{
return mParent;
}
/** This variable is NOT final,
* unlike the rest in this class,
* so that it can be updated. */
public boolean isChecked()
{
return mChecked;
}
public void setChecked( boolean pChecked )
{
mChecked = pChecked;
}
/**
*
* [EMAIL PROTECTED]
*/
public String toString()
{
return "Item name=" + getName()
+ ", routing="
+ getRouting()
+ ", id="
+ getId()
+ ", active="
+ getActive()
+ ",checked="
+ isChecked();
}
}
-------------------------------------------------------------------------------------
KeyProvider.java
-------------------------------------------------------------------------------------
package com.tyco.web.pages.enterprise;
import java.io.Serializable;
import net.sf.tacos.model.IKeyProvider;
import com.ingenix.freya.web.components.enterprise.Item;
public class KeyProvider implements IKeyProvider
{
public final static KeyProvider INSTANCE = new KeyProvider();
/**
*
* [EMAIL PROTECTED]
*/
public Serializable getKey(Object value)
{
Item item = (Item) value;
return item.getKeyId();
}
}
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Tacos-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tacos-devel