You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by muhibd23 <mu...@gmail.com> on 2012/11/15 23:03:20 UTC

Need help in trinidad tree component

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


-- 
View this message in context: http://old.nabble.com/Need-help-in-trinidad-tree-component-tp34684793p34684793.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


AW: Need help in trinidad tree component

Posted by "Dreher, Markus" <M....@dzbw.de>.
RequestContext.getCurrentInstance().addPartialTarget(tree)

-----Ursprüngliche Nachricht-----
Von: Muhibbul Chowdhury [mailto:muhibbul.chowdhury@gmail.com] 
Gesendet: Montag, 19. November 2012 18:46
An: Scott O'Bryan
Cc: MyFaces Discussion
Betreff: Re: Need help in trinidad tree component

Hi,
Thanks. Can you please send a sample code how to add trinidad component as a partial target?

On Mon, Nov 19, 2012 at 11:29 AM, Scott O'Bryan <da...@gmail.com> wrote:

> 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
>>
>>
>>
>
>


_______________________________________________________________________________

Abonnieren Sie unseren Infobrief und erfahren Sie regelmäßig die neuesten Nachrichten über unsere Lösungen, aktuellen Projekte und Entwicklungen. Melden sie sich an mit diesem Link http://www.datenzentrale.de/Info-Brief
_______________________________________________________________________________

Datenzentrale Baden-Württemberg, Anstalt des öffentlichen Rechts
Krailenshaldenstr. 44, 70469 Stuttgart 
Telefon (0711) 8108-20, Telefax (0711) 8108-21350
E-Mail info@dzbw.de, Internet www.datenzentrale.de
Vorstand: Karl Tramer (Vors.) und Joachim Kischlat, Vorsitzender des Verwaltungsrats: Gunter Czisch
USt-Id-Nr. DE147794223 
_______________________________________________________________________________



Re: Need help in trinidad tree component

Posted by Muhibbul Chowdhury <mu...@gmail.com>.
Hi Scott,
I solved that problem. It was basically java Script problem that has been
solved. However two more requirements:
1) I have to expand the tree by clicking the nodes.
2) When the web application will be first loaded, then the tree will be
expanded with only first level. That means:
If    A
      --->B
            ----->D
      ---->C
             ---->E

is a tree then my requirement is(When application is first loaded):
      A
       ----->B
       ------>C

I'm sure it can be done. Can u help me with some direction and sample code?

Thanks

On Mon, Nov 19, 2012 at 12:50 PM, Scott O'Bryan <da...@gmail.com> wrote:

> http://myfaces.apache.org/**trinidad/trinidad-api/apidocs/**
> org/apache/myfaces/trinidad/**context/RequestContext.html<http://myfaces.apache.org/trinidad/trinidad-api/apidocs/org/apache/myfaces/trinidad/context/RequestContext.html>
>
> Look up addPartialTarget.
>
> To do it declaratively you need to look in the tag doc under
> partialTriggers.
>
> Hope that helps.
>
> Scott
>
>
> On Mon Nov 19 10:45:45 2012, Muhibbul Chowdhury wrote:
>
>> Hi,
>> Thanks. Can you please send a sample code how to add trinidad
>> component as a partial target?
>>
>> On Mon, Nov 19, 2012 at 11:29 AM, Scott O'Bryan <darkarena@gmail.com
>> <ma...@gmail.com>> wrote:
>>
>>     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
>>
>>
>>
>>
>>
>>
>
>

Re: Need help in trinidad tree component

Posted by Scott O'Bryan <da...@gmail.com>.
http://myfaces.apache.org/trinidad/trinidad-api/apidocs/org/apache/myfaces/trinidad/context/RequestContext.html

Look up addPartialTarget.

To do it declaratively you need to look in the tag doc under 
partialTriggers.

Hope that helps.

Scott

On Mon Nov 19 10:45:45 2012, Muhibbul Chowdhury wrote:
> Hi,
> Thanks. Can you please send a sample code how to add trinidad
> component as a partial target?
>
> On Mon, Nov 19, 2012 at 11:29 AM, Scott O'Bryan <darkarena@gmail.com
> <ma...@gmail.com>> wrote:
>
>     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
>
>
>
>
>



Re: Need help in trinidad tree component

Posted by Muhibbul Chowdhury <mu...@gmail.com>.
Hi,
Thanks. Can you please send a sample code how to add trinidad component as
a partial target?

On Mon, Nov 19, 2012 at 11:29 AM, Scott O'Bryan <da...@gmail.com> wrote:

> 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
>>
>>
>>
>
>

Re: Need help in trinidad tree component

Posted by Scott O'Bryan <da...@gmail.com>.
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
>
>