8 Replies Latest reply on Oct 16, 2009 6:16 AM by Julia R

    Rich Faces Tree Cast Problem.

    Julia R Newbie

      Hi!

      I try to run example http://livedemo.exadel.com/richfaces-demo/richfaces/tree.jsf?tab=model&cid=2340784


      I get following error.


      java.lang.ClassCastException: org.richfaces.model.TreeNodeImpl cannot be cast to javax.swing.tree.TreeNode


      I have no idea why it tryes to cast to javax.swing.tree.
      I use jboss 4.2.3 and rich faces 3.3.1.GA

      Can anyone help me?

      P.S. Sorry. I am a newbie and if you need some extra information please let me know

        • 1. Re: Rich Faces Tree Cast Problem.
          Nick Belaevski Master

          Hi,

          What's bean code?

          • 2. Re: Rich Faces Tree Cast Problem.
            Julia R Newbie

            Just like in example :)

            /**
             * License Agreement.
             *
             * JBoss RichFaces - Ajax4jsf Component Library
             *
             * Copyright (C) 2007 Exadel, Inc.
             *
             * This library is free software; you can redistribute it and/or
             * modify it under the terms of the GNU Lesser General Public
             * License version 2.1 as published by the Free Software Foundation.
             *
             * This library is distributed in the hope that it will be useful,
             * but WITHOUT ANY WARRANTY; without even the implied warranty of
             * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
             * Lesser General Public License for more details.
             *
             * You should have received a copy of the GNU Lesser General Public
             * License along with this library; if not, write to the Free Software
             * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
             */
            
            package org.richfaces.demo.tree;
            
            import java.io.IOException;
            import java.io.InputStream;
            import java.util.ArrayList;
            import java.util.Iterator;
            import java.util.List;
            import java.util.Map;
            import java.util.Properties;
            
            import javax.faces.FacesException;
            import javax.faces.component.UIComponent;
            import javax.faces.context.ExternalContext;
            import javax.faces.context.FacesContext;
            
            import org.richfaces.component.UITree;
            import org.richfaces.component.html.HtmlTree;
            import org.richfaces.event.NodeSelectedEvent;
            import org.richfaces.model.TreeNode;
            import org.richfaces.model.TreeNodeImpl;
            
            public class SimpleTreeBean {
            
             private TreeNode rootNode = null;
             private List<String> selectedNodeChildren = new ArrayList<String>();
            
             private String nodeTitle;
             private static final String DATA_PATH = "/richfaces/tree/examples/simple-tree-data.properties";
            
             private void addNodes(String path, TreeNode node, Properties properties) {
             boolean end = false;
             int counter = 1;
            
             while (!end) {
             String key = path != null ? path + '.' + counter : String.valueOf(counter);
            
             String value = properties.getProperty(key);
             if (value != null) {
             TreeNodeImpl nodeImpl = new TreeNodeImpl();
             nodeImpl.setData(value);
             node.addChild(new Integer(counter), nodeImpl);
             addNodes(key, nodeImpl, properties);
             counter++;
             } else {
             end = true;
             }
             }
             }
            
             private void loadTree() {
             FacesContext facesContext = FacesContext.getCurrentInstance();
             ExternalContext externalContext = facesContext.getExternalContext();
             InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH);
             try {
             Properties properties = new Properties();
             properties.load(dataStream);
            
             rootNode = new TreeNodeImpl();
             addNodes(null, rootNode, properties);
            
             } catch (IOException e) {
             throw new FacesException(e.getMessage(), e);
             } finally {
             if (dataStream != null) {
             try {
             dataStream.close();
             } catch (IOException e) {
             externalContext.log(e.getMessage(), e);
             }
             }
             }
             }
            
             public void processSelection(NodeSelectedEvent event) {
             HtmlTree tree = (HtmlTree) event.getComponent();
             nodeTitle = (String) tree.getRowData();
             selectedNodeChildren.clear();
             TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
             if (currentNode.isLeaf()){
             selectedNodeChildren.add((String)currentNode.getData());
             }else
             {
             Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
             while (it!=null &&it.hasNext()) {
             Map.Entry<Object, TreeNode> entry = it.next();
             selectedNodeChildren.add(entry.getValue().getData().toString());
             }
             }
             }
            
             public TreeNode getTreeNode() {
             if (rootNode == null) {
             loadTree();
             }
            
             return rootNode;
             }
            
            
            
             public String getNodeTitle() {
             return nodeTitle;
             }
            
             public void setNodeTitle(String nodeTitle) {
             this.nodeTitle = nodeTitle;
             }
            
            
            }
            



            Except my path to file.

            • 3. Re: Rich Faces Tree Cast Problem.
              Nick Belaevski Master

              Hi,

              This should work ok.

              • 4. Re: Rich Faces Tree Cast Problem.
                Julia R Newbie

                Hi,

                I guess, it should be a working example. I just have no idea why it needs javax.swing. Do you have any guesses where a problem could be?

                Swing i use nowhere.

                • 5. Re: Rich Faces Tree Cast Problem.
                  Nick Belaevski Master

                  This can be classloader issue. Do you use EAR or WAR layout?

                  • 7. Re: Rich Faces Tree Cast Problem.
                    Nick Belaevski Master

                    Check that only richfaces-api is present in EAR, richfaces-impl and richfaces-ui should be in WAR.

                    • 8. Re: Rich Faces Tree Cast Problem.
                      Julia R Newbie

                      Thanks! You are right. I aslo thought that the problem was about classloaders but i wouldn't find it by myself.