I don't have time to look through the entire thing, but I think if you
add your tree component as a partial target, it might give you what you
want. I believe your model is being updated properly but I'm not sure
the changes to the component are being sent. This is just a quick
suggestion based on the symptom.
On Thu Nov 15 15:03:20 2012, muhibd23 wrote:
Hello,
I am working on a web application. Everything is working fine. I just need a
simple help. I have populated a tree using JSF/trinidad tree component.
However, I want to expand the tree branches by clicking (+) sign and
collapse the tree branches by clicking (-) sign. It's not working properly.
What's happening is that, I have to click the (+) sign and then click the
root node to expand the next branch. Similarly, While collapsing, I have to
click the (-) sign and then click the child node to collapse it, but I want
to expand/ collapse nodes by clicking (+) or (-) sign.
My jsp side code:
<trh:cellFormat valign="top">
<tr:panelHeader>
<script type="text/javascript"
src="CollapsibleLists.js"></script>
<tr:tree var="node"
value="#{fileTreeHandler.treeModel}">
<f:facet name="nodeStamp">
<tr:panelGroupLayout>
<tr:commandLink text =
"#{node.description}"
actionListener="#{fileTreeHandler.downloadFile}"/>
</tr:panelGroupLayout>
</f:facet>
</tr:tree>
</tr:panelHeader>
</trh:cellFormat>
Here is the filetreehandler code:
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.log4j.Logger;
import org.apache.myfaces.trinidad.component.core.data.CoreTree;
import org.apache.myfaces.trinidad.event.FocusEvent;
import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
import org.apache.myfaces.trinidad.model.RowKeySet;
import org.apache.myfaces.trinidad.model.TreeModel;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* A Simple tree model used to create a graphical tree representation for a
* given directory.
*
* @author Ric Smith, Oracle Corp.
*/
@SessionScoped
public class FileTreeHandler implements Serializable {
/** Apache tree model. */
private TreeModel treeModel;
private static final long serialVersionUID = 1L;
/** Was a node found. */
private boolean foundDirectory = false;
//private RowKeySet disclosedEntries;
private CoreTree tree;
private Object clickedNodeRowKey;
/** Logging for the class. */
private Logger logger = Logger.getLogger(this.getClass());
/**
* Constructor.
* Reads the given directory.
* Sets the treeModel nodes for all files and directories in the
* input directory.
*
* @param baseDirectory
*/
//RowKeySetImpl rowKeySet = new RowKeySetImpl();
public FileTreeHandler(String baseDirectory) {
logger.debug("In constructor");
List<FileNode> nodes = new ArrayList<FileNode>();
Authentication authentic =
SecurityContextHolder.getContext().getAuthentication();
String username = authentic.getName(); // Storing logged in username
into String
String dir = baseDirectory + "/" + username;
FileNode rootNode = buildFileTree(dir);
if (rootNode.getChildCount() == 0) {
setFoundDirectory(false);
} else {
setFoundDirectory(true);
}
nodes.add(rootNode);
treeModel = new ChildPropertyTreeModel(nodes, "children") {
public boolean isContainer() {
return ((FileNode) getRowData()).getChildCount() > 0;
}
};
//UIXHierarchy tree = (UIXHierarchy)treeModel.getRowData();
// RowKeySet disclosedEntries = new RowKeySetTreeImpl();
//disclosedEntries.setCollectionModel(treeModel);
}
/**
* Simple action event used to init the download of a file within the
tree.
*
*
* @param evt
* @throws IOException
*/
public void downloadFile(String evt) throws IOException {
FileNode selectedNode = ((FileNode) treeModel.getRowData());
if (!selectedNode.isDir()) {
File selectedFile = selectedNode.getFile();
downloadFile(selectedFile);
}
}
/**
* A helper method to setup the current session for the download.
*
* @param file
* @throws IOException
*/
private static void downloadFile(File file) throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext extContext = facesContext.getExternalContext();
Long length = file.length();
HttpServletResponse response = (HttpServletResponse) extContext
.getResponse();
//response.setContentType("applicatiion/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=\""
+ file.getName() + "\"");
response.setContentLength((int) length.intValue());
InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[4096];
int count;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.flush();
out.close();
facesContext.responseComplete();
}
/**
* Generates a tree of FileNodes for a given dir.
*
* @param dirpath
* @return
*/
private static FileNode buildFileTree(String dirpath) {
File root = new File(dirpath);
return visitAllDirsAndFiles(root);
}
/* public void handleRowDisclosure(RowDisclosureEvent rowDisclosureEvent)
throws Exception {
Object rowKey = null;
UIXHierarchy rowData = null;
String viewDefName = null;
TreeModel treemodel = (TreeModel)rowDisclosureEvent.getSource();
RowKeySet rks = rowDisclosureEvent.getAddedSet();
if (rks != null) {
int setSize = rks.size();
if (setSize > 1) {
throw new Exception("Unexpected multiple row disclosure row
sets");
}
if (setSize == 0)
return;
rowKey = rks.iterator().next();
treemodel.setRowKey(rowKey);
rowData = (UIXHierarchy)treemodel.getRowData();
if (rowData.getContainerRowKey() != null) {
viewDefName =
rowData.getContainerRowKey().g;
}
}
} */
/**
* Recurses over a given directory.
*
* @param dir
* @return
*/
private static FileNode visitAllDirsAndFiles(File dir) {
FileNode parentNode = process(dir);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
FileNode childNode = visitAllDirsAndFiles(new File(dir,
children[i]));
parentNode.getChildren().add(childNode);
}
}
return parentNode;
}
/**
* Creates a file node for a given file. Any file processing should be
done
* here.
*
* @param dir
* @return FileNode
*/
public static FileNode process(File dir) {
FileNode node = new FileNode(dir);
return node;
}
public void clickTree(FocusEvent event)
{
RowKeySet rks = getTree().getDisclosedRowKeys();
rks.invert();
List<List> clickedNodePath = (List<List>) clickedNodeRowKey;
Iterator i = getTree().getDisclosedRowKeys().iterator();
boolean closedNode = false;
while (i.hasNext()) {
List openNodePath = (List) i.next();
if (openNodePath.equals(clickedNodeRowKey)) {
rks.remove(clickedNodePath);
closedNode = true;
}
}
// open clicked node
if (!closedNode) {
rks.add(clickedNodePath);
}
}
/*public void handleRowDisclosure(RowDisclosureEvent event)
{
RowKeySet added = event.getAddedSet();
RowKeySet removed = event.getRemovedSet();
if(disclosedEntries == null)
{
disclosedEntries = added;
}
else
{
if(!added.isEmpty())
{
disclosedEntries.addAll(added);
}
if(!removed.isEmpty())
{
disclosedEntries.removeAll(removed);
}
}
}*/
public void setTreeModel(TreeModel treeModel) {
this.treeModel = treeModel;
}
public TreeModel getTreeModel() {
return treeModel;
}
public boolean getFoundDirectory() {
return foundDirectory;
}
public void setFoundDirectory(boolean foundDirectory) {
this.foundDirectory = foundDirectory;
}
public void setTree(CoreTree tree) {
this.tree = tree;
}
public CoreTree getTree() {
return tree;
}
public void setClickedNodeRowKey(Object clickedNodeRowKey) {
this.clickedNodeRowKey = clickedNodeRowKey;
}
public Object getClickedNodeRowKey() {
return clickedNodeRowKey;
}
/* public RowKeySetImpl getRowKeySet()
{
return rowKeySet;
}
public void setRowKeySet(RowKeySetImpl rowKeySet)
{
this.rowKeySet = rowKeySet;
} */
}
However, I'm using Spring here. I know I'm doing something wrong, but no
idea. Please help.
Thanks