cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Rauscher <...@blue-elephant-systems.com>
Subject Re: Help with tree widget
Date Fri, 11 Jul 2008 15:09:17 GMT
Hi Peter,

see below for a sample on how it works in 2.1.11 - might work as well on
2.2 or at least give you some pointers.

model:

<?xml version="1.0"?>
<fd:form
  xmlns:fd="http://apache.org/cocoon/forms/1.0#definition">

  <fd:widgets>

    <!--
      <fd:tree id="myTree">
        <fd:tree-model type="java"
class="org.apache.cocoon.forms.formmodel.tree.DefaultTreeModel$Sample"/>
      </fd:tree>
    -->

    <fd:tree id="myTree2">
      <fd:on-create>
        <fd:javascript>
         
importClass(Packages.org.apache.cocoon.forms.formmodel.tree.DefaultTreeModel);

          var node1 = new DefaultTreeModel.DefaultTreeNode("node 1")
          var node2 = new DefaultTreeModel.DefaultTreeNode("node 2")
          var root = new DefaultTreeModel.DefaultTreeNode("root")
          root.add("node1", node1);
          node1.add("node2", node2);
          var treeModel = new DefaultTreeModel(root);

          widget.setModel(treeModel);
        </fd:javascript>
      </fd:on-create>

    </fd:tree>
  </fd:widgets>
</fd:form>
   
template:

<?xml version="1.0"?>
<html xmlns:ft="http://apache.org/cocoon/forms/1.0#template"
      xmlns:fi="http://apache.org/cocoon/forms/1.0#instance"
      xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
      xmlns:i18n="http://apache.org/cocoon/i18n/2.1">

  <jx:import
uri="resource://org/apache/cocoon/forms/generation/jx-macros.xml"/>

  <jx:macro name="toggle-icon">
      <jx:choose>
        <jx:when test="${treeNode.isLeaf()}">
          <img
src="resources/forms/img/tree/win/${treeNode.iconType}.gif" border="0"/>
        </jx:when>
        <jx:otherwise>
           <a href="#" onclick="return
TreeToggleCollapse('${widget.fullName}', '${treeNode.path}')"><img
             src="resources/forms/img/tree/win/${treeNode.iconType}.gif"
border="0"/></a>
        </jx:otherwise>
      </jx:choose>
  </jx:macro>
 


  <head>
    <title>test</title>
  </head>

  <body>

    <script language="JavaScript">
      function TreeAction(id, act, path) {
        var elt = document.getElementById(id);
        if (!elt) {
            alert("Error: cannot find element with id '" + id + "'");
            return;
        }
        var form = forms_getForm(elt);
        var actInput = id + ":action";
        var pathInput = id + ":path";
        form[actInput].value = act
        form[pathInput].value = path;
        forms_submitForm(elt, id);
        // Reset fields (this form may be reposted later when in Ajax mode)
        form[actInput].value = "";
        form[pathInput].value = "";
        return false;
      }
      function TreeToggleCollapse(id, path) {
        return TreeAction(id, "toggle-collapse", path);
      }

      function TreeToggleSelect(id, path) {
        return TreeAction(id, "toggle-select", path);
      }

    </script>

    <ft:form-template action="#{$continuation/id}.continue"
method="POST" ajax="true">
      Mine: <br/>
      <ft:tree id="myTree2">
        <div>
          <ft:tree-nodes>
            <div style="margin-left:
15px"><toggle-icon/>${treeNode.node.data}
              <ft:tree-children/>
            </div>
          </ft:tree-nodes>
        </div>
      </ft:tree>
  </ft:form-template>
  </body>
</html>

Flow:

cocoon.load("resource://org/apache/cocoon/forms/flow/javascript/Form.js");

function test()
{
  var documentURI = cocoon.parameters["documentURI"];
  //var sourceXml = loadDocument(documentURI);
  var definitionURI = cocoon.parameters["definitionURI"];
  var bindingURI = cocoon.parameters["bindingURI"];
  var saveURI = cocoon.parameters["saveURI"];

  var document = loadDocument(documentURI);

  var form = new Form(definitionURI);
  form.createBinding(bindingURI);
  form.load(document);

  form.showForm("test-display");

  form.save(document);
  saveDocument(document, saveURI);
  cocoon.sendPage("test-success");
}

function loadDocument(uri) {
  var parser = null;
  var source = null;
  var resolver = null;
  try {
    parser =
cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
    resolver =
cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
    source = resolver.resolveURI(uri);
    var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
    is.setSystemId(source.getURI());
    return parser.parseDocument(is);
  }
  finally {
    if (source != null)
      resolver.release(source);
    cocoon.releaseComponent(parser);
    cocoon.releaseComponent(resolver);
  }
}

function saveDocument(document, uri) {
    var source = null;
    var resolver = null;
    var outputStream = null;
    try {
        resolver =
cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
        source = resolver.resolveURI(uri);

        var tf =
Packages.javax.xml.transform.TransformerFactory.newInstance();

        if (source instanceof
Packages.org.apache.excalibur.source.ModifiableSource
            &&
tf.getFeature(Packages.javax.xml.transform.sax.SAXTransformerFactory.FEATURE))
{

            outputStream = source.getOutputStream();
            var transformerHandler = tf.newTransformerHandler();
            var transformer = transformerHandler.getTransformer();
           
transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT,
"true");
           
transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD,
"xml");
            transformerHandler.setResult(new
Packages.javax.xml.transform.stream.StreamResult(outputStream));

            var streamer = new
Packages.org.apache.cocoon.xml.dom.DOMStreamer(transformerHandler);
            streamer.stream(document);
        } else {
            throw new
Packages.org.apache.cocoon.ProcessingException("Cannot write to source "
+ uri);
        }
    } finally {
        if (source != null)
            resolver.release(source);
        cocoon.releaseComponent(resolver);
        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (error) {
                cocoon.log.error("Could not flush/close outputstream: "
+ error);
            }
        }
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message