1 Reply Latest reply on Jan 7, 2014 2:30 PM by Edilmar Alves

    Is it possible to select more than one node of a rich:tree (4.3.4)?

    Edilmar Alves Expert

      Hi,

       

      I tried to select more nodes but it didn't work, then full tree was selected after select the first node.

        • 1. Re: Is it possible to select more than one node of a rich:tree (4.3.4)?
          Edilmar Alves Expert

          I made a mix of two 4.x showcase samples (rich:tree and treeAdaptors) because I need to import a XML file to the tree. Then, I used the rich:treeModelRecursiveAdaptor to fill recursively the tree. All works fine. But I also need to allow the user to select one or more fields from the generated XML tree, and to this, I used the rich:tree record music sample. Now, I get to select one, and just one group field from XML, but:

          1) I didn't get to select many fields, just the original problem of this message

          2) I didn't get to call the selectionChangeListener method for leaves of the tree (final field contents)

           

          Here is a sample JSF/RF code:

           

            <h:form>

              <rich:tree id="campos" var="item"

                        toggleType="client"

                        selectionType="ajax">

                <rich:treeSelectionChangeListener listener="#{emissaoCCe.selectionChanged}"/>

                <rich:treeModelRecursiveAdaptor roots="#{emissaoCCe.raizArqXMLAss}" nodes="#{item.grupos}">

                  <rich:treeNode expanded="true">

                    <h:outputText value="#{item.tituloGrupo}"/>

                  </rich:treeNode>

                  <rich:treeModelAdaptor nodes="#{item.campos}">

                    <rich:treeNode>

                      <h:outputText value="#{item.tituloCampo}"/>

                    </rich:treeNode>

                  </rich:treeModelAdaptor>

                </rich:treeModelRecursiveAdaptor>

              </rich:tree>

            </h:form>

           

            <a4j:outputPanel ajaxRendered="true" layout="block">

              <rich:panel header="Current Selection">

                <h:outputText value="Name:" />

                <h:outputText value="#{emissaoCCe.s}" />

              </rich:panel>

            </a4j:outputPanel>

           

          The bean:

           

          @Named("emissaoCCe")

          @ConversationScoped

          public class EmissaoCCeController extends GenericCadController<ConhecimentoEv> implements Serializable {

            @Inject ConhecimentoEvDAO daoI;

            ...

            private EmissaoCCeCampos currentSelection = null;

            public EmissaoCCeCampos getCurrentSelection() {

              return currentSelection;

            }

           

            public void setCurrentSelection(EmissaoCCeCampos currentSelection) {

              this.currentSelection = currentSelection;

            }

           

            public void selectionChanged(TreeSelectionChangeEvent selectionChangeEvent) {

              try {

                // considering only single selection

                List<Object> selection = new ArrayList<>(selectionChangeEvent.getNewSelection());

                UITree tree = (UITree) selectionChangeEvent.getSource();

                Object storedKey = tree.getRowKey();

           

                Object currentSelectionKey = selection.get(0);

                tree.setRowKey(currentSelectionKey);

                currentSelection = (EmissaoCCeCampos) tree.getRowData();

                if (currentSelection.isCampo())

                  s = currentSelection.getTituloCampo(); // THIS DOESNT WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                else

                  s = currentSelection.getTituloGrupo();

               

                tree.setRowKey(storedKey);

              } catch (Exception e) {

                e.printStackTrace();

              }

            }

           

            private String s;

           

            public String getS() {

              return s;

            }

           

            public void setS(String s) {

              this.s = s;

            }

            ...

          }

          And the class of nodes:

          public class EmissaoCCeCampos {

            private Node node;

            private List<EmissaoCCeCampos> grupos;

            private List<EmissaoCCeCampos> campos;

            private String tituloCampo;

            public EmissaoCCeCampos(Node node) {

              this.node = node;

              grupos = null;

              campos = null;

              this.tituloCampo = null;

            }

            public EmissaoCCeCampos(String tituloCampo) {

              this.node = null;

              grupos = null;

              campos = null;

              this.tituloCampo = tituloCampo;

            }

            public String getTituloGrupo() {

              return node.getNodeName();

            }

            public String getTituloCampo() {

              return tituloCampo;

            }

            public synchronized List<EmissaoCCeCampos> getGrupos() {

              if (grupos == null) {

                NodeList nodeList = node.getChildNodes();

                int len = nodeList.getLength();

                grupos = new ArrayList<>();

                campos = new ArrayList<>();

                int i;

                for (i = 0; i < len; i++) {

                  Node nodeAtual = nodeList.item(i);

                  if (nodeAtual.getNodeName().equals("Signature")) {

                    continue;

                  }

                  NodeList childrenNodeAtual = nodeAtual.getChildNodes();

                  int lenAtual = childrenNodeAtual.getLength();

                  if (lenAtual == 1 && childrenNodeAtual.item(0).getNodeName().equals("#text")) {

                    Node childNodeAtual = childrenNodeAtual.item(0);

                    campos.add(new EmissaoCCeCampos(nodeAtual.getNodeName() + "=" + childNodeAtual.getNodeValue()));

                  }

                  else

                    grupos.add(new EmissaoCCeCampos(nodeAtual));

                }

              }

              return grupos;

            }

            public synchronized List<EmissaoCCeCampos> getCampos() {

              return campos;

            }

            public boolean isGrupo() {

              return tituloCampo == null;

            }

            public boolean isCampo() {

              return tituloCampo != null;

            }

          }