Saturday, June 21, 2014

Create tree menu structure and control page navigation in ADF

If we want to create tree menu structure, try following steps.

Steps:
1. Create TreeItem Object
2. Create Menu Bean class and add Menu nodes.
3. Add af:tree component to page and link to tree model.
4. Define navigation action

Tree Item Object
----------------------------------------------
import java.util.List;

public class TreeItem {
    private String text, action;
    private List<TreeItem> children;

    public TreeItem() {
        super();
    }

    public TreeItem(String text, String action) {
        super();
        this.text = text;
        this.action = action;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getAction() {
        return action;
    }

    public void setChildren(List<TreeItem> children) {
        this.children = children;
    }

    public List<TreeItem> getChildren() {
        return children;
    }
}


Menu Bean
--------------
import java.util.ArrayList;

import java.util.List;

import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
import org.apache.myfaces.trinidad.model.TreeModel;

public class MenuBean {
    private Object instance = null;
    private transient TreeModel model = null;
    private ArrayList<TreeItem> root;

    public MenuBean() {
        root = new ArrayList<TreeItem>();
        TreeItem node1 = new TreeItem("Menu Header", "node1");
        root.add(node1);

        ArrayList<TreeItem> node1Children = new ArrayList<TreeItem>();
        TreeItem node1Child1 = new TreeItem("MenuChild1", "child1");
        node1Children.add(node1Child1);
        TreeItem node1Child2 = new TreeItem("MenuChild2", "child2");
        node1Children.add(node1Child2);
        TreeItem node1Child3 = new TreeItem("MenuChild2", "child3");
        node1Children.add(node1Child3);

        node1.setChildren(node1Children);

        setListInstance(root);
    }

    public TreeModel getModel() {
        if (model == null)
            model = new ChildPropertyTreeModel(instance, "children");
        return model;
    }

    public void setListInstance(List instance) {
        this.instance = instance;
        model = null;
    }
}


Page:
--------------
          <f:facet name="LeftFacet">
            <af:panelBox text="PanelBox1" id="pb1" showHeader="never"
                         showDisclosure="false">
              <af:tree value="#{menuBean.model}" var="node"
                       initiallyExpanded="true" rendered="#{sessionScope.isUserValid}">
                <f:facet name="nodeStamp">
                  <af:commandLink text="#{node.text}"
                                  actionListener="#{appBean.navNodeClicked}"
                                  id="ct1" partialSubmit="true"
                                  inlineStyle="font-weight:bold; font-size:9.0pt;"/>
                </f:facet>
              </af:tree>
            </af:panelBox>
          </f:facet>

Action:
-------------
    public void navNodeClicked(ActionEvent actionEvent) {
        // Add event code here...
        String linkId = ((RichCommandLink)actionEvent.getSource()).getText();

        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        Map<String, Object> pageFlowScope = adfFacesContext.getPageFlowScope();

        if (linkId.equals("MenuChild1")) {
            pageFlowScope.put("targetFlow", "child1");
        } else if (linkId.equals("MenuChild2")) {
            pageFlowScope.put("targetFlow", "child2");
        } else if (linkId.equals("MenuChild3")) {
            pageFlowScope.put("targetFlow", "child3");
        }
    }

Region in Page:
---------------
          <f:facet name="RightFacet">
            <af:group id="g1">
              <af:panelBox text="PanelBox1" id="pb2" showHeader="never"
                           showDisclosure="false">
                <af:region value="#{bindings.roottaskflow1.regionModel}"
                           binding="#{appBean.rightRegion}" id="r1"
                           partialTriggers="::pb1"
                           rendered="#{sessionScope.isUserValid}"/>
              </af:panelBox>
            </af:group>
          </f:facet>

Taskflow Parameter Definition in Page definition file:
--------------------------------------
  <executables>
    <variableIterator id="variables"/>
    <taskFlow id="roottaskflow1"
              taskFlowId="/WEB-INF/root-taskflow.xml#root-taskflow"
              activation="deferred"
              xmlns="http://xmlns.oracle.com/adf/controller/binding"
              Refresh="ifNeeded">
      <parameters>
        <parameter id="targetFlow" value="#{pageFlowScope.targetFlow}"/>
      </parameters>
    </taskFlow>
  </executables>

Taskflow Definition:
---------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
  <task-flow-definition id="root-taskflow">
    <default-activity id="__1">navigation</default-activity>
    <input-parameter-definition id="__10">
      <name id="__11">targetFlow</name>
      <value>#{pageFlowScope.targetFlow}</value>
      <class>java.lang.String</class>
    </input-parameter-definition>
    <router id="navigation">
     <case>
        <expression>#{pageFlowScope.targetFlow eq 'child1'}</expression>
        <outcome id="__12">child1</outcome>
      </case>
      <case>
        <expression>#{pageFlowScope.targetFlow eq 'child2'}</expression>
        <outcome id="__13">child2</outcome>
      </case>
      <case>
        <expression>#{pageFlowScope.targetFlow eq 'child3'}</expression>
        <outcome id="__17">child3</outcome>
      </case>    
      <default-outcome>child1</default-outcome>  
    </router>
    <task-flow-call id="child1-taskflow">
      <task-flow-reference>
        <document>/WEB-INF/child1-taskflow.xml</document>
        <id>child1-taskflow</id>
      </task-flow-reference>
    </task-flow-call>
    <task-flow-call id="child2-taskflow">
      <task-flow-reference>
        <document>/WEB-INF/child2-taskflow.xml</document>
        <id>child2-taskflow</id>
      </task-flow-reference>
    </task-flow-call>
    <task-flow-call id="child3-taskflow">
      <task-flow-reference>
        <document>/WEB-INF/child3-taskflow.xml</document>
        <id>child3-taskflow</id>
      </task-flow-reference>
    </task-flow-call>
    <control-flow-rule id="__2">
      <from-activity-id id="__3">navigation</from-activity-id>
      <control-flow-case id="__5">
        <from-outcome id="__6">child1</from-outcome>
        <to-activity-id id="__4">child1-taskflow</to-activity-id>
      </control-flow-case>
      <control-flow-case id="__7">
        <from-outcome id="__9">child2</from-outcome>
        <to-activity-id id="__8">child2-taskflow</to-activity-id>
      </control-flow-case>
      <control-flow-case id="__35">
        <from-outcome id="__33">child3</from-outcome>
        <to-activity-id id="__34">child3-taskflow</to-activity-id>
      </control-flow-case>
    </control-flow-rule>
    <use-page-fragments/>
  </task-flow-definition>
</adfc-config>

No comments:

Post a Comment

Provide your thoughts !